Skip to content

Commit

Permalink
feat: update openfaas-loki to use new OCI repository
Browse files Browse the repository at this point in the history
Update the install configuration and process for openfaas-loki so that
it uses the OCI repository hosted by Github

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler authored and alexellis committed Oct 2, 2023
1 parent c41c316 commit b3aa513
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
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 @@ -159,6 +159,7 @@ func FetchChart(chart, version string) error {
if mkErr != nil {
return mkErr
}

task := execute.ExecTask{
Command: env.LocalBinary("helm", subdir),
Args: []string{"fetch", chart, "--untar=true", "--untardir", chartsPath + versionStr},
Expand All @@ -177,6 +178,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 @@ -235,3 +240,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
}

0 comments on commit b3aa513

Please sign in to comment.