clusterinfo.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 clusterinfo
  14. import (
  15. "fmt"
  16. "github.com/pkg/errors"
  17. "k8s.io/api/core/v1"
  18. rbac "k8s.io/api/rbac/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apiserver/pkg/authentication/user"
  21. clientset "k8s.io/client-go/kubernetes"
  22. "k8s.io/client-go/tools/clientcmd"
  23. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  24. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  25. "k8s.io/klog"
  26. "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
  27. rbachelper "k8s.io/kubernetes/pkg/apis/rbac/v1"
  28. )
  29. const (
  30. // BootstrapSignerClusterRoleName sets the name for the ClusterRole that allows access to ConfigMaps in the kube-public ns
  31. BootstrapSignerClusterRoleName = "kubeadm:bootstrap-signer-clusterinfo"
  32. )
  33. // CreateBootstrapConfigMapIfNotExists creates the kube-public ConfigMap if it doesn't exist already
  34. func CreateBootstrapConfigMapIfNotExists(client clientset.Interface, file string) error {
  35. fmt.Printf("[bootstrap-token] Creating the %q ConfigMap in the %q namespace\n", bootstrapapi.ConfigMapClusterInfo, metav1.NamespacePublic)
  36. klog.V(1).Infoln("[bootstrap-token] loading admin kubeconfig")
  37. adminConfig, err := clientcmd.LoadFromFile(file)
  38. if err != nil {
  39. return errors.Wrap(err, "failed to load admin kubeconfig")
  40. }
  41. adminCluster := adminConfig.Contexts[adminConfig.CurrentContext].Cluster
  42. // Copy the cluster from admin.conf to the bootstrap kubeconfig, contains the CA cert and the server URL
  43. klog.V(1).Infoln("[bootstrap-token] copying the cluster from admin.conf to the bootstrap kubeconfig")
  44. bootstrapConfig := &clientcmdapi.Config{
  45. Clusters: map[string]*clientcmdapi.Cluster{
  46. "": adminConfig.Clusters[adminCluster],
  47. },
  48. }
  49. bootstrapBytes, err := clientcmd.Write(*bootstrapConfig)
  50. if err != nil {
  51. return err
  52. }
  53. // Create or update the ConfigMap in the kube-public namespace
  54. klog.V(1).Infoln("[bootstrap-token] creating/updating ConfigMap in kube-public namespace")
  55. return apiclient.CreateOrUpdateConfigMap(client, &v1.ConfigMap{
  56. ObjectMeta: metav1.ObjectMeta{
  57. Name: bootstrapapi.ConfigMapClusterInfo,
  58. Namespace: metav1.NamespacePublic,
  59. },
  60. Data: map[string]string{
  61. bootstrapapi.KubeConfigKey: string(bootstrapBytes),
  62. },
  63. })
  64. }
  65. // CreateClusterInfoRBACRules creates the RBAC rules for exposing the cluster-info ConfigMap in the kube-public namespace to unauthenticated users
  66. func CreateClusterInfoRBACRules(client clientset.Interface) error {
  67. klog.V(1).Infoln("creating the RBAC rules for exposing the cluster-info ConfigMap in the kube-public namespace")
  68. err := apiclient.CreateOrUpdateRole(client, &rbac.Role{
  69. ObjectMeta: metav1.ObjectMeta{
  70. Name: BootstrapSignerClusterRoleName,
  71. Namespace: metav1.NamespacePublic,
  72. },
  73. Rules: []rbac.PolicyRule{
  74. rbachelper.NewRule("get").Groups("").Resources("configmaps").Names(bootstrapapi.ConfigMapClusterInfo).RuleOrDie(),
  75. },
  76. })
  77. if err != nil {
  78. return err
  79. }
  80. return apiclient.CreateOrUpdateRoleBinding(client, &rbac.RoleBinding{
  81. ObjectMeta: metav1.ObjectMeta{
  82. Name: BootstrapSignerClusterRoleName,
  83. Namespace: metav1.NamespacePublic,
  84. },
  85. RoleRef: rbac.RoleRef{
  86. APIGroup: rbac.GroupName,
  87. Kind: "Role",
  88. Name: BootstrapSignerClusterRoleName,
  89. },
  90. Subjects: []rbac.Subject{
  91. {
  92. Kind: rbac.UserKind,
  93. Name: user.Anonymous,
  94. },
  95. },
  96. })
  97. }