Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt incompatible controller-runtime changes #38

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 0 additions & 81 deletions api/v1alpha1/reactivejob_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,3 @@ SPDX-License-Identifier: Apache-2.0
*/

package v1alpha1

import (
"context"
"fmt"
"time"

batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

var reactivejoblog = logf.Log.WithName("reactivejob-resource")
var reactivejobclient client.Client

func (r *ReactiveJob) SetupWebhookWithManager(mgr ctrl.Manager) error {
reactivejobclient = mgr.GetClient()
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// +kubebuilder:webhook:path=/mutate-batch-cs-sap-com-v1alpha1-reactivejob,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.cs.sap.com,resources=reactivejobs,verbs=create;update,versions=v1alpha1,name=mreactivejob.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &ReactiveJob{}

func (r *ReactiveJob) Default() {
reactivejoblog.Info("default", "name", r.Name)
}

// +kubebuilder:webhook:path=/validate-batch-cs-sap-com-v1alpha1-reactivejob,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.cs.sap.com,resources=reactivejobs,verbs=create;update,versions=v1alpha1,name=vreactivejob.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &ReactiveJob{}

func (r *ReactiveJob) ValidateCreate() (admission.Warnings, error) {
reactivejoblog.Info("validate create", "name", r.Name)
return nil, r.validate()
}

func (r *ReactiveJob) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
reactivejoblog.Info("validate update", "name", r.Name)
return nil, r.validate()
}

func (r *ReactiveJob) ValidateDelete() (admission.Warnings, error) {
reactivejoblog.Info("validate delete", "name", r.Name)
return nil, nil
}

func (r *ReactiveJob) validate() error {
if !r.DeletionTimestamp.IsZero() {
// Skip validation when object is being deleted e.g. while deleting the namespace,
// the validation would prevent the deletion because of finalizer changes.
return nil
}
if r.Spec.JobTemplate.Namespace != "" {
return fmt.Errorf("specified job template is inavlid: namespace must not be specified")
}
if r.Spec.JobTemplate.Name != "" {
return fmt.Errorf("specified job template is inavlid: name must not be specified")
}
if r.Spec.JobTemplate.GenerateName != "" {
return fmt.Errorf("specified job template is inavlid: generateName must not be specified")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
job := &batchv1.Job{
ObjectMeta: r.Spec.JobTemplate.ObjectMeta,
Spec: r.Spec.JobTemplate.Spec,
}
job.Namespace = r.Namespace
job.Name = r.Name + "-xxxxx"
err := client.NewDryRunClient(reactivejobclient).Create(ctx, job)
if err != nil {
return fmt.Errorf("specified job template is inavlid; dry-run output was: %s", err)
}
return nil
}
2 changes: 1 addition & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/sap/reactivejob-operator
go 1.23.4

require (
github.com/go-logr/logr v1.4.2
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.22.1
github.com/onsi/gomega v1.36.2
Expand All @@ -12,7 +13,7 @@ require (
k8s.io/apimachinery v0.32.0
k8s.io/client-go v0.32.0
k8s.io/code-generator v0.32.0
sigs.k8s.io/controller-runtime v0.19.3
sigs.k8s.io/controller-runtime v0.20.1
sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20250103111815-1ac370e560b1
sigs.k8s.io/controller-tools v0.17.0
)
Expand All @@ -24,8 +25,8 @@ require (
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -34,6 +35,7 @@ require (
github.com/gobuffalo/flect v1.0.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand All @@ -57,7 +59,6 @@ require (
github.com/x448/float16 v0.8.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg=
github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I=
github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand Down Expand Up @@ -140,8 +142,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
Expand Down Expand Up @@ -221,8 +221,8 @@ k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f h1:GA7//TjRY9yWGy1poLzYYJ
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f/go.mod h1:R/HEjbvWI0qdfb8viZUeVZm0X6IZnxAydC7YU42CMw4=
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 h1:M3sRQVHv7vB20Xc2ybTt7ODCeFj6JSWYFzOFnYeS6Ro=
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/controller-runtime v0.19.3 h1:XO2GvC9OPftRst6xWCpTgBZO04S2cbp0Qqkj8bX1sPw=
sigs.k8s.io/controller-runtime v0.19.3/go.mod h1:j4j87DqtsThvwTv5/Tc5NFRyyF/RF0ip4+62tbTSIUM=
sigs.k8s.io/controller-runtime v0.20.1 h1:JbGMAG/X94NeM3xvjenVUaBjy6Ui4Ogd/J5ZtjZnHaE=
sigs.k8s.io/controller-runtime v0.20.1/go.mod h1:BrP3w158MwvB3ZbNpaAcIKkHQ7YGpYnzpoSTZ8E14WU=
sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20250103111815-1ac370e560b1 h1:innGprbD8F7dakist62hsiV3VorWFvdPZgjtCcpdgoo=
sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20250103111815-1ac370e560b1/go.mod h1:Is2SwCWbWAoyGVoVBA627n1SWhWaEwUhaIYSEbtzHT4=
sigs.k8s.io/controller-tools v0.17.0 h1:KaEQZbhrdY6J3zLBHplt+0aKUp8PeIttlhtF2UDo6bI=
Expand Down
3 changes: 2 additions & 1 deletion internal/controllers/reactivejob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

batchv1alpha1 "github.com/sap/reactivejob-operator/api/v1alpha1"
"github.com/sap/reactivejob-operator/internal/webhooks"
)

const (
Expand Down Expand Up @@ -68,7 +69,7 @@ func (r *ReactiveJobReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

// Call the defaulting webhook logic also here (because defaulting through the webhook might be incomplete in case of generateName usage)
reactiveJob.Default()
(&webhooks.ReactiveJobWebhook{Client: r.Client, Log: log}).Default(ctx, reactiveJob)

// Acknowledge observed generation
reactiveJob.Status.ObservedGeneration = reactiveJob.Generation
Expand Down
6 changes: 5 additions & 1 deletion internal/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

batchv1alpha1 "github.com/sap/reactivejob-operator/api/v1alpha1"
"github.com/sap/reactivejob-operator/internal/controllers"
"github.com/sap/reactivejob-operator/internal/webhooks"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -124,7 +125,10 @@ var _ = BeforeSuite(func() {
}).SetupWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&batchv1alpha1.ReactiveJob{}).SetupWebhookWithManager(mgr)
err = (&webhooks.ReactiveJobWebhook{
Client: mgr.GetClient(),
Log: mgr.GetLogger().WithName("reactivejob-webhook"),
}).SetupWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

By("starting dummy controller-manager")
Expand Down
96 changes: 96 additions & 0 deletions internal/webhooks/reactivejob_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and reactivejob-operator contributors
SPDX-License-Identifier: Apache-2.0
*/

package webhooks

import (
"context"
"fmt"
"time"

"github.com/go-logr/logr"

batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

batchv1alpha1 "github.com/sap/reactivejob-operator/api/v1alpha1"
)

// +kubebuilder:webhook:path=/validate-batch-cs-sap-com-v1alpha1-reactivejob,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.cs.sap.com,resources=reactivejobs,verbs=create;update,versions=v1alpha1,name=vreactivejob.kb.io,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/mutate-batch-cs-sap-com-v1alpha1-reactivejob,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.cs.sap.com,resources=reactivejobs,verbs=create;update,versions=v1alpha1,name=mreactivejob.kb.io,admissionReviewVersions=v1

type ReactiveJobWebhook struct {
Client client.Client
Log logr.Logger
}

var _ webhook.CustomValidator = &ReactiveJobWebhook{}
var _ webhook.CustomDefaulter = &ReactiveJobWebhook{}

func (w *ReactiveJobWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
r := obj.(*batchv1alpha1.ReactiveJob)
w.Log.Info("validate create", "name", r.Name)
return nil, w.validate(r)
}

func (w *ReactiveJobWebhook) ValidateUpdate(tx context.Context, oldObj runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
r := newObj.(*batchv1alpha1.ReactiveJob)
w.Log.Info("validate update", "name", r.Name)
return nil, w.validate(r)
}

func (w *ReactiveJobWebhook) ValidateDelete(tx context.Context, obj runtime.Object) (admission.Warnings, error) {
r := obj.(*batchv1alpha1.ReactiveJob)
w.Log.Info("validate delete", "name", r.Name)
return nil, nil
}

func (w *ReactiveJobWebhook) Default(tx context.Context, obj runtime.Object) error {
r := obj.(*batchv1alpha1.ReactiveJob)
w.Log.Info("default", "name", r.Name)
return nil
}

func (w *ReactiveJobWebhook) validate(r *batchv1alpha1.ReactiveJob) error {
if !r.DeletionTimestamp.IsZero() {
// Skip validation when object is being deleted e.g. while deleting the namespace,
// the validation would prevent the deletion because of finalizer changes.
return nil
}
if r.Spec.JobTemplate.Namespace != "" {
return fmt.Errorf("specified job template is inavlid: namespace must not be specified")
}
if r.Spec.JobTemplate.Name != "" {
return fmt.Errorf("specified job template is inavlid: name must not be specified")
}
if r.Spec.JobTemplate.GenerateName != "" {
return fmt.Errorf("specified job template is inavlid: generateName must not be specified")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
job := &batchv1.Job{
ObjectMeta: r.Spec.JobTemplate.ObjectMeta,
Spec: r.Spec.JobTemplate.Spec,
}
job.Namespace = r.Namespace
job.Name = r.Name + "-xxxxx"
err := client.NewDryRunClient(w.Client).Create(ctx, job)
if err != nil {
return fmt.Errorf("specified job template is inavlid; dry-run output was: %s", err)
}
return nil
}

func (w *ReactiveJobWebhook) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(&batchv1alpha1.ReactiveJob{}).
WithValidator(w).
WithDefaulter(w).
Complete()
}
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

batchv1alpha1 "github.com/sap/reactivejob-operator/api/v1alpha1"
"github.com/sap/reactivejob-operator/internal/controllers"
"github.com/sap/reactivejob-operator/internal/webhooks"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -103,10 +104,14 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "ReactiveJob")
os.Exit(1)
}
if err = (&batchv1alpha1.ReactiveJob{}).SetupWebhookWithManager(mgr); err != nil {
if err = (&webhooks.ReactiveJobWebhook{
Client: mgr.GetClient(),
Log: mgr.GetLogger().WithName("reactivejob-webhook"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ReactiveJob")
os.Exit(1)
}

// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down