kubelet_config_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "fmt"
  16. authv1 "k8s.io/api/authorization/v1"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. "k8s.io/apimachinery/pkg/util/version"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. . "github.com/onsi/ginkgo"
  21. . "github.com/onsi/gomega"
  22. )
  23. const (
  24. kubeletConfigConfigMapKey = "kubelet"
  25. )
  26. var (
  27. kubeletConfigConfigMapName string
  28. kubeletConfigRoleName string
  29. kubeletConfigRoleBindingName string
  30. kubeletConfigConfigMapResource = &authv1.ResourceAttributes{
  31. Namespace: kubeSystemNamespace,
  32. Name: "",
  33. Resource: "configmaps",
  34. Verb: "get",
  35. }
  36. )
  37. // Define container for all the test specification aimed at verifying
  38. // that kubeadm creates the kubelet-config ConfigMap, that it is properly configured
  39. // and that all the related RBAC rules are in place
  40. var _ = KubeadmDescribe("kubelet-config ConfigMap", func() {
  41. // Get an instance of the k8s test framework
  42. f := framework.NewDefaultFramework("kubelet-config")
  43. // Tests in this container are not expected to create new objects in the cluster
  44. // so we are disabling the creation of a namespace in order to get a faster execution
  45. f.SkipNamespaceCreation = true
  46. // kubelet-config map is named using the kubernetesVersion as a suffix, and so
  47. // it is necessary to get it from the kubeadm-config ConfigMap before testing
  48. BeforeEach(func() {
  49. // if the kubelet-config map name is already known exit
  50. if kubeletConfigConfigMapName != "" {
  51. return
  52. }
  53. // gets the ClusterConfiguration from the kubeadm kubeadm-config ConfigMap as a untyped map
  54. m := getClusterConfiguration(f.ClientSet)
  55. // Extract the kubernetesVersion
  56. Expect(m).To(HaveKey("kubernetesVersion"))
  57. k8sVersionString := m["kubernetesVersion"].(string)
  58. k8sVersion, err := version.ParseSemantic(k8sVersionString)
  59. if err != nil {
  60. framework.Failf("error reading kubernetesVersion from %s ConfigMap: %v", kubeadmConfigName, err)
  61. }
  62. // Computes all the names derived from the kubernetesVersion
  63. kubeletConfigConfigMapName = fmt.Sprintf("kubelet-config-%d.%d", k8sVersion.Major(), k8sVersion.Minor())
  64. kubeletConfigRoleName = fmt.Sprintf("kubeadm:kubelet-config-%d.%d", k8sVersion.Major(), k8sVersion.Minor())
  65. kubeletConfigRoleBindingName = kubeletConfigRoleName
  66. kubeletConfigConfigMapResource.Name = kubeletConfigConfigMapName
  67. })
  68. It("should exist and be properly configured", func() {
  69. cm := GetConfigMap(f.ClientSet, kubeSystemNamespace, kubeletConfigConfigMapName)
  70. Expect(cm.Data).To(HaveKey(kubeletConfigConfigMapKey))
  71. })
  72. It("should have related Role and RoleBinding", func() {
  73. ExpectRole(f.ClientSet, kubeSystemNamespace, kubeletConfigRoleName)
  74. ExpectRoleBinding(f.ClientSet, kubeSystemNamespace, kubeletConfigRoleBindingName)
  75. })
  76. It("should be accessible for bootstrap tokens", func() {
  77. ExpectSubjectHasAccessToResource(f.ClientSet,
  78. rbacv1.GroupKind, bootstrapTokensGroup,
  79. kubeadmConfigConfigMapResource,
  80. )
  81. })
  82. It("should be accessible for nodes", func() {
  83. ExpectSubjectHasAccessToResource(f.ClientSet,
  84. rbacv1.GroupKind, nodesGroup,
  85. kubeadmConfigConfigMapResource,
  86. )
  87. })
  88. })