From 2e44ed4db5c3e64630c8e2c981968d81ab276c65 Mon Sep 17 00:00:00 2001 From: Quique Llorente Date: Thu, 10 Jan 2019 21:24:51 +0100 Subject: [PATCH] Make aggregatorClient part of virtAPIApp With this is easier to test api.go overwriting the aggregatorClient, no need to set "master" flag anymore. --- pkg/virt-api/api.go | 20 +++++++++++--------- pkg/virt-api/api_test.go | 6 ++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/virt-api/api.go b/pkg/virt-api/api.go index 055e9cd76aec..b4273cea6036 100644 --- a/pkg/virt-api/api.go +++ b/pkg/virt-api/api.go @@ -102,6 +102,7 @@ type virtAPIApp struct { SwaggerUI string SubresourcesOnly bool virtCli kubecli.KubevirtClient + aggregatorClient *aggregatorclient.Clientset authorizor rest.VirtApiAuthorizor certsDirectory string @@ -142,6 +143,13 @@ func (app *virtAPIApp) Execute() { panic(err) } + config, err := kubecli.GetConfig() + if err != nil { + panic(err) + } + + app.aggregatorClient = aggregatorclient.NewForConfigOrDie(config) + app.authorizor = authorizor app.virtCli = virtCli @@ -803,18 +811,12 @@ func (app *virtAPIApp) subresourceApiservice() *apiregistrationv1beta1.APIServic } func (app *virtAPIApp) createSubresourceApiservice() error { - config, err := kubecli.GetConfig() - if err != nil { - return err - } subresourceApiservice := app.subresourceApiservice() - aggregatorClient := aggregatorclient.NewForConfigOrDie(config) - registerApiService := false - apiService, err := aggregatorClient.ApiregistrationV1beta1().APIServices().Get(subresourceApiservice.Name, metav1.GetOptions{}) + apiService, err := app.aggregatorClient.ApiregistrationV1beta1().APIServices().Get(subresourceApiservice.Name, metav1.GetOptions{}) if err != nil { if k8serrors.IsNotFound(err) { registerApiService = true @@ -824,7 +826,7 @@ func (app *virtAPIApp) createSubresourceApiservice() error { } if registerApiService { - _, err = aggregatorClient.ApiregistrationV1beta1().APIServices().Create(app.subresourceApiservice()) + _, err = app.aggregatorClient.ApiregistrationV1beta1().APIServices().Create(app.subresourceApiservice()) if err != nil { return err } @@ -835,7 +837,7 @@ func (app *virtAPIApp) createSubresourceApiservice() error { // Always update spec to latest. apiService.Spec = app.subresourceApiservice().Spec - _, err := aggregatorClient.ApiregistrationV1beta1().APIServices().Update(apiService) + _, err := app.aggregatorClient.ApiregistrationV1beta1().APIServices().Update(apiService) if err != nil { return err } diff --git a/pkg/virt-api/api_test.go b/pkg/virt-api/api_test.go index c3a3a0aa2f0f..205526e7b8fa 100644 --- a/pkg/virt-api/api_test.go +++ b/pkg/virt-api/api_test.go @@ -21,7 +21,6 @@ package virt_api import ( "errors" - "flag" "io/ioutil" "net/http" "net/http/httptest" @@ -39,6 +38,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/cert" "k8s.io/client-go/util/cert/triple" + aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset" v1 "kubevirt.io/kubevirt/pkg/api/v1" "kubevirt.io/kubevirt/pkg/kubecli" @@ -82,6 +82,7 @@ var _ = Describe("Virt-api", func() { config, err := clientcmd.BuildConfigFromFlags(server.URL(), "") Expect(err).ToNot(HaveOccurred()) app.authorizor, err = rest.NewAuthorizorFromConfig(config) + app.aggregatorClient = aggregatorclient.NewForConfigOrDie(config) Expect(err).ToNot(HaveOccurred()) ctrl = gomock.NewController(GinkgoT()) authorizorMock = rest.NewMockVirtApiAuthorizor(ctrl) @@ -332,7 +333,6 @@ xw== ghttp.RespondWithJSONEncoded(http.StatusOK, nil), ), ) - flag.Set("master", server.URL()) err := app.createSubresourceApiservice() Expect(err).ToNot(HaveOccurred()) }, 5) @@ -352,7 +352,6 @@ xw== ghttp.RespondWithJSONEncoded(http.StatusOK, nil), ), ) - flag.Set("master", server.URL()) err := app.createSubresourceApiservice() Expect(err).ToNot(HaveOccurred()) }, 5) @@ -366,7 +365,6 @@ xw== ghttp.RespondWithJSONEncoded(http.StatusOK, badApiService), ), ) - flag.Set("master", server.URL()) err := app.createSubresourceApiservice() Expect(err).To(HaveOccurred()) }, 5)