Skip to content

Commit

Permalink
Merge pull request #3124 from sttts/sttts-admission-cluster-annotation
Browse files Browse the repository at this point in the history
🐛 admission/webhooks: set kcp.io/cluster annotation on create
  • Loading branch information
kcp-ci-bot committed May 3, 2024
2 parents 702ec3d + d7bf892 commit 0939cbe
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 515 deletions.
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

0 comments on commit 0939cbe

Please sign in to comment.