Skip to content

Commit 3571a15

Browse files
committed
✨ Add support for multiple helm charts in chart-update tool
Update hack/chart-update/main.go to process both cluster-api-operator and cluster-api-operator-providers charts when updating index.yaml. This ensures all charts are properly registered in the helm repository index during the release process. Signed-off-by: kahirokunn <[email protected]>
1 parent f694be5 commit 3571a15

File tree

1 file changed

+65
-29
lines changed

1 file changed

+65
-29
lines changed

hack/chart-update/main.go

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ const (
2424
repoName = "cluster-api-operator"
2525
)
2626

27+
// chartInfo represents information about a chart to be processed
28+
type chartInfo struct {
29+
name string
30+
description string
31+
}
32+
33+
// List of charts to process
34+
var charts = []chartInfo{
35+
{
36+
name: "cluster-api-operator",
37+
description: "Cluster API Operator",
38+
},
39+
{
40+
name: "cluster-api-operator-providers",
41+
description: "Cluster API Provider Custom Resources",
42+
},
43+
}
44+
2745
func main() {
2846
fmt.Println("🚀 Starting index.yaml update tool")
2947

@@ -38,45 +56,66 @@ func main() {
3856

3957
fmt.Println("⚙️ Loading index.yaml file from repo root directory")
4058

41-
indexFile := loadIndexFile(tag)
59+
indexFile := loadIndexFile()
60+
61+
fmt.Println("🔎 Finding chart archives in release assets")
4262

43-
fmt.Println("🔎 Finding chart archive in release assets")
63+
// Get all release assets once
64+
releaseAssets := getReleaseAssets(tag)
4465

45-
chartAsset := findChartReleaseAsset(tag)
66+
// Process each chart
67+
processedCharts := 0
68+
for _, chartInfo := range charts {
69+
fmt.Printf("\n📊 Processing chart: %s\n", chartInfo.name)
4670

47-
fmt.Println("📦 Downloading chart archive to a temp directory")
71+
// Check if chart already exists in index
72+
if _, err := indexFile.Get(chartInfo.name, tag[1:]); err == nil {
73+
fmt.Printf("✅ Chart %s already exists in index file, skipping\n", chartInfo.name)
74+
continue
75+
}
4876

49-
archivePath, chart := downloadChart(chartAsset)
77+
// Find chart asset
78+
chartAsset := findChartAsset(releaseAssets, chartInfo.name, tag)
79+
if chartAsset == nil {
80+
fmt.Printf("⚠️ Chart archive for %s not found in release assets, skipping\n", chartInfo.name)
81+
continue
82+
}
83+
84+
fmt.Printf("📦 Downloading %s chart archive to a temp directory\n", chartInfo.name)
85+
archivePath, chart := downloadChart(chartAsset)
86+
87+
fmt.Printf("👉🏻 Adding %s entry to index.yaml\n", chartInfo.name)
88+
addEntryToIndexFile(indexFile, chartAsset, archivePath, chart)
89+
90+
processedCharts++
91+
}
5092

51-
fmt.Println("👉🏻 Adding entry to index.yaml")
52-
addEntryToIndexFile(indexFile, chartAsset, archivePath, chart)
93+
if processedCharts == 0 {
94+
fmt.Println("\n⚠️ No new charts were added to index.yaml")
95+
os.Exit(0)
96+
}
5397

54-
fmt.Println("📝 Writing index.yaml file to repo root directory")
98+
fmt.Println("\n📝 Writing index.yaml file to repo root directory")
5599

56100
if err := indexFile.WriteFile(indexFilePath, 0644); err != nil {
57101
fmt.Println("❌ Error writing index file: ", err)
58102
os.Exit(1)
59103
}
60104

61-
fmt.Println("✅ Done updating index.yaml file")
105+
fmt.Printf("\n✅ Done updating index.yaml file. Added %d chart(s)\n", processedCharts)
62106
}
63107

64-
func loadIndexFile(tag string) *repo.IndexFile {
108+
func loadIndexFile() *repo.IndexFile {
65109
indexFile, err := repo.LoadIndexFile(indexFilePath)
66110
if err != nil {
67111
fmt.Println("❌ Error loading index file: ", err)
68112
os.Exit(1)
69113
}
70114

71-
if _, err := indexFile.Get(repoName, tag[1:]); err == nil {
72-
fmt.Println("✅ Chart already exists in index file, no need to update")
73-
os.Exit(0)
74-
}
75-
76115
return indexFile
77116
}
78117

79-
func findChartReleaseAsset(tag string) *github.ReleaseAsset {
118+
func getReleaseAssets(tag string) []*github.ReleaseAsset {
80119
ghClient := github.NewClient(nil)
81120

82121
release, _, err := ghClient.Repositories.GetReleaseByTag(context.TODO(), gitHubOrgName, repoName, tag)
@@ -85,22 +124,19 @@ func findChartReleaseAsset(tag string) *github.ReleaseAsset {
85124
os.Exit(1)
86125
}
87126

88-
chartAsset := &github.ReleaseAsset{}
89-
found := false
90-
for _, asset := range release.Assets {
91-
if *asset.Name == fmt.Sprintf("%s-%s.tgz", repoName, tag[1:]) {
92-
chartAsset = asset
93-
found = true
94-
break
95-
}
96-
}
127+
return release.Assets
128+
}
97129

98-
if !found {
99-
fmt.Printf("❌ Chart archive not found in release assets for release %s, please check if release was published correctly\n", tag)
100-
os.Exit(1)
130+
func findChartAsset(assets []*github.ReleaseAsset, chartName, tag string) *github.ReleaseAsset {
131+
expectedFileName := fmt.Sprintf("%s-%s.tgz", chartName, tag[1:])
132+
133+
for _, asset := range assets {
134+
if *asset.Name == expectedFileName {
135+
return asset
136+
}
101137
}
102138

103-
return chartAsset
139+
return nil
104140
}
105141

106142
func downloadChart(chartAsset *github.ReleaseAsset) (string, *chart.Chart) {

0 commit comments

Comments
 (0)