|
| 1 | +/* |
| 2 | +Copyright 2025 The Kube Bind 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 plugin |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + appsv1 "k8s.io/api/apps/v1" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + rbacv1 "k8s.io/api/rbac/v1" |
| 25 | + "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 30 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 31 | + "k8s.io/client-go/discovery" |
| 32 | + "k8s.io/client-go/dynamic" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + KonnectorNamespace = "kube-bind" |
| 37 | + KonnectorAppName = "konnector" |
| 38 | + KonnectorServiceAccount = "konnector" |
| 39 | + KonnectorClusterRole = "kube-bind-konnector" |
| 40 | + KonnectorClusterRoleBinding = "kube-bind-konnector" |
| 41 | +) |
| 42 | + |
| 43 | +func bootstrapKonnector( |
| 44 | + ctx context.Context, |
| 45 | + discoveryClient discovery.DiscoveryInterface, |
| 46 | + dynamicClient dynamic.Interface, |
| 47 | + image string, |
| 48 | + hostAliases []corev1.HostAlias, |
| 49 | +) error { |
| 50 | + resources := getAllKonnectorResources(image, hostAliases) |
| 51 | + |
| 52 | + for _, resource := range resources { |
| 53 | + if err := applyResource(ctx, dynamicClient, discoveryClient, resource); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// getAllKonnectorResources returns all konnector resources needed for deployment |
| 62 | +func getAllKonnectorResources(image string, hostAliases []corev1.HostAlias) []interface{} { |
| 63 | + return []interface{}{ |
| 64 | + getKonnectorNamespace(), |
| 65 | + getKonnectorServiceAccount(), |
| 66 | + getKonnectorClusterRole(), |
| 67 | + getKonnectorClusterRoleBinding(), |
| 68 | + getKonnectorDeployment(image, hostAliases), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// getKonnectorNamespace returns the namespace for konnector deployment |
| 73 | +func getKonnectorNamespace() *corev1.Namespace { |
| 74 | + return &corev1.Namespace{ |
| 75 | + TypeMeta: metav1.TypeMeta{ |
| 76 | + APIVersion: "v1", |
| 77 | + Kind: "Namespace", |
| 78 | + }, |
| 79 | + ObjectMeta: metav1.ObjectMeta{ |
| 80 | + Name: KonnectorNamespace, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// getKonnectorServiceAccount returns the service account for konnector |
| 86 | +func getKonnectorServiceAccount() *corev1.ServiceAccount { |
| 87 | + return &corev1.ServiceAccount{ |
| 88 | + TypeMeta: metav1.TypeMeta{ |
| 89 | + APIVersion: "v1", |
| 90 | + Kind: "ServiceAccount", |
| 91 | + }, |
| 92 | + ObjectMeta: metav1.ObjectMeta{ |
| 93 | + Name: KonnectorServiceAccount, |
| 94 | + Namespace: KonnectorNamespace, |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// getKonnectorClusterRole returns the cluster role for konnector |
| 100 | +func getKonnectorClusterRole() *rbacv1.ClusterRole { |
| 101 | + return &rbacv1.ClusterRole{ |
| 102 | + TypeMeta: metav1.TypeMeta{ |
| 103 | + APIVersion: "rbac.authorization.k8s.io/v1", |
| 104 | + Kind: "ClusterRole", |
| 105 | + }, |
| 106 | + ObjectMeta: metav1.ObjectMeta{ |
| 107 | + Name: KonnectorClusterRole, |
| 108 | + }, |
| 109 | + Rules: []rbacv1.PolicyRule{ |
| 110 | + { |
| 111 | + APIGroups: []string{"*"}, |
| 112 | + Resources: []string{"*"}, |
| 113 | + Verbs: []string{"*"}, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// getKonnectorClusterRoleBinding returns the cluster role binding for konnector |
| 120 | +func getKonnectorClusterRoleBinding() *rbacv1.ClusterRoleBinding { |
| 121 | + return &rbacv1.ClusterRoleBinding{ |
| 122 | + TypeMeta: metav1.TypeMeta{ |
| 123 | + APIVersion: "rbac.authorization.k8s.io/v1", |
| 124 | + Kind: "ClusterRoleBinding", |
| 125 | + }, |
| 126 | + ObjectMeta: metav1.ObjectMeta{ |
| 127 | + Name: KonnectorClusterRoleBinding, |
| 128 | + }, |
| 129 | + RoleRef: rbacv1.RoleRef{ |
| 130 | + APIGroup: "rbac.authorization.k8s.io", |
| 131 | + Kind: "ClusterRole", |
| 132 | + Name: KonnectorClusterRole, |
| 133 | + }, |
| 134 | + Subjects: []rbacv1.Subject{ |
| 135 | + { |
| 136 | + Kind: "ServiceAccount", |
| 137 | + Name: KonnectorServiceAccount, |
| 138 | + Namespace: KonnectorNamespace, |
| 139 | + }, |
| 140 | + }, |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// getKonnectorDeployment returns the deployment for konnector |
| 145 | +func getKonnectorDeployment(image string, hostAliases []corev1.HostAlias) *appsv1.Deployment { |
| 146 | + replicas := int32(2) |
| 147 | + |
| 148 | + return &appsv1.Deployment{ |
| 149 | + TypeMeta: metav1.TypeMeta{ |
| 150 | + APIVersion: "apps/v1", |
| 151 | + Kind: "Deployment", |
| 152 | + }, |
| 153 | + ObjectMeta: metav1.ObjectMeta{ |
| 154 | + Name: KonnectorAppName, |
| 155 | + Namespace: KonnectorNamespace, |
| 156 | + Labels: map[string]string{ |
| 157 | + "app": KonnectorAppName, |
| 158 | + }, |
| 159 | + }, |
| 160 | + Spec: appsv1.DeploymentSpec{ |
| 161 | + Replicas: &replicas, |
| 162 | + Selector: &metav1.LabelSelector{ |
| 163 | + MatchLabels: map[string]string{ |
| 164 | + "app": KonnectorAppName, |
| 165 | + }, |
| 166 | + }, |
| 167 | + Template: corev1.PodTemplateSpec{ |
| 168 | + ObjectMeta: metav1.ObjectMeta{ |
| 169 | + Labels: map[string]string{ |
| 170 | + "app": KonnectorAppName, |
| 171 | + }, |
| 172 | + }, |
| 173 | + Spec: corev1.PodSpec{ |
| 174 | + RestartPolicy: corev1.RestartPolicyAlways, |
| 175 | + ServiceAccountName: KonnectorServiceAccount, |
| 176 | + HostAliases: hostAliases, |
| 177 | + Containers: []corev1.Container{ |
| 178 | + { |
| 179 | + Name: KonnectorAppName, |
| 180 | + Image: image, |
| 181 | + Env: []corev1.EnvVar{ |
| 182 | + { |
| 183 | + Name: "POD_NAME", |
| 184 | + ValueFrom: &corev1.EnvVarSource{ |
| 185 | + FieldRef: &corev1.ObjectFieldSelector{ |
| 186 | + FieldPath: "metadata.name", |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + { |
| 191 | + Name: "POD_NAMESPACE", |
| 192 | + ValueFrom: &corev1.EnvVarSource{ |
| 193 | + FieldRef: &corev1.ObjectFieldSelector{ |
| 194 | + FieldPath: "metadata.namespace", |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + ReadinessProbe: &corev1.Probe{ |
| 200 | + ProbeHandler: corev1.ProbeHandler{ |
| 201 | + HTTPGet: &corev1.HTTPGetAction{ |
| 202 | + Path: "/healthz", |
| 203 | + Port: intstr.FromInt(8090), |
| 204 | + }, |
| 205 | + }, |
| 206 | + }, |
| 207 | + }, |
| 208 | + }, |
| 209 | + }, |
| 210 | + }, |
| 211 | + }, |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +func applyResource(ctx context.Context, dynamicClient dynamic.Interface, discoveryClient discovery.DiscoveryInterface, obj interface{}) error { |
| 216 | + runtimeObj, ok := obj.(runtime.Object) |
| 217 | + if !ok { |
| 218 | + return nil |
| 219 | + } |
| 220 | + |
| 221 | + // Convert to unstructured |
| 222 | + unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(runtimeObj) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + |
| 227 | + // Extract metadata |
| 228 | + metadata, ok := unstructuredObj["metadata"].(map[string]interface{}) |
| 229 | + if !ok { |
| 230 | + return nil |
| 231 | + } |
| 232 | + |
| 233 | + name, _ := metadata["name"].(string) |
| 234 | + namespace, _ := metadata["namespace"].(string) |
| 235 | + |
| 236 | + // Get GVK information |
| 237 | + gvk := runtimeObj.GetObjectKind().GroupVersionKind() |
| 238 | + |
| 239 | + // Get GVR from discovery |
| 240 | + gvr := getGVRFromGVK(gvk) |
| 241 | + |
| 242 | + // Apply the resource |
| 243 | + var resourceClient dynamic.ResourceInterface |
| 244 | + if namespace != "" { |
| 245 | + resourceClient = dynamicClient.Resource(gvr).Namespace(namespace) |
| 246 | + } else { |
| 247 | + resourceClient = dynamicClient.Resource(gvr) |
| 248 | + } |
| 249 | + |
| 250 | + // Try to get existing resource |
| 251 | + _, err = resourceClient.Get(ctx, name, metav1.GetOptions{}) |
| 252 | + if err != nil { |
| 253 | + if errors.IsNotFound(err) { |
| 254 | + // Create new resource |
| 255 | + _, err = resourceClient.Create(ctx, &unstructured.Unstructured{Object: unstructuredObj}, metav1.CreateOptions{}) |
| 256 | + return err |
| 257 | + } |
| 258 | + return err |
| 259 | + } |
| 260 | + |
| 261 | + // Update existing resource |
| 262 | + _, err = resourceClient.Update(ctx, &unstructured.Unstructured{Object: unstructuredObj}, metav1.UpdateOptions{}) |
| 263 | + return err |
| 264 | +} |
| 265 | + |
| 266 | +func getGVRFromGVK(gvk schema.GroupVersionKind) schema.GroupVersionResource { |
| 267 | + gvkMap := map[schema.GroupVersionKind]schema.GroupVersionResource{ |
| 268 | + {Group: "", Version: "v1", Kind: "Namespace"}: { |
| 269 | + Group: "", |
| 270 | + Version: "v1", |
| 271 | + Resource: "namespaces", |
| 272 | + }, |
| 273 | + {Group: "", Version: "v1", Kind: "ServiceAccount"}: { |
| 274 | + Group: "", |
| 275 | + Version: "v1", |
| 276 | + Resource: "serviceaccounts", |
| 277 | + }, |
| 278 | + {Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"}: { |
| 279 | + Group: "rbac.authorization.k8s.io", |
| 280 | + Version: "v1", |
| 281 | + Resource: "clusterroles", |
| 282 | + }, |
| 283 | + {Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRoleBinding"}: { |
| 284 | + Group: "rbac.authorization.k8s.io", |
| 285 | + Version: "v1", |
| 286 | + Resource: "clusterrolebindings", |
| 287 | + }, |
| 288 | + {Group: "apps", Version: "v1", Kind: "Deployment"}: { |
| 289 | + Group: "apps", |
| 290 | + Version: "v1", |
| 291 | + Resource: "deployments", |
| 292 | + }, |
| 293 | + } |
| 294 | + if gvr, found := gvkMap[gvk]; found { |
| 295 | + return gvr |
| 296 | + } |
| 297 | + |
| 298 | + return schema.GroupVersionResource{} |
| 299 | +} |
0 commit comments