Skip to content

Commit 07c6d70

Browse files
authored
Default serviceaccount (#208)
* refactoring * allow to specify default service account for target deployment * rework default service account logic * allow to set namespace creation policy on component level * fix MissingNamespacesPolicy handling * clm: populate generation context with new values for component name/namespace * cleanup * update docs * update hugo/docsy * update comments * fix impersonation logic, handle local client better * cleanup * fix handling of empty DefaultServiceAccount
1 parent 2f26efb commit 07c6d70

File tree

22 files changed

+372
-162
lines changed

22 files changed

+372
-162
lines changed

.github/workflows/publish-website.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
build:
2828
runs-on: ubuntu-24.04
2929
env:
30-
HUGO_VERSION: 0.111.2
30+
HUGO_VERSION: 0.142.0
3131

3232
steps:
3333
- name: Install Hugo

clm/internal/manifests/generate.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,28 @@ func Generate(manifestSources []string, valuesSources []string, reconcilerName s
7474

7575
var generator manifests.Generator
7676
if _, err = fs.Stat(fsys, "Chart.yaml"); err == nil {
77-
generator, err = helm.NewHelmGenerator(fsys, "", clnt)
77+
generator, err = helm.NewHelmGenerator(fsys, "", nil)
7878
if err != nil {
7979
return nil, err
8080
}
8181
} else if errors.Is(err, fs.ErrNotExist) {
82-
generator, err = kustomize.NewKustomizeGenerator(fsys, "", clnt, kustomize.KustomizeGeneratorOptions{})
82+
generator, err = kustomize.NewKustomizeGenerator(fsys, "", nil, kustomize.KustomizeGeneratorOptions{})
8383
if err != nil {
8484
return nil, err
8585
}
8686
} else {
8787
return nil, err
8888
}
8989

90-
// TODO: what about component and component digest
90+
releaseComponent := componentFromRelease(release, allValues)
91+
// TODO: what about component digest
9192
generateCtx := component.NewContext(context.TODO()).
9293
WithReconcilerName(reconcilerName).
94+
WithLocalClient(clnt).
9395
WithClient(clnt).
94-
WithComponent(componentFromRelease(release, allValues)).
96+
WithComponent(releaseComponent).
97+
WithComponentName(releaseComponent.GetName()).
98+
WithComponentNamespace(releaseComponent.GetNamespace()).
9599
WithComponentDigest("")
96100
objects, err := generator.Generate(generateCtx, release.GetNamespace(), release.GetName(), types.UnstructurableMap(allValues))
97101
if err != nil {

pkg/component/component.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ func (s *PolicySpec) GetDeletePolicy() reconciler.DeletePolicy {
214214
return s.DeletePolicy
215215
}
216216

217+
func (s *PolicySpec) GetMissingNamespacesPolicy() reconciler.MissingNamespacesPolicy {
218+
return s.MissingNamespacesPolicy
219+
}
220+
217221
// Check if state is Ready.
218222
func (s *Status) IsReady() bool {
219223
// caveat: this operates only on the status, so it does not check that observedGeneration == generation

pkg/component/context.go

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,33 @@ import (
1313
)
1414

1515
type (
16-
reconcilerNameContextKeyType struct{}
17-
clientContextKeyType struct{}
18-
componentContextKeyType struct{}
19-
componentDigestContextKeyType struct{}
16+
reconcilerNameContextKeyType struct{}
17+
localClientContextKeyType struct{}
18+
clientContextKeyType struct{}
19+
componentContextKeyType struct{}
20+
componentNameContextKeyType struct{}
21+
componentNamespaceContextKeyType struct{}
22+
componentDigestContextKeyType struct{}
2023
)
2124

2225
var (
23-
reconcilerNameContextKey = reconcilerNameContextKeyType{}
24-
clientContextKey = clientContextKeyType{}
25-
componentContextKey = componentContextKeyType{}
26-
componentDigestContextKey = componentDigestContextKeyType{}
26+
reconcilerNameContextKey = reconcilerNameContextKeyType{}
27+
localClientContextKey = localClientContextKeyType{}
28+
clientContextKey = clientContextKeyType{}
29+
componentContextKey = componentContextKeyType{}
30+
componentNameContextKey = componentNameContextKeyType{}
31+
componentNamespaceContextKey = componentNamespaceContextKeyType{}
32+
componentDigestContextKey = componentDigestContextKeyType{}
2733
)
2834

2935
type Context interface {
3036
context.Context
3137
WithReconcilerName(reconcilerName string) Context
38+
WithLocalClient(clnt cluster.Client) Context
3239
WithClient(clnt cluster.Client) Context
3340
WithComponent(component Component) Context
41+
WithComponentName(componentName string) Context
42+
WithComponentNamespace(componentNamespace string) Context
3443
WithComponentDigest(componentDigest string) Context
3544
}
3645

@@ -46,6 +55,10 @@ func (c *contextImpl) WithReconcilerName(reconcilerName string) Context {
4655
return &contextImpl{Context: context.WithValue(c, reconcilerNameContextKey, reconcilerName)}
4756
}
4857

58+
func (c *contextImpl) WithLocalClient(clnt cluster.Client) Context {
59+
return &contextImpl{Context: context.WithValue(c, localClientContextKey, clnt)}
60+
}
61+
4962
func (c *contextImpl) WithClient(clnt cluster.Client) Context {
5063
return &contextImpl{Context: context.WithValue(c, clientContextKey, clnt)}
5164
}
@@ -54,6 +67,14 @@ func (c *contextImpl) WithComponent(component Component) Context {
5467
return &contextImpl{Context: context.WithValue(c, componentContextKey, component)}
5568
}
5669

70+
func (c *contextImpl) WithComponentName(componentName string) Context {
71+
return &contextImpl{Context: context.WithValue(c, componentNameContextKey, componentName)}
72+
}
73+
74+
func (c *contextImpl) WithComponentNamespace(componentNamespace string) Context {
75+
return &contextImpl{Context: context.WithValue(c, componentNamespaceContextKey, componentNamespace)}
76+
}
77+
5778
func (c *contextImpl) WithComponentDigest(componentDigest string) Context {
5879
return &contextImpl{Context: context.WithValue(c, componentDigestContextKey, componentDigest)}
5980
}
@@ -65,20 +86,42 @@ func ReconcilerNameFromContext(ctx context.Context) (string, error) {
6586
return "", fmt.Errorf("reconciler name not found in context")
6687
}
6788

89+
func LocalClientFromContext(ctx context.Context) (cluster.Client, error) {
90+
if clnt, ok := ctx.Value(localClientContextKey).(cluster.Client); ok {
91+
return clnt, nil
92+
}
93+
return nil, fmt.Errorf("local client not found in context")
94+
}
95+
6896
func ClientFromContext(ctx context.Context) (cluster.Client, error) {
6997
if clnt, ok := ctx.Value(clientContextKey).(cluster.Client); ok {
7098
return clnt, nil
7199
}
72100
return nil, fmt.Errorf("client not found in context")
73101
}
74102

103+
// TODO: should this method be parameterized?
75104
func ComponentFromContext(ctx context.Context) (Component, error) {
76105
if component, ok := ctx.Value(componentContextKey).(Component); ok {
77106
return component, nil
78107
}
79108
return nil, fmt.Errorf("component not found in context")
80109
}
81110

111+
func ComponentNameFromContext(ctx context.Context) (string, error) {
112+
if componentName, ok := ctx.Value(componentNameContextKey).(string); ok {
113+
return componentName, nil
114+
}
115+
return "", fmt.Errorf("component name not found in context")
116+
}
117+
118+
func ComponentNamespaceFromContext(ctx context.Context) (string, error) {
119+
if componentNamespace, ok := ctx.Value(componentNamespaceContextKey).(string); ok {
120+
return componentNamespace, nil
121+
}
122+
return "", fmt.Errorf("component namespace not found in context")
123+
}
124+
82125
func ComponentDigestFromContext(ctx context.Context) (string, error) {
83126
if componentDigest, ok := ctx.Value(componentDigestContextKey).(string); ok {
84127
return componentDigest, nil

0 commit comments

Comments
 (0)