Skip to content

Commit

Permalink
feat: add support for kustomize legacy plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Ardika Bagus <[email protected]>
  • Loading branch information
ardikabs committed Nov 8, 2023
1 parent ced4a8b commit 263fc32
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 36 deletions.
82 changes: 77 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF > release-simple.yaml
Expand Down Expand Up @@ -75,7 +75,7 @@ spec:
EOF
```

Then, you put above Release yaml to the generators field in the `kustomization.yaml`:
#### Generate Manifest

```bash
cat <<EOF > kustomization.yaml
Expand All @@ -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 <<EOF > 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 <<EOF > 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 <<EOF > 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 <<EOF > 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
Expand Down
107 changes: 76 additions & 31 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -35,53 +44,89 @@ 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)")
return cmd
}

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
}
4 changes: 4 additions & 0 deletions examples/legacy/multi-releases/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generators:
- release-1.yaml
- release-2.yaml
- release-3.yaml
13 changes: 13 additions & 0 deletions examples/legacy/multi-releases/release-1.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions examples/legacy/multi-releases/release-1/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minecraftServer:
rcon:
password: password
13 changes: 13 additions & 0 deletions examples/legacy/multi-releases/release-2.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions examples/legacy/multi-releases/release-2/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
serviceAnnotations:
foo: bar
10 changes: 10 additions & 0 deletions examples/legacy/multi-releases/release-3.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions examples/legacy/simple/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generators:
- release.yaml
10 changes: 10 additions & 0 deletions examples/legacy/simple/release.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 263fc32

Please sign in to comment.