Skip to content

Commit

Permalink
Add helm plugin to distribute projects
Browse files Browse the repository at this point in the history
This PR introduces an optional Helm plugin, enabling users to scaffold Helm charts for Kubebuilder projects.

- **Helm Chart Scaffolding**: Allows generation of a Helm chart under the `dist/chart` directory, providing an alternative for distributing and managing projects via Helm.
- **Usage**:
  - **To add when init the project**: `kubebuilder init --plugins=helm/v1-alpha` for new projects.
  - **To Init/Update in a project previously scaffolded**: `kubebuilder edit --plugins=helm/v1-alpha` to add or update Helm charts for existing projects.
- **Sync Helm Chart**: Syncs the Helm chart with project manifests using the `edit` command, with `--force` required for updates after webhook or `DeployImage` plugin changes in order to overwritten the values.yaml and manager.yaml files.
  • Loading branch information
camilamacedo86 committed Nov 9, 2024
1 parent 0df6220 commit 2f49d33
Show file tree
Hide file tree
Showing 75 changed files with 3,510 additions and 65 deletions.
76 changes: 76 additions & 0 deletions .github/workflows/test-helm-samples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Helm Testdata Sample

on:
push:
paths:
- 'testdata/project-v4-with-plugins/**'
- '.github/workflows/test-helm-samples.yml'
pull_request:
paths:
- 'testdata/project-v4-with-plugins/**'
- '.github/workflows/test-helm-samples.yml'

jobs:
helm-test-project-v4-with-plugins:
runs-on: ubuntu-latest
strategy:
fail-fast: true
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'

- name: Install the latest version of kind
run: |
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
- name: Verify kind installation
run: kind version

- name: Create kind cluster
run: kind create cluster

- name: Prepare project-v4-with-plugins
run: |
cd testdata/project-v4-with-plugins/
go mod tidy
make docker-build IMG=project-v4-with-plugins:v0.1.0
kind load docker-image project-v4-with-plugins:v0.1.0
- name: Install Helm
run: |
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
- name: Verify Helm installation
run: helm version

- name: Lint Helm chart for project-v4-with-plugins
run: |
helm lint testdata/project-v4-with-plugins/dist/chart
- name: Install cert-manager via Helm
run: |
helm repo add jetstack https://charts.jetstack.io
helm repo update
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set installCRDs=true
- name: Wait for cert-manager to be ready
run: |
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook
- name: Install Helm chart for project-v4-with-plugins
run: |
helm install my-release testdata/project-v4-with-plugins/dist/chart --create-namespace --namespace project-v4-with-plugins-system
- name: Check Helm release status
run: |
helm status my-release --namespace project-v4-with-plugins-system
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes

.PHONY: yamllint
yamllint:
@files=$$(find testdata -name '*.yaml' ! -path 'testdata/*/dist/install.yaml'); \
@files=$$(find testdata -name '*.yaml' ! -path 'testdata/*/dist/*'); \
docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/yamllint:latest $$files -d "{extends: relaxed, rules: {line-length: {max: 120}}}" --no-warnings

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
Expand Down Expand Up @@ -171,3 +171,11 @@ test-spaces: ## Run the trailing spaces check
test-legacy: ## Run the tests to validate legacy path for webhooks
rm -rf ./testdata/**legacy**/
./test/testdata/legacy-webhook-path.sh

.PHONY: install-helm
install-helm: ## Install the latest version of Helm locally
@curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

.PHONY: helm-lint
helm-lint: install-helm ## Lint the Helm chart in testdata
helm lint testdata/project-v4-with-plugins/dist/chart
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
deployimagev1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
golangv4 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4"
grafanav1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
helmv1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
)

