kubeadm_config_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2019 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 e2e_kubeadm
  14. import (
  15. yaml "gopkg.in/yaml.v2"
  16. authv1 "k8s.io/api/authorization/v1"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. clientset "k8s.io/client-go/kubernetes"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. . "github.com/onsi/ginkgo"
  21. . "github.com/onsi/gomega"
  22. )
  23. const (
  24. kubeadmConfigName = "kubeadm-config"
  25. kubeadmConfigRoleName = "kubeadm:nodes-kubeadm-config"
  26. kubeadmConfigRoleBindingName = kubeadmConfigRoleName
  27. kubeadmConfigClusterConfigurationConfigMapKey = "ClusterConfiguration"
  28. kubeadmConfigClusterStatusConfigMapKey = "ClusterStatus"
  29. )
  30. var (
  31. kubeadmConfigConfigMapResource = &authv1.ResourceAttributes{
  32. Namespace: kubeSystemNamespace,
  33. Name: kubeadmConfigName,
  34. Resource: "configmaps",
  35. Verb: "get",
  36. }
  37. )
  38. // Define container for all the test specification aimed at verifying
  39. // that kubeadm creates the cluster-info ConfigMap, that it is properly configured
  40. // and that all the related RBAC rules are in place
  41. var _ = KubeadmDescribe("kubeadm-config ConfigMap", func() {
  42. // Get an instance of the k8s test framework
  43. f := framework.NewDefaultFramework("kubeadm-config")
  44. // Tests in this container are not expected to create new objects in the cluster
  45. // so we are disabling the creation of a namespace in order to get a faster execution
  46. f.SkipNamespaceCreation = true
  47. It("should exist and be properly configured", func() {
  48. cm := GetConfigMap(f.ClientSet, kubeSystemNamespace, kubeadmConfigName)
  49. Expect(cm.Data).To(HaveKey(kubeadmConfigClusterConfigurationConfigMapKey))
  50. Expect(cm.Data).To(HaveKey(kubeadmConfigClusterStatusConfigMapKey))
  51. m := unmarshalYaml(cm.Data[kubeadmConfigClusterStatusConfigMapKey])
  52. if _, ok := m["apiEndpoints"]; ok {
  53. d := m["apiEndpoints"].(map[interface{}]interface{})
  54. // get all control-plane nodes
  55. controlPlanes := getControlPlaneNodes(f.ClientSet)
  56. // checks that all the control-plane nodes are in the apiEndpoints list
  57. for _, cp := range controlPlanes.Items {
  58. if _, ok := d[cp.Name]; !ok {
  59. framework.Failf("failed to get apiEndpoints for control-plane %s in %s", cp.Name, kubeadmConfigClusterStatusConfigMapKey)
  60. }
  61. }
  62. } else {
  63. framework.Failf("failed to get apiEndpoints from %s", kubeadmConfigClusterStatusConfigMapKey)
  64. }
  65. })
  66. It("should have related Role and RoleBinding", func() {
  67. ExpectRole(f.ClientSet, kubeSystemNamespace, kubeadmConfigRoleName)
  68. ExpectRoleBinding(f.ClientSet, kubeSystemNamespace, kubeadmConfigRoleBindingName)
  69. })
  70. It("should be accessible for bootstrap tokens", func() {
  71. ExpectSubjectHasAccessToResource(f.ClientSet,
  72. rbacv1.GroupKind, bootstrapTokensGroup,
  73. kubeadmConfigConfigMapResource,
  74. )
  75. })
  76. It("should be accessible for for nodes", func() {
  77. ExpectSubjectHasAccessToResource(f.ClientSet,
  78. rbacv1.GroupKind, nodesGroup,
  79. kubeadmConfigConfigMapResource,
  80. )
  81. })
  82. })
  83. func getClusterConfiguration(c clientset.Interface) map[interface{}]interface{} {
  84. cm := GetConfigMap(c, kubeSystemNamespace, kubeadmConfigName)
  85. Expect(cm.Data).To(HaveKey(kubeadmConfigClusterConfigurationConfigMapKey))
  86. return unmarshalYaml(cm.Data[kubeadmConfigClusterConfigurationConfigMapKey])
  87. }
  88. func unmarshalYaml(data string) map[interface{}]interface{} {
  89. m := make(map[interface{}]interface{})
  90. err := yaml.Unmarshal([]byte(data), &m)
  91. if err != nil {
  92. framework.Failf("error parsing %s ConfigMap: %v", kubeadmConfigName, err)
  93. }
  94. return m
  95. }