Skip to content

Commit fff2b83

Browse files
authored
For NSX setup, it is possible that VirtualNetwork is not defined for a given VM service VM. (#3446)
In that case, CSI should rely on the NamespaceNetworkInfo CR's SNAT IP to determine the VM's external IP.
1 parent c847d56 commit fff2b83

File tree

6 files changed

+214
-4
lines changed

6 files changed

+214
-4
lines changed

manifests/supervisorcluster/1.29/cns-csi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ rules:
117117
- apiGroups: ["encryption.vmware.com"]
118118
resources: ["encryptionclasses"]
119119
verbs: ["get", "list", "watch"]
120+
- apiGroups: ["nsx.vmware.com"]
121+
resources: ["namespacenetworkinfos"]
122+
verbs: ["get", "list"]
120123
---
121124
kind: ClusterRoleBinding
122125
apiVersion: rbac.authorization.k8s.io/v1

manifests/supervisorcluster/1.30/cns-csi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ rules:
120120
- apiGroups: ["cluster.x-k8s.io"]
121121
resources: ["clusters"]
122122
verbs: ["get", "list", "watch"]
123+
- apiGroups: ["nsx.vmware.com"]
124+
resources: ["namespacenetworkinfos"]
125+
verbs: ["get", "list"]
123126
---
124127
kind: ClusterRoleBinding
125128
apiVersion: rbac.authorization.k8s.io/v1

manifests/supervisorcluster/1.31/cns-csi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ rules:
120120
- apiGroups: ["cluster.x-k8s.io"]
121121
resources: ["clusters"]
122122
verbs: ["get", "list", "watch"]
123+
- apiGroups: ["nsx.vmware.com"]
124+
resources: ["namespacenetworkinfos"]
125+
verbs: ["get", "list"]
123126
---
124127
kind: ClusterRoleBinding
125128
apiVersion: rbac.authorization.k8s.io/v1

manifests/supervisorcluster/1.32/cns-csi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ rules:
120120
- apiGroups: ["cluster.x-k8s.io"]
121121
resources: ["clusters"]
122122
verbs: ["get", "list", "watch"]
123+
- apiGroups: ["nsx.vmware.com"]
124+
resources: ["namespacenetworkinfos"]
125+
verbs: ["get", "list"]
123126
---
124127
kind: ClusterRoleBinding
125128
apiVersion: rbac.authorization.k8s.io/v1

pkg/syncer/cnsoperator/util/util.go

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2021 The Kubernetes Authors.
2+
Copyright 2021-2025 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -37,6 +37,8 @@ import (
3737

3838
vimtypes "github.com/vmware/govmomi/vim25/types"
3939
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/config"
40+
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common"
41+
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common/commonco"
4042
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger"
4143
)
4244

@@ -52,6 +54,12 @@ var networkInfoGVR = schema.GroupVersionResource{
5254
Resource: "networkinfos",
5355
}
5456

57+
var namespaceNetworkInfoGVR = schema.GroupVersionResource{
58+
Group: "nsx.vmware.com",
59+
Version: "v1alpha1",
60+
Resource: "namespacenetworkinfos",
61+
}
62+
5563
const (
5664
snatIPAnnotation = "ncp/snat_ip"
5765
// Namespace for system resources.
@@ -113,9 +121,16 @@ func GetTKGVMIP(ctx context.Context, vmOperatorClient client.Client, dc dynamic.
113121
return "", err
114122
}
115123

124+
isFileVolumesWithVmServiceVmSupported := commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx,
125+
common.FileVolumesWithVmService)
126+
116127
var networkNames []string
117128
for _, networkInterface := range virtualMachineInstance.Spec.Network.Interfaces {
118-
networkNames = append(networkNames, networkInterface.Network.Name)
129+
if !isFileVolumesWithVmServiceVmSupported {
130+
networkNames = append(networkNames, networkInterface.Network.Name)
131+
} else if networkInterface.Network.Name != "" {
132+
networkNames = append(networkNames, networkInterface.Network.Name)
133+
}
119134
}
120135
log.Debugf("VirtualMachine %s/%s is configured with networks %v", vmNamespace, vmName, networkNames)
121136

@@ -137,8 +152,27 @@ func GetTKGVMIP(ctx context.Context, vmOperatorClient client.Client, dc dynamic.
137152
}
138153
}
139154
if ip == "" {
140-
return "", fmt.Errorf("failed to get SNAT IP annotation from VirtualMachine %s/%s",
141-
vmNamespace, vmName)
155+
if !isFileVolumesWithVmServiceVmSupported {
156+
return "", fmt.Errorf("failed to get SNAT IP annotation from VirtualMachine %s/%s",
157+
vmNamespace, vmName)
158+
}
159+
if len(networkNames) != 0 {
160+
// If networkNames for VirtualNetwork were found on the VM,
161+
// then some error happened in getting the SNAT IP from VirtualNetwork CR.
162+
return "", fmt.Errorf("failed to get SNAT IP annotation for VirtualMachine %s/%s "+
163+
"from VirtualNetwrok",
164+
vmNamespace, vmName)
165+
}
166+
// It is likely an NSX setup with VM service VMs.
167+
// For TKG service VMs, virtual network CR will always be present.
168+
ip, err = getSnatIpFromNamespaceNetworkInfo(ctx, dc, vmNamespace, vmName)
169+
if err != nil {
170+
log.Errorf("failed to get SNAT IP from NameSpaceNetworkInfo. Err %s", err)
171+
return "", fmt.Errorf("failed to get SNAT IP from NameSpaceNetworkInfo %s/%s",
172+
vmNamespace, vmName)
173+
}
174+
log.Infof("Obtained SNAT IP %s from NamespaceNetworkInfo for VirtualMachine %s/%s",
175+
ip, vmNamespace, vmName)
142176
}
143177
} else if network_provider_type == VDSNetworkProvider {
144178
ip = virtualMachineInstance.Status.Network.PrimaryIP4
@@ -188,6 +222,35 @@ func GetTKGVMIP(ctx context.Context, vmOperatorClient client.Client, dc dynamic.
188222
return ip, nil
189223
}
190224

225+
// getSnatIpFromNamespaceNetworkInfo finds VM's SNAT IP from the namespace's default NamespaceNetworkInfo CR.
226+
func getSnatIpFromNamespaceNetworkInfo(ctx context.Context, dc dynamic.Interface,
227+
vmNamespace string, vmName string) (string, error) {
228+
log := logger.GetLogger(ctx)
229+
log.Infof("Determining SNAT IP for VM %s in namespace %s via NamespaceNetworkInfo CR", vmNamespace, vmName)
230+
231+
namespaceNetworkInfoInstance, err := dc.Resource(namespaceNetworkInfoGVR).Namespace(vmNamespace).Get(ctx,
232+
vmNamespace, metav1.GetOptions{})
233+
if err != nil {
234+
return "", err
235+
}
236+
log.Debugf("Got namespaceNetworkInfo instance %s/%s", vmNamespace, namespaceNetworkInfoInstance.GetName())
237+
snatIP, found, err := unstructured.NestedString(namespaceNetworkInfoInstance.Object, "topology", "defaultEgressIP")
238+
if err != nil {
239+
return "", fmt.Errorf("failed to get defaultEgressIP from namespaceNetworkInfo %s/%s with error: %v",
240+
vmNamespace, vmName, err)
241+
242+
}
243+
if !found {
244+
return "", fmt.Errorf("defaultEgressIP is not found on namespaceNetworkInfo %s/%s", vmNamespace, vmName)
245+
246+
}
247+
if snatIP == "" {
248+
return "", fmt.Errorf("empty SNAT IP for VM %s on namespaceNetworkInfo instance %s/%s", vmName, vmNamespace,
249+
namespaceNetworkInfoInstance.GetName())
250+
}
251+
return snatIP, nil
252+
}
253+
191254
// GetNetworkProvider reads the network-config configmap in Supervisor cluster.
192255
// Returns the network provider as NSXT_CONTAINER_PLUGIN for NSX-T, or
193256
// VSPHERE_NETWORK for VDS. Otherwise, returns an error, if network provider is
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
27+
"k8s.io/client-go/dynamic/fake"
28+
)
29+
30+
func TestGetSnatIpFromNamespaceNetworkInfo(t *testing.T) {
31+
ctx := context.Background()
32+
gvr := schema.GroupVersionResource{
33+
Group: "nsx.vmware.com",
34+
Version: "v1alpha1",
35+
Resource: "namespacenetworkinfos",
36+
}
37+
38+
namespace := "test-namespace"
39+
vmName := "test-vm"
40+
41+
tests := []struct {
42+
name string
43+
initialObjects []runtime.Object
44+
expectedIP string
45+
expectError bool
46+
}{
47+
{
48+
name: "Happy path - SNAT IP present",
49+
initialObjects: []runtime.Object{
50+
&unstructured.Unstructured{
51+
Object: map[string]interface{}{
52+
"apiVersion": "nsx.vmware.com/v1alpha1",
53+
"kind": "NamespaceNetworkInfo",
54+
"metadata": map[string]interface{}{
55+
"name": namespace,
56+
"namespace": namespace,
57+
},
58+
"topology": map[string]interface{}{
59+
"defaultEgressIP": "10.10.10.10",
60+
},
61+
},
62+
},
63+
},
64+
expectedIP: "10.10.10.10",
65+
expectError: false,
66+
},
67+
{
68+
name: "Error - resource not found",
69+
initialObjects: []runtime.Object{}, // Nothing in fake client
70+
expectedIP: "",
71+
expectError: true,
72+
},
73+
{
74+
name: "Error - defaultEgressIP missing",
75+
initialObjects: []runtime.Object{
76+
&unstructured.Unstructured{
77+
Object: map[string]interface{}{
78+
"apiVersion": "nsx.vmware.com/v1alpha1",
79+
"kind": "NamespaceNetworkInfo",
80+
"metadata": map[string]interface{}{
81+
"name": namespace,
82+
"namespace": namespace,
83+
},
84+
"spec": map[string]interface{}{
85+
"topology": map[string]interface{}{
86+
// defaultEgressIP intentionally missing
87+
},
88+
},
89+
},
90+
},
91+
},
92+
expectedIP: "",
93+
expectError: true,
94+
},
95+
{
96+
name: "Error - defaultEgressIP is empty",
97+
initialObjects: []runtime.Object{
98+
&unstructured.Unstructured{
99+
Object: map[string]interface{}{
100+
"apiVersion": "nsx.vmware.com/v1alpha1",
101+
"kind": "NamespaceNetworkInfo",
102+
"metadata": map[string]interface{}{
103+
"name": namespace,
104+
"namespace": namespace,
105+
},
106+
"spec": map[string]interface{}{
107+
"topology": map[string]interface{}{
108+
"defaultEgressIP": "",
109+
},
110+
},
111+
},
112+
},
113+
},
114+
expectedIP: "",
115+
expectError: true,
116+
},
117+
}
118+
119+
for _, tt := range tests {
120+
t.Run(tt.name, func(t *testing.T) {
121+
scheme := runtime.NewScheme()
122+
scheme.AddKnownTypes(gvr.GroupVersion())
123+
fakeClient := fake.NewSimpleDynamicClient(scheme, tt.initialObjects...)
124+
ip, err := getSnatIpFromNamespaceNetworkInfo(ctx, fakeClient, namespace, vmName)
125+
126+
if tt.expectError {
127+
assert.Error(t, err)
128+
assert.Empty(t, ip)
129+
} else {
130+
assert.NoError(t, err)
131+
assert.Equal(t, tt.expectedIP, ip)
132+
}
133+
})
134+
}
135+
}

0 commit comments

Comments
 (0)