Skip to content

Commit

Permalink
Merge pull request #28 from mgoltzsche/v2
Browse files Browse the repository at this point in the history
Merge v2 branch into master (Changes from Helm 2 to Helm 3)
  • Loading branch information
mgoltzsche authored Feb 3, 2022
2 parents fb3666a + 346fb30 commit 992b101
Show file tree
Hide file tree
Showing 69 changed files with 1,860 additions and 740 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
# Publish `master` as Docker `latest` image.
branches:
- master
- v2
- v1

# Publish `v1.2.3` tags as releases.
tags:
Expand Down Expand Up @@ -37,7 +35,7 @@ jobs:
id: extract_helm_version
run: |
printf '::set-output name=helm_version::' &&
grep k8s\.io/helm go.mod | sed -E -e 's!k8s\.io/helm|\s+|\+.*!!g; s!^v!!' | cut -d " " -f2 | grep -E .+
grep helm\.sh/helm/ go.mod | sed -E -e 's!helm\.sh/helm/v3|\s+|\+.*!!g; s!^v!!' | cut -d " " -f2 | grep -E .+
- name: Build & Test
run: make test check image e2e-test
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
id: extract_helm_version
run: |
printf '::set-output name=helm_version::' &&
grep k8s\.io/helm go.mod | sed -E -e 's!k8s\.io/helm|\s+|\+.*!!g; s!^v!!' | cut -d " " -f2 | grep -E .+
grep helm\.sh/helm/ go.mod | sed -E -e 's!helm\.sh/helm/v3|\s+|\+.*!!g; s!^v!!' | cut -d " " -f2 | grep -E .+
- name: Dry-run release
run: make snapshot
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM alpine:3.14 AS khelm
RUN apk update --no-cache
RUN mkdir /helm && chown root:nobody /helm && chmod 1777 /helm
ENV HELM_HOME=/helm
ENV HELM_REPOSITORY_CONFIG=/helm/repository/repositories.yaml
ENV HELM_REPOSITORY_CACHE=/helm/cache
COPY khelm /usr/local/bin/khelmfn
ENTRYPOINT ["/usr/local/bin/khelmfn"]

Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BATS = $(BIN_DIR)/bats

REV := $(shell git rev-parse --short HEAD 2> /dev/null || echo 'unknown')
VERSION ?= $(shell echo "$$(git describe --exact-match --tags $(git log -n1 --pretty='%h') 2> /dev/null || echo dev)-$(REV)" | sed 's/^v//')
HELM_VERSION := $(shell grep k8s\.io/helm go.mod | sed -E -e 's!k8s\.io/helm|\s+|\+.*!!g; s!^v!!' | cut -d " " -f2 | grep -E .+)
HELM_VERSION := $(shell grep helm\.sh/helm/ go.mod | sed -E -e 's!helm\.sh/helm/v3|\s+|\+.*!!g; s!^v!!' | cut -d " " -f2 | grep -E .+)
GO_LDFLAGS := -X main.khelmVersion=$(VERSION) -X main.helmVersion=$(HELM_VERSION) -s -w -extldflags '-static'
BUILDTAGS ?=
CGO_ENABLED ?= 0
Expand Down Expand Up @@ -62,6 +62,8 @@ clean:
rm -f $(BUILD_DIR)/bin/khelm-static
rm -rf example/localrefref/charts
rm -rf example/kpt/linkerd/dep
# TODO: fix "invalid trailing UTF-8 octet" yaml parser error
rm -f example/kpt/test-cases/output-remote.yaml

clean-all: clean
rm -rf $(BUILD_DIR)
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,22 @@ Install using curl (on OSX or Linux):
```sh
OS=$(uname | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m | sed 's/x86_64/amd64/; s/aarch64/arm64/')
mkdir -p $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v1/chartrenderer
curl -fsSL https://github.com/mgoltzsche/khelm/releases/latest/download/khelm-${OS}-${ARCH} > $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v1/chartrenderer/ChartRenderer
chmod +x $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v1/chartrenderer/ChartRenderer
mkdir -p $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v2/chartrenderer
curl -fsSL https://github.com/mgoltzsche/khelm/releases/latest/download/khelm-${OS}-${ARCH} > $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v2/chartrenderer/ChartRenderer
chmod +x $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v2/chartrenderer/ChartRenderer
```
or using `go`:
```sh
go get github.com/mgoltzsche/khelm/cmd/khelm
mkdir -p $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v1/chartrenderer
mv $GOPATH/bin/khelm $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v1/chartrenderer/ChartRenderer
go get github.com/mgoltzsche/khelm/v2/cmd/khelm
mkdir -p $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v2/chartrenderer
mv $GOPATH/bin/khelm $HOME/.config/kustomize/plugin/khelm.mgoltzsche.github.com/v2/chartrenderer/ChartRenderer
```

