Skip to content

Commit

Permalink
fix(controller): copy ownerreference from oldNs on namespace UPDATE a…
Browse files Browse the repository at this point in the history
…dmission requests
  • Loading branch information
oliverbaehler committed Sep 15, 2023
1 parent 7becdba commit c16fa89
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 70 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func main() {
route.TenantResourceObjects(utils.InCapsuleGroups(cfg, tntresource.WriteOpsHandler())),
route.NetworkPolicy(utils.InCapsuleGroups(cfg, networkpolicy.Handler())),
route.Tenant(tenant.NameHandler(), tenant.RoleBindingRegexHandler(), tenant.IngressClassRegexHandler(), tenant.StorageClassRegexHandler(), tenant.ContainerRegistryRegexHandler(), tenant.HostnameRegexHandler(), tenant.FreezedEmitter(), tenant.ServiceAccountNameHandler(), tenant.ForbiddenAnnotationsRegexHandler(), tenant.ProtectedHandler()),
route.OwnerReference(utils.InCapsuleGroups(cfg, namespacewebhook.OwnerReferenceHandler(), ownerreference.Handler(cfg))),
route.OwnerReference(utils.InCapsuleGroups(cfg, ownerreference.Handler(cfg))),
route.Cordoning(tenant.CordoningHandler(cfg), tenant.ResourceCounterHandler(manager.GetClient())),
route.Node(utils.InCapsuleGroups(cfg, node.UserMetadataHandler(cfg, kubeVersion))),
route.Defaults(defaults.Handler(cfg, kubeVersion)),
Expand Down
68 changes: 0 additions & 68 deletions pkg/webhook/namespace/owner_reference.go

This file was deleted.

34 changes: 33 additions & 1 deletion pkg/webhook/ownerreference/patching.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,39 @@ func (h *handler) OnDelete(client client.Client, decoder *admission.Decoder, rec

func (h *handler) OnUpdate(client client.Client, decoder *admission.Decoder, recorder record.EventRecorder) capsulewebhook.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
return nil
oldNs := &corev1.Namespace{}
if err := decoder.DecodeRaw(req.OldObject, oldNs); err != nil {
return utils.ErroredResponse(err)
}

if len(oldNs.OwnerReferences) == 0 {
return nil
}

newNs := &corev1.Namespace{}
if err := decoder.Decode(req, newNs); err != nil {
return utils.ErroredResponse(err)
}

o, err := json.Marshal(newNs.DeepCopy())
if err != nil {
response := admission.Errored(http.StatusInternalServerError, err)

return &response
}

newNs.OwnerReferences = oldNs.OwnerReferences

c, err := json.Marshal(newNs)
if err != nil {
response := admission.Errored(http.StatusInternalServerError, err)

return &response
}

response := admission.PatchResponseFromRaw(o, c)

return &response
}
}

Expand Down

0 comments on commit c16fa89

Please sign in to comment.