Skip to content

Commit

Permalink
Setup webhook client (#10)
Browse files Browse the repository at this point in the history
Signed-off-by: Pulak Kanti Bhowmick <pulak@appscode.com>
  • Loading branch information
pkbhowmick authored Apr 19, 2022
1 parent 8ef4323 commit 0be39c9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
16 changes: 6 additions & 10 deletions apis/supervisor/v1alpha1/clustermaintenancewindow_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package v1alpha1

import (
"context"
"errors"
"fmt"

"gomodules.xyz/pointer"
"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"
Expand All @@ -31,16 +31,8 @@ import (
// log is for logging in this package.
var (
clustermaintenancewindowlog = logf.Log.WithName("clustermaintenancewindow-resource")
cmwClient client.Client
)

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

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

//+kubebuilder:webhook:path=/mutate-supervisor-appscode-com-v1alpha1-clustermaintenancewindow,mutating=true,failurePolicy=fail,sideEffects=None,groups=supervisor.appscode.com,resources=clustermaintenancewindows,verbs=create;update,versions=v1alpha1,name=mclustermaintenancewindow.kb.io,admissionReviewVersions={v1,v1beta1}
Expand Down Expand Up @@ -89,8 +81,12 @@ func (r *ClusterMaintenanceWindow) validateClusterMaintenanceWindow(ctx context.
if !r.Spec.IsDefault {
return nil
}

if webhookClient == nil {
return errors.New("webhook client is not set")
}
cmwList := &ClusterMaintenanceWindowList{}
if err := cmwClient.List(ctx, cmwList, client.MatchingFields{
if err := webhookClient.List(ctx, cmwList, client.MatchingFields{
DefaultClusterMaintenanceWindowKey: "true",
}); err != nil {
return err
Expand Down
15 changes: 5 additions & 10 deletions apis/supervisor/v1alpha1/maintenancewindow_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package v1alpha1

import (
"context"
"errors"
"fmt"
"time"

"gomodules.xyz/pointer"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
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"
Expand All @@ -33,16 +33,8 @@ import (
// log is for logging in this package.
var (
maintenancewindowlog = logf.Log.WithName("maintenancewindow-resource")
mwClient client.Client
)

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

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

//+kubebuilder:webhook:path=/mutate-supervisor-appscode-com-v1alpha1-maintenancewindow,mutating=true,failurePolicy=fail,sideEffects=None,groups=supervisor.appscode.com,resources=maintenancewindows,verbs=create;update,versions=v1alpha1,name=mmaintenancewindow.kb.io,admissionReviewVersions={v1,v1beta1}
Expand Down Expand Up @@ -92,8 +84,11 @@ func (r *MaintenanceWindow) validateMaintenanceWindow(ctx context.Context) error
if !r.Spec.IsDefault {
return nil
}
if webhookClient == nil {
return errors.New("webhook client is not set")
}
mwList := &MaintenanceWindowList{}
if err := mwClient.List(ctx, mwList, client.InNamespace(r.Namespace), client.MatchingFields{
if err := webhookClient.List(ctx, mwList, client.InNamespace(r.Namespace), client.MatchingFields{
DefaultMaintenanceWindowKey: "true",
}); err != nil {
return err
Expand Down
7 changes: 0 additions & 7 deletions apis/supervisor/v1alpha1/recommendation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"gomodules.xyz/pointer"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
Expand All @@ -33,12 +32,6 @@ var (
recommendationlog = logf.Log.WithName("recommendation-resource")
)

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

var _ webhook.Defaulter = &Recommendation{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
Expand Down
27 changes: 27 additions & 0 deletions apis/supervisor/v1alpha1/webhook_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright AppsCode Inc. and Contributors.
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 (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var webhookClient client.Client

func SetupWebhookClient(c client.Client) {
webhookClient = c
}
2 changes: 2 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func (c completedConfig) New() (*SupervisorOperator, error) {
os.Exit(1)
}

api.SetupWebhookClient(mgr.GetClient())

if err := mgr.GetFieldIndexer().IndexField(context.Background(), &api.MaintenanceWindow{}, api.DefaultMaintenanceWindowKey, func(rawObj client.Object) []string {
app := rawObj.(*api.MaintenanceWindow)
if v, ok := app.Annotations[api.DefaultMaintenanceWindowKey]; ok && v == "true" {
Expand Down

0 comments on commit 0be39c9

Please sign in to comment.