|
| 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 kubebindprovider |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + kcpapiextensionsclientset "github.com/kcp-dev/client-go/apiextensions/client" |
| 24 | + kcpdynamic "github.com/kcp-dev/client-go/dynamic" |
| 25 | + apisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1" |
| 26 | + kcpclient "github.com/kcp-dev/kcp/sdk/client/clientset/versioned" |
| 27 | + kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster" |
| 28 | + "github.com/kcp-dev/logicalcluster/v3" |
| 29 | + "github.com/kube-bind/kube-bind/contrib/example-backend-kcp/bootstrap/config/kube-bind-provider/resources" |
| 30 | + |
| 31 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/apimachinery/pkg/util/sets" |
| 34 | + "k8s.io/apimachinery/pkg/util/wait" |
| 35 | + "k8s.io/klog/v2" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + // RootClusterName is the workspace to host common APIs. |
| 40 | + RootClusterName = logicalcluster.NewPath("root:kube-bind-provider") |
| 41 | +) |
| 42 | + |
| 43 | +// Bootstrap creates resources in this package by continuously retrying the list. |
| 44 | +// This is blocking, i.e. it only returns (with error) when the context is closed or with nil when |
| 45 | +// the bootstrapping is successfully completed. |
| 46 | +func Bootstrap( |
| 47 | + ctx context.Context, |
| 48 | + kcpClientSet kcpclientset.ClusterInterface, |
| 49 | + apiExtensionClusterClient kcpapiextensionsclientset.ClusterInterface, |
| 50 | + dynamicClusterClient kcpdynamic.ClusterInterface, |
| 51 | + batteriesIncluded sets.Set[string], |
| 52 | +) error { |
| 53 | + kcpClient := kcpClientSet.Cluster(RootClusterName) |
| 54 | + |
| 55 | + computeDiscoveryClient := apiExtensionClusterClient.Cluster(RootClusterName).Discovery() |
| 56 | + computeDynamicClient := dynamicClusterClient.Cluster(RootClusterName) |
| 57 | + |
| 58 | + crdClient := apiExtensionClusterClient.ApiextensionsV1().Cluster(RootClusterName).CustomResourceDefinitions() |
| 59 | + |
| 60 | + err := bindAPIExport(ctx, kcpClient, "kube-bind.io") |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + time.Sleep(10 * time.Second) |
| 66 | + |
| 67 | + return resources.Bootstrap(ctx, kcpClientSet, computeDiscoveryClient, computeDynamicClient, crdClient, batteriesIncluded) |
| 68 | +} |
| 69 | + |
| 70 | +func bindAPIExport(ctx context.Context, kcpClient kcpclient.Interface, exportName string) error { |
| 71 | + logger := klog.FromContext(ctx) |
| 72 | + |
| 73 | + binding := &apisv1alpha1.APIBinding{ |
| 74 | + ObjectMeta: metav1.ObjectMeta{ |
| 75 | + Name: exportName, |
| 76 | + }, |
| 77 | + Spec: apisv1alpha1.APIBindingSpec{ |
| 78 | + Reference: apisv1alpha1.BindingReference{ |
| 79 | + Export: &apisv1alpha1.ExportBindingReference{ |
| 80 | + Path: "root:kube-bind", |
| 81 | + Name: exportName, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + binding.Spec.PermissionClaims = []apisv1alpha1.AcceptablePermissionClaim{ |
| 88 | + { |
| 89 | + PermissionClaim: apisv1alpha1.PermissionClaim{ |
| 90 | + All: true, |
| 91 | + GroupResource: apisv1alpha1.GroupResource{ |
| 92 | + Group: "rbac.authorization.k8s.io", |
| 93 | + Resource: "clusterrolebindings", |
| 94 | + }, |
| 95 | + }, |
| 96 | + State: apisv1alpha1.ClaimAccepted, |
| 97 | + }, |
| 98 | + { |
| 99 | + PermissionClaim: apisv1alpha1.PermissionClaim{ |
| 100 | + All: true, |
| 101 | + GroupResource: apisv1alpha1.GroupResource{ |
| 102 | + Group: "rbac.authorization.k8s.io", |
| 103 | + Resource: "clusterroles", |
| 104 | + }, |
| 105 | + }, |
| 106 | + State: apisv1alpha1.ClaimAccepted, |
| 107 | + }, |
| 108 | + { |
| 109 | + PermissionClaim: apisv1alpha1.PermissionClaim{ |
| 110 | + All: true, |
| 111 | + GroupResource: apisv1alpha1.GroupResource{ |
| 112 | + Group: "", |
| 113 | + Resource: "serviceaccounts", |
| 114 | + }, |
| 115 | + }, |
| 116 | + State: apisv1alpha1.ClaimAccepted, |
| 117 | + }, |
| 118 | + { |
| 119 | + PermissionClaim: apisv1alpha1.PermissionClaim{ |
| 120 | + All: true, |
| 121 | + GroupResource: apisv1alpha1.GroupResource{ |
| 122 | + Group: "", |
| 123 | + Resource: "configmaps", |
| 124 | + }, |
| 125 | + }, |
| 126 | + State: apisv1alpha1.ClaimAccepted, |
| 127 | + }, |
| 128 | + { |
| 129 | + PermissionClaim: apisv1alpha1.PermissionClaim{ |
| 130 | + All: true, |
| 131 | + GroupResource: apisv1alpha1.GroupResource{ |
| 132 | + Group: "", |
| 133 | + Resource: "secrets", |
| 134 | + }, |
| 135 | + }, |
| 136 | + State: apisv1alpha1.ClaimAccepted, |
| 137 | + }, |
| 138 | + { |
| 139 | + PermissionClaim: apisv1alpha1.PermissionClaim{ |
| 140 | + All: true, |
| 141 | + GroupResource: apisv1alpha1.GroupResource{ |
| 142 | + Group: apisv1alpha1.SchemeGroupVersion.Group, |
| 143 | + Resource: "apiexports", |
| 144 | + }, |
| 145 | + }, |
| 146 | + State: apisv1alpha1.ClaimAccepted, |
| 147 | + }, |
| 148 | + { |
| 149 | + PermissionClaim: apisv1alpha1.PermissionClaim{ |
| 150 | + All: true, |
| 151 | + GroupResource: apisv1alpha1.GroupResource{ |
| 152 | + Group: "apiextensions.k8s.io", |
| 153 | + Resource: "customresourcedefinitions", |
| 154 | + }, |
| 155 | + }, |
| 156 | + State: apisv1alpha1.ClaimAccepted, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + _, err := kcpClient.ApisV1alpha1().APIBindings().Create(ctx, binding, metav1.CreateOptions{}) |
| 161 | + if err == nil { |
| 162 | + return nil |
| 163 | + } |
| 164 | + if !apierrors.IsAlreadyExists(err) { |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + if err := wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (bool, error) { |
| 169 | + existing, err := kcpClient.ApisV1alpha1().APIBindings().Get(ctx, exportName, metav1.GetOptions{}) |
| 170 | + if err != nil { |
| 171 | + logger.Error(err, "error getting APIBinding", "name", exportName) |
| 172 | + // Always keep trying. Don't ever return an error out of this function. |
| 173 | + return false, nil |
| 174 | + } |
| 175 | + |
| 176 | + logger.V(2).Info("Updating API binding") |
| 177 | + existing.Spec = binding.Spec |
| 178 | + |
| 179 | + _, err = kcpClient.ApisV1alpha1().APIBindings().Update(ctx, existing, metav1.UpdateOptions{}) |
| 180 | + if err == nil { |
| 181 | + return true, nil |
| 182 | + } |
| 183 | + if apierrors.IsConflict(err) { |
| 184 | + logger.V(2).Info("API binding update conflict, retrying") |
| 185 | + return false, nil |
| 186 | + } |
| 187 | + |
| 188 | + logger.Error(err, "error updating APIBinding") |
| 189 | + // Always keep trying. Don't ever return an error out of this function. |
| 190 | + return false, nil |
| 191 | + }); err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + |
| 195 | + return nil |
| 196 | +} |
0 commit comments