uploadconfig_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "context"
  16. "reflect"
  17. "testing"
  18. v1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. clientsetfake "k8s.io/client-go/kubernetes/fake"
  23. core "k8s.io/client-go/testing"
  24. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  25. kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
  26. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  27. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  28. configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
  29. )
  30. func TestUploadConfiguration(t *testing.T) {
  31. tests := []struct {
  32. name string
  33. errOnCreate error
  34. errOnUpdate error
  35. updateExisting bool
  36. errExpected bool
  37. verifyResult bool
  38. }{
  39. {
  40. name: "basic validation with correct key",
  41. verifyResult: true,
  42. },
  43. {
  44. name: "update existing should report no error",
  45. updateExisting: true,
  46. verifyResult: true,
  47. },
  48. {
  49. name: "unexpected errors for create should be returned",
  50. errOnCreate: apierrors.NewUnauthorized(""),
  51. errExpected: true,
  52. },
  53. {
  54. name: "update existing show report error if unexpected error for update is returned",
  55. errOnUpdate: apierrors.NewUnauthorized(""),
  56. updateExisting: true,
  57. errExpected: true,
  58. },
  59. }
  60. for _, tt := range tests {
  61. t.Run(tt.name, func(t2 *testing.T) {
  62. initialcfg := &kubeadmapiv1beta2.InitConfiguration{
  63. LocalAPIEndpoint: kubeadmapiv1beta2.APIEndpoint{
  64. AdvertiseAddress: "1.2.3.4",
  65. },
  66. BootstrapTokens: []kubeadmapiv1beta2.BootstrapToken{
  67. {
  68. Token: &kubeadmapiv1beta2.BootstrapTokenString{
  69. ID: "abcdef",
  70. Secret: "abcdef0123456789",
  71. },
  72. },
  73. },
  74. NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
  75. Name: "node-foo",
  76. CRISocket: "/var/run/custom-cri.sock",
  77. },
  78. }
  79. clustercfg := &kubeadmapiv1beta2.ClusterConfiguration{
  80. KubernetesVersion: kubeadmconstants.MinimumControlPlaneVersion.WithPatch(10).String(),
  81. }
  82. cfg, err := configutil.DefaultedInitConfiguration(initialcfg, clustercfg)
  83. if err != nil {
  84. t2.Fatalf("UploadConfiguration() error = %v", err)
  85. }
  86. cfg.ComponentConfigs = kubeadmapi.ComponentConfigMap{}
  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(context.TODO(), 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 len(decodedCfg.ComponentConfigs) != 0 {
  126. t2.Errorf("unexpected component configs in decodedCfg: %d", len(decodedCfg.ComponentConfigs))
  127. }
  128. // Force initialize with an empty map so that reflect.DeepEqual works
  129. decodedCfg.ComponentConfigs = kubeadmapi.ComponentConfigMap{}
  130. if !reflect.DeepEqual(decodedCfg, &cfg.ClusterConfiguration) {
  131. t2.Errorf("the initial and decoded ClusterConfiguration didn't match:\n%t\n===\n%t", decodedCfg.ComponentConfigs == nil, cfg.ComponentConfigs == nil)
  132. }
  133. statusData := controlPlaneCfg.Data[kubeadmconstants.ClusterStatusConfigMapKey]
  134. if statusData == "" {
  135. t2.Fatal("failed to find ClusterStatusConfigMapKey key")
  136. }
  137. decodedStatus := &kubeadmapi.ClusterStatus{}
  138. if err := runtime.DecodeInto(kubeadmscheme.Codecs.UniversalDecoder(), []byte(statusData), decodedStatus); err != nil {
  139. t2.Fatalf("unable to decode status from bytes: %v", err)
  140. }
  141. if !reflect.DeepEqual(decodedStatus, status) {
  142. t2.Error("the initial and decoded ClusterStatus didn't match")
  143. }
  144. }
  145. })
  146. }
  147. }
  148. func TestMutateClusterStatus(t *testing.T) {
  149. cm := &v1.ConfigMap{
  150. Data: map[string]string{
  151. kubeadmconstants.ClusterStatusConfigMapKey: "",
  152. },
  153. }
  154. endpoints := map[string]kubeadmapi.APIEndpoint{
  155. "some-node": {
  156. AdvertiseAddress: "127.0.0.1",
  157. BindPort: 6443,
  158. },
  159. }
  160. err := mutateClusterStatus(cm, func(cs *kubeadmapi.ClusterStatus) error {
  161. cs.APIEndpoints = endpoints
  162. return nil
  163. })
  164. if err != nil {
  165. t.Fatalf("could not mutate cluster status: %v", err)
  166. }
  167. // Try to unmarshal the cluster status back and compare with the original mutated structure
  168. cs, err := configutil.UnmarshalClusterStatus(cm.Data)
  169. if err != nil {
  170. t.Fatalf("could not unmarshal cluster status: %v", err)
  171. }
  172. if !reflect.DeepEqual(cs.APIEndpoints, endpoints) {
  173. t.Fatalf("mutation of cluster status failed: %v", err)
  174. }
  175. }