Skip to content

Commit 5d76e6b

Browse files
authored
Feat/webhook workload monitors (#1448)
## What this PR does 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. This resubmission of the PR now includes semantics to compare secrets to label selectors in CozystackResourceDefinitions to determine, whether they should be marked as user-facing or not. ### Release note ```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. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Adds a lineage mutating webhook that auto-applies ancestry labels to core resources and VMCluster. - Introduces secret include/exclude selectors in resource definitions for fine-grained tenant secret visibility. - Deploys webhook service with TLS via cert-manager (issuers, certificates) and updates deployment to expose webhook port. - Chores - Updates numerous container images to latest tags and digests across system and app components (controller, dashboard, kubeovn, cilium, kamaji, storage, etc.). <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 744a0f3 + 4620f7d commit 5d76e6b

43 files changed

Lines changed: 1062 additions & 35 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎api/v1alpha1/zz_generated.deepcopy.go‎

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎cmd/cozystack-controller/main.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838

3939
cozystackiov1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
4040
"github.com/cozystack/cozystack/internal/controller"
41+
lcw "github.com/cozystack/cozystack/internal/lineagecontrollerwebhook"
4142
"github.com/cozystack/cozystack/internal/telemetry"
4243

4344
helmv2 "github.com/fluxcd/helm-controller/api/v2"
@@ -214,6 +215,20 @@ func main() {
214215
os.Exit(1)
215216
}
216217

218+
// special one that's both a webhook and a reconciler
219+
lineageControllerWebhook := &lcw.LineageControllerWebhook{
220+
Client: mgr.GetClient(),
221+
Scheme: mgr.GetScheme(),
222+
}
223+
if err := lineageControllerWebhook.SetupWithManagerAsController(mgr); err != nil {
224+
setupLog.Error(err, "unable to setup controller", "controller", "LineageController")
225+
os.Exit(1)
226+
}
227+
if err := lineageControllerWebhook.SetupWithManagerAsWebhook(mgr); err != nil {
228+
setupLog.Error(err, "unable to setup webhook", "webhook", "LineageWebhook")
229+
os.Exit(1)
230+
}
231+
217232
// +kubebuilder:scaffold:builder
218233

219234
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lineagecontrollerwebhook
2+
3+
import (
4+
"fmt"
5+
6+
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
7+
helmv2 "github.com/fluxcd/helm-controller/api/v2"
8+
)
9+
10+
type chartRef struct {
11+
repo string
12+
chart string
13+
}
14+
15+
type appRef struct {
16+
group string
17+
kind string
18+
}
19+
20+
type runtimeConfig struct {
21+
chartAppMap map[chartRef]*cozyv1alpha1.CozystackResourceDefinition
22+
appCRDMap map[appRef]*cozyv1alpha1.CozystackResourceDefinition
23+
}
24+
25+
func (l *LineageControllerWebhook) initConfig() {
26+
l.initOnce.Do(func() {
27+
if l.config.Load() == nil {
28+
l.config.Store(&runtimeConfig{chartAppMap: make(map[chartRef]*cozyv1alpha1.CozystackResourceDefinition)})
29+
}
30+
})
31+
}
32+
33+
func (l *LineageControllerWebhook) Map(hr *helmv2.HelmRelease) (string, string, string, error) {
34+
cfg, ok := l.config.Load().(*runtimeConfig)
35+
if !ok {
36+
return "", "", "", fmt.Errorf("failed to load chart-app mapping from config")
37+
}
38+
s := hr.Spec.Chart.Spec
39+
val, ok := cfg.chartAppMap[chartRef{s.SourceRef.Name, s.Chart}]
40+
if !ok {
41+
return "", "", "", fmt.Errorf("cannot map helm release %s/%s to dynamic app", hr.Namespace, hr.Name)
42+
}
43+
return "apps.cozystack.io/v1alpha1", val.Spec.Application.Kind, val.Spec.Release.Prefix, nil
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package lineagecontrollerwebhook
2+
3+
import (
4+
"context"
5+
6+
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
7+
ctrl "sigs.k8s.io/controller-runtime"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
9+
"sigs.k8s.io/controller-runtime/pkg/log"
10+
)
11+
12+
// +kubebuilder:rbac:groups=cozystack.io,resources=cozystackresourcedefinitions,verbs=list;watch
13+
14+
func (c *LineageControllerWebhook) SetupWithManagerAsController(mgr ctrl.Manager) error {
15+
return ctrl.NewControllerManagedBy(mgr).
16+
For(&cozyv1alpha1.CozystackResourceDefinition{}).
17+
Complete(c)
18+
}
19+
20+
func (c *LineageControllerWebhook) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
21+
l := log.FromContext(ctx)
22+
crds := &cozyv1alpha1.CozystackResourceDefinitionList{}
23+
if err := c.List(ctx, crds, &client.ListOptions{Namespace: "cozy-system"}); err != nil {
24+
l.Error(err, "failed reading CozystackResourceDefinitions")
25+
return ctrl.Result{}, err
26+
}
27+
cfg := &runtimeConfig{
28+
chartAppMap: make(map[chartRef]*cozyv1alpha1.CozystackResourceDefinition),
29+
appCRDMap: make(map[appRef]*cozyv1alpha1.CozystackResourceDefinition),
30+
}
31+
for _, crd := range crds.Items {
32+
chRef := chartRef{
33+
crd.Spec.Release.Chart.SourceRef.Name,
34+
crd.Spec.Release.Chart.Name,
35+
}
36+
appRef := appRef{
37+
"apps.cozystack.io",
38+
crd.Spec.Application.Kind,
39+
}
40+
41+
newRef := crd
42+
if _, exists := cfg.chartAppMap[chRef]; exists {
43+
l.Info("duplicate chart mapping detected; ignoring subsequent entry", "key", chRef)
44+
} else {
45+
cfg.chartAppMap[chRef] = &newRef
46+
}
47+
if _, exists := cfg.appCRDMap[appRef]; exists {
48+
l.Info("duplicate app mapping detected; ignoring subsequent entry", "key", appRef)
49+
} else {
50+
cfg.appCRDMap[appRef] = &newRef
51+
}
52+
}
53+
c.config.Store(cfg)
54+
return ctrl.Result{}, nil
55+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package lineagecontrollerwebhook
2+
3+
import (
4+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5+
"k8s.io/apimachinery/pkg/labels"
6+
)
7+
8+
func matchLabelsToSelector(l map[string]string, s *metav1.LabelSelector) bool {
9+
// TODO: emit warning if error
10+
sel, err := metav1.LabelSelectorAsSelector(s)
11+
if err != nil {
12+
return false
13+
}
14+
return sel.Matches(labels.Set(l))
15+
}
16+
17+
func matchLabelsToSelectorArray(l map[string]string, ss []*metav1.LabelSelector) bool {
18+
for _, s := range ss {
19+
if matchLabelsToSelector(l, s) {
20+
return true
21+
}
22+
}
23+
return false
24+
}
25+
26+
func matchLabelsToExcludeInclude(l map[string]string, ex, in []*metav1.LabelSelector) bool {
27+
if matchLabelsToSelectorArray(l, ex) {
28+
return false
29+
}
30+
if matchLabelsToSelectorArray(l, in) {
31+
return true
32+
}
33+
return false
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package lineagecontrollerwebhook
2+
3+
import (
4+
"sync"
5+
"sync/atomic"
6+
7+
"k8s.io/apimachinery/pkg/api/meta"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/client-go/dynamic"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
12+
)
13+
14+
// +kubebuilder:webhook:path=/mutate-lineage,mutating=true,failurePolicy=Fail,sideEffects=None,groups="",resources=pods,secrets,services,persistentvolumeclaims,verbs=create;update,versions=v1,name=mlineage.cozystack.io,admissionReviewVersions={v1}
15+
type LineageControllerWebhook struct {
16+
client.Client
17+
Scheme *runtime.Scheme
18+
decoder admission.Decoder
19+
dynClient dynamic.Interface
20+
mapper meta.RESTMapper
21+
config atomic.Value
22+
initOnce sync.Once
23+
}

0 commit comments

Comments
 (0)