-
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
Signed-off-by: ruochen <[email protected]>
- Loading branch information
Showing
3 changed files
with
173 additions
and
23 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,130 @@ | ||
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 := ` | ||
{"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":{"name":"kubeadm-config","namespace":"kube-system"}}` | ||
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": { | ||
"name": "kubeadm-config", | ||
"namespace": "kube-system", | ||
} | ||
}` | ||
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": { | ||
"name": "kubeadm-config", | ||
"namespace": "kube-system", | ||
} | ||
}` | ||
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": { | ||
"name": "kubeadm-config", | ||
"namespace": "kube-system", | ||
} | ||
}` | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} |
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