Skip to content

Commit 7d6378a

Browse files
committed
fix: adds more logic to fetch images script
1 parent 70846ee commit 7d6378a

File tree

1 file changed

+71
-9
lines changed

1 file changed

+71
-9
lines changed

hack/tools/fetch-images/main.go

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/d2iq-labs/helm-list-images/pkg/k8s"
1616
yamlv2 "gopkg.in/yaml.v2"
1717
corev1 "k8s.io/api/core/v1"
18+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1819
"k8s.io/apimachinery/pkg/util/yaml"
1920
)
2021

@@ -24,25 +25,26 @@ const (
2425
)
2526

2627
type ChartInfo struct {
27-
repo string
28-
name string
29-
valuesFile string
28+
repo string
29+
name string
30+
valuesFile string
31+
stringValues []string
3032
}
3133

3234
func main() {
3335
args := os.Args
3436
var (
35-
outputFile string
3637
chartDirectory string
3738
helmChartConfigMap string
39+
carenVersion string
3840
)
3941
flagSet := flag.NewFlagSet(createImagesCMD, flag.ExitOnError)
40-
flagSet.StringVar(&outputFile, "output-file", "",
41-
"output file name to write config map to.")
4242
flagSet.StringVar(&chartDirectory, "chart-directory", "",
4343
"path to chart directory for CAREN")
4444
flagSet.StringVar(&helmChartConfigMap, "helm-chart-configmap", "",
4545
"path to chart directory for CAREN")
46+
flagSet.StringVar(&carenVersion, "caren-version", "",
47+
"caren version for images override")
4648
err := flagSet.Parse(args[1:])
4749
if err != nil {
4850
fmt.Println("failed to parse args", err.Error())
@@ -62,9 +64,16 @@ func main() {
6264
fmt.Println("chart-directory helm-chart-configmap must be set")
6365
os.Exit(1)
6466
}
65-
images, err := getImagesForChart(&ChartInfo{
67+
i := &ChartInfo{
6668
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)
6877
if err != nil {
6978
fmt.Println("failed to get images", err.Error())
7079
os.Exit(1)
@@ -128,6 +137,33 @@ func getImagesForAddons(helmChartConfigMap, carenChartDirectory string) ([]strin
128137
if valuesFile != "" {
129138
info.valuesFile = valuesFile
130139
}
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+
}
131167
chartImages, err := getImagesForChart(info)
132168
if err != nil {
133169
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
137173
return images, nil
138174
}
139175

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+
140191
func getValuesFileForChartIfNeeded(chartName, carenChartDirectory string) string {
141192
switch chartName {
142193
case "nutanix-csi-storage":
@@ -147,6 +198,11 @@ func getValuesFileForChartIfNeeded(chartName, carenChartDirectory string) string
147198
return path.Join(carenChartDirectory, "addons", "csi", "snapshot-controller", defaultHelmAddonFilename)
148199
case "cilium":
149200
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"))
150206
default:
151207
return ""
152208
}
@@ -161,6 +217,10 @@ func getImagesForChart(info *ChartInfo) ([]string, error) {
161217
if info.valuesFile != "" {
162218
images.ValueFiles.Set(info.valuesFile)
163219
}
220+
if len(info.stringValues) > 0 {
221+
images.StringValues = info.stringValues
222+
}
223+
// kubeVersion needs to be set for some addons
164224
images.KubeVersion = "v1.29.0"
165225
images.SetRelease("sample")
166226
images.SetLogger("INFO")
@@ -177,7 +237,9 @@ func getImagesForChart(info *ChartInfo) ([]string, error) {
177237
return nil, err
178238
}
179239
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
181243
}
182244

183245
func getTemplatedHelmConfigMap(helmChartConfigMap string) (string, error) {

0 commit comments

Comments
 (0)