Created
December 1, 2024 20:42
-
-
Save aburan28/2e231c977f405bd03d6cd36616170df5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"helm.sh/helm/v3/pkg/cli" | |
"helm.sh/helm/v3/pkg/downloader" | |
"helm.sh/helm/v3/pkg/getter" | |
"helm.sh/helm/v3/pkg/repo" | |
"k8s.io/apimachinery/pkg/util/yaml" | |
) | |
func main() { | |
chartName := "istio/istiod" // The name of the Helm chart | |
version := "1.24.1" // Specify the version of the chart | |
destDir := "./charts" // Destination directory to download the chart | |
repoName := "istio" // Repository name | |
repoURL := "https://istio-release.storage.googleapis.com/charts" // Repository URL | |
// Ensure the destination directory exists | |
err := os.MkdirAll(destDir, os.ModePerm) | |
if err != nil { | |
log.Fatalf("Failed to create directory %s: %v", destDir, err) | |
} | |
// Initialize the Helm settings | |
settings := cli.New() | |
settings.RepositoryCache = os.Getenv("HOME") + "/.cache/helm/repository" | |
settings.RepositoryConfig = os.Getenv("HOME") + "/.config/helm/repositories.yaml" | |
addRepoIfNotExists(settings, repoName, repoURL) | |
fmt.Println(settings) | |
// Configure the chart downloader | |
chartDownloader := downloader.ChartDownloader{ | |
Out: os.Stderr, | |
Verify: downloader.VerifyNever, | |
RepositoryConfig: "/Users/adamburan/.config/helm/repositories.yaml", | |
RepositoryCache: "/Users/adamburan/.cache/helm", | |
Getters: getter.All(settings), | |
Options: []getter.Option{}, | |
} | |
fmt.Printf("Downloading chart %s:%s to %s...\n", chartName, version, "") | |
filename, _, err := chartDownloader.DownloadTo(chartName, version, destDir) | |
fmt.Println("downloaded to", filename) | |
if err != nil { | |
log.Fatalf("Failed to download chart: %v", err) | |
} | |
fmt.Printf("Chart downloaded successfully: %s\n", filename) | |
} | |
func addRepoIfNotExists(settings *cli.EnvSettings, repoName string, repoURL string) { | |
// Repository file location | |
repoFile := settings.RepositoryConfig | |
fmt.Println("repoFile", repoFile) | |
_, err := os.Stat(repoFile) | |
if err != nil { | |
fmt.Println("Creating repository file since it doesn't exist") | |
os.Mkdir(settings.RepositoryConfig, os.ModePerm) | |
} | |
// err := os.MkdirAll(filepath.Dir(repoFile), os.ModePerm) | |
if err != nil { | |
log.Fatal(err) | |
} | |
b, err := os.ReadFile(repoFile) | |
if err != nil && !os.IsNotExist(err) { | |
log.Fatal(err) | |
} | |
var f repo.File | |
if err := yaml.Unmarshal(b, &f); err != nil { | |
log.Fatal(err) | |
} | |
if err != nil { | |
log.Fatalf("Failed to load repository file: %v", err) | |
} | |
for _, entry := range f.Repositories { | |
fmt.Println("entry", entry) | |
if entry.Name == repoName { | |
fmt.Printf("Repository '%s' already exists\n", repoName) | |
return | |
} | |
} | |
// Add the repository | |
fmt.Printf("Adding repository '%s' with URL '%s'\n", repoName, repoURL) | |
newRepo := &repo.Entry{ | |
Name: repoName, | |
URL: repoURL, | |
} | |
chartRepo, err := repo.NewChartRepository(newRepo, getter.All(settings)) | |
if err != nil { | |
log.Fatalf("Failed to create chart repository: %v", err) | |
} | |
fmt.Println("chartRepo", chartRepo) | |
_, err = chartRepo.DownloadIndexFile() | |
if err != nil { | |
log.Fatalf("Failed to download index for repo '%s': %v", repoName, err) | |
} | |
f.Add(newRepo) | |
if err := f.WriteFile(repoFile, os.ModePerm); err != nil { | |
log.Fatalf("Failed to write repository file: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment