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

🐛 admission/webhooks: set kcp.io/cluster annotation on create #3124

Merged
merged 1 commit into from
May 3, 2024
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
13 changes: 13 additions & 0 deletions pkg/admission/mutatingwebhook/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
"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"
"k8s.io/apiserver/pkg/admission"
Expand All @@ -37,6 +38,7 @@ import (
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"

kcpinitializers "github.com/kcp-dev/kcp/pkg/admission/initializers"
"github.com/kcp-dev/kcp/pkg/admission/validatingwebhook"
apisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
kcpinformers "github.com/kcp-dev/kcp/sdk/client/informers/externalversions"
)
Expand Down Expand Up @@ -122,6 +124,17 @@ 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())
}
if undo := validatingwebhook.SetClusterAnnotation(u, clusterName); undo != nil {
defer undo()
}
}

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

Expand Down
40 changes: 40 additions & 0 deletions pkg/admission/validatingwebhook/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
"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"
"k8s.io/apiserver/pkg/admission"
Expand Down Expand Up @@ -122,6 +123,17 @@ 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())
}
if undo := SetClusterAnnotation(u, clusterName); undo != nil {
defer undo()
}
}

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

Expand Down Expand Up @@ -188,3 +200,31 @@ func (p *Plugin) SetKcpInformers(local, global kcpinformers.SharedInformerFactor
return local.Apis().V1alpha1().APIBindings().Lister().Cluster(clusterName).List(labels.Everything())
}
}

// SetClusterAnnotation sets the cluster annotation on the given object to the given clusterName,
// returning an undo function that can be used to revert the change.
func SetClusterAnnotation(obj metav1.Object, clusterName logicalcluster.Name) (undoFn func()) {
anns := obj.GetAnnotations()
if anns == nil {
obj.SetAnnotations(map[string]string{logicalcluster.AnnotationKey: clusterName.String()})
return func() { obj.SetAnnotations(nil) }
}

old, ok := anns[logicalcluster.AnnotationKey]
if old == clusterName.String() {
return nil
}

anns[logicalcluster.AnnotationKey] = clusterName.String()
obj.SetAnnotations(anns)
if ok {
return func() {
anns[logicalcluster.AnnotationKey] = old
obj.SetAnnotations(anns)
}
}
return func() {
delete(anns, logicalcluster.AnnotationKey)
obj.SetAnnotations(anns)
}
}
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
Loading