func init() {
Expand Down Expand Up @@ -61,6 +62,7 @@ func main() {
&kustomizecommonv2.Plugin{},
&deployimagev1alpha1.Plugin{},
&grafanav1alpha1.Plugin{},
&helmv1alpha1.Plugin{},
),
cli.WithPlugins(externalPlugins...),
cli.WithDefaultPlugins(cfgv3.Version, gov4Bundle),
Expand Down
31 changes: 26 additions & 5 deletions docs/book/src/cronjob-tutorial/testdata/project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,48 @@ make undeploy

## Project Distribution

Following are the steps to build the installer and distribute this project to users.
Following the options to release and provide this solution to the users.

### By providing a bundle with all YAML files

1. Build the installer for the image built and published in the registry:

```sh
make build-installer IMG=<some-registry>/project:tag
```

NOTE: The makefile target mentioned above generates an 'install.yaml'
**NOTE:** The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.
with Kustomize, which are necessary to install this project without its
dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:
Users can just run 'kubectl apply -f <URL for YAML BUNDLE>' to install
the project, i.e.:

```sh
kubectl apply -f https://raw.githubusercontent.com/<org>/project/<tag or branch>/dist/install.yaml
```

### By providing a Helm Chart

1. Build the chart using the optional helm plugin

```sh
kubebuilder edit --plugins=helm/v1-alpha
```

2. See that a chart was generated under 'dist/chart', and users
can obtain this solution from there.

**NOTE:** If you change the project, you need to update the Helm Chart
using the same command above to sync the latest changes. Furthermore,
if you create webhooks, you need to use the above command with
the '--force' flag and manually ensure that any custom configuration
previously added to 'dist/chart/values.yaml' or 'dist/chart/manager/manager.yaml'
is manually re-applied afterwards.

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This NetworkPolicy allows ingress traffic
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those
# namespaces are able to gathering data from the metrics endpoint.
# namespaces are able to gather data from the metrics endpoint.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
Expand Down
31 changes: 26 additions & 5 deletions docs/book/src/getting-started/testdata/project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,48 @@ make undeploy

## Project Distribution

Following are the steps to build the installer and distribute this project to users.
Following the options to release and provide this solution to the users.

### By providing a bundle with all YAML files

1. Build the installer for the image built and published in the registry:

```sh
make build-installer IMG=<some-registry>/project:tag
```

NOTE: The makefile target mentioned above generates an 'install.yaml'
**NOTE:** The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.
with Kustomize, which are necessary to install this project without its
dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:
Users can just run 'kubectl apply -f <URL for YAML BUNDLE>' to install
the project, i.e.:

```sh
kubectl apply -f https://raw.githubusercontent.com/<org>/project/<tag or branch>/dist/install.yaml
```

### By providing a Helm Chart

1. Build the chart using the optional helm plugin

```sh
kubebuilder edit --plugins=helm/v1-alpha
```

2. See that a chart was generated under 'dist/chart', and users
can obtain this solution from there.

**NOTE:** If you change the project, you need to update the Helm Chart
using the same command above to sync the latest changes. Furthermore,
if you create webhooks, you need to use the above command with
the '--force' flag and manually ensure that any custom configuration
previously added to 'dist/chart/values.yaml' or 'dist/chart/manager/manager.yaml'
is manually re-applied afterwards.

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This NetworkPolicy allows ingress traffic
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those
# namespaces are able to gathering data from the metrics endpoint.
# namespaces are able to gather data from the metrics endpoint.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
Expand Down
31 changes: 26 additions & 5 deletions docs/book/src/multiversion-tutorial/testdata/project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,48 @@ make undeploy

## Project Distribution

Following are the steps to build the installer and distribute this project to users.
Following the options to release and provide this solution to the users.

### By providing a bundle with all YAML files

1. Build the installer for the image built and published in the registry:

```sh
make build-installer IMG=<some-registry>/project:tag
```

NOTE: The makefile target mentioned above generates an 'install.yaml'
**NOTE:** The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.
with Kustomize, which are necessary to install this project without its
dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:
Users can just run 'kubectl apply -f <URL for YAML BUNDLE>' to install
the project, i.e.:

```sh
kubectl apply -f https://raw.githubusercontent.com/<org>/project/<tag or branch>/dist/install.yaml
```

### By providing a Helm Chart

1. Build the chart using the optional helm plugin

```sh
kubebuilder edit --plugins=helm/v1-alpha
```

2. See that a chart was generated under 'dist/chart', and users
can obtain this solution from there.

**NOTE:** If you change the project, you need to update the Helm Chart
using the same command above to sync the latest changes. Furthermore,
if you create webhooks, you need to use the above command with
the '--force' flag and manually ensure that any custom configuration
previously added to 'dist/chart/values.yaml' or 'dist/chart/manager/manager.yaml'
is manually re-applied afterwards.

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This NetworkPolicy allows ingress traffic
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those
# namespaces are able to gathering data from the metrics endpoint.
# namespaces are able to gather data from the metrics endpoint.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
Expand Down
92 changes: 92 additions & 0 deletions docs/book/src/plugins/available/helm-v1-alpha.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Helm Plugin (`helm/v1-alpha`)

The Helm plugin is an optional plugin that can be used to scaffold a Helm chart, allowing you to distribute the project using Helm.

By default, users can generate a bundle with all the manifests by running the following command:

```bash
make build-installer IMG=<some-registry>/<project-name:tag>
```

This allows the project consumer to install the solution by applying the bundle with:

```bash
kubectl apply -f https://raw.githubusercontent.com/<org>/project-v4/<tag or branch>/dist/install.yaml
```

However, in many scenarios, you might prefer to provide a Helm chart to package your solution.
If so, you can use this plugin to generate the Helm chart under the `dist` directory.

<aside class="note">
<h1>Examples</h1>

You can check the plugin usage by looking at `project-v4-with-plugins` samples
under the [testdata][testdata] directory on the root directory of the Kubebuilder project.

</aside>

## When to use it

- If you want to provide a Helm chart for users to install and manage your project.
- If you need to update the Helm chart generated under `dist/chart/` with the latest project changes:
- After generating new manifests, use the `edit` option to sync the Helm chart.
- **IMPORTANT:** If you have created a webhook or an API using the [DeployImage][deployImage-plugin] plugin,
you must run the `edit` command with the `--force` flag to regenerate the Helm chart values based
on the latest manifests (_after running `make manifests`_) to ensure that the HelmChart values are
updated accordingly. In this case, if you have customized the files
under `dist/chart/values.yaml`, and the `templates/manager/manager.yaml`, you will need to manually reapply your customizations on top
of the latest changes after regenerating the Helm chart.

## How to use it ?

### Basic Usage

The Helm plugin is attached to the `init` subcommand and the `edit` subcommand:

```sh

# Initialize a new project with helm chart
kubebuilder init --plugins=helm/v1-alpha

# Enable or Update the helm chart via the helm plugin to an existing project
# Before run the edit command, run `make manifests` to generate the manifest under `config/`
make manifests
kubebuilder edit --plugins=helm/v1-alpha
```
<aside class="note">
<h1>Use the edit command to update the Helm Chart with the latest changes</h1>

After making changes to your project, ensure that you run `make manifests` and then
use the command `kubebuilder edit --plugins=helm/v1-alpha` to update the Helm Chart.

Note that the following files will **not** be updated unless you use the `--force` flag:

<pre>
dist/chart/
├── values.yaml
└── templates/
└── manager/
└── manager.yaml
</pre>

The files `chart/Chart.yaml`, `chart/templates/_helpers.tpl`, and `chart/.helmignore` are never updated
after their initial creation unless you remove them.

</aside>

## Subcommands

The Helm plugin implements the following subcommands:

- edit (`$ kubebuilder edit [OPTIONS]`)

- init (`$ kubebuilder init [OPTIONS]`)

## Affected files

The following scaffolds will be created or updated by this plugin:

- `dist/chart/*`

[testdata]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/testdata/project-v4-with-plugins
[deployImage-plugin]: ./deploy-image-plugin-v1-alpha.md
Loading

0 comments on commit 2f49d33

Please sign in to comment.