|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "slices" |
| 24 | + |
| 25 | + "github.com/spf13/pflag" |
| 26 | + "go.uber.org/zap/zapcore" |
| 27 | + |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 30 | + "k8s.io/apimachinery/pkg/util/runtime" |
| 31 | + "k8s.io/client-go/kubernetes/scheme" |
| 32 | + "k8s.io/client-go/rest" |
| 33 | + ctrl "sigs.k8s.io/controller-runtime" |
| 34 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 37 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 38 | + "sigs.k8s.io/controller-runtime/pkg/manager/signals" |
| 39 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 40 | + |
| 41 | + mcbuilder "sigs.k8s.io/multicluster-runtime/pkg/builder" |
| 42 | + mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" |
| 43 | + mcreconcile "sigs.k8s.io/multicluster-runtime/pkg/reconcile" |
| 44 | + |
| 45 | + apisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1" |
| 46 | + corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1" |
| 47 | + "github.com/kcp-dev/kcp/sdk/apis/tenancy/initialization" |
| 48 | + tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1" |
| 49 | + |
| 50 | + "github.com/kcp-dev/multicluster-provider/initializingworkspaces" |
| 51 | +) |
| 52 | + |
| 53 | +func init() { |
| 54 | + runtime.Must(corev1alpha1.AddToScheme(scheme.Scheme)) |
| 55 | + runtime.Must(tenancyv1alpha1.AddToScheme(scheme.Scheme)) |
| 56 | + runtime.Must(apisv1alpha1.AddToScheme(scheme.Scheme)) |
| 57 | +} |
| 58 | + |
| 59 | +func main() { |
| 60 | + var ( |
| 61 | + server string |
| 62 | + initializerName string |
| 63 | + provider *initializingworkspaces.Provider |
| 64 | + verbosity int |
| 65 | + ) |
| 66 | + |
| 67 | + pflag.StringVar(&server, "server", "", "Override for kubeconfig server URL") |
| 68 | + pflag.StringVar(&initializerName, "initializer", "initializer:example", "Name of the initializer to use") |
| 69 | + pflag.IntVar(&verbosity, "v", 1, "Log verbosity level") |
| 70 | + pflag.Parse() |
| 71 | + |
| 72 | + logOpts := zap.Options{ |
| 73 | + Development: true, |
| 74 | + Level: zapcore.Level(-verbosity), |
| 75 | + } |
| 76 | + log.SetLogger(zap.New(zap.UseFlagOptions(&logOpts))) |
| 77 | + |
| 78 | + ctx := signals.SetupSignalHandler() |
| 79 | + entryLog := log.Log.WithName("entrypoint") |
| 80 | + cfg := ctrl.GetConfigOrDie() |
| 81 | + cfg = rest.CopyConfig(cfg) |
| 82 | + |
| 83 | + if server != "" { |
| 84 | + cfg.Host = server |
| 85 | + } |
| 86 | + |
| 87 | + entryLog.Info("Setting up manager") |
| 88 | + opts := manager.Options{} |
| 89 | + |
| 90 | + var err error |
| 91 | + provider, err = initializingworkspaces.New(cfg, initializingworkspaces.Options{InitializerName: initializerName}) |
| 92 | + if err != nil { |
| 93 | + entryLog.Error(err, "unable to construct cluster provider") |
| 94 | + os.Exit(1) |
| 95 | + } |
| 96 | + |
| 97 | + mgr, err := mcmanager.New(cfg, provider, opts) |
| 98 | + if err != nil { |
| 99 | + entryLog.Error(err, "unable to set up overall controller manager") |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | + |
| 103 | + if err := mcbuilder.ControllerManagedBy(mgr). |
| 104 | + Named("kcp-initializer-controller"). |
| 105 | + For(&corev1alpha1.LogicalCluster{}). |
| 106 | + Complete(mcreconcile.Func( |
| 107 | + func(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) { |
| 108 | + log := log.FromContext(ctx).WithValues("cluster", req.ClusterName) |
| 109 | + cl, err := mgr.GetCluster(ctx, req.ClusterName) |
| 110 | + if err != nil { |
| 111 | + return reconcile.Result{}, fmt.Errorf("failed to get cluster: %w", err) |
| 112 | + } |
| 113 | + client := cl.GetClient() |
| 114 | + lc := &corev1alpha1.LogicalCluster{} |
| 115 | + if err := client.Get(ctx, req.NamespacedName, lc); err != nil { |
| 116 | + return reconcile.Result{}, fmt.Errorf("failed to get logical cluster: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + log.Info("Reconciling LogicalCluster", "logical cluster", lc.Spec) |
| 120 | + initializer := corev1alpha1.LogicalClusterInitializer(initializerName) |
| 121 | + // check if your initializer is still set on the logicalcluster |
| 122 | + if slices.Contains(lc.Status.Initializers, initializer) { |
| 123 | + log.Info("Starting to initialize cluster") |
| 124 | + workspaceName := fmt.Sprintf("initialized-workspace-%s", req.ClusterName) |
| 125 | + ws := &tenancyv1alpha1.Workspace{} |
| 126 | + err = client.Get(ctx, ctrlclient.ObjectKey{Name: workspaceName}, ws) |
| 127 | + if err != nil { |
| 128 | + if !apierrors.IsNotFound(err) { |
| 129 | + log.Error(err, "Error checking for existing workspace") |
| 130 | + return reconcile.Result{}, nil |
| 131 | + } |
| 132 | + |
| 133 | + log.Info("Creating child workspace", "name", workspaceName) |
| 134 | + ws = &tenancyv1alpha1.Workspace{ |
| 135 | + ObjectMeta: ctrl.ObjectMeta{ |
| 136 | + Name: workspaceName, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + if err := client.Create(ctx, ws); err != nil { |
| 141 | + log.Error(err, "Failed to create workspace") |
| 142 | + return reconcile.Result{}, nil |
| 143 | + } |
| 144 | + log.Info("Workspace created successfully", "name", workspaceName) |
| 145 | + } |
| 146 | + |
| 147 | + if ws.Status.Phase != corev1alpha1.LogicalClusterPhaseReady { |
| 148 | + log.Info("Workspace not ready yet", "current-phase", ws.Status.Phase) |
| 149 | + return reconcile.Result{Requeue: true}, nil |
| 150 | + } |
| 151 | + log.Info("Workspace is ready, proceeding to create ConfigMap") |
| 152 | + |
| 153 | + s := &corev1.ConfigMap{ |
| 154 | + ObjectMeta: ctrl.ObjectMeta{ |
| 155 | + Name: "kcp-initializer-cm", |
| 156 | + Namespace: "default", |
| 157 | + }, |
| 158 | + Data: map[string]string{ |
| 159 | + "test-data": "example-value", |
| 160 | + }, |
| 161 | + } |
| 162 | + log.Info("Reconciling ConfigMap", "name", s.Name, "uuid", s.UID) |
| 163 | + if err := client.Create(ctx, s); err != nil { |
| 164 | + return reconcile.Result{}, fmt.Errorf("failed to create configmap: %w", err) |
| 165 | + } |
| 166 | + log.Info("ConfigMap created successfully", "name", s.Name, "uuid", s.UID) |
| 167 | + } |
| 168 | + // Remove the initializer from the logical cluster status |
| 169 | + // so that it won't be processed again. |
| 170 | + if !slices.Contains(lc.Status.Initializers, initializer) { |
| 171 | + log.Info("Initializer already absent, skipping patch") |
| 172 | + return reconcile.Result{}, nil |
| 173 | + } |
| 174 | + |
| 175 | + patch := ctrlclient.MergeFrom(lc.DeepCopy()) |
| 176 | + lc.Status.Initializers = initialization.EnsureInitializerAbsent(initializer, lc.Status.Initializers) |
| 177 | + if err := client.Status().Patch(ctx, lc, patch); err != nil { |
| 178 | + return reconcile.Result{}, err |
| 179 | + } |
| 180 | + log.Info("Removed initializer from LogicalCluster status", "name", lc.Name, "uuid", lc.UID) |
| 181 | + return reconcile.Result{}, nil |
| 182 | + }, |
| 183 | + )); err != nil { |
| 184 | + entryLog.Error(err, "failed to build controller") |
| 185 | + os.Exit(1) |
| 186 | + } |
| 187 | + |
| 188 | + if provider != nil { |
| 189 | + entryLog.Info("Starting provider") |
| 190 | + go func() { |
| 191 | + if err := provider.Run(ctx, mgr); err != nil { |
| 192 | + entryLog.Error(err, "unable to run provider") |
| 193 | + os.Exit(1) |
| 194 | + } |
| 195 | + }() |
| 196 | + } |
| 197 | + |
| 198 | + entryLog.Info("Starting manager") |
| 199 | + if err := mgr.Start(ctx); err != nil { |
| 200 | + entryLog.Error(err, "unable to run manager") |
| 201 | + os.Exit(1) |
| 202 | + } |
| 203 | +} |
0 commit comments