Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from cybozu-go/add-scheduling-gate
Browse files Browse the repository at this point in the history
Define default mutation webhook to add pod scheduling gate
  • Loading branch information
hanapedia authored Aug 31, 2023
2 parents 987c640 + 4b59fab commit f769639
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 3 deletions.
1 change: 0 additions & 1 deletion config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ webhooks:
- v1
operations:
- CREATE
- UPDATE
resources:
- pods
sideEffects: None
11 changes: 9 additions & 2 deletions hooks/pod_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

const podSchedulingGateName = "cat-gate.cybozu.io/gate"

// log is for logging in this package.
// var podLogger = logf.Log.WithName("pod-defaulter")

Expand All @@ -20,17 +22,22 @@ func SetupPodWebhookWithManager(mgr ctrl.Manager) error {
Complete()
}

//+kubebuilder:webhook:path=/mutate--v1-pod,mutating=true,failurePolicy=fail,sideEffects=None,groups=core,resources=pods,verbs=create;update,versions=v1,name=mpod.kb.io,admissionReviewVersions=v1
//+kubebuilder:webhook:path=/mutate--v1-pod,mutating=true,failurePolicy=fail,sideEffects=None,groups=core,resources=pods,verbs=create,versions=v1,name=mpod.kb.io,admissionReviewVersions=v1

type PodDefaulter struct{}

var _ admission.CustomDefaulter = &PodDefaulter{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (*PodDefaulter) Default(ctx context.Context, obj runtime.Object) error {
_, ok := obj.(*corev1.Pod)
pod, ok := obj.(*corev1.Pod)
if !ok {
return fmt.Errorf("unknown newObj type %T", obj)
}
for _, c := range pod.Spec.Containers {
if c.ImagePullPolicy != corev1.PullAlways {
pod.Spec.SchedulingGates = append(pod.Spec.SchedulingGates, corev1.PodSchedulingGate{Name: podSchedulingGateName})
}
}
return nil
}
41 changes: 41 additions & 0 deletions hooks/pod_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package hooks

import (
"context"

. "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"
)

var _ = Describe("Webhook Test", func() {
ctx := context.Background()

It("should add scheduling gate to pod", func() {
sample := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "invalid-sample",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "sample",
Image: "example.com/sample-image:1.0.0",
},
},
},
}
err := k8sClient.Create(ctx, sample)

Expect(err).NotTo(HaveOccurred())

pod := &corev1.Pod{}
err = k8sClient.Get(ctx, client.ObjectKey{Name: "invalid-sample", Namespace: "default"}, pod)

Expect(err).NotTo(HaveOccurred())
Expect(pod.Spec.SchedulingGates).To(ConsistOf(corev1.PodSchedulingGate{Name: podSchedulingGateName}))
})
})
115 changes: 115 additions & 0 deletions hooks/webhook_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package hooks

import (
"context"
"crypto/tls"
"fmt"
"net"
"path/filepath"
"testing"
"time"

//+kubebuilder:scaffold:imports
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "Webhook Suite")
}

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

ctx, cancel = context.WithCancel(context.TODO())

By("bootstrapping test environment")
testEnv = &envtest.Environment{
ErrorIfCRDPathMissing: false,
WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "config", "webhook")},
},
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := runtime.NewScheme()
err = admissionv1beta1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = clientgoscheme.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

// start webhook server using Manager
webhookInstallOptions := &testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Host: webhookInstallOptions.LocalServingHost,
Port: webhookInstallOptions.LocalServingPort,
CertDir: webhookInstallOptions.LocalServingCertDir,
LeaderElection: false,
MetricsBindAddress: "0",
})
Expect(err).NotTo(HaveOccurred())

err = SetupPodWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
defer GinkgoRecover()
err = mgr.Start(ctx)
Expect(err).NotTo(HaveOccurred())
}()

// wait for the webhook server to get ready
dialer := &net.Dialer{Timeout: time.Second}
addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort)
Eventually(func() error {
conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true})
if err != nil {
return err
}
conn.Close()
return nil
}).Should(Succeed())

})

var _ = AfterSuite(func() {
cancel()
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})

0 comments on commit f769639

Please sign in to comment.