Skip to content

Commit

Permalink
OCPBUGS-17054: Nutanix CCM should scope secret informers per namespace (
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhua121 authored Aug 10, 2023
1 parent 18ab800 commit b4b5a3a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
12 changes: 1 addition & 11 deletions pkg/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package provider
import (
"context"
"fmt"
"os"

prismgoclient "github.com/nutanix-cloud-native/prism-go-client"
"github.com/nutanix-cloud-native/prism-go-client/environment"
Expand All @@ -32,7 +31,6 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

"github.com/nutanix-cloud-native/cloud-provider-nutanix/internal/constants"
"github.com/nutanix-cloud-native/cloud-provider-nutanix/pkg/provider/config"
"github.com/nutanix-cloud-native/cloud-provider-nutanix/pkg/provider/interfaces"
)
Expand Down Expand Up @@ -86,7 +84,7 @@ func (n *nutanixClient) setupEnvironment() error {
if n.env != nil {
return nil
}
ccmNamespace, err := n.getCCMNamespace()
ccmNamespace, err := GetCCMNamespace()
if err != nil {
return err
}
Expand Down Expand Up @@ -127,11 +125,3 @@ func (n *nutanixClient) syncCache(informer cache.SharedInformer) {
}
}
}

func (n *nutanixClient) getCCMNamespace() (string, error) {
ns := os.Getenv(constants.CCMNamespaceKey)
if ns == "" {
return "", fmt.Errorf("failed to retrieve CCM namespace. Make sure %s env variable is set", constants.CCMNamespaceKey)
}
return ns, nil
}
18 changes: 14 additions & 4 deletions pkg/provider/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@ func newNutanixManager(config config.Config) (*nutanixManager, error) {
return m, nil
}

func (nc *nutanixManager) setInformers(sharedInformers informers.SharedInformerFactory) {
nc.nutanixClient.SetInformers(sharedInformers)
}

func (nc *nutanixManager) setKubernetesClient(client clientset.Interface) {
nc.client = client
nc.setInformers()
}

func (nc *nutanixManager) setInformers() {
// Set the nutanixClient's informersFactory with the ccm namespace
ccmNamespace, err := GetCCMNamespace()
if err != nil {
klog.Fatal(err.Error())
}
informerFactory := informers.NewSharedInformerFactoryWithOptions(
nc.client, NoResyncPeriodFunc(), informers.WithNamespace(ccmNamespace))
nc.nutanixClient.SetInformers(informerFactory)

klog.Infof("Set the informers with namespace %q", ccmNamespace)
}

func (n *nutanixManager) getInstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
Expand Down
6 changes: 0 additions & 6 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"io/ioutil"

"k8s.io/client-go/informers"
clientset "k8s.io/client-go/kubernetes"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -81,11 +80,6 @@ func (nc *NtnxCloud) Initialize(clientBuilder cloudprovider.ControllerClientBuil
klog.Infof("Client initialized")
}

// SetInformers sets the informer on the cloud object. Implements cloudprovider.InformerUser
func (nc *NtnxCloud) SetInformers(informerFactory informers.SharedInformerFactory) {
nc.manager.setInformers(informerFactory)
}

func (nc *NtnxCloud) addKubernetesClient(kclient clientset.Interface) {
nc.client = kclient
nc.manager.setKubernetesClient(kclient)
Expand Down
12 changes: 2 additions & 10 deletions pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ package provider
import (
"bytes"
"encoding/json"
"os"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"

"github.com/nutanix-cloud-native/cloud-provider-nutanix/internal/constants"
Expand Down Expand Up @@ -57,14 +56,7 @@ var _ = Describe("Test Provider", func() {
},
instancesV2: &instancesV2{},
}
})

Context("Test SetInformers", func() {
It("should set the informers", func() {
informerFactory := informers.NewSharedInformerFactory(kClient, time.Minute)
ntnxCloud.SetInformers(informerFactory)
Expect(nClient.env).To(BeNil())
})
os.Setenv(constants.CCMNamespaceKey, "ccm-namespace")
})

Context("Test AddKubernetesClient", func() {
Expand Down
23 changes: 23 additions & 0 deletions pkg/provider/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package provider

import (
"fmt"
"os"
"time"

"github.com/nutanix-cloud-native/cloud-provider-nutanix/internal/constants"
)

// GetCCMNamespace returns the CCM controller pod namespace
func GetCCMNamespace() (string, error) {
ns := os.Getenv(constants.CCMNamespaceKey)
if ns == "" {
return "", fmt.Errorf("failed to retrieve CCM namespace. Make sure %s env variable is set", constants.CCMNamespaceKey)
}
return ns, nil
}

// NoResyncPeriodFunc returns the 0 resync period
func NoResyncPeriodFunc() time.Duration {
return 0
}

0 comments on commit b4b5a3a

Please sign in to comment.