From 84c30c1a6a48c36b7956798eaafc12208da6b028 Mon Sep 17 00:00:00 2001 From: Michal Fojtik Date: Mon, 18 Dec 2017 10:48:24 +0100 Subject: [PATCH 1/2] UPSTREAM: 0000: shim GetControllerFromReference for drain --- .../kubernetes/pkg/kubectl/cmd/drain.go | 18 +------------ .../pkg/kubectl/cmd/testing/fake.go | 4 +++ .../pkg/kubectl/cmd/util/factory.go | 4 +++ .../kubectl/cmd/util/factory_client_access.go | 26 ++++++++++++++++--- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/drain.go b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/drain.go index 910654f6f6ad..b1b337f98fa5 100644 --- a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/drain.go +++ b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/drain.go @@ -328,22 +328,6 @@ func (o *DrainOptions) deleteOrEvictPodsSimple(nodeInfo *resource.Info) error { return err } -func (o *DrainOptions) getController(namespace string, controllerRef *metav1.OwnerReference) (interface{}, error) { - switch controllerRef.Kind { - case "ReplicationController": - return o.client.Core().ReplicationControllers(namespace).Get(controllerRef.Name, metav1.GetOptions{}) - case "DaemonSet": - return o.client.Extensions().DaemonSets(namespace).Get(controllerRef.Name, metav1.GetOptions{}) - case "Job": - return o.client.Batch().Jobs(namespace).Get(controllerRef.Name, metav1.GetOptions{}) - case "ReplicaSet": - return o.client.Extensions().ReplicaSets(namespace).Get(controllerRef.Name, metav1.GetOptions{}) - case "StatefulSet": - return o.client.AppsV1beta1().StatefulSets(namespace).Get(controllerRef.Name, metav1.GetOptions{}) - } - return nil, fmt.Errorf("Unknown controller kind %q", controllerRef.Kind) -} - func (o *DrainOptions) getPodController(pod corev1.Pod) (*metav1.OwnerReference, error) { controllerRef := metav1.GetControllerOf(&pod) if controllerRef == nil { @@ -355,7 +339,7 @@ func (o *DrainOptions) getPodController(pod corev1.Pod) (*metav1.OwnerReference, // TODO(mml): something more sophisticated than this // TODO(juntee): determine if it's safe to remove getController(), // so that drain can work for controller types that we don't know about - _, err := o.getController(pod.Namespace, controllerRef) + _, err := o.Factory.GetControllerFromReference(pod.Namespace, controllerRef) if err != nil { return nil, err } diff --git a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/testing/fake.go b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/testing/fake.go index d57706d4c1ba..ffc86148ffce 100644 --- a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/testing/fake.go +++ b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/testing/fake.go @@ -445,6 +445,10 @@ func (f *FakeFactory) Resumer(info *resource.Info) ([]byte, error) { return nil, nil } +func (f *FakeFactory) GetControllerFromReference(ns string, ref *metav1.OwnerReference) (runtime.Object, error) { + return nil, nil +} + func (f *FakeFactory) ResolveImage(name string) (string, error) { return name, nil } diff --git a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory.go b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory.go index f4c0799596f9..f06e147eb3aa 100644 --- a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory.go +++ b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory.go @@ -157,6 +157,10 @@ type ClientAccessFactory interface { // in case the object is already resumed. Resumer(info *resource.Info) ([]byte, error) + // GetControllerFromReference fetches the referenced controller from the object + // reference. + GetControllerFromReference(ns string, ref *metav1.OwnerReference) (runtime.Object, error) + // ResolveImage resolves the image names. For kubernetes this function is just // passthrough but it allows to perform more sophisticated image name resolving for // third-party vendors. diff --git a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory_client_access.go b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory_client_access.go index d53f9c38a794..039ae845badb 100644 --- a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory_client_access.go +++ b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory_client_access.go @@ -53,12 +53,12 @@ import ( "k8s.io/client-go/util/homedir" "k8s.io/kubernetes/pkg/api/legacyscheme" "k8s.io/kubernetes/pkg/apis/apps" + "k8s.io/kubernetes/pkg/apis/batch" + "k8s.io/kubernetes/pkg/apis/core" api "k8s.io/kubernetes/pkg/apis/core" "k8s.io/kubernetes/pkg/apis/extensions" "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" "k8s.io/kubernetes/pkg/kubectl" - "k8s.io/kubernetes/pkg/apis/core" - "k8s.io/kubernetes/pkg/apis/batch" "k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/printers" printersinternal "k8s.io/kubernetes/pkg/printers/internalversion" @@ -299,7 +299,6 @@ func (f *ring0Factory) UpdatePodSpecForObject(obj runtime.Object, fn func(*v1.Po } } - func ConvertExteralPodSpecToInternal(inFn func(*v1.PodSpec) error) func(*core.PodSpec) error { return func(specToMutate *core.PodSpec) error { externalPodSpec := &v1.PodSpec{} @@ -499,6 +498,27 @@ func (f *ring0Factory) Resumer(info *resource.Info) ([]byte, error) { } } +func (f *ring0Factory) GetControllerFromReference(namespace string, controllerRef *metav1.OwnerReference) (runtime.Object, error) { + kc, err := f.ClientSet() + if err != nil { + return nil, err + } + switch controllerRef.Kind { + case "ReplicationController": + return kc.Core().ReplicationControllers(namespace).Get(controllerRef.Name, metav1.GetOptions{}) + case "DaemonSet": + return kc.Extensions().DaemonSets(namespace).Get(controllerRef.Name, metav1.GetOptions{}) + case "Job": + return kc.Batch().Jobs(namespace).Get(controllerRef.Name, metav1.GetOptions{}) + case "ReplicaSet": + return kc.Extensions().ReplicaSets(namespace).Get(controllerRef.Name, metav1.GetOptions{}) + case "StatefulSet": + return kc.Apps().StatefulSets(namespace).Get(controllerRef.Name, metav1.GetOptions{}) + } + return nil, fmt.Errorf("Unknown controller kind %q", controllerRef.Kind) + +} + func (f *ring0Factory) DefaultNamespace() (string, bool, error) { return f.clientConfig.Namespace() } From 9c500d75a3b4e262178c760a2930a4d53e2c95ba Mon Sep 17 00:00:00 2001 From: Michal Fojtik Date: Mon, 18 Dec 2017 10:48:48 +0100 Subject: [PATCH 2/2] cli: wire build configs to drain --- pkg/oc/cli/util/clientcmd/factory_client_access.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/oc/cli/util/clientcmd/factory_client_access.go b/pkg/oc/cli/util/clientcmd/factory_client_access.go index 08ea8aeed5dc..38236745991e 100644 --- a/pkg/oc/cli/util/clientcmd/factory_client_access.go +++ b/pkg/oc/cli/util/clientcmd/factory_client_access.go @@ -282,6 +282,18 @@ func (f *ring0Factory) Printer(mapping *meta.RESTMapping, options kprinters.Prin return describe.NewHumanReadablePrinter(f.JSONEncoder(), f.Decoder(true), options), nil } +func (f *ring0Factory) GetControllerFromReference(namespace string, controllerRef *metav1.OwnerReference) (runtime.Object, error) { + switch controllerRef.Kind { + case "Build": + buildClient, err := f.OpenshiftInternalBuildClient() + if err != nil { + return nil, err + } + return buildClient.Build().BuildConfigs(namespace).Get(controllerRef.Name, metav1.GetOptions{}) + } + return f.kubeClientAccessFactory.GetControllerFromReference(namespace, controllerRef) +} + func (f *ring0Factory) Pauser(info *resource.Info) ([]byte, error) { switch t := info.Object.(type) { case *deployapi.DeploymentConfig: