Skip to content

Commit

Permalink
Try different registration
Browse files Browse the repository at this point in the history
  • Loading branch information
eberlep committed Sep 14, 2024
1 parent bdf1ac5 commit c44e561
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
30 changes: 14 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/metal-stack/v"
coreosv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
zalando "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
Expand All @@ -26,7 +27,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

databasev1 "github.com/fi-ts/postgreslet/api/v1"
"github.com/fi-ts/postgreslet/controllers"
Expand All @@ -40,8 +40,6 @@ import (

// +kubebuilder:scaffold:imports
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"

corev1 "k8s.io/api/core/v1"
)

const (
Expand Down Expand Up @@ -493,22 +491,22 @@ func main() {
}
// +kubebuilder:scaffold:builder

svcClusterMgr.GetWebhookServer().Register(
"/mutate-apps-v1-statefulset",
&webhook.Admission{
Handler: &webhooks.FsGroupChangePolicySetter{
SvcClient: svcClusterMgr.GetClient(),
Decoder: admission.NewDecoder(svcClusterMgr.GetScheme()),
Log: ctrl.Log.WithName("webhooks").WithName("FsGroupChangePolicySetter"),
},
},
)
// svcClusterMgr.GetWebhookServer().Register(
// "/mutate-apps-v1-statefulset",
// &webhook.Admission{
// Handler: &webhooks.FsGroupChangePolicySetter{
// SvcClient: svcClusterMgr.GetClient(),
// Decoder: admission.NewDecoder(svcClusterMgr.GetScheme()),
// Log: ctrl.Log.WithName("webhooks").WithName("FsGroupChangePolicySetter"),
// },
// },
// )
// svcClusterMgr.GetWebhookServer().Register("/mutate-v1-pod", &webhook.Admission{Handler: &webhooks.PodAnnotator{Client: svcClusterMgr.GetClient(), Decoder: admission.NewDecoder(svcClusterMgr.GetScheme()), Log: ctrl.Log.WithName("webhooks").WithName("PodAnnotator")}})
if err := builder.WebhookManagedBy(svcClusterMgr).
For(&corev1.Pod{}).
WithDefaulter(&webhooks.PodAnnotator{Log: ctrl.Log.WithName("webhooks").WithName("PodAnnotator")}).
For(&appsv1.StatefulSet{}).
WithDefaulter(&webhooks.STSAnnotator{Log: ctrl.Log.WithName("webhooks").WithName("STSAnnotator")}).
Complete(); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Pod")
setupLog.Error(err, "unable to create webhook", "webhook", "StatefulSet")
os.Exit(1)
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/webhooks/stsannotator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package webhooks

import (
"context"
"fmt"

"github.com/go-logr/logr"

appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// +kubebuilder:webhook:path=/mutate-apps-v1-statefulset,mutating=true,failurePolicy=fail,groups="apps",resources=statefulsets,verbs=create;update,versions=v1,name=sts.postgres.fits.cloud

type STSAnnotator struct {
Log logr.Logger
}

func (a *STSAnnotator) Default(ctx context.Context, obj runtime.Object) error {
log := a.Log.WithValues("obj", obj)
log.V(1).Info("handling admission request")

sts, ok := obj.(*appsv1.StatefulSet)
if !ok {
log.V(1).Info("failed to cast object to statefulset")
return fmt.Errorf("expected a Pod but got a %T", obj)
}

// mutate the fields in pod
if sts.Annotations == nil {
sts.Annotations = map[string]string{}
}
log.V(1).Info("Mutating StatefulSet", "sts", sts)
sts.Annotations["example-mutating-admission-webhook"] = "sts"

log.V(1).Info("done")
return nil
}

0 comments on commit c44e561

Please sign in to comment.