-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix get null podCIDR and serviceCIDR
fix: fix get null podCIDR and serviceCIDR Signed-off-by: ruochen <[email protected]>
- Loading branch information
Showing
2 changed files
with
181 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package coordinatormanager | ||
|
||
import ( | ||
"encoding/json" | ||
corev1 "k8s.io/api/core/v1" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestExtractK8sCIDRFromKubeadmConfigMap(t *testing.T) { | ||
type args struct { | ||
cm *corev1.ConfigMap | ||
} | ||
|
||
clusterConfigurationInOneLineJson := ` | ||
{"metadata":{"name":"kubeadm-config","namespace":"kube-system","uid":"01abef5a-5f01-46c6-9a13-e9f5438e3a23","resourceVersion":"25563","creationTimestamp":"2024-12-06T10:45:23Z","annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"data\":{\"ClusterConfiguration\":\"apiServer:\\n certSANs:\\n - 127.0.0.1\\n - apiserver.cluster.local\\n - 10.103.97.2\\n - 192.168.165.128\\n extraArgs:\\n audit-log-format: json\\n audit-log-maxage: \\\"7\\\"\\n audit-log-maxbackup: \\\"10\\\"\\n audit-log-maxsize: \\\"100\\\"\\n audit-log-path: /var/log/kubernetes/audit.log\\n audit-policy-file: /etc/kubernetes/audit-policy.yml\\n authorization-mode: Node,RBAC\\n enable-aggregator-routing: \\\"true\\\"\\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\\n extraVolumes:\\n - hostPath: /etc/kubernetes\\n mountPath: /etc/kubernetes\\n name: audit\\n pathType: DirectoryOrCreate\\n - hostPath: /var/log/kubernetes\\n mountPath: /var/log/kubernetes\\n name: audit-log\\n pathType: DirectoryOrCreate\\n - hostPath: /etc/localtime\\n mountPath: /etc/localtime\\n name: localtime\\n pathType: File\\n readOnly: true\\n timeoutForControlPlane: 4m0s\\napiVersion: kubeadm.k8s.io/v1beta2\\ncertificatesDir: /etc/kubernetes/pki\\nclusterName: kubernetes\\ncontrolPlaneEndpoint: apiserver.cluster.local:6443\\ncontrollerManager:\\n extraArgs:\\n bind-address: 0.0.0.0\\n cluster-signing-duration: 876000h\\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\\n extraVolumes:\\n - hostPath: /etc/localtime\\n mountPath: /etc/localtime\\n name: localtime\\n pathType: File\\n readOnly: true\\ndns:\\n type: CoreDNS\\netcd:\\n local:\\n dataDir: /var/lib/etcd\\n extraArgs:\\n listen-metrics-urls: http://0.0.0.0:2381\\nimageRepository: k8s.gcr.io\\nkind: ClusterConfiguration\\nkubernetesVersion: v1.21.14\\nnetworking:\\n dnsDomain: cluster.local\\n podSubnet: 192.168.165.0/24\\n serviceSubnet: 245.100.128.0/18\\nscheduler:\\n extraArgs:\\n bind-address: 0.0.0.0\\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\\n extraVolumes:\\n - hostPath: /etc/localtime\\n mountPath: /etc/localtime\\n name: localtime\\n pathType: File\\n readOnly: true\\n\",\"ClusterStatus\":\"apiEndpoints:\\n anolios79:\\n advertiseAddress: 192.168.165.128\\n bindPort: 6443\\napiVersion: kubeadm.k8s.io/v1beta2\\nkind: ClusterStatus\\n\"},\"kind\":\"ConfigMap\",\"metadata\":{\"annotations\":{},\"name\":\"kubeadm-config\",\"namespace\":\"kube-system\"}}\n"},"managedFields":[{"manager":"kubectl-client-side-apply","operation":"Update","apiVersion":"v1","time":"2024-12-06T10:45:23Z","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:ClusterConfiguration":{},"f:ClusterStatus":{}},"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}}}}}]},"data":{"ClusterConfiguration":"apiServer:\n certSANs:\n - 127.0.0.1\n - apiserver.cluster.local\n - 10.103.97.2\n - 192.168.165.128\n extraArgs:\n audit-log-format: json\n audit-log-maxage: \"7\"\n audit-log-maxbackup: \"10\"\n audit-log-maxsize: \"100\"\n audit-log-path: /var/log/kubernetes/audit.log\n audit-policy-file: /etc/kubernetes/audit-policy.yml\n authorization-mode: Node,RBAC\n enable-aggregator-routing: \"true\"\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/kubernetes\n mountPath: /etc/kubernetes\n name: audit\n pathType: DirectoryOrCreate\n - hostPath: /var/log/kubernetes\n mountPath: /var/log/kubernetes\n name: audit-log\n pathType: DirectoryOrCreate\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\n timeoutForControlPlane: 4m0s\napiVersion: kubeadm.k8s.io/v1beta2\ncertificatesDir: /etc/kubernetes/pki\nclusterName: kubernetes\ncontrolPlaneEndpoint: apiserver.cluster.local:6443\ncontrollerManager:\n extraArgs:\n bind-address: 0.0.0.0\n cluster-signing-duration: 876000h\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\ndns:\n type: CoreDNS\netcd:\n local:\n dataDir: /var/lib/etcd\n extraArgs:\n listen-metrics-urls: http://0.0.0.0:2381\nimageRepository: k8s.gcr.io\nkind: ClusterConfiguration\nkubernetesVersion: v1.21.14\nnetworking:\n dnsDomain: cluster.local\n podSubnet: 192.168.165.0/24\n serviceSubnet: 245.100.128.0/18\nscheduler:\n extraArgs:\n bind-address: 0.0.0.0\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\n","ClusterStatus":"apiEndpoints:\n anolios79:\n advertiseAddress: 192.168.165.128\n bindPort: 6443\napiVersion: kubeadm.k8s.io/v1beta2\nkind: ClusterStatus\n"}} | ||
` | ||
clusterConfigurationJson := `{ | ||
"apiVersion": "v1", | ||
"data": { | ||
"ClusterConfiguration": "apiServer:\n certSANs:\n - 127.0.0.1\n - apiserver.cluster.local\n - 10.103.97.2\n - 192.168.165.128\n extraArgs:\n audit-log-format: json\n audit-log-maxage: \"7\"\n audit-log-maxbackup: \"10\"\n audit-log-maxsize: \"100\"\n audit-log-path: /var/log/kubernetes/audit.log\n audit-policy-file: /etc/kubernetes/audit-policy.yml\n authorization-mode: Node,RBAC\n enable-aggregator-routing: \"true\"\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/kubernetes\n mountPath: /etc/kubernetes\n name: audit\n pathType: DirectoryOrCreate\n - hostPath: /var/log/kubernetes\n mountPath: /var/log/kubernetes\n name: audit-log\n pathType: DirectoryOrCreate\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\n timeoutForControlPlane: 4m0s\napiVersion: kubeadm.k8s.io/v1beta2\ncertificatesDir: /etc/kubernetes/pki\nclusterName: kubernetes\ncontrolPlaneEndpoint: apiserver.cluster.local:6443\ncontrollerManager:\n extraArgs:\n bind-address: 0.0.0.0\n cluster-signing-duration: 876000h\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\ndns:\n type: CoreDNS\netcd:\n local:\n dataDir: /var/lib/etcd\n extraArgs:\n listen-metrics-urls: http://0.0.0.0:2381\nimageRepository: k8s.gcr.io\nkind: ClusterConfiguration\nkubernetesVersion: v1.21.14\nnetworking:\n dnsDomain: cluster.local\n podSubnet: 192.168.165.0/24\n serviceSubnet: 245.100.128.0/18\nscheduler:\n extraArgs:\n bind-address: 0.0.0.0\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\n", | ||
"ClusterStatus": "apiEndpoints:\n anolios79:\n advertiseAddress: 192.168.165.128\n bindPort: 6443\napiVersion: kubeadm.k8s.io/v1beta2\nkind: ClusterStatus\n" | ||
}, | ||
"kind": "ConfigMap", | ||
"metadata": { | ||
"creationTimestamp": "2024-12-06T08:58:59Z", | ||
"name": "kubeadm-config", | ||
"namespace": "kube-system", | ||
"resourceVersion": "12661", | ||
"uid": "bd80980d-4e5a-4e8a-85a1-7dd69a3d033f" | ||
} | ||
}` | ||
noClusterConfigurationJson := `{ | ||
"apiVersion": "v1", | ||
"data": { | ||
"ClusterStatus": "apiEndpoints:\n anolios79:\n advertiseAddress: 192.168.165.128\n bindPort: 6443\napiVersion: kubeadm.k8s.io/v1beta2\nkind: ClusterStatus\n" | ||
}, | ||
"kind": "ConfigMap", | ||
"metadata": { | ||
"creationTimestamp": "2024-12-06T08:58:59Z", | ||
"name": "kubeadm-config", | ||
"namespace": "kube-system", | ||
"resourceVersion": "12661", | ||
"uid": "bd80980d-4e5a-4e8a-85a1-7dd69a3d033f" | ||
} | ||
}` | ||
noCIDRJson := `{ | ||
"apiVersion": "v1", | ||
"data": { | ||
"ClusterConfiguration": "apiServer:\n certSANs:\n - 127.0.0.1\n - apiserver.cluster.local\n - 10.103.97.2\n - 192.168.165.128\n extraArgs:\n audit-log-format: json\n audit-log-maxage: \"7\"\n audit-log-maxbackup: \"10\"\n audit-log-maxsize: \"100\"\n audit-log-path: /var/log/kubernetes/audit.log\n audit-policy-file: /etc/kubernetes/audit-policy.yml\n authorization-mode: Node,RBAC\n enable-aggregator-routing: \"true\"\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/kubernetes\n mountPath: /etc/kubernetes\n name: audit\n pathType: DirectoryOrCreate\n - hostPath: /var/log/kubernetes\n mountPath: /var/log/kubernetes\n name: audit-log\n pathType: DirectoryOrCreate\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\n timeoutForControlPlane: 4m0s\napiVersion: kubeadm.k8s.io/v1beta2\ncertificatesDir: /etc/kubernetes/pki\nclusterName: kubernetes\ncontrolPlaneEndpoint: apiserver.cluster.local:6443\ncontrollerManager:\n extraArgs:\n bind-address: 0.0.0.0\n cluster-signing-duration: 876000h\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\ndns:\n type: CoreDNS\netcd:\n local:\n dataDir: /var/lib/etcd\n extraArgs:\n listen-metrics-urls: http://0.0.0.0:2381\nimageRepository: k8s.gcr.io\nkind: ClusterConfiguration\nkubernetesVersion: v1.21.14\nnetworking:\n dnsDomain: cluster.local\nscheduler:\n extraArgs:\n bind-address: 0.0.0.0\n feature-gates: EphemeralContainers=true,TTLAfterFinished=true\n extraVolumes:\n - hostPath: /etc/localtime\n mountPath: /etc/localtime\n name: localtime\n pathType: File\n readOnly: true\n", | ||
"ClusterStatus": "apiEndpoints:\n anolios79:\n advertiseAddress: 192.168.165.128\n bindPort: 6443\napiVersion: kubeadm.k8s.io/v1beta2\nkind: ClusterStatus\n" | ||
}, | ||
"kind": "ConfigMap", | ||
"metadata": { | ||
"creationTimestamp": "2024-12-06T08:58:59Z", | ||
"name": "kubeadm-config", | ||
"namespace": "kube-system", | ||
"resourceVersion": "12661", | ||
"uid": "bd80980d-4e5a-4e8a-85a1-7dd69a3d033f" | ||
} | ||
}` | ||
|
||
clusterConfigurationInOneLineCm := &corev1.ConfigMap{} | ||
if err := json.Unmarshal([]byte(clusterConfigurationInOneLineJson), clusterConfigurationInOneLineCm); err != nil { | ||
t.Fatalf("Failed to unmarshal clusterConfigurationInOneLineJson: %v", err) | ||
} | ||
clusterConfigurationJsonCm := &corev1.ConfigMap{} | ||
if err := json.Unmarshal([]byte(clusterConfigurationJson), clusterConfigurationJsonCm); err != nil { | ||
t.Fatalf("Failed to unmarshal clusterConfigurationJson: %v", err) | ||
} | ||
|
||
noClusterConfigurationJsonCm := &corev1.ConfigMap{} | ||
if err := json.Unmarshal([]byte(noClusterConfigurationJson), noClusterConfigurationJsonCm); err != nil { | ||
t.Fatalf("Failed to unmarshal noClusterConfigurationJson: %v", err) | ||
} | ||
noCIDRJsonCm := &corev1.ConfigMap{} | ||
if err := json.Unmarshal([]byte(noCIDRJson), noCIDRJsonCm); err != nil { | ||
t.Fatalf("Failed to unmarshal noCIDRJson: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
podCIDR []string | ||
serviceCIDR []string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "ClusterConfiguration In One line", | ||
args: args{ | ||
cm: clusterConfigurationInOneLineCm, | ||
}, | ||
podCIDR: []string{"192.168.165.0/24"}, | ||
serviceCIDR: []string{"245.100.128.0/18"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "ClusterConfiguration", | ||
args: args{ | ||
cm: clusterConfigurationJsonCm, | ||
}, | ||
podCIDR: []string{"192.168.165.0/24"}, | ||
serviceCIDR: []string{"245.100.128.0/18"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "No ClusterConfiguration", | ||
args: args{ | ||
cm: noClusterConfigurationJsonCm, | ||
}, | ||
podCIDR: nil, | ||
serviceCIDR: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "No CIDR", | ||
args: args{ | ||
cm: noCIDRJsonCm, | ||
}, | ||
podCIDR: nil, | ||
serviceCIDR: nil, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, got1, err := ExtractK8sCIDRFromKubeadmConfigMap(tt.args.cm) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ExtractK8sCIDRFromKubeadmConfigMap() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.podCIDR) { | ||
t.Errorf("ExtractK8sCIDRFromKubeadmConfigMap() got = %v, podCIDR %v", got, tt.podCIDR) | ||
} | ||
if !reflect.DeepEqual(got1, tt.serviceCIDR) { | ||
t.Errorf("ExtractK8sCIDRFromKubeadmConfigMap() got1 = %v, serviceCIDR %v", got1, tt.serviceCIDR) | ||
} | ||
}) | ||
} | ||
} |