Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add optional filtered node IP address config #187

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
k8s.io/cloud-provider v0.30.2
k8s.io/component-base v0.30.2
k8s.io/klog/v2 v2.130.0
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
)

require (
Expand Down Expand Up @@ -110,7 +111,6 @@ require (
k8s.io/controller-manager v0.30.2 // indirect
k8s.io/kms v0.30.2 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions internal/testing/mock/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
MockVMNamePoweredOff = "mock-vm-poweredoff"
MockVMNameCategories = "mock-vm-categories"
MockVMNameNoAddresses = "mock-vm-no-addresses"
MockVMNameFilteredNodeAddresses = "mock-vm-filtered-node-addresses"
MockVMNamePoweredOnClusterCategories = "mock-vm-poweredon-cluster-categories"

MockNodeNameVMNotExisting = "mock-node-no-vm-exists"
Expand Down
18 changes: 18 additions & 0 deletions internal/testing/mock/mock_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/utils/ptr"

"github.com/nutanix-cloud-native/cloud-provider-nutanix/internal/constants"
)
Expand Down Expand Up @@ -109,6 +110,21 @@ func CreateMockEnvironment(ctx context.Context, kClient *fake.Clientset) (*MockE
return nil, err
}

filteredAddressesVM := getDefaultVMSpec(MockVMNameFilteredNodeAddresses, cluster)
filteredAddressesVM.Status.Resources.NicList = []*prismClientV3.VMNicOutputStatus{{
IPEndpointList: []*prismClientV3.IPAddress{{
IP: ptr.To("127.100.100.1"),
}, {
IP: ptr.To("127.200.200.1"),
}, {
IP: ptr.To("127.300.300.1"),
}},
}}
filteredAddressesNode, err := createNodeForVM(ctx, kClient, filteredAddressesVM)
if err != nil {
return nil, err
}

nonExistingVMNode := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: MockNodeNameVMNotExisting,
Expand Down Expand Up @@ -139,6 +155,7 @@ func CreateMockEnvironment(ctx context.Context, kClient *fake.Clientset) (*MockE
*categoriesVM.Metadata.UUID: categoriesVM,
*noAddressesVM.Metadata.UUID: noAddressesVM,
*poweredOnVMClusterCategories.Metadata.UUID: poweredOnVMClusterCategories,
*filteredAddressesVM.Metadata.UUID: filteredAddressesVM,
},
managedMockClusters: map[string]*prismClientV3.ClusterIntentResponse{
*cluster.Metadata.UUID: cluster,
Expand All @@ -153,6 +170,7 @@ func CreateMockEnvironment(ctx context.Context, kClient *fake.Clientset) (*MockE
MockNodeNameVMNotExisting: nonExistingVMNode,
MockNodeNameNoSystemUUID: noSystemUUIDNode,
MockVMNamePoweredOnClusterCategories: poweredOnClusterCategoriesNode,
MockVMNameFilteredNodeAddresses: filteredAddressesNode,
},
}, nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {

command := app.NewCloudControllerManagerCommand(ccmOptions,
cloudInitializer, controllerInitializers, map[string]string{}, fss, wait.NeverStop)

code := cli.Run(command)
os.Exit(code)
}
Expand Down
3 changes: 2 additions & 1 deletion manifests/cm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ data:
"enableCustomLabeling": false,
"topologyDiscovery": {
"type": "Prism"
}
},
"ignoredNodeIPs": ["${IGNORED_NODE_IPS:=gst}"]
thunderboltsid marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions pkg/provider/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
PrismCentral credentialTypes.NutanixPrismEndpoint `json:"prismCentral"`
TopologyDiscovery TopologyDiscovery `json:"topologyDiscovery"`
EnableCustomLabeling bool `json:"enableCustomLabeling"`
IgnoredNodeIPs []string `json:"ignoredNodeIPs,omitempty"`
}

type TopologyDiscovery struct {
Expand Down
36 changes: 24 additions & 12 deletions pkg/provider/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,28 @@
)

