From fe043845eefba8992bb7709d49582e6916d3a0f2 Mon Sep 17 00:00:00 2001 From: Lucas Roesler Date: Sat, 30 Sep 2023 19:08:45 +0200 Subject: [PATCH] feat: update openfaas-loki to use new OCI repository Update the install configuration and process for openfaas-loki so that it uses the OCI repository hosted by Github Signed-off-by: Lucas Roesler --- cmd/apps/of_loki.go | 18 ++++++++----- pkg/apps/chart_app.go | 22 +++++++++++----- pkg/helm/helm.go | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 14 deletions(-) diff --git a/cmd/apps/of_loki.go b/cmd/apps/of_loki.go index 41dba1267..ab39342fe 100644 --- a/cmd/apps/of_loki.go +++ b/cmd/apps/of_loki.go @@ -26,10 +26,11 @@ func MakeInstallOpenFaaSLoki() *cobra.Command { OpenFaaSlokiApp.Flags().StringP("namespace", "n", "default", "The namespace to install loki (default: default") OpenFaaSlokiApp.Flags().Bool("update-repo", true, "Update the helm repo") + OpenFaaSlokiApp.Flags().MarkDeprecated("update-repo", "this flag is deprecated, it is no longer needed for the openfaas-loki") OpenFaaSlokiApp.Flags().String("openfaas-namespace", "openfaas", "set the namespace that OpenFaaS is installed into") OpenFaaSlokiApp.Flags().String("loki-url", "http://loki-stack.default:3100", "set the loki url (default http://loki-stack.default:3100)") + OpenFaaSlokiApp.Flags().String("version", "", "Override chart version, must be >= 1.7.3") OpenFaaSlokiApp.Flags().StringArray("set", []string{}, "Use custom flags or override existing flags \n(example --set grafana.enabled=true)") - OpenFaaSlokiApp.RunE = func(command *cobra.Command, args []string) error { kubeConfigPath, _ := command.Flags().GetString("kubeconfig") @@ -38,8 +39,6 @@ func MakeInstallOpenFaaSLoki() *cobra.Command { openfaasNamespace, _ := OpenFaaSlokiApp.Flags().GetString("openfaas-namespace") lokiURL, _ := OpenFaaSlokiApp.Flags().GetString("loki-url") - updateRepo, _ := OpenFaaSlokiApp.Flags().GetBool("update-repo") - overrides := map[string]string{} overrides["lokiURL"] = lokiURL @@ -49,13 +48,18 @@ func MakeInstallOpenFaaSLoki() *cobra.Command { return err } + version, _ := command.Flags().GetString("version") + lokiOptions := types.DefaultInstallOptions(). WithNamespace(namespace). - WithHelmRepo("lucas/openfaas-loki"). - WithHelmURL("https://lucasroesler.com/helm-charts"). - WithHelmUpdateRepo(updateRepo). + WithHelmURL("oci://ghcr.io/lucasroesler/charts/openfaas-loki"). WithOverrides(overrides). - WithKubeconfigPath(kubeConfigPath) + WithKubeconfigPath(kubeConfigPath). + WithHelmRepoVersion(version) + + // The default options includes the `values.yaml` file but this is already + // implied when using the OCI chart. + lokiOptions.Helm.ValuesFile = "" _, err := apps.MakeInstallChart(lokiOptions) if err != nil { diff --git a/pkg/apps/chart_app.go b/pkg/apps/chart_app.go index 9790ba647..b66521db0 100644 --- a/pkg/apps/chart_app.go +++ b/pkg/apps/chart_app.go @@ -50,16 +50,24 @@ func MakeInstallChart(options *types.InstallerOptions) (*types.InstallerOutput, return nil, err } - err = helm.AddHelmRepo(options.Helm.Repo.Name, options.Helm.Repo.URL, options.Helm.UpdateRepo) - if err != nil { - return result, err - } + installer := helm.Helm3OCIUpgrade + name := options.Helm.Repo.URL + if !helm.IsOCI(options.Helm.Repo.URL) { + err = helm.AddHelmRepo(options.Helm.Repo.Name, options.Helm.Repo.URL, options.Helm.UpdateRepo) + if err != nil { + return result, err + } - if err := helm.FetchChart(options.Helm.Repo.Name, options.Helm.Repo.Version); err != nil { - return result, err + if err := helm.FetchChart(options.Helm.Repo.Name, options.Helm.Repo.Version); err != nil { + return result, err + } + installer = helm.Helm3Upgrade + name = options.Helm.Repo.Name } - if err := helm.Helm3Upgrade(options.Helm.Repo.Name, options.Namespace, + if err := installer( + name, + options.Namespace, options.Helm.ValuesFile, options.Helm.Repo.Version, options.Helm.Overrides, diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index 56d997576..2f29e8686 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -179,6 +179,7 @@ func FetchChart(chart, version string) error { if mkErr != nil { return mkErr } + task := execute.ExecTask{ Command: fmt.Sprintf("%s fetch %s --untar=true --untardir %s%s", env.LocalBinary("helm", subdir), chart, chartsPath, versionStr), Env: os.Environ(), @@ -196,6 +197,10 @@ func FetchChart(chart, version string) error { return nil } +func IsOCI(chart string) bool { + return strings.HasPrefix(chart, "oci://") +} + func Helm3Upgrade(chart, namespace, values, version string, overrides map[string]string, wait bool) error { chartName := chart @@ -254,3 +259,59 @@ func Helm3Upgrade(chart, namespace, values, version string, overrides map[string return nil } + +func Helm3OCIUpgrade(chart, namespace, values, version string, overrides map[string]string, wait bool) error { + + if !IsOCI(chart) { + return fmt.Errorf("chart %s is not an OCI chart URL", chart) + } + + index := strings.LastIndex(chart, "/") + if index < 0 { + return fmt.Errorf("chart %s is not an OCI chart URL", chart) + } + name := chart[index+1:] + + args := []string{"upgrade", "--install", name, chart, "--namespace", namespace} + if len(version) > 0 { + args = append(args, "--version", version) + } + + if wait { + args = append(args, "--wait") + } + + fmt.Println("VALUES", values) + if len(values) > 0 { + args = append(args, "--values", values) + } + + for k, v := range overrides { + args = append(args, "--set") + args = append(args, fmt.Sprintf("%s=%s", k, v)) + } + + task := execute.ExecTask{ + Command: env.LocalBinary("helm", ""), + Args: args, + Env: os.Environ(), + StreamStdio: true, + } + + fmt.Printf("Command: %s %s\n", task.Command, task.Args) + res, err := task.Execute(context.TODO()) + + if err != nil { + return err + } + + if res.ExitCode != 0 { + return fmt.Errorf("exit code %d, stderr: %s", res.ExitCode, res.Stderr) + } + + if len(res.Stderr) > 0 { + log.Printf("stderr: %s\n", res.Stderr) + } + + return nil +}