@@ -15,6 +15,7 @@ import (
15
15
"github.com/d2iq-labs/helm-list-images/pkg/k8s"
16
16
yamlv2 "gopkg.in/yaml.v2"
17
17
corev1 "k8s.io/api/core/v1"
18
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
18
19
"k8s.io/apimachinery/pkg/util/yaml"
19
20
)
20
21
@@ -24,25 +25,26 @@ const (
24
25
)
25
26
26
27
type ChartInfo struct {
27
- repo string
28
- name string
29
- valuesFile string
28
+ repo string
29
+ name string
30
+ valuesFile string
31
+ stringValues []string
30
32
}
31
33
32
34
func main () {
33
35
args := os .Args
34
36
var (
35
- outputFile string
36
37
chartDirectory string
37
38
helmChartConfigMap string
39
+ carenVersion string
38
40
)
39
41
flagSet := flag .NewFlagSet (createImagesCMD , flag .ExitOnError )
40
- flagSet .StringVar (& outputFile , "output-file" , "" ,
41
- "output file name to write config map to." )
42
42
flagSet .StringVar (& chartDirectory , "chart-directory" , "" ,
43
43
"path to chart directory for CAREN" )
44
44
flagSet .StringVar (& helmChartConfigMap , "helm-chart-configmap" , "" ,
45
45
"path to chart directory for CAREN" )
46
+ flagSet .StringVar (& carenVersion , "caren-version" , "" ,
47
+ "caren version for images override" )
46
48
err := flagSet .Parse (args [1 :])
47
49
if err != nil {
48
50
fmt .Println ("failed to parse args" , err .Error ())
@@ -62,9 +64,16 @@ func main() {
62
64
fmt .Println ("chart-directory helm-chart-configmap must be set" )
63
65
os .Exit (1 )
64
66
}
65
- images , err := getImagesForChart ( & ChartInfo {
67
+ i := & ChartInfo {
66
68
name : chartDirectory ,
67
- })
69
+ }
70
+ if carenVersion != "" {
71
+ i .stringValues = []string {
72
+ fmt .Sprintf ("image.tag=%s" , carenVersion ),
73
+ fmt .Sprintf ("helmRepositoryImage.tag=%s" , carenVersion ),
74
+ }
75
+ }
76
+ images , err := getImagesForChart (i )
68
77
if err != nil {
69
78
fmt .Println ("failed to get images" , err .Error ())
70
79
os .Exit (1 )
@@ -128,6 +137,33 @@ func getImagesForAddons(helmChartConfigMap, carenChartDirectory string) ([]strin
128
137
if valuesFile != "" {
129
138
info .valuesFile = valuesFile
130
139
}
140
+ if chartName == "aws-cloud-controller-manager" {
141
+ values , err := getHelmValues (carenChartDirectory )
142
+ if err != nil {
143
+ return nil , err
144
+ }
145
+ awsImages , found , err := unstructured .NestedStringMap (values , "hooks" , "ccm" , "aws" , "k8sMinorVersionToCCMVersion" )
146
+ if ! found {
147
+ return images , fmt .Errorf ("failed to find k8sMinorVersionToCCMVersion from file %s" ,
148
+ path .Join (carenChartDirectory , "values.yaml" ))
149
+ }
150
+ if err != nil {
151
+ return images , fmt .Errorf ("failed to get map k8sMinorVersionToCCMVersion with error %w" ,
152
+ err )
153
+ }
154
+ for _ , tag := range awsImages {
155
+ info .stringValues = []string {
156
+ fmt .Sprintf ("image.tag=%s" , tag ),
157
+ }
158
+ chartImages , err := getImagesForChart (info )
159
+ if err != nil {
160
+ return nil , fmt .Errorf ("failed to get images for %s with error %w" , info .name , err )
161
+ }
162
+ images = append (images , chartImages ... )
163
+ }
164
+ // skip the to next addon because we got what we needed
165
+ continue
166
+ }
131
167
chartImages , err := getImagesForChart (info )
132
168
if err != nil {
133
169
return nil , fmt .Errorf ("failed to get images for %s with error %w" , info .name , err )
@@ -137,6 +173,21 @@ func getImagesForAddons(helmChartConfigMap, carenChartDirectory string) ([]strin
137
173
return images , nil
138
174
}
139
175
176
+ func getHelmValues (carenChartDirectory string ) (map [string ]interface {}, error ) {
177
+ values := path .Join (carenChartDirectory , "values.yaml" )
178
+ valuesFile , err := os .Open (values )
179
+ if err != nil {
180
+ return nil , fmt .Errorf ("failed to open file %s with %w" , values , err )
181
+ }
182
+ defer valuesFile .Close ()
183
+ m := make (map [string ]interface {})
184
+ err = yaml .NewYAMLOrJSONDecoder (valuesFile , 1024 ).Decode (& m )
185
+ if err != nil {
186
+ return nil , fmt .Errorf ("failed to open file %s with %w" , values , err )
187
+ }
188
+ return m , nil
189
+ }
190
+
140
191
func getValuesFileForChartIfNeeded (chartName , carenChartDirectory string ) string {
141
192
switch chartName {
142
193
case "nutanix-csi-storage" :
@@ -147,6 +198,11 @@ func getValuesFileForChartIfNeeded(chartName, carenChartDirectory string) string
147
198
return path .Join (carenChartDirectory , "addons" , "csi" , "snapshot-controller" , defaultHelmAddonFilename )
148
199
case "cilium" :
149
200
return path .Join (carenChartDirectory , "addons" , "cni" , "cilium" , defaultHelmAddonFilename )
201
+ // this uses the values from kustomize because the file at addons/cluster-autoscaler/values-template.yaml
202
+ // is a file that is templated
203
+ case "cluster-autoscaler" :
204
+ return path .Clean (path .Join (carenChartDirectory , ".." , ".." ,
205
+ "hack" , "addons" , "kustomize" , "cluster-autoscaler" , "manifests" , "helm-values.yaml" ))
150
206
default :
151
207
return ""
152
208
}
@@ -161,6 +217,10 @@ func getImagesForChart(info *ChartInfo) ([]string, error) {
161
217
if info .valuesFile != "" {
162
218
images .ValueFiles .Set (info .valuesFile )
163
219
}
220
+ if len (info .stringValues ) > 0 {
221
+ images .StringValues = info .stringValues
222
+ }
223
+ // kubeVersion needs to be set for some addons
164
224
images .KubeVersion = "v1.29.0"
165
225
images .SetRelease ("sample" )
166
226
images .SetLogger ("INFO" )
@@ -177,7 +237,9 @@ func getImagesForChart(info *ChartInfo) ([]string, error) {
177
237
return nil , err
178
238
}
179
239
ret := strings .Split (string (raw ), "\n " )
180
- return ret [0 : len (ret )- 1 ], nil
240
+ // this skips the last new line.
241
+ ret = ret [0 : len (ret )- 1 ]
242
+ return ret , nil
181
243
}
182
244
183
245
func getTemplatedHelmConfigMap (helmChartConfigMap string ) (string , error ) {
0 commit comments