1
16
17 package componentconfigs
18
19 import (
20 "fmt"
21 "reflect"
22 "testing"
23
24 "github.com/lithammer/dedent"
25
26 v1 "k8s.io/api/core/v1"
27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28 "k8s.io/apimachinery/pkg/runtime/schema"
29 clientsetfake "k8s.io/client-go/kubernetes/fake"
30 componentbaseconfig "k8s.io/component-base/config/v1alpha1"
31 kubeproxyconfig "k8s.io/kube-proxy/config/v1alpha1"
32
33 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
34 kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
35 "k8s.io/kubernetes/cmd/kubeadm/app/constants"
36 )
37
38 func testKubeProxyConfigMap(contents string) *v1.ConfigMap {
39 return &v1.ConfigMap{
40 ObjectMeta: metav1.ObjectMeta{
41 Name: constants.KubeProxyConfigMap,
42 Namespace: metav1.NamespaceSystem,
43 },
44 Data: map[string]string{
45 constants.KubeProxyConfigMapKey: dedent.Dedent(contents),
46 },
47 }
48 }
49
50 func TestKubeProxyDefault(t *testing.T) {
51 tests := []struct {
52 name string
53 clusterCfg kubeadmapi.ClusterConfiguration
54 endpoint kubeadmapi.APIEndpoint
55 expected kubeProxyConfig
56 }{
57 {
58 name: "No specific defaulting works",
59 clusterCfg: kubeadmapi.ClusterConfiguration{},
60 endpoint: kubeadmapi.APIEndpoint{},
61 expected: kubeProxyConfig{
62 config: kubeproxyconfig.KubeProxyConfiguration{
63 FeatureGates: map[string]bool{},
64 BindAddress: kubeadmapiv1.DefaultProxyBindAddressv6,
65 ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
66 Kubeconfig: kubeproxyKubeConfigFileName,
67 },
68 },
69 },
70 },
71 {
72 name: "IPv4 bind address",
73 clusterCfg: kubeadmapi.ClusterConfiguration{},
74 endpoint: kubeadmapi.APIEndpoint{
75 AdvertiseAddress: "1.2.3.4",
76 },
77 expected: kubeProxyConfig{
78 config: kubeproxyconfig.KubeProxyConfiguration{
79 FeatureGates: map[string]bool{},
80 BindAddress: kubeadmapiv1.DefaultProxyBindAddressv4,
81 ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
82 Kubeconfig: kubeproxyKubeConfigFileName,
83 },
84 },
85 },
86 },
87 {
88 name: "ClusterCIDR is fetched from PodSubnet",
89 clusterCfg: kubeadmapi.ClusterConfiguration{
90 Networking: kubeadmapi.Networking{
91 PodSubnet: "192.168.0.0/16",
92 },
93 },
94 endpoint: kubeadmapi.APIEndpoint{},
95 expected: kubeProxyConfig{
96 config: kubeproxyconfig.KubeProxyConfiguration{
97 FeatureGates: map[string]bool{},
98 BindAddress: kubeadmapiv1.DefaultProxyBindAddressv6,
99 ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
100 Kubeconfig: kubeproxyKubeConfigFileName,
101 },
102 ClusterCIDR: "192.168.0.0/16",
103 },
104 },
105 },
106 }
107
108 for _, test := range tests {
109 t.Run(test.name, func(t *testing.T) {
110
111 expected := test.expected
112 expected.configBase.GroupVersion = kubeproxyconfig.SchemeGroupVersion
113
114 got := &kubeProxyConfig{
115 configBase: configBase{
116 GroupVersion: kubeproxyconfig.SchemeGroupVersion,
117 },
118 }
119 got.Default(&test.clusterCfg, &test.endpoint, &kubeadmapi.NodeRegistrationOptions{})
120 if !reflect.DeepEqual(got, &expected) {
121 t.Fatalf("Missmatch between expected and got:\nExpected:\n%v\n---\nGot:\n%v", expected, got)
122 }
123 })
124 }
125 }
126
127
128 func runKubeProxyFromTest(t *testing.T, perform func(gvk schema.GroupVersionKind, yaml string) (kubeadmapi.ComponentConfig, error)) {
129 const (
130 kind = "KubeProxyConfiguration"
131 clusterCIDR = "1.2.3.4/16"
132 )
133
134 gvk := kubeProxyHandler.GroupVersion.WithKind(kind)
135 yaml := fmt.Sprintf("apiVersion: %s\nkind: %s\nclusterCIDR: %s", kubeProxyHandler.GroupVersion, kind, clusterCIDR)
136
137 cfg, err := perform(gvk, yaml)
138
139 if err != nil {
140 t.Fatalf("unexpected failure: %v", err)
141 }
142 if cfg == nil {
143 t.Fatal("no config loaded where it should have been")
144 }
145 if kubeproxyCfg, ok := cfg.(*kubeProxyConfig); !ok {
146 t.Fatalf("found different object type than expected: %s", reflect.TypeOf(cfg))
147 } else if kubeproxyCfg.config.ClusterCIDR != clusterCIDR {
148 t.Fatalf("unexpected control value (clusterDomain):\n\tgot: %q\n\texpected: %q", kubeproxyCfg.config.ClusterCIDR, clusterCIDR)
149 }
150 }
151
152 func TestKubeProxyFromDocumentMap(t *testing.T) {
153 runKubeProxyFromTest(t, func(gvk schema.GroupVersionKind, yaml string) (kubeadmapi.ComponentConfig, error) {
154 return kubeProxyHandler.FromDocumentMap(kubeadmapi.DocumentMap{
155 gvk: []byte(yaml),
156 })
157 })
158 }
159
160 func TestKubeProxyFromCluster(t *testing.T) {
161 runKubeProxyFromTest(t, func(_ schema.GroupVersionKind, yaml string) (kubeadmapi.ComponentConfig, error) {
162 client := clientsetfake.NewSimpleClientset(
163 testKubeProxyConfigMap(yaml),
164 )
165
166 return kubeProxyHandler.FromCluster(client, testClusterCfg())
167 })
168 }
169
View as plain text