type nutanixManager struct {
client clientset.Interface
config config.Config
nutanixClient interfaces.Client
client clientset.Interface
config config.Config
nutanixClient interfaces.Client
ignoredNodeIPs map[string]struct{}
}

func newNutanixManager(config config.Config) (*nutanixManager, error) {
klog.V(1).Info("Creating new newNutanixManager")

// Initialize the ignoredNodeIPs map
ignoredNodeIPs := make(map[string]struct{}, len(config.IgnoredNodeIPs))
for _, ip := range config.IgnoredNodeIPs {
ignoredNodeIPs[ip] = struct{}{}

Check warning on line 50 in pkg/provider/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/provider/manager.go#L50

Added line #L50 was not covered by tests
}

m := &nutanixManager{
config: config,
nutanixClient: &nutanixClient{
config: config,
clientCache: prismclientv3.NewClientCache(prismclientv3.WithSessionAuth(true)),
},
ignoredNodeIPs: ignoredNodeIPs,
}
return m, nil
}
Expand Down Expand Up @@ -262,26 +271,29 @@
return fmt.Sprintf("%s://%s", constants.ProviderName, strings.ToLower(vmUUID)), nil
}

func (n *nutanixManager) getNodeAddresses(ctx context.Context, vm *prismclientv3.VMIntentResponse) ([]v1.NodeAddress, error) {
func (n *nutanixManager) getNodeAddresses(_ context.Context, vm *prismclientv3.VMIntentResponse) ([]v1.NodeAddress, error) {
if vm == nil {
return nil, fmt.Errorf("vm cannot be nil when getting node addresses")
}
addresses := make([]v1.NodeAddress, 0)
foundIPs := 0

var addresses []v1.NodeAddress
for _, nic := range vm.Status.Resources.NicList {
for _, ipEndpoint := range nic.IPEndpointList {
if ipEndpoint.IP != nil {
addresses = append(addresses, v1.NodeAddress{
Type: v1.NodeInternalIP,
Address: *ipEndpoint.IP,
})
foundIPs++
// Ignore the IP address if it is one of the specified ignoredNodeIPs.
if _, ok := n.ignoredNodeIPs[*ipEndpoint.IP]; !ok {
addresses = append(addresses, v1.NodeAddress{
Type: v1.NodeInternalIP,
Address: *ipEndpoint.IP,
})
}
}
}
}
if foundIPs == 0 {
if len(addresses) == 0 {
return addresses, fmt.Errorf("unable to determine network interfaces from VM with UUID %s", *vm.Metadata.UUID)
}

addresses = append(addresses, v1.NodeAddress{
Type: v1.NodeHostName,
Address: *vm.Spec.Name,
Expand Down
14 changes: 14 additions & 0 deletions pkg/provider/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ var _ = Describe("Test Manager", func() {
ZoneCategory: mock.MockDefaultZone,
},
},
IgnoredNodeIPs: []string{"127.100.100.1", "127.200.200.1"},
},
client: kClient,
nutanixClient: nutanixClient,
ignoredNodeIPs: map[string]struct{}{
"127.100.100.1": struct{}{},
"127.200.200.1": struct{}{},
},
}
})

Expand Down Expand Up @@ -146,6 +151,15 @@ var _ = Describe("Test Manager", func() {
),
)
})

It("should filter node addresses if matching specified filtered addresses", func() {
vm := mockEnvironment.GetVM(ctx, mock.MockVMNameFilteredNodeAddresses)
Expect(vm).ToNot(BeNil())
addresses, err := m.getNodeAddresses(ctx, vm)
Expect(err).ShouldNot(HaveOccurred())
Expect(len(addresses)).To(Equal(2), "Received addresses: %v", addresses)
Expect(addresses).Should(ContainElement(v1.NodeAddress{Type: v1.NodeInternalIP, Address: "127.300.300.1"}))
})
})

Context("Test generateProviderID", func() {
Expand Down
Loading