Skip to content

Commit

Permalink
Feat: add webhook to resourceFlavor
Browse files Browse the repository at this point in the history
Signed-off-by: kerthcet <kerthcet@gmail.com>
  • Loading branch information
kerthcet committed Jul 15, 2022
1 parent 888cedb commit ff7dfc1
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 9 deletions.
45 changes: 45 additions & 0 deletions apis/kueue/v1alpha1/resource_flavor_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var resourceFlavorLog = ctrl.Log.WithName("resourceFlavor-webhook")

func (rf *ResourceFlavor) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(rf).
Complete()
}

// +kubebuilder:webhook:path=/mutate-kueue-x-k8s-io-v1alpha1-resourceflavor,mutating=true,failurePolicy=fail,sideEffects=None,groups=kueue.x-k8s.io,resources=resourceflavors,verbs=create,versions=v1alpha1,name=mresourceflavor.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &ResourceFlavor{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (rf *ResourceFlavor) Default() {
resourceFlavorLog.Info("defaulter", "resourceFlavor", klog.KObj(rf))
if !controllerutil.ContainsFinalizer(rf, ResourceInUseFinalizerName) {
controllerutil.AddFinalizer(rf, ResourceInUseFinalizerName)
}
}
19 changes: 19 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ metadata:
creationTimestamp: null
name: mutating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-kueue-x-k8s-io-v1alpha1-resourceflavor
failurePolicy: Fail
name: mresourceflavor.kb.io
rules:
- apiGroups:
- kueue.x-k8s.io
apiVersions:
- v1alpha1
operations:
- CREATE
resources:
- resourceflavors
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ func setupControllers(mgr ctrl.Manager, cCache *cache.Cache, queues *queue.Manag
setupLog.Error(err, "unable to create webhook", "webhook", "Workload")
os.Exit(1)
}
if err := (&kueuev1alpha1.ResourceFlavor{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ResourceFlavor")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
}

Expand Down
9 changes: 1 addition & 8 deletions pkg/controller/core/resourceflavor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,7 @@ func (r *ResourceFlavorReconciler) Reconcile(ctx context.Context, req ctrl.Reque
ctx = ctrl.LoggerInto(ctx, log)
log.V(2).Info("Reconciling ResourceFlavor")

if flavor.ObjectMeta.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(&flavor, kueue.ResourceInUseFinalizerName) {
controllerutil.AddFinalizer(&flavor, kueue.ResourceInUseFinalizerName)
if err := r.client.Update(ctx, &flavor); err != nil {
return ctrl.Result{}, err
}
}
} else {
if !flavor.ObjectMeta.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(&flavor, kueue.ResourceInUseFinalizerName) {
if cqName, ok := r.cache.FlavorInUse(flavor.Name); ok {
log.V(3).Info("resourceFlavor is still in use", "ClusterQueue", cqName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var _ = ginkgo.Describe("ResourceFlavor controller", func() {
})

ginkgo.AfterEach(func() {
gomega.Expect(framework.DeleteResourceFlavor(ctx, k8sClient, resourceFlavor)).To(gomega.Succeed())
framework.ExpectedResourceFlavorToBeDeleted(ctx, k8sClient, resourceFlavor, true)
})

ginkgo.It("Should have finalizer in new created resourceFlavor", func() {
Expand Down
8 changes: 8 additions & 0 deletions test/integration/controller/core/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"

kueuev1alpha1 "sigs.k8s.io/kueue/apis/kueue/v1alpha1"
"sigs.k8s.io/kueue/pkg/cache"
"sigs.k8s.io/kueue/pkg/controller/core"
"sigs.k8s.io/kueue/pkg/queue"
Expand All @@ -53,6 +54,7 @@ var _ = ginkgo.BeforeSuite(func() {
fwk = &framework.Framework{
ManagerSetup: managerSetup,
CRDPath: filepath.Join("..", "..", "..", "..", "config", "crd", "bases"),
WebhookPath: filepath.Join("..", "..", "..", "..", "config", "webhook"),
}
ctx, cfg, k8sClient = fwk.Setup()
})
Expand All @@ -68,6 +70,12 @@ func managerSetup(mgr manager.Manager, ctx context.Context) {
err = cache.SetupIndexes(mgr.GetFieldIndexer())
gomega.Expect(err).NotTo(gomega.HaveOccurred())

err = (&kueuev1alpha1.Workload{}).SetupWebhookWithManager(mgr)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

err = (&kueuev1alpha1.ResourceFlavor{}).SetupWebhookWithManager(mgr)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

cCache := cache.New(mgr.GetClient())
queues := queue.NewManager(mgr.GetClient(), cCache)

Expand Down
8 changes: 8 additions & 0 deletions test/integration/scheduler/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"

kueuev1alpha1 "sigs.k8s.io/kueue/apis/kueue/v1alpha1"
"sigs.k8s.io/kueue/pkg/cache"
"sigs.k8s.io/kueue/pkg/constants"
"sigs.k8s.io/kueue/pkg/controller/core"
Expand Down Expand Up @@ -56,6 +57,7 @@ var _ = ginkgo.BeforeSuite(func() {
fwk = &framework.Framework{
ManagerSetup: managerAndSchedulerSetup,
CRDPath: filepath.Join("..", "..", "..", "config", "crd", "bases"),
WebhookPath: filepath.Join("..", "..", "..", "config", "webhook"),
}
ctx, cfg, k8sClient = fwk.Setup()
})
Expand All @@ -71,6 +73,12 @@ func managerAndSchedulerSetup(mgr manager.Manager, ctx context.Context) {
err = cache.SetupIndexes(mgr.GetFieldIndexer())
gomega.Expect(err).NotTo(gomega.HaveOccurred())

err = (&kueuev1alpha1.Workload{}).SetupWebhookWithManager(mgr)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

err = (&kueuev1alpha1.ResourceFlavor{}).SetupWebhookWithManager(mgr)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

cCache := cache.New(mgr.GetClient())
queues := queue.NewManager(mgr.GetClient(), cCache)

Expand Down
58 changes: 58 additions & 0 deletions test/integration/webhook/v1alpha1/resourceFlavor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/kueue/apis/kueue/v1alpha1"
"sigs.k8s.io/kueue/pkg/util/testing"
"sigs.k8s.io/kueue/test/integration/framework"
)

var _ = ginkgo.Describe("ResourceFlavor Webhook", func() {
var ns *corev1.Namespace

ginkgo.BeforeEach(func() {
ns = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "core-",
},
}
gomega.Expect(k8sClient.Create(ctx, ns)).To(gomega.Succeed())
})

ginkgo.AfterEach(func() {
gomega.Expect(framework.DeleteNamespace(ctx, k8sClient, ns)).To(gomega.Succeed())
})

ginkgo.When("Creating a ResourceFlavor", func() {
ginkgo.It("Should have a finalizer", func() {
ginkgo.By("Creating a new resourceFlavor")
resourceFlavor := testing.MakeResourceFlavor("resource-flavor").Obj()
gomega.Expect(k8sClient.Create(ctx, resourceFlavor)).Should(gomega.Succeed())
defer func() {
gomega.Expect(framework.DeleteResourceFlavor(ctx, k8sClient, resourceFlavor)).To(gomega.Succeed())
}()

var created v1alpha1.ResourceFlavor
gomega.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(resourceFlavor), &created)).Should(gomega.Succeed())
gomega.Expect(created.GetFinalizers()).Should(gomega.Equal([]string{v1alpha1.ResourceInUseFinalizerName}))
})
})
})
2 changes: 2 additions & 0 deletions test/integration/webhook/v1alpha1/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var _ = ginkgo.BeforeSuite(func() {
ManagerSetup: func(mgr manager.Manager, ctx context.Context) {
err := (&kueuev1alpha1.Workload{}).SetupWebhookWithManager(mgr)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = (&kueuev1alpha1.ResourceFlavor{}).SetupWebhookWithManager(mgr)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
},
}
ctx, cfg, k8sClient = fwk.Setup()
Expand Down

0 comments on commit ff7dfc1

Please sign in to comment.