Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update openfaas-loki to use new OCI repository #979

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions cmd/apps/of_loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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

Expand All @@ -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 {
Expand Down
22 changes: 15 additions & 7 deletions pkg/apps/chart_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand Down Expand Up @@ -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
}