Skip to content

Commit

Permalink
Incorporate some differences from a more recent kubebuilder version.
Browse files Browse the repository at this point in the history
Minimize the diffs the diffs we have as we go forward.

Signed-off-by: Dean Roehrich <[email protected]>
  • Loading branch information
roehrich-hpe committed Aug 12, 2024
1 parent c71937d commit da08be1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
7 changes: 5 additions & 2 deletions api/v1beta1/lustrefilesystem_webhook.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Hewlett Packard Enterprise Development LP
* Copyright 2021-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
Expand Down Expand Up @@ -39,15 +39,18 @@ import (
// log is for logging in this package.
var lustrefilesystemlog = logf.Log.WithName("lustrefilesystem-resource")

// SetupWebhookWithManager will setup the manager to manage the webhooks
func (r *LustreFileSystem) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
//+kubebuilder:webhook:path=/validate-lus-cray-hpe-com-v1beta1-lustrefilesystem,mutating=false,failurePolicy=fail,sideEffects=None,groups=lus.cray.hpe.com,resources=lustrefilesystems,verbs=create;update,versions=v1beta1,name=vlustrefilesystem.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &LustreFileSystem{}
Expand Down
10 changes: 8 additions & 2 deletions api/v1beta1/lustrefilesystem_webhook_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Hewlett Packard Enterprise Development LP
* Copyright 2021-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
Expand Down Expand Up @@ -32,7 +32,13 @@ import (
// These tests are written in BDD-style using Ginkgo framework. Refer to
// http://onsi.github.io/ginkgo to learn more.

var _ = Describe("LustreFileSystemWebhook", func() {
var _ = Describe("LustreFileSystem Webhook", func() {

// We already have api/<spoke_ver>/conversion_test.go that is
// digging deep into the conversion routines, and we have
// internal/controllers/conversion_test.go that is verifing that the
// conversion webhook is hooked up to those routines.

var (
key types.NamespacedName
createdFS, retrievedFS *LustreFileSystem
Expand Down
27 changes: 18 additions & 9 deletions api/v1beta1/webhook_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Hewlett Packard Enterprise Development LP
* Copyright 2021-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
Expand All @@ -25,6 +25,7 @@ import (
"fmt"
"net"
"path/filepath"
"runtime"
"testing"
"time"

Expand All @@ -33,7 +34,7 @@ import (

admissionv1 "k8s.io/api/admission/v1"
//+kubebuilder:scaffold:imports
"k8s.io/apimachinery/pkg/runtime"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -68,16 +69,27 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: false,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.29.0-%s-%s", runtime.GOOS, runtime.GOARCH)),

WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
},
}

cfg, err := testEnv.Start()
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()
scheme := apimachineryruntime.NewScheme()
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -112,9 +124,7 @@ var _ = BeforeSuite(func() {
go func() {
defer GinkgoRecover()
err = mgr.Start(ctx)
if err != nil {
Expect(err).NotTo(HaveOccurred())
}
Expect(err).NotTo(HaveOccurred())
}()

// wait for the webhook server to get ready
Expand All @@ -125,8 +135,7 @@ var _ = BeforeSuite(func() {
if err != nil {
return err
}
conn.Close()
return nil
return conn.Close()
}).Should(Succeed())

})
Expand Down
10 changes: 6 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Hewlett Packard Enterprise Development LP
* Copyright 2021-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
Expand Down Expand Up @@ -90,9 +90,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "LustreFileSystem")
os.Exit(1)
}
if err = (&lusv1beta1.LustreFileSystem{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "LustreFileSystem")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&lusv1beta1.LustreFileSystem{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "LustreFileSystem")
os.Exit(1)
}
}
//+kubebuilder:scaffold:builder

Expand Down

0 comments on commit da08be1

Please sign in to comment.