|
| 1 | +/* |
| 2 | +Copyright 2020 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 internal |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/blang/semver" |
| 24 | + "github.com/pkg/errors" |
| 25 | + rbac "k8s.io/api/rbac/v1" |
| 26 | + rbacv1 "k8s.io/api/rbac/v1" |
| 27 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/runtime" |
| 30 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + // NodeBootstrapTokenAuthGroup specifies which group a Node Bootstrap Token should be authenticated in |
| 35 | + NodeBootstrapTokenAuthGroup = "system:bootstrappers:kubeadm:default-node-token" |
| 36 | + |
| 37 | + // GetNodesClusterRoleName defines the name of the ClusterRole and ClusterRoleBinding to get nodes |
| 38 | + GetNodesClusterRoleName = "kubeadm:get-nodes" |
| 39 | + |
| 40 | + // NodesGroup defines the well-known group for all nodes. |
| 41 | + NodesGroup = "system:nodes" |
| 42 | + |
| 43 | + // KubeletConfigMapRolePrefix defines base kubelet configuration ConfigMap role prefix. |
| 44 | + KubeletConfigMapRolePrefix = "kubeadm:" |
| 45 | + |
| 46 | + // KubeletConfigMapName defines base kubelet configuration ConfigMap name. |
| 47 | + KubeletConfigMapName = "kubelet-config-%d.%d" |
| 48 | +) |
| 49 | + |
| 50 | +// EnsureResource creates a resoutce if the target resource doesn't exist. If the resource exists already, this function will ignore the resource instead. |
| 51 | +func (w *Workload) EnsureResource(ctx context.Context, obj runtime.Object) error { |
| 52 | + testObj := obj.DeepCopyObject() |
| 53 | + key, err := ctrlclient.ObjectKeyFromObject(obj) |
| 54 | + if err != nil { |
| 55 | + return errors.Wrap(err, "unable to derive key for resource") |
| 56 | + } |
| 57 | + if err := w.Client.Get(ctx, key, testObj); err != nil && !apierrors.IsNotFound(err) { |
| 58 | + return errors.Wrapf(err, "failed to determine if resource %s/%s already exists", key.Namespace, key.Name) |
| 59 | + } else if err == nil { |
| 60 | + // If object already exists, nothing left to do |
| 61 | + return nil |
| 62 | + } |
| 63 | + if err := w.Client.Create(ctx, obj); err != nil { |
| 64 | + if !apierrors.IsAlreadyExists(err) { |
| 65 | + return errors.Wrapf(err, "unable to create resource %s/%s on workload cluster", key.Namespace, key.Name) |
| 66 | + } |
| 67 | + } |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// AllowBootstrapTokensToGetNodes creates RBAC rules to allow Node Bootstrap Tokens to list nodes |
| 72 | +func (w *Workload) AllowBootstrapTokensToGetNodes(ctx context.Context) error { |
| 73 | + if err := w.EnsureResource(ctx, &rbac.ClusterRole{ |
| 74 | + ObjectMeta: metav1.ObjectMeta{ |
| 75 | + Name: GetNodesClusterRoleName, |
| 76 | + Namespace: metav1.NamespaceSystem, |
| 77 | + }, |
| 78 | + Rules: []rbac.PolicyRule{ |
| 79 | + { |
| 80 | + Verbs: []string{"get"}, |
| 81 | + APIGroups: []string{""}, |
| 82 | + Resources: []string{"nodes"}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }); err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + return w.EnsureResource(ctx, &rbac.ClusterRoleBinding{ |
| 90 | + ObjectMeta: metav1.ObjectMeta{ |
| 91 | + Name: GetNodesClusterRoleName, |
| 92 | + Namespace: metav1.NamespaceSystem, |
| 93 | + }, |
| 94 | + RoleRef: rbac.RoleRef{ |
| 95 | + APIGroup: rbac.GroupName, |
| 96 | + Kind: "ClusterRole", |
| 97 | + Name: GetNodesClusterRoleName, |
| 98 | + }, |
| 99 | + Subjects: []rbac.Subject{ |
| 100 | + { |
| 101 | + Kind: rbac.GroupKind, |
| 102 | + Name: NodeBootstrapTokenAuthGroup, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +func generateKubeletConfigName(version semver.Version) string { |
| 109 | + return fmt.Sprintf(KubeletConfigMapName, version.Major, version.Minor) |
| 110 | +} |
| 111 | + |
| 112 | +func generateKubeletConfigRoleName(version semver.Version) string { |
| 113 | + return KubeletConfigMapRolePrefix + generateKubeletConfigName(version) |
| 114 | +} |
| 115 | + |
| 116 | +// ReconcileKubeletRBACBinding will create a RoleBinding for the new kubelet version during upgrades. |
| 117 | +// If the role binding already exists this function is a no-op. |
| 118 | +func (w *Workload) ReconcileKubeletRBACBinding(ctx context.Context, version semver.Version) error { |
| 119 | + roleName := generateKubeletConfigRoleName(version) |
| 120 | + return w.EnsureResource(ctx, &rbac.RoleBinding{ |
| 121 | + ObjectMeta: metav1.ObjectMeta{ |
| 122 | + Namespace: metav1.NamespaceSystem, |
| 123 | + Name: roleName, |
| 124 | + }, |
| 125 | + Subjects: []rbacv1.Subject{ |
| 126 | + { |
| 127 | + APIGroup: rbac.GroupName, |
| 128 | + Kind: rbac.GroupKind, |
| 129 | + Name: NodesGroup, |
| 130 | + }, |
| 131 | + { |
| 132 | + APIGroup: rbac.GroupName, |
| 133 | + Kind: rbac.GroupKind, |
| 134 | + Name: NodeBootstrapTokenAuthGroup, |
| 135 | + }, |
| 136 | + }, |
| 137 | + RoleRef: rbacv1.RoleRef{ |
| 138 | + APIGroup: rbac.GroupName, |
| 139 | + Kind: "Role", |
| 140 | + Name: roleName, |
| 141 | + }, |
| 142 | + }) |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | +// ReconcileKubeletRBACRole will create a Role for the new kubelet version during upgrades. |
| 147 | +// If the role already exists this function is a no-op. |
| 148 | +func (w *Workload) ReconcileKubeletRBACRole(ctx context.Context, version semver.Version) error { |
| 149 | + return w.EnsureResource(ctx, &rbacv1.Role{ |
| 150 | + ObjectMeta: metav1.ObjectMeta{ |
| 151 | + Name: generateKubeletConfigRoleName(version), |
| 152 | + Namespace: metav1.NamespaceSystem, |
| 153 | + }, |
| 154 | + Rules: []rbacv1.PolicyRule{ |
| 155 | + { |
| 156 | + Verbs: []string{"get"}, |
| 157 | + APIGroups: []string{""}, |
| 158 | + Resources: []string{"configmaps"}, |
| 159 | + ResourceNames: []string{generateKubeletConfigName(version)}, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }) |
| 163 | +} |
0 commit comments