|
| 1 | +/* |
| 2 | +Copyright 2025 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 | +// This example demonstrates how to use the ClusterProfiles provider to create |
| 18 | +// a multi-cluster operator that can watch and reconcile resources across |
| 19 | +// multiple Kubernetes clusters using ClusterProfile resources. |
| 20 | +// |
| 21 | +// The operator watches for ClusterProfile resources in a specified namespace |
| 22 | +// and automatically creates connections to the clusters they reference using |
| 23 | +// configured credential providers. |
| 24 | +// |
| 25 | +// Usage: |
| 26 | +// |
| 27 | +// go run main.go --namespace cluster-inventory --credential-providers-file clusterprofile-provider-file.json |
| 28 | +// |
| 29 | +// This will: |
| 30 | +// 1. Set up the ClusterProfiles provider with credential providers |
| 31 | +// 2. Watch for ClusterProfile resources in the specified namespace |
| 32 | +// 3. Create a multi-cluster controller that watches ConfigMaps across all connected clusters |
| 33 | +// 4. Log information about discovered ConfigMaps |
| 34 | +package main |
| 35 | + |
| 36 | +import ( |
| 37 | + "context" |
| 38 | + "errors" |
| 39 | + "flag" |
| 40 | + "log" |
| 41 | + "os" |
| 42 | + |
| 43 | + "k8s.io/apimachinery/pkg/util/runtime" |
| 44 | + "k8s.io/client-go/kubernetes/scheme" |
| 45 | + "sigs.k8s.io/cluster-inventory-api/pkg/credentials" |
| 46 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 47 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 48 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 49 | + |
| 50 | + corev1 "k8s.io/api/core/v1" |
| 51 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 52 | + clusterinventory "sigs.k8s.io/cluster-inventory-api/apis/v1alpha1" |
| 53 | + ctrl "sigs.k8s.io/controller-runtime" |
| 54 | + ctrllog "sigs.k8s.io/controller-runtime/pkg/log" |
| 55 | + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 56 | + mcbuilder "sigs.k8s.io/multicluster-runtime/pkg/builder" |
| 57 | + mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" |
| 58 | + mcreconcile "sigs.k8s.io/multicluster-runtime/pkg/reconcile" |
| 59 | + clusterprofileprovider "sigs.k8s.io/multicluster-runtime/providers/clusterprofile" |
| 60 | +) |
| 61 | + |
| 62 | +func init() { |
| 63 | + runtime.Must(clusterinventory.AddToScheme(scheme.Scheme)) |
| 64 | +} |
| 65 | + |
| 66 | +func main() { |
| 67 | + // Set up command line flags for credential providers configuration |
| 68 | + credentialsProviders := credentials.SetupProviderFileFlag() |
| 69 | + flag.Parse() |
| 70 | + |
| 71 | + // Define the namespace where ClusterProfile resources will be watched |
| 72 | + var clusterInventoryNamespace string |
| 73 | + flag.StringVar(&clusterInventoryNamespace, "namespace", "", "Cluster inventory namespace") |
| 74 | + |
| 75 | + // Set up logging options |
| 76 | + opts := zap.Options{ |
| 77 | + Development: true, |
| 78 | + } |
| 79 | + opts.BindFlags(flag.CommandLine) |
| 80 | + flag.Parse() |
| 81 | + |
| 82 | + // Initialize logger and signal handler |
| 83 | + ctrllog.SetLogger(zap.New(zap.UseFlagOptions(&opts))) |
| 84 | + entryLog := ctrllog.Log.WithName("entrypoint") |
| 85 | + ctx := ctrl.SetupSignalHandler() |
| 86 | + |
| 87 | + entryLog.Info("Starting ClusterProfiles provider example", "namespace", clusterInventoryNamespace) |
| 88 | + |
| 89 | + // Load credential providers from configuration file |
| 90 | + cpCreds, err := credentials.NewFromFile(*credentialsProviders) |
| 91 | + if err != nil { |
| 92 | + log.Fatalf("Got error reading credentials providers: %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + // Create the clusterprofiles provider with options |
| 96 | + providerOpts := clusterprofileprovider.Options{ |
| 97 | + Namespace: clusterInventoryNamespace, |
| 98 | + CredentialsProvider: cpCreds, |
| 99 | + Scheme: scheme.Scheme, |
| 100 | + } |
| 101 | + |
| 102 | + // Create the provider instance |
| 103 | + provider := clusterprofileprovider.New(providerOpts) |
| 104 | + |
| 105 | + // Setup a cluster-aware Manager with the provider to lookup clusters |
| 106 | + managerOpts := manager.Options{ |
| 107 | + Metrics: metricsserver.Options{ |
| 108 | + BindAddress: "0", // Disable metrics server |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + // Create multicluster manager that will coordinate across all discovered clusters |
| 113 | + entryLog.Info("Creating multicluster manager") |
| 114 | + mgr, err := mcmanager.New(ctrl.GetConfigOrDie(), provider, managerOpts) |
| 115 | + if err != nil { |
| 116 | + entryLog.Error(err, "Unable to create manager") |
| 117 | + os.Exit(1) |
| 118 | + } |
| 119 | + |
| 120 | + // Setup provider controller with the manager to start watching ClusterProfile resources |
| 121 | + err = provider.SetupWithManager(ctx, mgr) |
| 122 | + if err != nil { |
| 123 | + entryLog.Error(err, "Unable to setup provider with manager") |
| 124 | + os.Exit(1) |
| 125 | + } |
| 126 | + |
| 127 | + // Create a multi-cluster controller that watches ConfigMaps across all connected clusters |
| 128 | + // This demonstrates how to build controllers that operate across multiple clusters |
| 129 | + err = mcbuilder.ControllerManagedBy(mgr). |
| 130 | + Named("multicluster-configmaps"). |
| 131 | + For(&corev1.ConfigMap{}). |
| 132 | + Complete(mcreconcile.Func( |
| 133 | + func(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) { |
| 134 | + log := ctrllog.FromContext(ctx).WithValues("cluster", req.ClusterName) |
| 135 | + log.Info("Reconciling ConfigMap") |
| 136 | + |
| 137 | + // Get the cluster client for the specific cluster where this resource lives |
| 138 | + cl, err := mgr.GetCluster(ctx, req.ClusterName) |
| 139 | + if err != nil { |
| 140 | + return reconcile.Result{}, err |
| 141 | + } |
| 142 | + |
| 143 | + // Retrieve the ConfigMap from the specific cluster |
| 144 | + cm := &corev1.ConfigMap{} |
| 145 | + if err := cl.GetClient().Get(ctx, req.Request.NamespacedName, cm); err != nil { |
| 146 | + if apierrors.IsNotFound(err) { |
| 147 | + return reconcile.Result{}, nil |
| 148 | + } |
| 149 | + return reconcile.Result{}, err |
| 150 | + } |
| 151 | + |
| 152 | + log.Info("ConfigMap found", "namespace", cm.Namespace, "name", cm.Name, "cluster", req.ClusterName) |
| 153 | + |
| 154 | + // Here you would add your multi-cluster reconciliation logic |
| 155 | + // For example: |
| 156 | + // - Sync resources across clusters |
| 157 | + // - Aggregate data from multiple clusters |
| 158 | + // - Implement cross-cluster policies |
| 159 | + |
| 160 | + return ctrl.Result{}, nil |
| 161 | + }, |
| 162 | + )) |
| 163 | + if err != nil { |
| 164 | + entryLog.Error(err, "unable to create controller") |
| 165 | + os.Exit(1) |
| 166 | + } |
| 167 | + |
| 168 | + // Start the manager - this will begin watching ClusterProfile resources |
| 169 | + // and automatically connect to discovered clusters |
| 170 | + entryLog.Info("Starting manager") |
| 171 | + err = mgr.Start(ctx) |
| 172 | + if err != nil && !errors.Is(err, context.Canceled) { |
| 173 | + entryLog.Error(err, "unable to start") |
| 174 | + os.Exit(1) |
| 175 | + } |
| 176 | +} |
0 commit comments