#### Plugin usage example

A _plugin descriptor_ specifies the helm repository, chart, version and values that should be used in a kubernetes-style resource can be referenced in the `generators` section of a `kustomization.yaml` and can look as follows:
```yaml
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: cert-manager # fallback for `name`
Expand Down Expand Up @@ -167,7 +167,7 @@ docker run mgoltzsche/khelm:latest template cert-manager --version=0.9.x --repo=

### Go API

The khelm Go API `github.com/mgoltzsche/khelm/pkg/helm` provides a simple templating interface on top of the Helm Go API.
The khelm Go API `github.com/mgoltzsche/khelm/v2/pkg/helm` provides a simple templating interface on top of the Helm Go API.
It exposes a `Helm` struct that provides a `Render()` function that returns the rendered resources as `kyaml` objects.

## Configuration options
Expand All @@ -182,7 +182,7 @@ It exposes a `Helm` struct that provides a `Render()` function that returns the
| `apiVersions` | `--api-versions` | Kubernetes api versions used for Capabilities.APIVersions. |
| `kubeVersion` | `--kube-version` | Kubernetes version used for Capabilities.KubeVersion. |
| `name` | `--name` | Release name used to render the chart. |
| `verify` | `--verify` | If enabled verifies the signature of all charts using the `keyring` (see [Helm 2 provenance and integrity](https://v2.helm.sh/docs/provenance/)). |
| `verify` | `--verify` | If enabled verifies the signature of all charts using the `keyring` (see [Helm 3 provenance and integrity](https://helm.sh/docs/topics/provenance/)). |
| `keyring` | `--keyring` | GnuPG keyring file (default `~/.gnupg/pubring.gpg`). |
| `replaceLockFile` | `--replace-lock-file` | Remove requirements.lock and reload charts when it is out of sync. |
| `include` | | List of resource selectors that include matching resources from the output. If no selector specified all resources are included. Fails if a selector doesn't match any resource. Inclusions precede exclusions. |
Expand All @@ -195,6 +195,7 @@ It exposes a `Helm` struct that provides a `Render()` function that returns the
| `exclude[].kind` | | Excludes resources by kind. |
| `exclude[].namespace` | | Excludes resources by namespace. |
| `exclude[].name` | | Excludes resources by name. |
| `excludeCRDs` | `--skip-crds` | If true Custom Resource Definitions are excluded from the output. |
| `excludeHooks` | `--no-hooks` | If enabled excludes chart hooks from the output. |
| `namespace` | `--namespace` | Set the namespace used by Helm templates. |
| `namespacedOnly` | `--namespaced-only` | If enabled fail on known cluster-scoped resources and those of unknown kinds. |
Expand Down
4 changes: 2 additions & 2 deletions cmd/khelm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os/signal"
"syscall"

"github.com/mgoltzsche/khelm/pkg/config"
"github.com/mgoltzsche/khelm/pkg/helm"
"github.com/mgoltzsche/khelm/v2/pkg/config"
"github.com/mgoltzsche/khelm/v2/pkg/helm"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Expand Down
8 changes: 4 additions & 4 deletions cmd/khelm/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"strconv"
"strings"

"github.com/mgoltzsche/khelm/internal/matcher"
"github.com/mgoltzsche/khelm/internal/output"
"github.com/mgoltzsche/khelm/pkg/config"
"github.com/mgoltzsche/khelm/pkg/helm"
"github.com/mgoltzsche/khelm/v2/internal/matcher"
"github.com/mgoltzsche/khelm/v2/internal/output"
"github.com/mgoltzsche/khelm/v2/pkg/config"
"github.com/mgoltzsche/khelm/v2/pkg/helm"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
Expand Down
4 changes: 2 additions & 2 deletions cmd/khelm/fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"path/filepath"
"testing"

"github.com/mgoltzsche/khelm/internal/output"
"github.com/mgoltzsche/khelm/pkg/config"
"github.com/mgoltzsche/khelm/v2/internal/output"
"github.com/mgoltzsche/khelm/v2/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/kyaml/yaml"
Expand Down
6 changes: 3 additions & 3 deletions cmd/khelm/kustomize_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"io"
"strings"

"github.com/mgoltzsche/khelm/internal/output"
"github.com/mgoltzsche/khelm/pkg/config"
"github.com/mgoltzsche/khelm/pkg/helm"
"github.com/mgoltzsche/khelm/v2/internal/output"
"github.com/mgoltzsche/khelm/v2/pkg/config"
"github.com/mgoltzsche/khelm/v2/pkg/helm"
)

func runAsKustomizePlugin(h *helm.Helm, generatorYAML string, writer io.Writer) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/khelm/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strconv"
"strings"

"github.com/mgoltzsche/khelm/pkg/helm"
"github.com/mgoltzsche/khelm/v2/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down
26 changes: 18 additions & 8 deletions cmd/khelm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import (
"fmt"
"io"

"github.com/mgoltzsche/khelm/internal/output"
"github.com/mgoltzsche/khelm/pkg/config"
"github.com/mgoltzsche/khelm/pkg/helm"
"github.com/mgoltzsche/khelm/v2/internal/output"
"github.com/mgoltzsche/khelm/v2/pkg/config"
"github.com/mgoltzsche/khelm/v2/pkg/helm"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/strvals"
"helm.sh/helm/v3/pkg/strvals"
)

func templateCommand(h *helm.Helm, writer io.Writer) *cobra.Command {
req := config.NewChartConfig()
req.Name = "release-name"
defaultReleaseName := "release-name"
req.Name = defaultReleaseName
outOpts := output.Options{Writer: writer}
trustAnyRepo := false
cmd := &cobra.Command{
Use: "template",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
if len(args) != 1 && len(args) != 2 {
_ = cmd.Help()
return fmt.Errorf("accepts single CHART argument but received %d arguments", len(args))
return fmt.Errorf("accepts [NAME] CHART arguments but received %d arguments", len(args))
}
return nil
},
Expand All @@ -36,7 +37,15 @@ func templateCommand(h *helm.Helm, writer io.Writer) *cobra.Command {
if err != nil {
return err
}
req.Chart = args[0]
if len(args) > 1 {
if req.Name != defaultReleaseName {
return fmt.Errorf("cannot provide both the --name option and the argument")
}
req.Name = args[0]
req.Chart = args[1]
} else {
req.Chart = args[0]
}
resources, err := render(h, req)
if err != nil {
return err
Expand Down Expand Up @@ -68,6 +77,7 @@ func templateCommand(h *helm.Helm, writer io.Writer) *cobra.Command {
f.StringSliceVarP(&req.ValueFiles, "values", "f", nil, "Specify values in a YAML file or a URL (can specify multiple)")
f.StringSliceVar(&req.APIVersions, "api-versions", nil, "Kubernetes api versions used for Capabilities.APIVersions")
f.StringVar(&req.KubeVersion, "kube-version", req.KubeVersion, "Kubernetes version used as Capabilities.KubeVersion.Major/Minor")
f.BoolVar(&req.ExcludeCRDs, "skip-crds", false, "excludes CRDs from the chart output if enabled")
f.BoolVar(&req.ExcludeHooks, "no-hooks", req.ExcludeHooks, "If enabled hooks are omitted from the output")
f.BoolVar(&req.ExcludeHooks, "exclude-hooks", req.ExcludeHooks, "If enabled hooks are omitted from the output")
f.Lookup("exclude-hooks").Hidden = true
Expand Down
4 changes: 2 additions & 2 deletions cmd/khelm/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"k8s.io/helm/pkg/repo"
"helm.sh/helm/v3/pkg/repo"
)

func TestTemplateCommand(t *testing.T) {
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestTemplateCommandError(t *testing.T) {
defer os.Unsetenv("HELM_HOME")
err = os.Mkdir(repoDir, 0755)
require.NoError(t, err)
err = repo.NewRepoFile().WriteFile(filepath.Join(repoDir, "repositories.yaml"), 0644)
err = repo.NewFile().WriteFile(filepath.Join(repoDir, "repositories.yaml"), 0644)
require.NoError(t, err)
for _, c := range []struct {
name string
Expand Down
12 changes: 12 additions & 0 deletions e2e/cli-tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ teardown() {

@test "CLI should template remote chart without repositories.yaml" {
docker run --rm -u $(id -u):$(id -g) -v "$OUT_DIR:/out" "$IMAGE" template cert-manager \
--name=myrelease \
--version 1.0.4 \
--repo https://charts.jetstack.io \
--output /out/subdir/manifest.yaml \
--debug
ls -la $OUT_DIR/subdir
[ -f "$OUT_DIR/subdir/manifest.yaml" ]
grep -q myrelease "$OUT_DIR/subdir/manifest.yaml"
}

@test "CLI should output kustomization" {
Expand All @@ -26,3 +28,13 @@ teardown() {
[ -f "$OUT_DIR/kdir/kustomization.yaml" ]
[ -f "$OUT_DIR/kdir/configmap_myconfigb.yaml" ]
}

@test "CLI should accept release name as argument" {
docker run --rm -u $(id -u):$(id -g) -v "$OUT_DIR:/out" "$IMAGE" template myreleasex cert-manager \
--version 1.0.4 \
--repo https://charts.jetstack.io \
--output /out/manifest.yaml \
--debug
[ -f "$OUT_DIR/manifest.yaml" ]
grep -q myreleasex "$OUT_DIR/manifest.yaml"
}
2 changes: 1 addition & 1 deletion e2e/kustomize-plugin-tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ teardown() {

# ARGS: KUSTOMIZATION_DIR EXPECT_OUTPUT_CONTAINS
kustomizeBuild() {
PLUGIN_DIR=$TMP_DIR/kustomize/plugin/khelm.mgoltzsche.github.com/v1/chartrenderer
PLUGIN_DIR=$TMP_DIR/kustomize/plugin/khelm.mgoltzsche.github.com/v2/chartrenderer
mkdir -p $PLUGIN_DIR
cp build/bin/khelm $PLUGIN_DIR/ChartRenderer
chmod +x $PLUGIN_DIR/ChartRenderer
Expand Down
2 changes: 1 addition & 1 deletion example/apiversions-condition/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: v1
apiVersion: v2
description: example chart using an apiversion condition
name: apiversions-condition
version: 0.1.0
3 changes: 1 addition & 2 deletions example/apiversions-condition/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: apiversions-condition
namespace: apiversions-condition-env
chart: ./chart
apiVersions:
- myfancyapi/v1
kubeVersion: 1.17
2 changes: 1 addition & 1 deletion example/cert-manager/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: cert-manager
Expand Down
2 changes: 1 addition & 1 deletion example/chart-hooks-disabled/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: chart-hooks-disabled
Expand Down
2 changes: 1 addition & 1 deletion example/chart-hooks/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: v1
apiVersion: v2
description: example chart with helm hooks
name: hooks
version: 0.1.0
2 changes: 1 addition & 1 deletion example/chart-hooks/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: chart-hooks
Expand Down
2 changes: 1 addition & 1 deletion example/cluster-scoped-forbidden/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: mychart
Expand Down
2 changes: 1 addition & 1 deletion example/cluster-scoped/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: mychart
Expand Down
2 changes: 1 addition & 1 deletion example/empty-annotations/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: v1
apiVersion: v2
description: example chart with resource with empty annotations field. This is to verify that the kpt function maps paths reliably
name: empty-annotations
version: 0.1.0
2 changes: 1 addition & 1 deletion example/exclude-nomatch/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: mychart
Expand Down
2 changes: 1 addition & 1 deletion example/exclude/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: mychart
Expand Down
2 changes: 1 addition & 1 deletion example/expand-list/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: expand-list
Expand Down
2 changes: 1 addition & 1 deletion example/force-namespace/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: v1
apiVersion: v2
description: example chart with resources with and without namespace
name: namespace
version: 0.1.0
2 changes: 1 addition & 1 deletion example/force-namespace/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: namespace-test
Expand Down
2 changes: 1 addition & 1 deletion example/include/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: mychart
Expand Down
2 changes: 1 addition & 1 deletion example/invalid-requirements-lock/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: invalid-requirements-example
Expand Down
2 changes: 1 addition & 1 deletion example/jenkins/generator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: khelm.mgoltzsche.github.com/v1
apiVersion: khelm.mgoltzsche.github.com/v2
kind: ChartRenderer
metadata:
name: myjenkins
Expand Down
Loading

0 comments on commit 992b101

Please sign in to comment.