Skip to content

Commit c43f61f

Browse files
ensure --force flag updates values.yaml when manager specs change
1 parent 9932c89 commit c43f61f

File tree

7 files changed

+182
-16
lines changed

7 files changed

+182
-16
lines changed

docs/book/src/cronjob-tutorial/testdata/project/dist/chart/values.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# [MANAGER]: Manager Deployment Configurations
22
controllerManager:
3-
replicas: 1
3+
replicas:1
44
container:
55
image:
66
repository: controller
77
tag: latest
88
args:
99
- "--leader-elect"
10-
- "--metrics-bind-address=:8443"
1110
- "--health-probe-bind-address=:8081"
1211
resources:
1312
limits:
@@ -37,7 +36,7 @@ controllerManager:
3736
runAsNonRoot: true
3837
seccompProfile:
3938
type: RuntimeDefault
40-
terminationGracePeriodSeconds: 10
39+
terminationGracePeriodSeconds:10
4140
serviceAccountName: project-controller-manager
4241

4342
# [RBAC]: To enable RBAC (Permissions) configurations

docs/book/src/getting-started/testdata/project/dist/chart/values.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# [MANAGER]: Manager Deployment Configurations
22
controllerManager:
3-
replicas: 1
3+
replicas:1
44
container:
55
image:
66
repository: controller
77
tag: latest
88
args:
99
- "--leader-elect"
10-
- "--metrics-bind-address=:8443"
1110
- "--health-probe-bind-address=:8081"
1211
resources:
1312
limits:
@@ -37,7 +36,7 @@ controllerManager:
3736
runAsNonRoot: true
3837
seccompProfile:
3938
type: RuntimeDefault
40-
terminationGracePeriodSeconds: 10
39+
terminationGracePeriodSeconds:10
4140
serviceAccountName: project-controller-manager
4241

4342
# [RBAC]: To enable RBAC (Permissions) configurations

docs/book/src/multiversion-tutorial/testdata/project/dist/chart/values.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# [MANAGER]: Manager Deployment Configurations
22
controllerManager:
3-
replicas: 1
3+
replicas:1
44
container:
55
image:
66
repository: controller
77
tag: latest
88
args:
99
- "--leader-elect"
10-
- "--metrics-bind-address=:8443"
1110
- "--health-probe-bind-address=:8081"
1211
resources:
1312
limits:
@@ -37,7 +36,7 @@ controllerManager:
3736
runAsNonRoot: true
3837
seccompProfile:
3938
type: RuntimeDefault
40-
terminationGracePeriodSeconds: 10
39+
terminationGracePeriodSeconds:10
4140
serviceAccountName: project-controller-manager
4241

4342
# [RBAC]: To enable RBAC (Permissions) configurations

pkg/plugins/optional/helm/v1alpha/edit.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ dist/chart/
5353
└── manager/
5454
└── manager.yaml
5555
56+
When the "--force" flag is used, values.yaml will be updated to reflect changes made to config/manager/manager.yaml,
57+
including any changes to:
58+
- Replica count
59+
- Container resource requests and limits
60+
- Container arguments
61+
- Termination grace period
62+
5663
The following files are never updated after their initial creation:
5764
- chart/Chart.yaml
5865
- chart/templates/_helpers.tpl

pkg/plugins/optional/helm/v1alpha/scaffolds/init.go

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"path/filepath"
2323
"regexp"
24+
"strconv"
2425
"strings"
2526

2627
"sigs.k8s.io/yaml"
@@ -50,13 +51,17 @@ type initScaffolder struct {
5051
fs machinery.Filesystem
5152

5253
force bool
54+
55+
// Store manager specs extracted from config/manager/manager.yaml
56+
managerSpecs map[string]interface{}
5357
}
5458

5559
// NewInitHelmScaffolder returns a new Scaffolder for HelmPlugin
5660
func NewInitHelmScaffolder(cfg config.Config, force bool) plugins.Scaffolder {
5761
return &initScaffolder{
58-
config: cfg,
59-
force: force,
62+
config: cfg,
63+
force: force,
64+
managerSpecs: make(map[string]interface{}),
6065
}
6166
}
6267

