From 45c253ba0f7fd9af25428fd179b9588a890e1846 Mon Sep 17 00:00:00 2001 From: mozhulee <21621232@zju.edu.cn> Date: Mon, 28 Aug 2017 12:06:27 +0800 Subject: [PATCH] Update clientset to interface Update newcontroller methods to use clientset interface parameter, so we can use a fake clientset for unit tests. Change-Id: I6dc7291a2979227e60641e72384e5d7f144d77b8 Implements: blueprint enhance-unit-testing Signed-off-by: mozhuli <21621232@zju.edu.cn> --- .../rbacmanager/rbac_controller.go | 4 ++-- .../tenant/tenant_controller.go | 4 ++-- pkg/network-controller/network_controller.go | 18 +++++++++--------- pkg/service-controller/service_controller.go | 1 - 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/pkg/auth-controller/rbacmanager/rbac_controller.go b/pkg/auth-controller/rbacmanager/rbac_controller.go index 31f3889..ef9c7ae 100644 --- a/pkg/auth-controller/rbacmanager/rbac_controller.go +++ b/pkg/auth-controller/rbacmanager/rbac_controller.go @@ -40,14 +40,14 @@ const ( // Controller manages life cycle of namespace's rbac. type Controller struct { - k8sclient *kubernetes.Clientset + k8sclient kubernetes.Interface kubeCRDClient crdClient.Interface userCIDR string userGateway string } // NewRBACController creates a new RBAC controller. -func NewRBACController(kubeClient *kubernetes.Clientset, kubeCRDClient crdClient.Interface, userCIDR string, +func NewRBACController(kubeClient kubernetes.Interface, kubeCRDClient crdClient.Interface, userCIDR string, userGateway string) (*Controller, error) { c := &Controller{ k8sclient: kubeClient, diff --git a/pkg/auth-controller/tenant/tenant_controller.go b/pkg/auth-controller/tenant/tenant_controller.go index fcaf447..4a06584 100644 --- a/pkg/auth-controller/tenant/tenant_controller.go +++ b/pkg/auth-controller/tenant/tenant_controller.go @@ -36,13 +36,13 @@ import ( // TenantController manages the life cycle of Tenant. type TenantController struct { - k8sClient *kubernetes.Clientset + k8sClient kubernetes.Interface kubeCRDClient crdClient.Interface openstackClient openstack.Interface } // NewTenantController creates a new tenant controller. -func NewTenantController(kubeClient *kubernetes.Clientset, +func NewTenantController(kubeClient kubernetes.Interface, osClient openstack.Interface, kubeExtClient *apiextensionsclient.Clientset) (*TenantController, error) { // initialize CRD if it does not exist diff --git a/pkg/network-controller/network_controller.go b/pkg/network-controller/network_controller.go index 4cab055..9c45f85 100644 --- a/pkg/network-controller/network_controller.go +++ b/pkg/network-controller/network_controller.go @@ -50,7 +50,7 @@ const ( // NetworkController manages the life cycle of Network. type NetworkController struct { - k8sclient *kubernetes.Clientset + k8sclient kubernetes.Interface kubeCRDClient kubecrd.Interface driver openstack.Interface networkInformer cache.Controller @@ -67,7 +67,7 @@ func (c *NetworkController) Run(stopCh <-chan struct{}) error { } // NewNetworkController creates a new NetworkController. -func NewNetworkController(kubeClient *kubernetes.Clientset, osClient openstack.Interface, kubeExtClient *apiextensionsclient.Clientset) (*NetworkController, error) { +func NewNetworkController(kubeClient kubernetes.Interface, osClient openstack.Interface, kubeExtClient *apiextensionsclient.Clientset) (*NetworkController, error) { // initialize CRD if it does not exist _, err := kubecrd.CreateNetworkCRD(kubeExtClient) if err != nil && !apierrors.IsAlreadyExists(err) { @@ -155,7 +155,7 @@ func (c *NetworkController) onDelete(obj interface{}) { } // Delete kube-dns services for non-system namespaces. if !util.IsSystemNamespace(net.Namespace) { - if err := c.k8sclient.CoreV1Client.Services(net.Namespace).Delete("kube-dns", apismetav1.NewDeleteOptions(0)); err != nil { + if err := c.k8sclient.Core().Services(net.Namespace).Delete("kube-dns", apismetav1.NewDeleteOptions(0)); err != nil { glog.Warningf("error on deleting kube-dns service: %v", err) } } @@ -191,13 +191,13 @@ func (c *NetworkController) createKubeDNSDeployment(namespace string) error { if err = kuberuntime.DecodeInto(scheme.Codecs.UniversalDecoder(), dnsDeploymentBytes, kubeDNSDeploy); err != nil { return fmt.Errorf("unable to decode kube-dns deployment %v", err) } - _, err = c.k8sclient.ExtensionsV1beta1Client.Deployments(namespace).Create(kubeDNSDeploy) + _, err = c.k8sclient.ExtensionsV1beta1().Deployments(namespace).Create(kubeDNSDeploy) if err != nil { if !apierrors.IsAlreadyExists(err) { return fmt.Errorf("unable to create a new kube-dns deployment: %v", err) } - if _, err = c.k8sclient.ExtensionsV1beta1Client.Deployments(namespace).Update(kubeDNSDeploy); err != nil { + if _, err = c.k8sclient.ExtensionsV1beta1().Deployments(namespace).Update(kubeDNSDeploy); err != nil { return fmt.Errorf("unable to update the kube-dns deployment: %v", err) } } @@ -206,12 +206,12 @@ func (c *NetworkController) createKubeDNSDeployment(namespace string) error { } func (c *NetworkController) deleteDeployment(namespace, name string) error { - if err := c.k8sclient.ExtensionsV1beta1Client.Deployments(namespace).Delete(name, apismetav1.NewDeleteOptions(0)); err != nil { + if err := c.k8sclient.ExtensionsV1beta1().Deployments(namespace).Delete(name, apismetav1.NewDeleteOptions(0)); err != nil { return err } err := wait.Poll(500*time.Millisecond, 60*time.Second, func() (bool, error) { - _, err := c.k8sclient.ExtensionsV1beta1Client.Deployments(namespace).Get(name, apismetav1.GetOptions{}) + _, err := c.k8sclient.ExtensionsV1beta1().Deployments(namespace).Get(name, apismetav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { return true, nil @@ -238,13 +238,13 @@ func (c *NetworkController) createKubeDNSService(namespace string) error { if err = kuberuntime.DecodeInto(scheme.Codecs.UniversalDecoder(), dnsServiceBytes, dnsService); err != nil { return fmt.Errorf("unable to decode kube-dns service %v", err) } - _, err = c.k8sclient.CoreV1Client.Services(namespace).Create(dnsService) + _, err = c.k8sclient.Core().Services(namespace).Create(dnsService) if err != nil { if !apierrors.IsAlreadyExists(err) { return fmt.Errorf("unable to create a new kube-dns service: %v", err) } - if _, err = c.k8sclient.CoreV1Client.Services(namespace).Update(dnsService); err != nil { + if _, err = c.k8sclient.Core().Services(namespace).Update(dnsService); err != nil { return fmt.Errorf("unable to update the kube-dns service: %v", err) } } diff --git a/pkg/service-controller/service_controller.go b/pkg/service-controller/service_controller.go index 99fc6bd..c2f4810 100644 --- a/pkg/service-controller/service_controller.go +++ b/pkg/service-controller/service_controller.go @@ -297,7 +297,6 @@ func (s *ServiceController) persistUpdate(service *v1.Service) error { var err error for i := 0; i < clientRetryCount; i++ { _, err = s.kubeClient.Core().Services(service.Namespace).UpdateStatus(service) - //_, err = s.kubeClient.CoreV1Client.Services(service.Namespace).UpdateStatus(service) if err == nil { return nil }