|
| 1 | +/* |
| 2 | +Copyright 2023 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 controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + csov1alpha1 "github.com/SovereignCloudStack/cluster-stack-operator/api/v1alpha1" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 29 | + "sigs.k8s.io/cluster-api/util/predicates" |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 37 | +) |
| 38 | + |
| 39 | +// ClusterAddonCreateReconciler reconciles a Cluster object. |
| 40 | +type ClusterAddonCreateReconciler struct { |
| 41 | + client.Client |
| 42 | + WatchFilterValue string |
| 43 | +} |
| 44 | + |
| 45 | +//+kubebuilder:rbac:groups=clusterstack.x-k8s.io,resources=clusteraddons,verbs=create |
| 46 | + |
| 47 | +// Reconcile is part of the main Kubernetes reconciliation loop which aims to |
| 48 | +// move the current state of the cluster closer to the desired state. |
| 49 | +func (r *ClusterAddonCreateReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { |
| 50 | + cluster := &clusterv1.Cluster{} |
| 51 | + if err := r.Get(ctx, req.NamespacedName, cluster); err != nil { |
| 52 | + // if the cluster is not found, exit the reconciliation |
| 53 | + if apierrors.IsNotFound(err) { |
| 54 | + return reconcile.Result{}, nil |
| 55 | + } |
| 56 | + return reconcile.Result{}, fmt.Errorf("failed to get cluster: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + clusterAddonName := fmt.Sprintf("cluster-addon-%s", cluster.Name) |
| 60 | + |
| 61 | + var existingClusterAddon csov1alpha1.ClusterAddon |
| 62 | + err := r.Get(ctx, types.NamespacedName{Namespace: cluster.Namespace, Name: clusterAddonName}, &existingClusterAddon) |
| 63 | + // no error means that the object already exists - nothing to do |
| 64 | + if err == nil { |
| 65 | + return reconcile.Result{}, nil |
| 66 | + } |
| 67 | + // unexpected error - return it |
| 68 | + if !apierrors.IsNotFound(err) { |
| 69 | + return reconcile.Result{}, fmt.Errorf("failed to get ClusterAddon object: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + // clusterAddon could not be found - create it |
| 73 | + clusterAddon := &csov1alpha1.ClusterAddon{ |
| 74 | + ObjectMeta: metav1.ObjectMeta{Name: clusterAddonName, Namespace: cluster.Namespace}, |
| 75 | + TypeMeta: metav1.TypeMeta{Kind: "ClusterAddon", APIVersion: "clusterstack.x-k8s.io/v1alpha1"}, |
| 76 | + Spec: csov1alpha1.ClusterAddonSpec{ |
| 77 | + ClusterRef: &corev1.ObjectReference{ |
| 78 | + APIVersion: cluster.APIVersion, |
| 79 | + Kind: cluster.Kind, |
| 80 | + Name: cluster.Name, |
| 81 | + UID: cluster.UID, |
| 82 | + Namespace: cluster.Namespace, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + clusterAddon.OwnerReferences = append(clusterAddon.OwnerReferences, metav1.OwnerReference{ |
| 88 | + APIVersion: cluster.APIVersion, |
| 89 | + Kind: cluster.Kind, |
| 90 | + Name: cluster.Name, |
| 91 | + UID: cluster.UID, |
| 92 | + }) |
| 93 | + |
| 94 | + if err := r.Create(ctx, clusterAddon); err != nil { |
| 95 | + return reconcile.Result{}, fmt.Errorf("failed to create ClusterAddon object: %w", err) |
| 96 | + } |
| 97 | + return reconcile.Result{}, nil |
| 98 | +} |
| 99 | + |
| 100 | +// SetupWithManager sets up the controller with the Manager. |
| 101 | +func (r *ClusterAddonCreateReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { |
| 102 | + return ctrl.NewControllerManagedBy(mgr). |
| 103 | + WithOptions(options). |
| 104 | + For(&clusterv1.Cluster{}). |
| 105 | + WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log.FromContext(ctx), r.WatchFilterValue)). |
| 106 | + WithEventFilter(predicate.Funcs{ |
| 107 | + // We're only interested in the create events for a cluster object |
| 108 | + DeleteFunc: func(e event.DeleteEvent) bool { |
| 109 | + return false |
| 110 | + }, |
| 111 | + GenericFunc: func(e event.GenericEvent) bool { |
| 112 | + return false |
| 113 | + }, |
| 114 | + UpdateFunc: func(e event.UpdateEvent) bool { |
| 115 | + return false |
| 116 | + }, |
| 117 | + }). |
| 118 | + Complete(r) |
| 119 | +} |
0 commit comments