@@ -69,6 +74,11 @@ func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
6974
func (s *initScaffolder) Scaffold() error {
7075
log.Println("Generating Helm Chart to distribute project")
7176

77+
// Read manager specs from config/manager/manager.yaml
78+
if err := s.readManagerSpecs(); err != nil {
79+
log.Printf("Warning: %v", err)
80+
}
81+
7282
imagesEnvVars := s.getDeployImagesEnvVars()
7383

7484
scaffold := machinery.NewScaffold(s.fs,
@@ -89,6 +99,7 @@ func (s *initScaffolder) Scaffold() error {
8999
HasWebhooks: hasWebhooks,
90100
DeployImages: imagesEnvVars,
91101
Force: s.force,
102+
ManagerSpecs: s.managerSpecs,
92103
},
93104
&templates.HelmIgnore{},
94105
&charttemplates.HelmHelpers{},
@@ -560,3 +571,127 @@ func hasWebhooksWith(c config.Config) bool {
560571

561572
return false
562573
}
574+
575+
// readManagerSpecs reads the manager.yaml file from config/manager/manager.yaml
576+
// and extracts relevant specs to be used in values.yaml generation
577+
func (s *initScaffolder) readManagerSpecs() error {
578+
managerFilePath := "config/manager/manager.yaml"
579+
580+
// Check if the manager.yaml file exists
581+
if _, err := os.Stat(managerFilePath); os.IsNotExist(err) {
582+
return fmt.Errorf("manager file not found at %s", managerFilePath)
583+
}
584+
585+
// Read the manager.yaml file
586+
content, err := os.ReadFile(managerFilePath)
587+
if err != nil {
588+
return fmt.Errorf("failed to read manager file %s: %w", managerFilePath, err)
589+
}
590+
591+
// Split the file by "---" to handle multiple YAML documents
592+
docs := strings.Split(string(content), "---")
593+
594+
// Look for the Deployment document
595+
for _, doc := range docs {
596+
if strings.TrimSpace(doc) == "" {
597+
continue
598+
}
599+
600+
var obj map[string]interface{}
601+
if err := yaml.Unmarshal([]byte(doc), &obj); err != nil {
602+
log.Printf("Error unmarshalling YAML document: %v", err)
603+
continue
604+
}
605+
606+
// Check if this is the Deployment
607+
kind, ok := obj["kind"].(string)
608+
if !ok || kind != "Deployment" {
609+
continue
610+
}
611+
612+
// Extract specs
613+
if spec, ok := obj["spec"].(map[string]interface{}); ok {
614+
// Extract replicas
615+
s.extractReplicas(spec)
616+
// Extract template specs
617+
s.extractTemplateSpecs(spec)
618+
}
619+
620+
log.Printf("Successfully extracted manager specs from %s", managerFilePath)
621+
return nil
622+
}
623+
624+
return fmt.Errorf("deployment not found in manager.yaml")
625+
}
626+
627+
// extractReplicas extracts the replicas count from the deployment spec
628+
func (s *initScaffolder) extractReplicas(spec map[string]interface{}) {
629+
if replicas, ok := spec["replicas"]; ok {
630+
// Handle different types
631+
switch v := replicas.(type) {
632+
case int:
633+
s.managerSpecs["replicas"] = v
634+
case int64:
635+
s.managerSpecs["replicas"] = int(v)
636+
case float64: // YAML often unmarshals numbers as float64
637+
s.managerSpecs["replicas"] = int(v)
638+
case string:
639+
// Try to convert string to int
640+
if val, err := strconv.Atoi(v); err == nil {
641+
s.managerSpecs["replicas"] = val
642+
}
643+
}
644+
}
645+
}
646+
647+
// extractTemplateSpecs extracts specs from the template section
648+
func (s *initScaffolder) extractTemplateSpecs(spec map[string]interface{}) {
649+
if template, ok := spec["template"].(map[string]interface{}); ok {
650+
if templateSpec, ok := template["spec"].(map[string]interface{}); ok {
651+
// Extract terminationGracePeriodSeconds
652+
s.extractTerminationGracePeriod(templateSpec)
653+
// Extract container specs
654+
s.extractContainerSpecs(templateSpec)
655+
}
656+
}
657+
}
658+
659+
// extractTerminationGracePeriod extracts the terminationGracePeriodSeconds value
660+
func (s *initScaffolder) extractTerminationGracePeriod(templateSpec map[string]interface{}) {
661+
if terminationGracePeriod, ok := templateSpec["terminationGracePeriodSeconds"]; ok {
662+
switch v := terminationGracePeriod.(type) {
663+
case int:
664+
s.managerSpecs["terminationGracePeriodSeconds"] = v
665+
case int64:
666+
s.managerSpecs["terminationGracePeriodSeconds"] = int(v)
667+
case float64:
668+
s.managerSpecs["terminationGracePeriodSeconds"] = int(v)
669+
}
670+
}
671+
}
672+
673+
// extractContainerSpecs extracts specs from the first container
674+
func (s *initScaffolder) extractContainerSpecs(templateSpec map[string]interface{}) {
675+
if containers, ok := templateSpec["containers"].([]interface{}); ok && len(containers) > 0 {
676+
if container, ok := containers[0].(map[string]interface{}); ok {
677+
// Extract resources
678+
s.extractResources(container)
679+
// Extract args
680+
if args, ok := container["args"].([]interface{}); ok && len(args) > 0 {
681+
s.managerSpecs["args"] = args
682+
}
683+
}
684+
}
685+
}
686+
687+
// extractResources extracts resource limits and requests
688+
func (s *initScaffolder) extractResources(container map[string]interface{}) {
689+
if resources, ok := container["resources"].(map[string]interface{}); ok {
690+
if limits, ok := resources["limits"].(map[string]interface{}); ok && len(limits) > 0 {
691+
s.managerSpecs["resources.limits"] = limits
692+
}
693+
if requests, ok := resources["requests"].(map[string]interface{}); ok && len(requests) > 0 {
694+
s.managerSpecs["resources.requests"] = requests
695+
}
696+
}
697+
}

pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/values.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type HelmValues struct {
3434
Force bool
3535
// HasWebhooks is true when webhooks were found in the config
3636
HasWebhooks bool
37+
// ManagerSpecs contains specs from the manager.yaml that should be reflected in values.yaml
38+
ManagerSpecs map[string]interface{}
3739
}
3840

3941
// SetTemplateDefaults implements machinery.Template
@@ -54,22 +56,44 @@ func (f *HelmValues) SetTemplateDefaults() error {
5456

5557
const helmValuesTemplate = `# [MANAGER]: Manager Deployment Configurations
5658
controllerManager:
57-
replicas: 1
59+
replicas: {{- if and .ManagerSpecs (index .ManagerSpecs "replicas") -}}
60+
{{ index .ManagerSpecs "replicas" }}
61+
{{- else -}}
62+
1
63+
{{- end }}
5864
container:
5965
image:
6066
repository: controller
6167
tag: latest
6268
args:
69+
{{- if and .ManagerSpecs (index .ManagerSpecs "args") }}
70+
{{- range $arg := index .ManagerSpecs "args" }}
71+
- "{{ $arg }}"
72+
{{- end }}
73+
{{- else }}
6374
- "--leader-elect"
6475
- "--metrics-bind-address=:8443"
6576
- "--health-probe-bind-address=:8081"
77+
{{- end }}
6678
resources:
6779
limits:
80+
{{- if and .ManagerSpecs (index .ManagerSpecs "resources.limits") }}
81+
{{- range $key, $value := index .ManagerSpecs "resources.limits" }}
82+
{{ $key }}: {{ $value }}
83+
{{- end }}
84+
{{- else }}
6885
cpu: 500m
6986
memory: 128Mi
87+
{{- end }}
7088
requests:
89+
{{- if and .ManagerSpecs (index .ManagerSpecs "resources.requests") }}
90+
{{- range $key, $value := index .ManagerSpecs "resources.requests" }}
91+
{{ $key }}: {{ $value }}
92+
{{- end }}
93+
{{- else }}
7194
cpu: 10m
7295
memory: 64Mi
96+
{{- end }}
7397
livenessProbe:
7498
initialDelaySeconds: 15
7599
periodSeconds: 20
@@ -97,7 +121,11 @@ controllerManager:
97121
runAsNonRoot: true
98122
seccompProfile:
99123
type: RuntimeDefault
100-
terminationGracePeriodSeconds: 10
124+
terminationGracePeriodSeconds: {{- if and .ManagerSpecs (index .ManagerSpecs "terminationGracePeriodSeconds") -}}
125+
{{ index .ManagerSpecs "terminationGracePeriodSeconds" }}
126+
{{- else -}}
127+
10
128+
{{- end }}
101129
serviceAccountName: {{ .ProjectName }}-controller-manager
102130
103131
# [RBAC]: To enable RBAC (Permissions) configurations

testdata/project-v4-with-plugins/dist/chart/values.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# [MANAGER]: Manager Deployment Configurations
22
controllerManager:
3-
replicas: 1
3+
replicas:1
44
container:
55
image:
66
repository: controller
77
tag: latest
88
args:
99
- "--leader-elect"
10-
- "--metrics-bind-address=:8443"
1110
- "--health-probe-bind-address=:8081"
1211
resources:
1312
limits:
@@ -40,7 +39,7 @@ controllerManager:
4039
runAsNonRoot: true
4140
seccompProfile:
4241
type: RuntimeDefault
43-
terminationGracePeriodSeconds: 10
42+
terminationGracePeriodSeconds:10
4443
serviceAccountName: project-v4-with-plugins-controller-manager
4544

4645
# [RBAC]: To enable RBAC (Permissions) configurations

0 commit comments

Comments
 (0)