Skip to content

Commit

Permalink
Make aggregatorClient part of virtAPIApp
Browse files Browse the repository at this point in the history
With this is easier to test api.go overwriting the aggregatorClient, no
need to set "master" flag anymore.
  • Loading branch information
qinqon committed Jan 10, 2019
1 parent 01fd4da commit 2e44ed4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
20 changes: 11 additions & 9 deletions pkg/virt-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type virtAPIApp struct {
SwaggerUI string
SubresourcesOnly bool
virtCli kubecli.KubevirtClient
aggregatorClient *aggregatorclient.Clientset
authorizor rest.VirtApiAuthorizor
certsDirectory string

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/virt-api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package virt_api

import (
"errors"
"flag"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -332,7 +333,6 @@ xw==
ghttp.RespondWithJSONEncoded(http.StatusOK, nil),
),
)
flag.Set("master", server.URL())
err := app.createSubresourceApiservice()
Expect(err).ToNot(HaveOccurred())
}, 5)
Expand All @@ -352,7 +352,6 @@ xw==
ghttp.RespondWithJSONEncoded(http.StatusOK, nil),
),
)
flag.Set("master", server.URL())
err := app.createSubresourceApiservice()
Expect(err).ToNot(HaveOccurred())
}, 5)
Expand All @@ -366,7 +365,6 @@ xw==
ghttp.RespondWithJSONEncoded(http.StatusOK, badApiService),
),
)
flag.Set("master", server.URL())
err := app.createSubresourceApiservice()
Expect(err).To(HaveOccurred())
}, 5)
Expand Down

0 comments on commit 2e44ed4

Please sign in to comment.