Skip to content

A helm v3 plugin to adopt existing k8s resources into a new generated helm chart

License

Notifications You must be signed in to change notification settings

HamzaZo/helm-adopt

Repository files navigation

Licence Helm

helm-adopt

Overview

helm-adopt is a helm plugin to adopt existing k8s resources into a new generated helm chart, the idea behind the plugin was inspired by issue 2730.

adopt plugin allows you to :

  • adopt existing k8s resources by generating a helm chart
  • migrate adopted resources to be controlled by helm
  • create a helm release using secret as driver
  • perform a dry-run/debug-mode (optional)

Note: adopt does not re-create resources.

Getting started

Installation

To install the plugin:

$ helm plugin install https://github.com/HamzaZo/helm-adopt

Update to latest

$ helm plugin update adopt

Install a specific version

$ helm plugin install https://github.com/HamzaZo/helm-adopt --version 0.3.1

You can also verify it's been installed using

$ helm plugin list

Usage

$ helm adopt
Adopt k8s resources into a new helm chart. It's expected to match plural resources kinds.

Usage:
  adopt [command]

Available Commands:
  help        Help about any command
  resources   adopt k8s resources into a new generated helm chart

Flags:
  -h, --help   help for adopt

Use "adopt [command] --help" for more information about a command.

Adopt k8s resources

$ helm adopt resources -h
Adopt k8s resources into a new generated helm chart 

Examples:
        
    $ helm adopt resources deployments:nginx services:my-svc -o/--output frontend

    $ helm adopt resources deployments:nginx clusterrolebindings:binding-rbac -o/--output frontend -n/--namespace <ns>

    $ helm adopt resources statefulsets:nginx services:my-svc -r/--release RELEASE-NAME -o/--output frontend -c/--kube-context <ctx>

    $ helm adopt resources deployments:nginx services:my-svc -r/--release RELEASE-NAME -o/--output frontend -k/--kubeconfig <kcfg>

Usage:
  adopt resources <pluralKind>:<name> [flags]

Flags:
      --debug-mode            Show the generated manifests on STDOUT
      --dry-run               Print what resources will be adopted 
  -h, --help                  help for resources
  -c, --kube-context string   name of the kubeconfig context to use
  -k, --kubeconfig string     path to the kubeconfig file
  -n, --namespace string      namespace scope for this request
  -o, --output string         Specify the chart directory of loaded yaml files
  -r, --release string        Specify the name for the generated release


Example User case

In the following example the adopt plugin will :

  • Generate a chart directory called example
  • adopt frontend,backend deployments and generate deployments-0.yaml deployments-1.yaml files.
  • adopt frontend,backend services and generate services-0.yaml services-1.yaml files.
  • adopt default-lr limitranges and generate limitranges-0.yaml file.
  • migrate adopted resources control to Helm
  • create example release with version 1 using secret as driver.
$ helm adopt resources deployments:frontend,backend services:frontend,backend limitranges:default-lr --output example --release example
INFO[0000] Adopting resources..                         
INFO[0000] Generating chart example                     
INFO[0000] Added resource as file deployments-0 into example chart 
INFO[0000] Added resource as file deployments-1 into example chart 
INFO[0000] Added resource as file limitranges-0 into example chart 
INFO[0000] Added resource as file services-0 into example chart 
INFO[0000] Added resource as file services-1 into example chart 
INFO[0000] Chart example is released as example.1 

Note: You must use a semicolon after each name <pluralKind>:<name>,<name>,<name> if you need to adopt a bunch of resources under a specific pluralKind as shown in the example above.

check the release status

$ helm ls
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
example default         1               2021-05-18 12:04:53.171943 +0200 CEST   deployed        example-0.1.0   0.1.0       
$ kubectl get secret
NAME                            TYPE                                  DATA   AGE
sh.helm.release.v1.example.v1   helm.sh/release.v1                    1      51s

check generated chart directory.

$ tree example 
example
├── Chart.yaml
├── charts
├── templates
│   ├── _helpers.tpl
│   ├── deployments-0.yaml
│   ├── deployments-1.yaml
│   ├── limitranges-0.yaml
│   ├── services-0.yaml
│   └── services-1.yaml
└── values.yaml

2 directories, 8 files

Afterwards, you can edit the manifests in example/templates and turn them into templates and/or rework the generated chart into subcharts and then do a helm upgrade to fully transform the release.

Hints: To find out the pluralKind for a specific kind use kubectl api-resources