Skip to content

Commit

Permalink
simplify resource builder usage
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed May 4, 2018
1 parent 54cf942 commit b8aa7ba
Show file tree
Hide file tree
Showing 47 changed files with 397 additions and 352 deletions.
3 changes: 3 additions & 0 deletions pkg/kubectl/cmd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ go_library(
"//vendor/github.com/renstrom/dedent:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/github.com/spf13/pflag:go_default_library",
"//vendor/k8s.io/api/autoscaling/v1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/api/policy/v1beta1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
Expand Down Expand Up @@ -129,7 +130,9 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
"//vendor/k8s.io/apiserver/pkg/util/flag:go_default_library",
"//vendor/k8s.io/client-go/discovery:go_default_library",
"//vendor/k8s.io/client-go/dynamic:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/typed/autoscaling/v1:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
"//vendor/k8s.io/client-go/scale:go_default_library",
Expand Down
65 changes: 30 additions & 35 deletions pkg/kubectl/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
scaleclient "k8s.io/client-go/scale"
oapi "k8s.io/kube-openapi/pkg/util/proto"
api "k8s.io/kubernetes/pkg/apis/core"
Expand Down Expand Up @@ -285,8 +286,13 @@ func (o *ApplyOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
return err
}

mapper, err := f.RESTMapper()
if err != nil {
return err
}

if o.Prune {
o.PruneResources, err = parsePruneResources(r.Mapper().RESTMapper, o.PruneWhitelist)
o.PruneResources, err = parsePruneResources(mapper, o.PruneWhitelist)
if err != nil {
return err
}
Expand All @@ -297,7 +303,6 @@ func (o *ApplyOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {

encoder := scheme.DefaultJSONEncoder()
deserializer := scheme.Codecs.UniversalDeserializer()
mapper := r.Mapper().RESTMapper

visitedUids := sets.NewString()
visitedNamespaces := sets.NewString()
Expand Down Expand Up @@ -382,12 +387,16 @@ func (o *ApplyOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
return err
}
helper := resource.NewHelper(info.Client, info.Mapping)
dynamicClient, err := f.DynamicClient()
if err != nil {
return err
}
patcher := &patcher{
encoder: encoder,
decoder: deserializer,
mapping: info.Mapping,
helper: helper,
clientFunc: f.UnstructuredClientForMapping,
dynamicClient: dynamicClient,
clientsetFunc: f.ClientSet,
overwrite: o.Overwrite,
backOff: clockwork.NewRealClock(),
Expand Down Expand Up @@ -470,9 +479,14 @@ func (o *ApplyOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
return nil
}

dynamicClient, err := f.DynamicClient()
if err != nil {
return err
}

p := pruner{
mapper: mapper,
clientFunc: f.UnstructuredClientForMapping,
dynamicClient: dynamicClient,
clientsetFunc: f.ClientSet,

labelSelector: o.Selector,
Expand Down Expand Up @@ -560,7 +574,7 @@ func getRESTMappings(mapper meta.RESTMapper, pruneResources *[]pruneResource) (n

type pruner struct {
mapper meta.RESTMapper
clientFunc resource.ClientMapperFunc
dynamicClient dynamic.DynamicInterface
clientsetFunc func() (internalclientset.Interface, error)

visitedUids sets.String
Expand All @@ -577,24 +591,17 @@ type pruner struct {
}

func (p *pruner) prune(f cmdutil.Factory, namespace string, mapping *meta.RESTMapping, includeUninitialized bool) error {
c, err := p.clientFunc(mapping)
if err != nil {
return err
}

objList, err := resource.NewHelper(c, mapping).List(
namespace,
mapping.GroupVersionKind.Version,
false,
&metav1.ListOptions{
objList, err := p.dynamicClient.Resource(mapping.GroupVersionKind.GroupVersion().WithResource(mapping.Resource)).
Namespace(namespace).
List(metav1.ListOptions{
LabelSelector: p.labelSelector,
FieldSelector: p.fieldSelector,
IncludeUninitialized: includeUninitialized,
},
)
})
if err != nil {
return err
}

objs, err := meta.ExtractList(objList)
if err != nil {
return err
Expand Down Expand Up @@ -635,20 +642,12 @@ func (p *pruner) prune(f cmdutil.Factory, namespace string, mapping *meta.RESTMa
}

func (p *pruner) delete(namespace, name string, mapping *meta.RESTMapping, scaleClient scaleclient.ScalesGetter) error {
c, err := p.clientFunc(mapping)
if err != nil {
return err
}

return runDelete(namespace, name, mapping, c, nil, p.cascade, p.gracePeriod, p.clientsetFunc, scaleClient)
return runDelete(namespace, name, mapping, p.dynamicClient, p.cascade, p.gracePeriod, p.clientsetFunc, scaleClient)
}

func runDelete(namespace, name string, mapping *meta.RESTMapping, c resource.RESTClient, helper *resource.Helper, cascade bool, gracePeriod int, clientsetFunc func() (internalclientset.Interface, error), scaleClient scaleclient.ScalesGetter) error {
func runDelete(namespace, name string, mapping *meta.RESTMapping, c dynamic.DynamicInterface, cascade bool, gracePeriod int, clientsetFunc func() (internalclientset.Interface, error), scaleClient scaleclient.ScalesGetter) error {
if !cascade {
if helper == nil {
helper = resource.NewHelper(c, mapping)
}
return helper.Delete(namespace, name)
return c.Resource(mapping.GroupVersionKind.GroupVersion().WithResource(mapping.Resource)).Namespace(namespace).Delete(name, nil)
}
cs, err := clientsetFunc()
if err != nil {
Expand All @@ -659,7 +658,7 @@ func runDelete(namespace, name string, mapping *meta.RESTMapping, c resource.RES
if _, ok := err.(*kubectl.NoSuchReaperError); !ok {
return err
}
return resource.NewHelper(c, mapping).Delete(namespace, name)
return c.Resource(mapping.GroupVersionKind.GroupVersion().WithResource(mapping.Resource)).Namespace(namespace).Delete(name, nil)
}
var options *metav1.DeleteOptions
if gracePeriod >= 0 {
Expand All @@ -672,11 +671,7 @@ func runDelete(namespace, name string, mapping *meta.RESTMapping, c resource.RES
}

func (p *patcher) delete(namespace, name string) error {
c, err := p.clientFunc(p.mapping)
if err != nil {
return err
}
return runDelete(namespace, name, p.mapping, c, p.helper, p.cascade, p.gracePeriod, p.clientsetFunc, p.scaleClient)
return runDelete(namespace, name, p.mapping, p.dynamicClient, p.cascade, p.gracePeriod, p.clientsetFunc, p.scaleClient)
}

type patcher struct {
Expand All @@ -685,7 +680,7 @@ type patcher struct {

mapping *meta.RESTMapping
helper *resource.Helper
clientFunc resource.ClientMapperFunc
dynamicClient dynamic.DynamicInterface
clientsetFunc func() (internalclientset.Interface, error)

overwrite bool
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (p *AttachOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, argsIn [
}

builder := f.NewBuilder().
Internal(legacyscheme.Scheme).
WithScheme(legacyscheme.Scheme).
NamespaceParam(namespace).DefaultNamespace()

switch len(argsIn) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/auth/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args
}

r := f.NewBuilder().
Internal(legacyscheme.Scheme).
WithScheme(legacyscheme.Scheme).
ContinueOnError().
NamespaceParam(namespace).DefaultNamespace().
FilenameParam(enforceNamespace, options).
Expand Down
44 changes: 20 additions & 24 deletions pkg/kubectl/cmd/autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"github.com/golang/glog"
"github.com/spf13/cobra"

autoscalingv1 "k8s.io/api/autoscaling/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
autoscalingv1client "k8s.io/client-go/kubernetes/typed/autoscaling/v1"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
Expand Down Expand Up @@ -70,11 +72,11 @@ type AutoscaleOptions struct {
namespace string
dryRun bool
builder *resource.Builder
mapper meta.RESTMapper
canBeAutoscaled func(kind schema.GroupKind) error
clientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error)
generatorFunc func(string, *meta.RESTMapping) (kubectl.StructuredGenerator, error)

HPAClient autoscalingv1client.HorizontalPodAutoscalersGetter

genericclioptions.IOStreams
}

Expand Down Expand Up @@ -132,12 +134,6 @@ func (o *AutoscaleOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args
o.createAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)
o.builder = f.NewBuilder()
o.canBeAutoscaled = f.CanBeAutoscaled
o.mapper, err = f.RESTMapper()
if err != nil {
return err
}

o.clientForMapping = f.ClientForMapping
o.args = args
o.RecordFlags.Complete(f.Command(cmd, false))

Expand All @@ -146,6 +142,12 @@ func (o *AutoscaleOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args
return err
}

kubeClient, err := f.KubernetesClientSet()
if err != nil {
return err
}
o.HPAClient = kubeClient.AutoscalingV1()

// get the generator
o.generatorFunc = func(name string, mapping *meta.RESTMapping) (kubectl.StructuredGenerator, error) {
switch o.Generator {
Expand Down Expand Up @@ -199,7 +201,7 @@ func (o *AutoscaleOptions) Validate() error {

func (o *AutoscaleOptions) Run() error {
r := o.builder.
Internal(legacyscheme.Scheme).
WithScheme(legacyscheme.Scheme).
ContinueOnError().
NamespaceParam(o.namespace).DefaultNamespace().
FilenameParam(o.enforceNamespace, o.FilenameOptions).
Expand Down Expand Up @@ -231,20 +233,14 @@ func (o *AutoscaleOptions) Run() error {
if err != nil {
return err
}

resourceMapper := &resource.Mapper{
RESTMapper: o.mapper,
ClientMapper: resource.ClientMapperFunc(o.clientForMapping),
Decoder: cmdutil.InternalVersionDecoder(),
hpa, ok := object.(*autoscalingv1.HorizontalPodAutoscaler)
if !ok {
return fmt.Errorf("generator made %T, not autoscalingv1.HorizontalPodAutoscaler", object)
}
hpa, err := resourceMapper.InfoForObject(object, legacyscheme.Scheme, nil)
if err != nil {
return err
}
if err := o.Recorder.Record(hpa.Object); err != nil {

if err := o.Recorder.Record(hpa); err != nil {
glog.V(4).Infof("error recording current command: %v", err)
}
object = hpa.Object

if o.dryRun {
count++
Expand All @@ -253,14 +249,14 @@ func (o *AutoscaleOptions) Run() error {
if err != nil {
return err
}
return printer.PrintObj(cmdutil.AsDefaultVersionedOrOriginal(hpa.Object, hpa.Mapping), o.Out)
return printer.PrintObj(hpa, o.Out)
}

if err := kubectl.CreateOrUpdateAnnotation(o.createAnnotation, hpa.Object, cmdutil.InternalVersionJSONEncoder()); err != nil {
if err := kubectl.CreateOrUpdateAnnotation(o.createAnnotation, hpa, cmdutil.InternalVersionJSONEncoder()); err != nil {
return err
}

_, err = resource.NewHelper(hpa.Client, hpa.Mapping).Create(o.namespace, false, object)
actualHPA, err := o.HPAClient.HorizontalPodAutoscalers(o.namespace).Create(hpa)
if err != nil {
return err
}
Expand All @@ -270,7 +266,7 @@ func (o *AutoscaleOptions) Run() error {
if err != nil {
return err
}
return printer.PrintObj(cmdutil.AsDefaultVersionedOrOriginal(info.Object, hpa.Mapping), o.Out)
return printer.PrintObj(actualHPA, o.Out)
})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (o *CertificateOptions) RunCertificateDeny(force bool) error {
func (options *CertificateOptions) modifyCertificateCondition(builder *resource.Builder, clientSet internalclientset.Interface, force bool, modify func(csr *certificates.CertificateSigningRequest) (*certificates.CertificateSigningRequest, bool)) error {
var found int
r := builder.
Internal(legacyscheme.Scheme).
WithScheme(legacyscheme.Scheme).
ContinueOnError().
FilenameParam(false, &options.FilenameOptions).
ResourceNames("certificatesigningrequest", options.csrNames...).
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/clusterinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (o *ClusterInfoOptions) Run() error {

// TODO use generalized labels once they are implemented (#341)
b := o.Builder.
Internal(legacyscheme.Scheme).
WithScheme(legacyscheme.Scheme).
NamespaceParam(o.Namespace).DefaultNamespace().
LabelSelectorParam("kubernetes.io/cluster-service=true").
ResourceTypeOrNameArgs(false, []string{"services"}...).
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (o *ConvertOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) (err er

// build the builder
o.builder = f.NewBuilder().
Internal(scheme.Scheme).
WithScheme(scheme.Scheme).
LocalParam(o.local)
if !o.local {
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
Expand Down
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/create/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ go_library(
"//vendor/k8s.io/api/rbac/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
Expand Down
23 changes: 12 additions & 11 deletions pkg/kubectl/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/api/legacyscheme"
Expand Down Expand Up @@ -404,29 +405,29 @@ func RunCreateSubcommand(f cmdutil.Factory, options *CreateSubcommandOptions) er
if err != nil {
return err
}
client, err := f.ClientForMapping(mapping)
if err != nil {

if err := kubectl.CreateOrUpdateAnnotation(options.CreateAnnotation, obj, cmdutil.InternalVersionJSONEncoder()); err != nil {
return err
}
resourceMapper := &resource.Mapper{
RESTMapper: mapper,
ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),

asUnstructured := &unstructured.Unstructured{}
if err := legacyscheme.Scheme.Convert(obj, asUnstructured, nil); err != nil {
return err
}
info, err := resourceMapper.InfoForObject(obj, legacyscheme.Scheme, nil)
dynamicClient, err := f.DynamicClient()
if err != nil {
return err
}
if err := kubectl.CreateOrUpdateAnnotation(options.CreateAnnotation, info.Object, cmdutil.InternalVersionJSONEncoder()); err != nil {
return err
if mapping.Scope.Name() == meta.RESTScopeNameRoot {
namespace = ""
}

obj, err = resource.NewHelper(client, mapping).Create(namespace, false, info.Object)
actualObject, err := dynamicClient.Resource(mapping.GroupVersionKind.GroupVersion().WithResource(mapping.Resource)).Namespace(namespace).Create(asUnstructured)
if err != nil {
return err
}

// ensure we pass a versioned object to the printer
obj = cmdutil.AsDefaultVersionedOrOriginal(info.Object, info.Mapping)
obj = actualObject
} else {
if meta, err := meta.Accessor(obj); err == nil && nsOverriden {
meta.SetNamespace(namespace)
Expand Down
Loading

0 comments on commit b8aa7ba

Please sign in to comment.