...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/clusterinfo/clusterinfo.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/clusterinfo

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package clusterinfo
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	"k8s.io/api/core/v1"
    25  	rbac "k8s.io/api/rbac/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apiserver/pkg/authentication/user"
    28  	clientset "k8s.io/client-go/kubernetes"
    29  	"k8s.io/client-go/tools/clientcmd"
    30  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    31  	bootstrapapi "k8s.io/cluster-bootstrap/token/api"
    32  	"k8s.io/klog/v2"
    33  
    34  	"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
    35  )
    36  
    37  const (
    38  	// BootstrapSignerClusterRoleName sets the name for the ClusterRole that allows access to ConfigMaps in the kube-public ns
    39  	BootstrapSignerClusterRoleName = "kubeadm:bootstrap-signer-clusterinfo"
    40  )
    41  
    42  // CreateBootstrapConfigMapIfNotExists creates the kube-public ConfigMap if it doesn't exist already
    43  func CreateBootstrapConfigMapIfNotExists(client clientset.Interface, file string) error {
    44  
    45  	fmt.Printf("[bootstrap-token] Creating the %q ConfigMap in the %q namespace\n", bootstrapapi.ConfigMapClusterInfo, metav1.NamespacePublic)
    46  
    47  	klog.V(1).Infoln("[bootstrap-token] loading admin kubeconfig")
    48  	adminConfig, err := clientcmd.LoadFromFile(file)
    49  	if err != nil {
    50  		return errors.Wrap(err, "failed to load admin kubeconfig")
    51  	}
    52  	if err = clientcmdapi.FlattenConfig(adminConfig); err != nil {
    53  		return err
    54  	}
    55  
    56  	adminCluster := adminConfig.Contexts[adminConfig.CurrentContext].Cluster
    57  	// Copy the cluster from admin.conf to the bootstrap kubeconfig, contains the CA cert and the server URL
    58  	klog.V(1).Infoln("[bootstrap-token] copying the cluster from admin.conf to the bootstrap kubeconfig")
    59  	bootstrapConfig := &clientcmdapi.Config{
    60  		Clusters: map[string]*clientcmdapi.Cluster{
    61  			"": adminConfig.Clusters[adminCluster],
    62  		},
    63  	}
    64  	bootstrapBytes, err := clientcmd.Write(*bootstrapConfig)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	// Create or update the ConfigMap in the kube-public namespace
    70  	klog.V(1).Infoln("[bootstrap-token] creating/updating ConfigMap in kube-public namespace")
    71  	return apiclient.CreateOrUpdateConfigMap(client, &v1.ConfigMap{
    72  		ObjectMeta: metav1.ObjectMeta{
    73  			Name:      bootstrapapi.ConfigMapClusterInfo,
    74  			Namespace: metav1.NamespacePublic,
    75  		},
    76  		Data: map[string]string{
    77  			bootstrapapi.KubeConfigKey: string(bootstrapBytes),
    78  		},
    79  	})
    80  }
    81  
    82  // CreateClusterInfoRBACRules creates the RBAC rules for exposing the cluster-info ConfigMap in the kube-public namespace to unauthenticated users
    83  func CreateClusterInfoRBACRules(client clientset.Interface) error {
    84  	klog.V(1).Infoln("creating the RBAC rules for exposing the cluster-info ConfigMap in the kube-public namespace")
    85  	err := apiclient.CreateOrUpdateRole(client, &rbac.Role{
    86  		ObjectMeta: metav1.ObjectMeta{
    87  			Name:      BootstrapSignerClusterRoleName,
    88  			Namespace: metav1.NamespacePublic,
    89  		},
    90  		Rules: []rbac.PolicyRule{
    91  			{
    92  				Verbs:         []string{"get"},
    93  				APIGroups:     []string{""},
    94  				Resources:     []string{"configmaps"},
    95  				ResourceNames: []string{bootstrapapi.ConfigMapClusterInfo},
    96  			},
    97  		},
    98  	})
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	return apiclient.CreateOrUpdateRoleBinding(client, &rbac.RoleBinding{
   104  		ObjectMeta: metav1.ObjectMeta{
   105  			Name:      BootstrapSignerClusterRoleName,
   106  			Namespace: metav1.NamespacePublic,
   107  		},
   108  		RoleRef: rbac.RoleRef{
   109  			APIGroup: rbac.GroupName,
   110  			Kind:     "Role",
   111  			Name:     BootstrapSignerClusterRoleName,
   112  		},
   113  		Subjects: []rbac.Subject{
   114  			{
   115  				Kind: rbac.UserKind,
   116  				Name: user.Anonymous,
   117  			},
   118  		},
   119  	})
   120  }
   121  

View as plain text