diff --git a/README.md b/README.md index 486bd72..5ce4d1a 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ With the likelihood of [glob support](https://github.com/kubernetes-sigs/kustomi ## Usage -### Create directory +### Exec KRM Function -The Release specification: +#### Create `HelmRelease` Manifest ```bash cat < release-simple.yaml @@ -75,7 +75,7 @@ spec: EOF ``` -Then, you put above Release yaml to the generators field in the `kustomization.yaml`: +#### Generate Manifest ```bash cat < kustomization.yaml @@ -87,13 +87,85 @@ generators: - release-with-glob.yaml - release-with-oci-repo.yaml EOF + +kustomize build --enable-alpha-plugins --enable-exec . +``` + +### Legacy Plugin + +#### Download Helmize binary + +```bash +curl -sSfL -O https://github.com/ardikabs/helmize/releases/download/v0.1.1/helmize_0.1.1_linux_amd64 + +export HELMIZE_PLUGIN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/kustomize/plugin/toolkit.ardikabs.com/v1alpha1/helmrelease" +mkdir -p $HELMIZE_PLUGIN_DIR +mv helmize_0.1.1_linux_amd64 "${HELMIZE_PLUGIN_DIR}/HelmRelease" +``` + +#### Create `HelmRelease` Manifest + +```bash +cat < release-simple.yaml +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: simple-a + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts +EOF + +cat < release-with-glob.yaml +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: service-a + namespace: default +spec: + chart: common-app + repo: + name: ardikabs + url: https://charts.ardikabs.com + version: 0.4.1 + values: + - values.yaml + - values/*.yaml + - values/**/*.yaml +EOF + +cat < release-with-oci-repo.yaml +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: envoy-gateway + namespace: envoy-gateway-system +spec: + repo: + url: oci://docker.io/envoyproxy/gateway-helm + version: v0.5.0 + includeCRDs: true + createNamespace: true +EOF ``` -To generate the manifest you need to use the following command: +#### Generate Manifest + ```bash +cat < kustomization.yaml +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization -$ kustomize build --enable-alpha-plugins --enable-exec . +generators: +- release-simple.yaml +- release-with-glob.yaml +- release-with-oci-repo.yaml +EOF +kustomize build --enable-alpha-plugins . ``` ## More diff --git a/cmd/root.go b/cmd/root.go index 3b16e2d..417d0a2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,7 +16,16 @@ func MakeRoot() *cobra.Command { Use: "helmize", Short: "helmize is a KRM Function to enable Helm on Kustomize with Glob support", Example: dedent.Dedent(` - Initially Helmize used as KRM function only, but it also support for direct use through flag. + Initially Helmize used as KRM function only, but it also support for direct use through flag + as well as a Kustomize Legacy Plugin. + + > Legacy usage: + + export HELMIZE_PLUGIN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/kustomize/plugin/toolkit.ardikabs.com/v1alpha1/helmrelease" + mkdir -p $HELMIZE_PLUGIN_DIR + mv /path/to/helmize-binary "${HELMIZE_PLUGIN_DIR}/HelmRelease" + + $ kustomize build --enable-alpha-plugins /path/to/kustomization_dir > KRM usage: @@ -35,7 +44,7 @@ func MakeRoot() *cobra.Command { `), } - cmd.Args = cobra.MaximumNArgs(0) + cmd.Args = cobra.MaximumNArgs(1) cmd.RunE = run cmd.Flags().StringArrayVarP(&files, "file", "f", []string{}, "specify HelmRelease in a YAML file (can specify multiple times)") @@ -43,45 +52,81 @@ func MakeRoot() *cobra.Command { } func run(cmd *cobra.Command, args []string) error { - // Direct usage - if len(files) > 0 { - var generatedObjects fn.KubeObjects - for _, file := range files { - - raw, err := os.ReadFile(file) - if err != nil { - return err - } - - kubeObjects, err := fn.ParseKubeObjects(raw) - if err != nil { - return err - } - - generated, err := processor.ProcessKubeObjects(kubeObjects) - if err != nil { - return err - } - generatedObjects = append(generatedObjects, generated...) + switch { + case len(files) > 0: + // Direct Use or Standalone + if err := runAsStandalone(); err != nil { + return err + } + case len(args) > 0: + // Kustomize Legacy Plugin + if err := runAsLegacyPlugin(args[0]); err != nil { + return err + } + default: + // Kustomize with KRM function + stdinStat, _ := os.Stdin.Stat() + // Check the StdIn content. + if (stdinStat.Mode() & os.ModeCharDevice) != 0 { + return cmd.Help() } - if _, err := os.Stdout.Write([]byte(generatedObjects.String())); err != nil { + if err := fn.AsMain(fn.ResourceListProcessorFunc(processor.ProcessResourceList)); err != nil { + os.Exit(1) + } + } + + return nil +} + +func runAsStandalone() error { + var generatedObjects fn.KubeObjects + for _, file := range files { + + rawObj, err := os.ReadFile(file) + if err != nil { return err } - return nil + kubeObjects, err := fn.ParseKubeObjects(rawObj) + if err != nil { + return err + } + + generated, err := processor.ProcessKubeObjects(kubeObjects) + if err != nil { + return err + } + generatedObjects = append(generatedObjects, generated...) } - // KRM usage - stdinStat, _ := os.Stdin.Stat() - // Check the StdIn content. - if (stdinStat.Mode() & os.ModeCharDevice) != 0 { - return cmd.Help() + if _, err := os.Stdout.Write([]byte(generatedObjects.String())); err != nil { + return err } - if err := fn.AsMain(fn.ResourceListProcessorFunc(processor.ProcessResourceList)); err != nil { - os.Exit(1) + return nil + +} + +func runAsLegacyPlugin(resourceFile string) error { + rawObj, err := os.ReadFile(resourceFile) + if err != nil { + return err + } + + kubeObjects, err := fn.ParseKubeObjects(rawObj) + if err != nil { + return err } + var generatedObjects fn.KubeObjects + generated, err := processor.ProcessKubeObjects(kubeObjects) + if err != nil { + return err + } + generatedObjects = append(generatedObjects, generated...) + if _, err := os.Stdout.Write([]byte(generatedObjects.String())); err != nil { + return err + } return nil } diff --git a/examples/legacy/multi-releases/kustomization.yaml b/examples/legacy/multi-releases/kustomization.yaml new file mode 100644 index 0000000..0696c7d --- /dev/null +++ b/examples/legacy/multi-releases/kustomization.yaml @@ -0,0 +1,4 @@ +generators: + - release-1.yaml + - release-2.yaml + - release-3.yaml diff --git a/examples/legacy/multi-releases/release-1.yaml b/examples/legacy/multi-releases/release-1.yaml new file mode 100644 index 0000000..3d492db --- /dev/null +++ b/examples/legacy/multi-releases/release-1.yaml @@ -0,0 +1,13 @@ +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: test-1 + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts + version: 3.1.3 + values: + - release-1/values.yaml diff --git a/examples/legacy/multi-releases/release-1/values.yaml b/examples/legacy/multi-releases/release-1/values.yaml new file mode 100644 index 0000000..caade9a --- /dev/null +++ b/examples/legacy/multi-releases/release-1/values.yaml @@ -0,0 +1,3 @@ +minecraftServer: + rcon: + password: password diff --git a/examples/legacy/multi-releases/release-2.yaml b/examples/legacy/multi-releases/release-2.yaml new file mode 100644 index 0000000..d08b03b --- /dev/null +++ b/examples/legacy/multi-releases/release-2.yaml @@ -0,0 +1,13 @@ +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: test-2 + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts + version: 3.1.4 + values: + - release-2/values.yaml diff --git a/examples/legacy/multi-releases/release-2/values.yaml b/examples/legacy/multi-releases/release-2/values.yaml new file mode 100644 index 0000000..297ad60 --- /dev/null +++ b/examples/legacy/multi-releases/release-2/values.yaml @@ -0,0 +1,2 @@ +serviceAnnotations: + foo: bar diff --git a/examples/legacy/multi-releases/release-3.yaml b/examples/legacy/multi-releases/release-3.yaml new file mode 100644 index 0000000..568fe07 --- /dev/null +++ b/examples/legacy/multi-releases/release-3.yaml @@ -0,0 +1,10 @@ +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: test-3 + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts diff --git a/examples/legacy/simple/kustomization.yaml b/examples/legacy/simple/kustomization.yaml new file mode 100644 index 0000000..8afd781 --- /dev/null +++ b/examples/legacy/simple/kustomization.yaml @@ -0,0 +1,2 @@ +generators: + - release.yaml diff --git a/examples/legacy/simple/release.yaml b/examples/legacy/simple/release.yaml new file mode 100644 index 0000000..42dc471 --- /dev/null +++ b/examples/legacy/simple/release.yaml @@ -0,0 +1,10 @@ +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: test-1 + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts