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>
This commit is contained in:
mozhulee 2017-08-28 12:06:27 +08:00
parent 604dd5b0c9
commit 45c253ba0f
4 changed files with 13 additions and 14 deletions

View File

@ -40,14 +40,14 @@ const (
// Controller manages life cycle of namespace's rbac. // Controller manages life cycle of namespace's rbac.
type Controller struct { type Controller struct {
k8sclient *kubernetes.Clientset k8sclient kubernetes.Interface
kubeCRDClient crdClient.Interface kubeCRDClient crdClient.Interface
userCIDR string userCIDR string
userGateway string userGateway string
} }
// NewRBACController creates a new RBAC controller. // 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) { userGateway string) (*Controller, error) {
c := &Controller{ c := &Controller{
k8sclient: kubeClient, k8sclient: kubeClient,

View File

@ -36,13 +36,13 @@ import (
// TenantController manages the life cycle of Tenant. // TenantController manages the life cycle of Tenant.
type TenantController struct { type TenantController struct {
k8sClient *kubernetes.Clientset k8sClient kubernetes.Interface
kubeCRDClient crdClient.Interface kubeCRDClient crdClient.Interface
openstackClient openstack.Interface openstackClient openstack.Interface
} }
// NewTenantController creates a new tenant controller. // NewTenantController creates a new tenant controller.
func NewTenantController(kubeClient *kubernetes.Clientset, func NewTenantController(kubeClient kubernetes.Interface,
osClient openstack.Interface, osClient openstack.Interface,
kubeExtClient *apiextensionsclient.Clientset) (*TenantController, error) { kubeExtClient *apiextensionsclient.Clientset) (*TenantController, error) {
// initialize CRD if it does not exist // initialize CRD if it does not exist

View File

@ -50,7 +50,7 @@ const (
// NetworkController manages the life cycle of Network. // NetworkController manages the life cycle of Network.
type NetworkController struct { type NetworkController struct {
k8sclient *kubernetes.Clientset k8sclient kubernetes.Interface
kubeCRDClient kubecrd.Interface kubeCRDClient kubecrd.Interface
driver openstack.Interface driver openstack.Interface
networkInformer cache.Controller networkInformer cache.Controller
@ -67,7 +67,7 @@ func (c *NetworkController) Run(stopCh <-chan struct{}) error {
} }
// NewNetworkController creates a new NetworkController. // 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 // initialize CRD if it does not exist
_, err := kubecrd.CreateNetworkCRD(kubeExtClient) _, err := kubecrd.CreateNetworkCRD(kubeExtClient)
if err != nil && !apierrors.IsAlreadyExists(err) { if err != nil && !apierrors.IsAlreadyExists(err) {
@ -155,7 +155,7 @@ func (c *NetworkController) onDelete(obj interface{}) {
} }
// Delete kube-dns services for non-system namespaces. // Delete kube-dns services for non-system namespaces.
if !util.IsSystemNamespace(net.Namespace) { 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) 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 { if err = kuberuntime.DecodeInto(scheme.Codecs.UniversalDecoder(), dnsDeploymentBytes, kubeDNSDeploy); err != nil {
return fmt.Errorf("unable to decode kube-dns deployment %v", err) 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 err != nil {
if !apierrors.IsAlreadyExists(err) { if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("unable to create a new kube-dns deployment: %v", 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) 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 { 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 return err
} }
err := wait.Poll(500*time.Millisecond, 60*time.Second, func() (bool, error) { 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 err != nil {
if apierrors.IsNotFound(err) { if apierrors.IsNotFound(err) {
return true, nil 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 { if err = kuberuntime.DecodeInto(scheme.Codecs.UniversalDecoder(), dnsServiceBytes, dnsService); err != nil {
return fmt.Errorf("unable to decode kube-dns service %v", err) 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 err != nil {
if !apierrors.IsAlreadyExists(err) { if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("unable to create a new kube-dns service: %v", 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) return fmt.Errorf("unable to update the kube-dns service: %v", err)
} }
} }

View File

@ -297,7 +297,6 @@ func (s *ServiceController) persistUpdate(service *v1.Service) error {
var err error var err error
for i := 0; i < clientRetryCount; i++ { for i := 0; i < clientRetryCount; i++ {
_, err = s.kubeClient.Core().Services(service.Namespace).UpdateStatus(service) _, err = s.kubeClient.Core().Services(service.Namespace).UpdateStatus(service)
//_, err = s.kubeClient.CoreV1Client.Services(service.Namespace).UpdateStatus(service)
if err == nil { if err == nil {
return nil return nil
} }