...
1
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
39 BootstrapSignerClusterRoleName = "kubeadm:bootstrap-signer-clusterinfo"
40 )
41
42
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
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
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
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