Skip to content

Commit

Permalink
admission/webhooks: set kcp.io/cluster annotation on create
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
  • Loading branch information
sttts committed May 2, 2024
1 parent 702ec3d commit 4d27de9
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 515 deletions.
17 changes: 17 additions & 0 deletions pkg/admission/mutatingwebhook/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (

kcpkubernetesinformers "github.com/kcp-dev/client-go/informers"
kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
"github.com/kcp-dev/kcp/sdk/apis/core"
"github.com/kcp-dev/logicalcluster/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -122,6 +124,21 @@ func (p *Plugin) Admit(ctx context.Context, attr admission.Attributes, o admissi
return fmt.Errorf("error validating MutatingWebhook initialization: %w", err)
}

// Add cluster annotation on create
if attr.GetOperation() == admission.Create {
u, ok := attr.GetObject().(metav1.Object)
if !ok {
return fmt.Errorf("unexpected type %T", attr.GetObject())
}

annotations := u.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[core.LogicalClusterPathAnnotationKey] = clusterName.String()
u.SetAnnotations(annotations)
}

return plugin.Admit(ctx, attr, o)
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/admission/validatingwebhook/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (

kcpkubernetesinformers "github.com/kcp-dev/client-go/informers"
kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
"github.com/kcp-dev/kcp/sdk/apis/core"
"github.com/kcp-dev/logicalcluster/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -122,6 +124,21 @@ func (p *Plugin) Validate(ctx context.Context, attr admission.Attributes, o admi
return fmt.Errorf("error validating ValidatingAdmissionWebhook initialization: %w", err)
}

// Add cluster annotation on create
if attr.GetOperation() == admission.Create {
u, ok := attr.GetObject().(metav1.Object)
if !ok {
return fmt.Errorf("unexpected type %T", attr.GetObject())
}

annotations := u.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[core.LogicalClusterPathAnnotationKey] = clusterName.String()
u.SetAnnotations(annotations)
}

return plugin.Validate(ctx, attr, o)
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/admission/webhook/OWNERS

This file was deleted.

187 changes: 0 additions & 187 deletions pkg/admission/webhook/generic_webhook.go

This file was deleted.

Loading

0 comments on commit 4d27de9

Please sign in to comment.