-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define strategy to provide value for bundleLookupTag in Cluster Templ…
…ate (#287) * Define strategy to provide value for bundleLookupTag in Cluster Template Signed-off-by: Hui Chen <[email protected]>
- Loading branch information
1 parent
ae784dc
commit 26b4bc2
Showing
8 changed files
with
247 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2021 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"errors" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
) | ||
|
||
// log is for logging in this package. | ||
var byoclusterlog = logf.Log.WithName("byocluster-resource") | ||
|
||
func (r *ByoCluster) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(r). | ||
Complete() | ||
} | ||
|
||
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-byocluster,mutating=true,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=byoclusters,verbs=create;update,versions=v1beta1,name=mbyocluster.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Defaulter = &ByoCluster{} | ||
|
||
// Default implements webhook.Defaulter so a webhook will be registered for the type | ||
// nolint: stylecheck | ||
func (r *ByoCluster) Default() { | ||
} | ||
|
||
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. | ||
//+kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-byocluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=byoclusters,verbs=create;update,versions=v1beta1,name=vbyocluster.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &ByoCluster{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *ByoCluster) ValidateCreate() error { | ||
byoclusterlog.Info("validate create", "name", r.Name) | ||
if r.Spec.BundleLookupTag == "" { | ||
return apierrors.NewInvalid(r.GroupVersionKind().GroupKind(), r.Name, field.ErrorList{ | ||
field.InternalError(nil, errors.New("cannot create ByoCluster without Spec.BundleLookupTag")), | ||
}) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *ByoCluster) ValidateUpdate(old runtime.Object) error { | ||
byoclusterlog.Info("validate update", "name", r.Name) | ||
if r.Spec.BundleLookupTag == "" { | ||
return apierrors.NewInvalid(r.GroupVersionKind().GroupKind(), r.Name, field.ErrorList{ | ||
field.InternalError(nil, errors.New("cannot update ByoCluster with empty Spec.BundleLookupTag")), | ||
}) | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *ByoCluster) ValidateDelete() error { | ||
// TODO(user): fill in your validation logic upon object deletion. | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2021 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/kubectl/pkg/scheme" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var _ = Describe("ByoclusterWebhook", func() { | ||
Context("When ByoCluster gets a create request", func() { | ||
var ( | ||
byoCluster *ByoCluster | ||
ctx context.Context | ||
k8sClientUncached client.Client | ||
) | ||
BeforeEach(func() { | ||
ctx = context.Background() | ||
var clientErr error | ||
k8sClientUncached, clientErr = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
Expect(clientErr).NotTo(HaveOccurred()) | ||
|
||
byoCluster = &ByoCluster{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "ByoCluster", | ||
APIVersion: clusterv1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "byocluster-create", | ||
Namespace: "default", | ||
}, | ||
Spec: ByoClusterSpec{}, | ||
} | ||
}) | ||
|
||
It("should reject the request when BundleLookupTag is empty", func() { | ||
err := k8sClientUncached.Create(ctx, byoCluster) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err).To(MatchError("admission webhook \"vbyocluster.kb.io\" denied the request: ByoCluster.infrastructure.cluster.x-k8s.io \"" + byoCluster.Name + "\" is invalid: <nil>: Internal error: cannot create ByoCluster without Spec.BundleLookupTag")) | ||
}) | ||
|
||
It("should success when BundleLookupTag is not empty", func() { | ||
byoCluster.Spec.BundleLookupTag = "v0.1.0_alpha.2" | ||
err := k8sClientUncached.Create(ctx, byoCluster) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
}) | ||
|
||
Context("When ByoCluster gets an update request", func() { | ||
var ( | ||
byoCluster *ByoCluster | ||
ctx context.Context | ||
k8sClientUncached client.Client | ||
) | ||
BeforeEach(func() { | ||
ctx = context.Background() | ||
var clientErr error | ||
k8sClientUncached, clientErr = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
Expect(clientErr).NotTo(HaveOccurred()) | ||
|
||
byoCluster = &ByoCluster{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "ByoCluster", | ||
APIVersion: clusterv1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "byocluster-update", | ||
Namespace: "default", | ||
}, | ||
Spec: ByoClusterSpec{ | ||
BundleLookupTag: "v0.1.0_alpha.2", | ||
}, | ||
} | ||
|
||
Expect(k8sClientUncached.Create(ctx, byoCluster)).Should(Succeed()) | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(k8sClientUncached.Delete(ctx, byoCluster)).Should(Succeed()) | ||
}) | ||
|
||
It("should reject the request when BundleLookupTag is empty", func() { | ||
byoCluster.Spec.BundleLookupTag = "" | ||
err := k8sClientUncached.Update(ctx, byoCluster) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err).To(MatchError("admission webhook \"vbyocluster.kb.io\" denied the request: ByoCluster.infrastructure.cluster.x-k8s.io \"" + byoCluster.Name + "\" is invalid: <nil>: Internal error: cannot update ByoCluster with empty Spec.BundleLookupTag")) | ||
}) | ||
|
||
It("should update the cluster with new BundleLookupTag value", func() { | ||
newBundleLookupTag := "new_tag" | ||
|
||
byoCluster.Spec.BundleLookupTag = newBundleLookupTag | ||
err := k8sClientUncached.Update(ctx, byoCluster) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
updatedByoCluster := &ByoCluster{} | ||
byoCLusterLookupKey := types.NamespacedName{Name: byoCluster.Name, Namespace: byoCluster.Namespace} | ||
Expect(k8sClientUncached.Get(ctx, byoCLusterLookupKey, updatedByoCluster)).Should(Not(HaveOccurred())) | ||
Expect(updatedByoCluster.Spec.BundleLookupTag).To(Equal(newBundleLookupTag)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters