|
| 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 v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/SovereignCloudStack/cluster-stack-operator/pkg/clusterstack" |
| 24 | + "github.com/SovereignCloudStack/cluster-stack-operator/pkg/release" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 29 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 34 | +) |
| 35 | + |
| 36 | +// Cluster implements a validating and defaulting webhook for Cluster. |
| 37 | +// +k8s:deepcopy-gen=false |
| 38 | +type Cluster struct { |
| 39 | + Client client.Client |
| 40 | +} |
| 41 | + |
| 42 | +// SetupWebhookWithManager initializes webhook manager for ClusterStack. |
| 43 | +func (r *Cluster) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 44 | + r.Client = mgr.GetClient() |
| 45 | + return ctrl.NewWebhookManagedBy(mgr). |
| 46 | + For(&clusterv1.Cluster{}). |
| 47 | + WithValidator(r). |
| 48 | + Complete() |
| 49 | +} |
| 50 | + |
| 51 | +//+kubebuilder:webhook:path=/validate-cluster-x-k8s-io-v1beta1-cluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=cluster.x-k8s.io,resources=clusters,verbs=create;update,versions=v1beta1,name=validation.cluster.cluster.x-k8s.io,admissionReviewVersions={v1,v1beta1} |
| 52 | + |
| 53 | +var _ webhook.CustomValidator = &Cluster{} |
| 54 | + |
| 55 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. |
| 56 | +func (r *Cluster) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 57 | + cluster, ok := obj.(*clusterv1.Cluster) |
| 58 | + if !ok { |
| 59 | + return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a Cluster but got a %T", obj)) |
| 60 | + } |
| 61 | + |
| 62 | + return r.isVersionCorrect(ctx, cluster) |
| 63 | +} |
| 64 | + |
| 65 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. |
| 66 | +func (r *Cluster) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 67 | + oldCluster, ok := oldObj.(*clusterv1.Cluster) |
| 68 | + if !ok { |
| 69 | + return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an Cluster but got a %T", oldCluster)) |
| 70 | + } |
| 71 | + |
| 72 | + newCluster, ok := newObj.(*clusterv1.Cluster) |
| 73 | + if !ok { |
| 74 | + return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an Cluster but got a %T", newCluster)) |
| 75 | + } |
| 76 | + |
| 77 | + var allErrs field.ErrorList |
| 78 | + |
| 79 | + warnings, err := r.isVersionCorrect(ctx, newCluster) |
| 80 | + if len(warnings) > 0 || err != nil { |
| 81 | + return warnings, err |
| 82 | + } |
| 83 | + |
| 84 | + csOld, err := clusterstack.NewFromClusterClassProperties(oldCluster.Spec.Topology.Class) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("expected a clusterclass of form <provider>-<clusterStackName>-<kubernetesVersion>-<clusterStackVersion> but got %s: %w", oldCluster.Spec.Topology.Class, err) |
| 87 | + } |
| 88 | + |
| 89 | + csNew, err := clusterstack.NewFromClusterClassProperties(newCluster.Spec.Topology.Class) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("expected a clusterclass of form <provider>-<clusterStackName>-<kubernetesVersion>-<clusterStackVersion> but got %s: %w", newCluster.Spec.Topology.Class, err) |
| 92 | + } |
| 93 | + |
| 94 | + // provider must not change |
| 95 | + if csOld.Provider != csNew.Provider { |
| 96 | + allErrs = append(allErrs, |
| 97 | + field.Invalid(field.NewPath("spec", "topology", "class"), newCluster.Spec.Topology.Class, fmt.Sprintf("provider name must not change. Got %s, want %s", csNew.Provider, csOld.Provider))) |
| 98 | + } |
| 99 | + |
| 100 | + // cluster stack name must not change |
| 101 | + if csOld.Name != csNew.Name { |
| 102 | + allErrs = append(allErrs, |
| 103 | + field.Invalid(field.NewPath("spec", "topology", "class"), newCluster.Spec.Topology.Class, fmt.Sprintf("cluster stack name must not change. Got %s, want %s", csNew.Name, csOld.Name))) |
| 104 | + } |
| 105 | + |
| 106 | + // kubernetes version must be the same or higher by one |
| 107 | + if csNew.KubernetesVersion.Minor-csOld.KubernetesVersion.Minor != 1 && csNew.KubernetesVersion.Minor-csOld.KubernetesVersion.Minor != 0 { |
| 108 | + allErrs = append(allErrs, |
| 109 | + field.Invalid(field.NewPath("spec", "topology", "class"), newCluster.Spec.Topology.Class, fmt.Sprintf("kubernetes version must be the same or higher by one. Got %s, want %s or 1-%d", csNew.KubernetesVersion, csOld.KubernetesVersion, csOld.KubernetesVersion.Minor+1))) |
| 110 | + } |
| 111 | + |
| 112 | + return nil, aggregateObjErrors(oldCluster.GroupVersionKind().GroupKind(), oldCluster.Name, allErrs) |
| 113 | +} |
| 114 | + |
| 115 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. |
| 116 | +func (*Cluster) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { |
| 117 | + return nil, nil |
| 118 | +} |
| 119 | + |
| 120 | +func (r *Cluster) isVersionCorrect(ctx context.Context, cluster *clusterv1.Cluster) (admission.Warnings, error) { |
| 121 | + if cluster.Spec.Topology == nil { |
| 122 | + return nil, field.Invalid(field.NewPath("spec", "topology"), cluster.Spec.Topology, "topology field cannot be empty") |
| 123 | + } |
| 124 | + |
| 125 | + if cluster.Spec.Topology.Class == "" { |
| 126 | + return nil, field.Invalid(field.NewPath("spec", "topology", "class"), cluster.Spec.Topology.Class, "class field cannot be empty") |
| 127 | + } |
| 128 | + |
| 129 | + wantKubernetesVersion, err := r.getClusterStackReleaseVersion(ctx, release.ConvertFromClusterClassToClusterStackFormat(cluster.Spec.Topology.Class), cluster.Namespace) |
| 130 | + if err != nil { |
| 131 | + return admission.Warnings{fmt.Sprintf("cannot validate clusterClass and Kubernetes version. Getting clusterStackRelease object failed: %s", err.Error())}, nil |
| 132 | + } |
| 133 | + |
| 134 | + if wantKubernetesVersion == "" { |
| 135 | + return admission.Warnings{fmt.Sprintf("no Kubernetes version set in status of clusterStackRelease object. Cannot validate Kubernetes version. Check out the ClusterStackReleaseObject %s/%s manually", cluster.Namespace, cluster.Spec.Topology.Class)}, nil |
| 136 | + } |
| 137 | + |
| 138 | + if cluster.Spec.Topology.Version != wantKubernetesVersion { |
| 139 | + return nil, field.Invalid(field.NewPath("spec", "topology", "version"), cluster.Spec.Topology.Version, fmt.Sprintf("clusterClass %s expects Kubernetes version %s, but got %s", cluster.Spec.Topology.Class, wantKubernetesVersion, cluster.Spec.Topology.Version)) |
| 140 | + } |
| 141 | + return nil, nil |
| 142 | +} |
| 143 | + |
| 144 | +func (r *Cluster) getClusterStackReleaseVersion(ctx context.Context, name, namespace string) (string, error) { |
| 145 | + clusterStackRelCR := &ClusterStackRelease{} |
| 146 | + namespacedName := types.NamespacedName{Name: name, Namespace: namespace} |
| 147 | + |
| 148 | + if err := r.Client.Get(ctx, namespacedName, clusterStackRelCR); apierrors.IsNotFound(err) { |
| 149 | + return "", fmt.Errorf("clusterclass does not exist: %w", err) |
| 150 | + } else if err != nil { |
| 151 | + return "", fmt.Errorf("failed to get ClusterStackRelease - cannot validate Kubernetes version: %w", err) |
| 152 | + } |
| 153 | + return clusterStackRelCR.Status.KubernetesVersion, nil |
| 154 | +} |
0 commit comments