Skip to content

Commit 6782e98

Browse files
committed
[lineage, controller] Implement name selectors
This patch implements name-based selectors for `CozystackResourceDefinitions.spec.secrets`. Application developers may now specify secrets that should or should not be visible to end users by specifying a `resourceNames` field with a string slice of acceptable names. This will, for instance, let developers exclude a secret like `postgres-dbname-superuser` that has a predictable name even if it does not have predictable labels. Simple templates are supported, so `postgres-{{ .name }}-superuser` is also a valid entry under `resourceNames`. ```release-note [lineage, controller] Let application developers determine resource visibility for end users by name, as well as by labels. ``` Signed-off-by: Timofei Larkin <[email protected]>
1 parent 7c1e103 commit 6782e98

7 files changed

Lines changed: 186 additions & 42 deletions

File tree

‎api/v1alpha1/cozystackresourcedefinitions_types.go‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type CozystackResourceDefinitionSpec struct {
5151
Release CozystackResourceDefinitionRelease `json:"release"`
5252

5353
// Secret selectors
54-
Secrets CozystackResourceDefinitionSecrets `json:"secrets,omitempty"`
54+
Secrets CozystackResourceDefinitionResources `json:"secrets,omitempty"`
5555

5656
// Dashboard configuration for this resource
5757
Dashboard *CozystackResourceDefinitionDashboard `json:"dashboard,omitempty"`
@@ -95,16 +95,45 @@ type CozystackResourceDefinitionRelease struct {
9595
Prefix string `json:"prefix"`
9696
}
9797

98-
type CozystackResourceDefinitionSecrets struct {
99-
// Exclude contains an array of label selectors that target secrets.
100-
// If a secret matches the selector in any of the elements in the array, it is
98+
// CozystackResourceDefinitionResourceSelector extends metav1.LabelSelector with resourceNames support.
99+
// A resource matches this selector only if it satisfies ALL criteria:
100+
// - Label selector conditions (matchExpressions and matchLabels)
101+
// - AND has a name that matches one of the names in resourceNames (if specified)
102+
//
103+
// The resourceNames field supports Go templates with the following variables available:
104+
// - {{ .name }}: The name of the managing application (from apps.cozystack.io/application.name)
105+
// - {{ .kind }}: The lowercased kind of the managing application (from apps.cozystack.io/application.kind)
106+
//
107+
// Example YAML:
108+
// secrets:
109+
// include:
110+
// - matchExpressions:
111+
// - key: badlabel
112+
// operator: DoesNotExist
113+
// matchLabels:
114+
// goodlabel: goodvalue
115+
// resourceNames:
116+
// - "{{ .name }}-secret"
117+
// - "{{ .kind }}-{{ .name }}-tls"
118+
// - "specificname"
119+
type CozystackResourceDefinitionResourceSelector struct {
120+
metav1.LabelSelector `json:",inline"`
121+
// ResourceNames is a list of resource names to match
122+
// If specified, the resource must have one of these exact names to match the selector
123+
// +optional
124+
ResourceNames []string `json:"resourceNames,omitempty"`
125+
}
126+
127+
type CozystackResourceDefinitionResources struct {
128+
// Exclude contains an array of resource selectors that target resources.
129+
// If a resource matches the selector in any of the elements in the array, it is
101130
// hidden from the user, regardless of the matches in the include array.
102-
Exclude []*metav1.LabelSelector `json:"exclude,omitempty"`
103-
// Include contains an array of label selectors that target secrets.
104-
// If a secret matches the selector in any of the elements in the array, and
105-
// matches none of the selectors in the exclude array that secret is marked
106-
// as a tenant secret and is visible to users.
107-
Include []*metav1.LabelSelector `json:"include,omitempty"`
131+
Exclude []*CozystackResourceDefinitionResourceSelector `json:"exclude,omitempty"`
132+
// Include contains an array of resource selectors that target resources.
133+
// If a resource matches the selector in any of the elements in the array, and
134+
// matches none of the selectors in the exclude array that resource is marked
135+
// as a tenant resource and is visible to users.
136+
Include []*CozystackResourceDefinitionResourceSelector `json:"include,omitempty"`
108137
}
109138

110139
// ---- Dashboard types ----

‎api/v1alpha1/zz_generated.deepcopy.go‎

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

‎internal/lineagecontrollerwebhook/matcher.go‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,69 @@
11
package lineagecontrollerwebhook
22

33
import (
4+
"bytes"
5+
"text/template"
6+
7+
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
48
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
59
"k8s.io/apimachinery/pkg/labels"
610
)
711

8-
func matchLabelsToSelector(l map[string]string, s *metav1.LabelSelector) bool {
12+
// matchName checks if the provided name matches any of the resource names in the array.
13+
// Each entry in resourceNames is treated as a Go template that gets rendered using the passed context.
14+
// A nil resourceNames array matches any string.
15+
func matchName(name string, context map[string]string, resourceNames []string) bool {
16+
if resourceNames == nil {
17+
return true
18+
}
19+
20+
for _, templateStr := range resourceNames {
21+
tmpl, err := template.New("resourceName").Parse(templateStr)
22+
if err != nil {
23+
// TODO: emit warning if error
24+
continue
25+
}
26+
27+
var buf bytes.Buffer
28+
err = tmpl.Execute(&buf, context)
29+
if err != nil {
30+
// TODO: emit warning if error
31+
continue
32+
}
33+
34+
if buf.String() == name {
35+
return true
36+
}
37+
}
38+
39+
return false
40+
}
41+
42+
func matchResourceToSelector(name string, ctx, l map[string]string, s *cozyv1alpha1.CozystackResourceDefinitionResourceSelector) bool {
943
// TODO: emit warning if error
10-
sel, err := metav1.LabelSelectorAsSelector(s)
44+
sel, err := metav1.LabelSelectorAsSelector(&s.LabelSelector)
1145
if err != nil {
1246
return false
1347
}
14-
return sel.Matches(labels.Set(l))
48+
labelMatches := sel.Matches(labels.Set(l))
49+
nameMatches := matchName(name, ctx, s.ResourceNames)
50+
return labelMatches && nameMatches
1551
}
1652

17-
func matchLabelsToSelectorArray(l map[string]string, ss []*metav1.LabelSelector) bool {
53+
func matchResourceToSelectorArray(name string, ctx, l map[string]string, ss []*cozyv1alpha1.CozystackResourceDefinitionResourceSelector) bool {
1854
for _, s := range ss {
19-
if matchLabelsToSelector(l, s) {
55+
if matchResourceToSelector(name, ctx, l, s) {
2056
return true
2157
}
2258
}
2359
return false
2460
}
2561

26-
func matchLabelsToExcludeInclude(l map[string]string, ex, in []*metav1.LabelSelector) bool {
27-
if matchLabelsToSelectorArray(l, ex) {
62+
func matchResourceToExcludeInclude(name string, ctx, l map[string]string, ex, in []*cozyv1alpha1.CozystackResourceDefinitionResourceSelector) bool {
63+
if matchResourceToSelectorArray(name, ctx, l, ex) {
2864
return false
2965
}
30-
if matchLabelsToSelectorArray(l, in) {
66+
if matchResourceToSelectorArray(name, ctx, l, in) {
3167
return true
3268
}
3369
return false

‎internal/lineagecontrollerwebhook/webhook.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"strings"
89

910
"github.com/cozystack/cozystack/pkg/lineage"
1011
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -130,6 +131,10 @@ func (h *LineageControllerWebhook) computeLabels(ctx context.Context, o *unstruc
130131
"apps.cozystack.io/application.kind": obj.GetKind(),
131132
"apps.cozystack.io/application.name": obj.GetName(),
132133
}
134+
templateLabels := map[string]string{
135+
"kind": strings.ToLower(obj.GetKind()),
136+
"name": obj.GetName(),
137+
}
133138
if o.GetAPIVersion() != "v1" || o.GetKind() != "Secret" {
134139
return labels, err
135140
}
@@ -142,7 +147,7 @@ func (h *LineageControllerWebhook) computeLabels(ctx context.Context, o *unstruc
142147
return "true"
143148
}
144149
return "false"
145-
}(matchLabelsToExcludeInclude(o.GetLabels(), crd.Spec.Secrets.Exclude, crd.Spec.Secrets.Include))
150+
}(matchResourceToExcludeInclude(o.GetName(), templateLabels, o.GetLabels(), crd.Spec.Secrets.Exclude, crd.Spec.Secrets.Include))
146151
return labels, err
147152
}
148153

‎packages/system/cozystack-api/templates/cozystack-resource-definitions/monitoring.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ spec:
3333
exclude:
3434
- matchLabels:
3535
apps.cozystack.io/tenantresource: "false"
36-
include: [{}]
36+
include:
37+
- resourceNames:
38+
- grafana-admin-password

‎packages/system/cozystack-api/templates/cozystack-resource-definitions/postgres.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ spec:
4040
exclude:
4141
- matchLabels:
4242
apps.cozystack.io/tenantresource: "false"
43-
include: [{}]
43+
include:
44+
- resourceNames:
45+
- postgres-{{ .name }}-app

‎packages/system/cozystack-controller/templates/crds/cozystack.io_cozystackresourcedefinitions.yaml‎

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,32 @@ spec:
175175
properties:
176176
exclude:
177177
description: |-
178-
Exclude contains an array of label selectors that target secrets.
179-
If a secret matches the selector in any of the elements in the array, it is
178+
Exclude contains an array of resource selectors that target resources.
179+
If a resource matches the selector in any of the elements in the array, it is
180180
hidden from the user, regardless of the matches in the include array.
181181
items:
182182
description: |-
183-
A label selector is a label query over a set of resources. The result of matchLabels and
184-
matchExpressions are ANDed. An empty label selector matches all objects. A null
185-
label selector matches no objects.
183+
CozystackResourceDefinitionResourceSelector extends metav1.LabelSelector with resourceNames support.
184+
A resource matches this selector only if it satisfies ALL criteria:
185+
- Label selector conditions (matchExpressions and matchLabels)
186+
- AND has a name that matches one of the names in resourceNames (if specified)
187+
188+
The resourceNames field supports Go templates with the following variables available:
189+
- {{ .name }}: The name of the managing application (from apps.cozystack.io/application.name)
190+
- {{ .kind }}: The lowercased kind of the managing application (from apps.cozystack.io/application.kind)
191+
192+
Example YAML:
193+
secrets:
194+
include:
195+
- matchExpressions:
196+
- key: badlabel
197+
operator: DoesNotExist
198+
matchLabels:
199+
goodlabel: goodvalue
200+
resourceNames:
201+
- "{{ .name }}-secret"
202+
- "{{ .kind }}-{{ .name }}-tls"
203+
- "specificname"
186204
properties:
187205
matchExpressions:
188206
description: matchExpressions is a list of label selector
@@ -225,20 +243,45 @@ spec:
225243
map is equivalent to an element of matchExpressions, whose key field is "key", the
226244
operator is "In", and the values array contains only "value". The requirements are ANDed.
227245
type: object
246+
resourceNames:
247+
description: |-
248+
ResourceNames is a list of resource names to match
249+
If specified, the resource must have one of these exact names to match the selector
250+
items:
251+
type: string
252+
type: array
228253
type: object
229254
x-kubernetes-map-type: atomic
230255
type: array
231256
include:
232257
description: |-
233-
Include contains an array of label selectors that target secrets.
234-
If a secret matches the selector in any of the elements in the array, and
235-
matches none of the selectors in the exclude array that secret is marked
236-
as a tenant secret and is visible to users.
258+
Include contains an array of resource selectors that target resources.
259+
If a resource matches the selector in any of the elements in the array, and
260+
matches none of the selectors in the exclude array that resource is marked
261+
as a tenant resource and is visible to users.
237262
items:
238263
description: |-
239-
A label selector is a label query over a set of resources. The result of matchLabels and
240-
matchExpressions are ANDed. An empty label selector matches all objects. A null
241-
label selector matches no objects.
264+
CozystackResourceDefinitionResourceSelector extends metav1.LabelSelector with resourceNames support.
265+
A resource matches this selector only if it satisfies ALL criteria:
266+
- Label selector conditions (matchExpressions and matchLabels)
267+
- AND has a name that matches one of the names in resourceNames (if specified)
268+
269+
The resourceNames field supports Go templates with the following variables available:
270+
- {{ .name }}: The name of the managing application (from apps.cozystack.io/application.name)
271+
- {{ .kind }}: The lowercased kind of the managing application (from apps.cozystack.io/application.kind)
272+
273+
Example YAML:
274+
secrets:
275+
include:
276+
- matchExpressions:
277+
- key: badlabel
278+
operator: DoesNotExist
279+
matchLabels:
280+
goodlabel: goodvalue
281+
resourceNames:
282+
- "{{ .name }}-secret"
283+
- "{{ .kind }}-{{ .name }}-tls"
284+
- "specificname"
242285
properties:
243286
matchExpressions:
244287
description: matchExpressions is a list of label selector
@@ -281,6 +324,13 @@ spec:
281324
map is equivalent to an element of matchExpressions, whose key field is "key", the
282325
operator is "In", and the values array contains only "value". The requirements are ANDed.
283326
type: object
327+
resourceNames:
328+
description: |-
329+
ResourceNames is a list of resource names to match
330+
If specified, the resource must have one of these exact names to match the selector
331+
items:
332+
type: string
333+
type: array
284334
type: object
285335
x-kubernetes-map-type: atomic
286336
type: array

0 commit comments

Comments
 (0)