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

chore: Simplify admission webhook setup #1633

Merged
merged 9 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion .testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ override:
path: ^internal/resourcelock$
- threshold: 84
path: ^internal/webhookcert$
- threshold: 87
- threshold: 85
path: ^webhook/logpipeline/v1alpha1$
- threshold: 84
path: ^webhook/logpipeline/v1beta1$
Expand Down
25 changes: 10 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,7 @@ func run() error {
return fmt.Errorf("failed to enable webhook server: %w", err)
}

mgr.GetWebhookServer().Register("/validate-logpipeline", &webhook.Admission{
Handler: logpipelinewebhookv1alpha1.NewValidatingWebhookHandler(mgr.GetClient(), scheme),
})
mgr.GetWebhookServer().Register("/validate-logparser", &webhook.Admission{
Handler: logparserwebhookv1alpha1.NewValidatingWebhookHandler(scheme),
})

if err := setupMutatingWebhooks(mgr); err != nil {
if err := setupAdmissionsWebhooks(mgr); err != nil {
return fmt.Errorf("failed to setup mutating webhooks: %w", err)
skhalash marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -347,37 +340,39 @@ func run() error {
return nil
}

func setupMutatingWebhooks(mgr manager.Manager) error {
if err := metricpipelinewebhookv1alpha1.SetupMetricPipelineWebhookWithManager(mgr); err != nil {
func setupAdmissionsWebhooks(mgr manager.Manager) error {
if err := metricpipelinewebhookv1alpha1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup metric pipeline v1alpha1 webhook: %w", err)
}

if featureflags.IsEnabled(featureflags.V1Beta1) {
if err := metricpipelinewebhookv1beta1.SetupMetricPipelineWebhookWithManager(mgr); err != nil {
if err := metricpipelinewebhookv1beta1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup metric pipeline v1beta1 webhook: %w", err)
}
}

if err := tracepipelinewebhookv1alpha1.SetupTracePipelineWebhookWithManager(mgr); err != nil {
if err := tracepipelinewebhookv1alpha1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup trace pipeline v1alpha1 webhook: %w", err)
}

if featureflags.IsEnabled(featureflags.V1Beta1) {
if err := tracepipelinewebhookv1beta1.SetupTracePipelineWebhookWithManager(mgr); err != nil {
if err := tracepipelinewebhookv1beta1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup trace pipeline v1beta1 webhook: %w", err)
}
}

if err := logpipelinewebhookv1alpha1.SetupLogPipelineWebhookWithManager(mgr); err != nil {
if err := logpipelinewebhookv1alpha1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup log pipeline v1alpha1 webhook: %w", err)
}

if featureflags.IsEnabled(featureflags.V1Beta1) {
if err := logpipelinewebhookv1beta1.SetupLogPipelineWebhookWithManager(mgr); err != nil {
if err := logpipelinewebhookv1beta1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup log pipeline v1beta1 webhook: %w", err)
}
}

logparserwebhookv1alpha1.SetupWithManager(mgr)

return nil
}

Expand Down
12 changes: 12 additions & 0 deletions webhook/logparser/v1alpha1/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

func SetupWithManager(mgr ctrl.Manager) {
mgr.GetWebhookServer().Register("/validate-logparser", &webhook.Admission{
Handler: NewValidatingWebhookHandler(mgr.GetScheme()),
shorim marked this conversation as resolved.
Show resolved Hide resolved
})
}
10 changes: 0 additions & 10 deletions webhook/logpipeline/v1alpha1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"

telemetryv1alpha1 "github.com/kyma-project/telemetry-manager/apis/telemetry/v1alpha1"
Expand All @@ -19,15 +18,6 @@ type LogPipelineDefaulter struct {
ApplicationInputKeepOriginalBody bool
}

func SetupLogPipelineWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).For(&telemetryv1alpha1.LogPipeline{}).
WithDefaulter(&LogPipelineDefaulter{
ApplicationInputEnabled: true,
ApplicationInputKeepOriginalBody: true,
}).
Complete()
}

func (ld LogPipelineDefaulter) Default(ctx context.Context, obj runtime.Object) error {
pipeline, ok := obj.(*telemetryv1alpha1.LogPipeline)
if !ok {
Expand Down
25 changes: 25 additions & 0 deletions webhook/logpipeline/v1alpha1/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"

telemetryv1alpha1 "github.com/kyma-project/telemetry-manager/apis/telemetry/v1alpha1"
)

func SetupWithManager(mgr ctrl.Manager) error {
if err := ctrl.NewWebhookManagedBy(mgr).For(&telemetryv1alpha1.LogPipeline{}).
WithDefaulter(&LogPipelineDefaulter{
ApplicationInputEnabled: true,
ApplicationInputKeepOriginalBody: true,
}).
Complete(); err != nil {
return err
}

mgr.GetWebhookServer().Register("/validate-logpipeline", &webhook.Admission{
Handler: NewValidatingWebhookHandler(mgr.GetClient(), mgr.GetScheme()),
shorim marked this conversation as resolved.
Show resolved Hide resolved
})

return nil
}
2 changes: 1 addition & 1 deletion webhook/logpipeline/v1beta1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type LogPipelineDefaulter struct {
DefaultOTLPOutputProtocol telemetryv1beta1.OTLPProtocol
}

func SetupLogPipelineWebhookWithManager(mgr ctrl.Manager) error {
func SetupWithManager(mgr ctrl.Manager) error {
skhalash marked this conversation as resolved.
Show resolved Hide resolved
return ctrl.NewWebhookManagedBy(mgr).For(&telemetryv1beta1.LogPipeline{}).
WithDefaulter(&LogPipelineDefaulter{
RuntimeInputEnabled: true,
Expand Down
2 changes: 1 addition & 1 deletion webhook/metricpipeline/v1alpha1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type RuntimeInputResourceDefaults struct {
Job bool
}

func SetupMetricPipelineWebhookWithManager(mgr ctrl.Manager) error {
func SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).For(&telemetryv1alpha1.MetricPipeline{}).
WithDefaulter(&MetricPipelineDefaulter{
ExcludeNamespaces: []string{"kyma-system", "kube-system", "istio-system", "compass-system"},
Expand Down
2 changes: 1 addition & 1 deletion webhook/metricpipeline/v1beta1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type RuntimeInputResourceDefaults struct {
Job bool
}

func SetupMetricPipelineWebhookWithManager(mgr ctrl.Manager) error {
func SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).For(&telemetryv1beta1.MetricPipeline{}).
WithDefaulter(&MetricPipelineDefaulter{
ExcludeNamespaces: []string{"kyma-system", "kube-system", "istio-system", "compass-system"},
Expand Down
2 changes: 1 addition & 1 deletion webhook/tracepipeline/v1alpha1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TracePipelineDefaulter struct {
DefaultOTLPOutputProtocol string
}

func SetupTracePipelineWebhookWithManager(mgr ctrl.Manager) error {
func SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).For(&telemetryv1alpha1.TracePipeline{}).
WithDefaulter(&TracePipelineDefaulter{
DefaultOTLPOutputProtocol: telemetryv1alpha1.OTLPProtocolGRPC,
Expand Down
2 changes: 1 addition & 1 deletion webhook/tracepipeline/v1beta1/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TracePipelineDefaulter struct {
DefaultOTLPOutputProtocol telemetryv1beta1.OTLPProtocol
}

func SetupTracePipelineWebhookWithManager(mgr ctrl.Manager) error {
func SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).For(&telemetryv1beta1.TracePipeline{}).
WithDefaulter(&TracePipelineDefaulter{
DefaultOTLPOutputProtocol: telemetryv1beta1.OTLPProtocolGRPC,
Expand Down
Loading