Files
cozystack/internal/lineagecontrollerwebhook/matcher.go
Timofei Larkin 562145e69b [cozystack-controller] Ancestor tracking webhook
Many resources created as part of managed apps in cozystack (pods,
secrets, etc) do not carry predictable labels that unambiguously
indicate which app originally triggered their creation. Some resources
are managed by controllers and other custom resources and this
indirection can lead to loss of information. Other controllers sometimes
simply do not allow setting labels on controlled resources and the
latter do not inherit labels from the owner. This patch implements a
webhook that sidesteps this problem with a universal solution. On
creation of a pod/secret/PVC etc it walks through the owner references
until a HelmRelease is found that can be matched with a managed app
dynamically registered in the Cozystack API server. The pod is mutated
with labels identifying the managed app.

```release-note
[cozystack-controller] Add a mutating webhook to identify the Cozystack
managed app that ultimately owns low-level resources created in the
cluster and label these resources with a reference to said app.
```

Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
2025-09-24 12:07:18 +03:00

35 lines
769 B
Go

package lineagecontrollerwebhook
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
func matchLabelsToSelector(l map[string]string, s *metav1.LabelSelector) bool {
// TODO: emit warning if error
sel, err := metav1.LabelSelectorAsSelector(s)
if err != nil {
return false
}
return sel.Matches(labels.Set(l))
}
func matchLabelsToSelectorArray(l map[string]string, ss []*metav1.LabelSelector) bool {
for _, s := range ss {
if matchLabelsToSelector(l, s) {
return true
}
}
return false
}
func matchLabelsToExcludeInclude(l map[string]string, ex, in []*metav1.LabelSelector) bool {
if matchLabelsToSelectorArray(l, ex) {
return false
}
if matchLabelsToSelectorArray(l, in) {
return true
}
return false
}