uploadconfig_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package uploadconfig
  14. import (
  15. "reflect"
  16. "testing"
  17. v1 "k8s.io/api/core/v1"
  18. apierrors "k8s.io/apimachinery/pkg/api/errors"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. clientsetfake "k8s.io/client-go/kubernetes/fake"
  22. core "k8s.io/client-go/testing"
  23. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  24. kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
  25. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  26. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  27. configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
  28. )
  29. func TestUploadConfiguration(t *testing.T) {
  30. tests := []struct {
  31. name string
  32. errOnCreate error
  33. errOnUpdate error
  34. updateExisting bool
  35. errExpected bool
  36. verifyResult bool
  37. }{
  38. {
  39. name: "basic validation with correct key",
  40. verifyResult: true,
  41. },
  42. {
  43. name: "update existing should report no error",
  44. updateExisting: true,
  45. verifyResult: true,
  46. },
  47. {
  48. name: "unexpected errors for create should be returned",
  49. errOnCreate: apierrors.NewUnauthorized(""),
  50. errExpected: true,
  51. },
  52. {
  53. name: "update existing show report error if unexpected error for update is returned",
  54. errOnUpdate: apierrors.NewUnauthorized(""),
  55. updateExisting: true,
  56. errExpected: true,
  57. },
  58. }
  59. for _, tt := range tests {
  60. t.Run(tt.name, func(t2 *testing.T) {
  61. initialcfg := &kubeadmapiv1beta2.InitConfiguration{
  62. LocalAPIEndpoint: kubeadmapiv1beta2.APIEndpoint{
  63. AdvertiseAddress: "1.2.3.4",
  64. },
  65. BootstrapTokens: []kubeadmapiv1beta2.BootstrapToken{
  66. {
  67. Token: &kubeadmapiv1beta2.BootstrapTokenString{
  68. ID: "abcdef",
  69. Secret: "abcdef0123456789",
  70. },
  71. },
  72. },
  73. NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
  74. Name: "node-foo",
  75. CRISocket: "/var/run/custom-cri.sock",
  76. },
  77. }
  78. clustercfg := &kubeadmapiv1beta2.ClusterConfiguration{
  79. KubernetesVersion: kubeadmconstants.MinimumControlPlaneVersion.WithPatch(10).String(),
  80. }
  81. cfg, err := configutil.DefaultedInitConfiguration(initialcfg, clustercfg)
  82. // cleans up component config to make cfg and decodedcfg comparable (now component config are not stored anymore in kubeadm-config config map)
  83. cfg.ComponentConfigs = kubeadmapi.ComponentConfigs{}
  84. if err != nil {
  85. t2.Fatalf("UploadConfiguration() error = %v", err)
  86. }
  87. status := &kubeadmapi.ClusterStatus{
  88. APIEndpoints: map[string]kubeadmapi.APIEndpoint{
  89. "node-foo": cfg.LocalAPIEndpoint,
  90. },
  91. }
  92. client := clientsetfake.NewSimpleClientset()
  93. if tt.errOnCreate != nil {
  94. client.PrependReactor("create", "configmaps", func(action core.Action) (bool, runtime.Object, error) {
  95. return true, nil, tt.errOnCreate
  96. })
  97. }
  98. // For idempotent test, we check the result of the second call.
  99. if err := UploadConfiguration(cfg, client); !tt.updateExisting && (err != nil) != tt.errExpected {
  100. t2.Fatalf("UploadConfiguration() error = %v, wantErr %v", err, tt.errExpected)
  101. }
  102. if tt.updateExisting {
  103. if tt.errOnUpdate != nil {
  104. client.PrependReactor("update", "configmaps", func(action core.Action) (bool, runtime.Object, error) {
  105. return true, nil, tt.errOnUpdate
  106. })
  107. }
  108. if err := UploadConfiguration(cfg, client); (err != nil) != tt.errExpected {
  109. t2.Fatalf("UploadConfiguration() error = %v", err)
  110. }
  111. }
  112. if tt.verifyResult {
  113. controlPlaneCfg, err := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(kubeadmconstants.KubeadmConfigConfigMap, metav1.GetOptions{})
  114. if err != nil {
  115. t2.Fatalf("Fail to query ConfigMap error = %v", err)
  116. }
  117. configData := controlPlaneCfg.Data[kubeadmconstants.ClusterConfigurationConfigMapKey]
  118. if configData == "" {
  119. t2.Fatal("Fail to find ClusterConfigurationConfigMapKey key")
  120. }
  121. decodedCfg := &kubeadmapi.ClusterConfiguration{}
  122. if err := runtime.DecodeInto(kubeadmscheme.Codecs.UniversalDecoder(), []byte(configData), decodedCfg); err != nil {
  123. t2.Fatalf("unable to decode config from bytes: %v", err)
  124. }
  125. if !reflect.DeepEqual(decodedCfg, &cfg.ClusterConfiguration) {
  126. t2.Errorf("the initial and decoded ClusterConfiguration didn't match")
  127. }
  128. statusData := controlPlaneCfg.Data[kubeadmconstants.ClusterStatusConfigMapKey]
  129. if statusData == "" {
  130. t2.Fatal("failed to find ClusterStatusConfigMapKey key")
  131. }
  132. decodedStatus := &kubeadmapi.ClusterStatus{}
  133. if err := runtime.DecodeInto(kubeadmscheme.Codecs.UniversalDecoder(), []byte(statusData), decodedStatus); err != nil {
  134. t2.Fatalf("unable to decode status from bytes: %v", err)
  135. }
  136. if !reflect.DeepEqual(decodedStatus, status) {
  137. t2.Error("the initial and decoded ClusterStatus didn't match")
  138. }
  139. }
  140. })
  141. }
  142. }
  143. func TestMutateClusterStatus(t *testing.T) {
  144. cm := &v1.ConfigMap{
  145. Data: map[string]string{
  146. kubeadmconstants.ClusterStatusConfigMapKey: "",
  147. },
  148. }
  149. endpoints := map[string]kubeadmapi.APIEndpoint{
  150. "some-node": {
  151. AdvertiseAddress: "127.0.0.1",
  152. BindPort: 6443,
  153. },
  154. }
  155. err := mutateClusterStatus(cm, func(cs *kubeadmapi.ClusterStatus) error {
  156. cs.APIEndpoints = endpoints
  157. return nil
  158. })
  159. if err != nil {
  160. t.Fatalf("could not mutate cluster status: %v", err)
  161. }
  162. // Try to unmarshal the cluster status back and compare with the original mutated structure
  163. cs, err := configutil.UnmarshalClusterStatus(cm.Data)
  164. if err != nil {
  165. t.Fatalf("could not unmarshal cluster status: %v", err)
  166. }
  167. if !reflect.DeepEqual(cs.APIEndpoints, endpoints) {
  168. t.Fatalf("mutation of cluster status failed: %v", err)
  169. }
  170. }