|
| 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 | +package vmware |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2" |
| 23 | + apitypes "k8s.io/apimachinery/pkg/types" |
| 24 | + capvcontext "sigs.k8s.io/cluster-api-provider-vsphere/pkg/context" |
| 25 | + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" |
| 26 | + "sigs.k8s.io/cluster-api/util/predicates" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 34 | +) |
| 35 | + |
| 36 | +// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters,verbs=get;list;watch |
| 37 | +// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters/status,verbs=get |
| 38 | +// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=virtualmachinegroups,verbs=get;list;watch;create;update;patch;delete |
| 39 | +// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=virtualmachinegroups/status,verbs=get |
| 40 | +// +kubebuilder:rbac:groups=vmware.infrastructure.cluster.x-k8s.io,resources=vsphereclusters,verbs=get;list;watch |
| 41 | +// +kubebuilder:rbac:groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachines,verbs=get;list;watch |
| 42 | +// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machinedeployments,verbs=get;list;watch |
| 43 | +// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines,verbs=get;list;watch |
| 44 | + |
| 45 | +// AddVirtualMachineGroupControllerToManager adds the VirtualMachineGroup controller to the provided |
| 46 | +// manager. |
| 47 | +func AddVirtualMachineGroupControllerToManager(ctx context.Context, controllerManagerCtx *capvcontext.ControllerManagerContext, mgr manager.Manager, options controller.Options) error { |
| 48 | + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "virtualmachinegroup") |
| 49 | + |
| 50 | + reconciler := &VirtualMachineGroupReconciler{ |
| 51 | + Client: controllerManagerCtx.Client, |
| 52 | + Recorder: mgr.GetEventRecorderFor("virtualmachinegroup-controller"), |
| 53 | + } |
| 54 | + |
| 55 | + // Predicate: only allow VMG with the cluster-name label |
| 56 | + hasClusterNameLabel := predicate.NewPredicateFuncs(func(obj ctrlclient.Object) bool { |
| 57 | + labels := obj.GetLabels() |
| 58 | + if labels == nil { |
| 59 | + return false |
| 60 | + } |
| 61 | + _, ok := labels[clusterv1.ClusterNameLabel] |
| 62 | + return ok |
| 63 | + }) |
| 64 | + |
| 65 | + builder := ctrl.NewControllerManagedBy(mgr). |
| 66 | + For(&vmoprv1.VirtualMachineGroup{}). |
| 67 | + WithOptions(options). |
| 68 | + WithEventFilter(hasClusterNameLabel). |
| 69 | + Watches( |
| 70 | + &clusterv1.Cluster{}, |
| 71 | + handler.EnqueueRequestsFromMapFunc(reconciler.ClusterToVirtualMachineGroup), |
| 72 | + ). |
| 73 | + WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, controllerManagerCtx.WatchFilterValue)) |
| 74 | + |
| 75 | + return builder.Complete(reconciler) |
| 76 | +} |
| 77 | + |
| 78 | +func (r VirtualMachineGroupReconciler) ClusterToVirtualMachineGroup(ctx context.Context, a ctrlclient.Object) []reconcile.Request { |
| 79 | + cluster, ok := a.(*clusterv1.Cluster) |
| 80 | + if !ok { |
| 81 | + return nil |
| 82 | + } |
| 83 | + |
| 84 | + // Always enqueue a request for the "would-be VMG" |
| 85 | + return []reconcile.Request{{ |
| 86 | + NamespacedName: apitypes.NamespacedName{ |
| 87 | + Namespace: cluster.Namespace, |
| 88 | + Name: cluster.Name, |
| 89 | + }, |
| 90 | + }} |
| 91 | +} |
0 commit comments