Skip to content

Commit

Permalink
support chart path in name
Browse files Browse the repository at this point in the history
  • Loading branch information
arttor committed Sep 24, 2021
1 parent 2d1a16c commit 6233e66
Show file tree
Hide file tree
Showing 45 changed files with 131 additions and 86 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Tested with operator-sdk version: "v1.8.0".
```makefile
HELMIFY = $(shell pwd)/bin/helmify
helmify:
$(call go-get-tool,$(HELMIFY),github.com/arttor/helmify/cmd/helmify@v0.3.0)
$(call go-get-tool,$(HELMIFY),github.com/arttor/helmify/cmd/helmify@v0.3.2)
helm: manifests kustomize helmify
$(KUSTOMIZE) build config/default | $(HELMIFY)
Expand All @@ -63,7 +63,7 @@ Unpack the helmify binary and add it to your PATH and you are good to go!
Helmify takes a chart name for an argument.
Usage:

```helmify [flags] CHART_NAME``` - `CHART_NAME` is optional. Default is 'chart'.
```helmify [flags] CHART_NAME``` - `CHART_NAME` is optional. Default is 'chart'. Can be a directory, e.g. 'deploy/charts/mychart'.

| flag | description | sample |
| --- | --- | --- |
Expand Down
10 changes: 8 additions & 2 deletions cmd/helmify/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/arttor/helmify/pkg/config"
)
Expand All @@ -20,7 +21,7 @@ Example 3: 'awk 'FNR==1 && NR!=1 {print "---"}{print}' /my_directory/*.yaml | h
- will create 'mychart' directory with Helm chart from all yaml files in my_directory directory.
Usage:
helmify [flags] CHART_NAME - CHART_NAME is optional. Default is 'chart'.
helmify [flags] CHART_NAME - CHART_NAME is optional. Default is 'chart'. Can be a directory, e.g. 'deploy/charts/mychart'.
Flags:
`
Expand All @@ -44,6 +45,11 @@ func ReadFlags() config.Config {
printVersion()
os.Exit(0)
}
result.ChartName = flag.Arg(0)
name := flag.Arg(0)
if name != "" {
result.ChartName = filepath.Base(name)
result.ChartDir = filepath.Dir(name)
}

return result
}
6 changes: 3 additions & 3 deletions examples/app/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ spec:
secretKeyRef:
key: VAR2
name: {{ include "app.fullname" . }}-secret-vars
image: {{ .Values.image.app.repository }}:{{ .Values.image.app.tag | default .Chart.AppVersion
}}
image: {{ .Values.app.app.image.repository }}:{{ .Values.app.app.image.tag | default
.Chart.AppVersion }}
livenessProbe:
httpGet:
path: /healthz
Expand Down Expand Up @@ -67,7 +67,7 @@ spec:
- args:
- --secure-listen-address=0.0.0.0:8443
- --v=10
image: {{ .Values.image.proxySidecar.repository }}:{{ .Values.image.proxySidecar.tag
image: {{ .Values.app.proxySidecar.image.repository }}:{{ .Values.app.proxySidecar.image.tag
| default .Chart.AppVersion }}
name: proxy-sidecar
ports:
Expand Down
14 changes: 7 additions & 7 deletions examples/app/values.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
app:
app:
image:
repository: controller
tag: latest
resources:
limits:
cpu: 100m
memory: 30Mi
requests:
cpu: 100m
memory: 20Mi
proxySidecar:
image:
repository: gcr.io/kubebuilder/kube-rbac-proxy
tag: v0.8.0
replicas: 3
appService:
ports:
Expand All @@ -30,13 +37,6 @@ configProps:
myProp1: "1"
myProp2: val 1
myProp3: "true"
image:
app:
repository: controller
tag: latest
proxySidecar:
repository: gcr.io/kubebuilder/kube-rbac-proxy
tag: v0.8.0
secretCa:
caCrt: ""
secretVars:
Expand Down
6 changes: 3 additions & 3 deletions examples/operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spec:
- --upstream=http://127.0.0.1:8080/
- --logtostderr=true
- --v=10
image: {{ .Values.image.kubeRbacProxy.repository }}:{{ .Values.image.kubeRbacProxy.tag
image: {{ .Values.controllerManager.kubeRbacProxy.image.repository }}:{{ .Values.controllerManager.kubeRbacProxy.image.tag
| default .Chart.AppVersion }}
name: kube-rbac-proxy
ports:
Expand All @@ -49,8 +49,8 @@ spec:
secretKeyRef:
key: VAR1
name: {{ include "operator.fullname" . }}-secret-vars
image: {{ .Values.image.manager.repository }}:{{ .Values.image.manager.tag | default
.Chart.AppVersion }}
image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag
| default .Chart.AppVersion }}
livenessProbe:
httpGet:
path: /healthz
Expand Down
4 changes: 2 additions & 2 deletions examples/operator/templates/secret-ca.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ metadata:
labels:
{{- include "operator.labels" . | nindent 4 }}
data:
ca.crt: '{{ required "secret secretCa.caCrt is required" .Values.secretCa.caCrt
| b64enc }}'
ca.crt: {{ required "secretCa.caCrt is required" .Values.secretCa.caCrt | b64enc
| quote }}
8 changes: 4 additions & 4 deletions examples/operator/templates/secret-vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
labels:
{{- include "operator.labels" . | nindent 4 }}
data:
VAR1: '{{ required "secret secretVars.var1 is required" .Values.secretVars.var1
| b64enc }}'
VAR2: '{{ required "secret secretVars.var2 is required" .Values.secretVars.var2
| b64enc }}'
VAR1: {{ required "secretVars.var1 is required" .Values.secretVars.var1 | b64enc
| quote }}
VAR2: {{ required "secretVars.var2 is required" .Values.secretVars.var2 | b64enc
| quote }}
14 changes: 7 additions & 7 deletions examples/operator/values.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
controllerManager:
kubeRbacProxy:
image:
repository: gcr.io/kubebuilder/kube-rbac-proxy
tag: v0.8.0
manager:
image:
repository: controller
tag: latest
resources:
limits:
cpu: 100m
Expand All @@ -8,13 +15,6 @@ controllerManager:
cpu: 100m
memory: 20Mi
replicas: 1
image:
kubeRbacProxy:
repository: gcr.io/kubebuilder/kube-rbac-proxy
tag: v0.8.0
manager:
repository: controller
tag: latest
managerConfig:
controllerManagerConfigYaml:
health:
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package app

import (
"context"
"github.com/arttor/helmify/pkg/processor"
"io"
"os"
"os/signal"
"syscall"

"github.com/arttor/helmify/pkg/processor"

"github.com/arttor/helmify/pkg/config"
"github.com/arttor/helmify/pkg/decoder"
"github.com/arttor/helmify/pkg/helm"
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *appContext) CreateHelm(stop <-chan struct{}) error {
default:
}
}
return c.output.Create(c.appMeta.ChartName(), templates)
return c.output.Create(c.config.ChartDir, c.config.ChartName, templates)
}

func (c *appContext) process(obj *unstructured.Unstructured) (helmify.Template, error) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ const defaultChartName = "chart"

// Config for Helmify application.
type Config struct {
// ChartName overrides DefaultChartName.
// ChartName name of the Helm chart and its base directory where Chart.yaml is located.
ChartName string
// ChartDir - optional path to chart dir. Full chart path will be: ChartDir/ChartName/Chart.yaml.
ChartDir string
// Verbose set true to see WARN and INFO logs.
Verbose bool
// VeryVerbose set true to see WARN, INFO, and DEBUG logs.
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfig_Validate(t *testing.T) {
Expand Down
24 changes: 13 additions & 11 deletions pkg/helm/chart.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package helm

import (
"github.com/arttor/helmify/pkg/helmify"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"

"github.com/arttor/helmify/pkg/helmify"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"sigs.k8s.io/yaml"
)

Expand All @@ -26,8 +27,8 @@ type output struct{}
// └── templates/ # The template files
// └── _helpers.tp # Helm default template partials
// Overwrites existing values.yaml and templates in templates dir on every run.
func (o output) Create(chartName string, templates []helmify.Template) error {
err := initChartDir(chartName)
func (o output) Create(chartDir, chartName string, templates []helmify.Template) error {
err := initChartDir(chartDir, chartName)
if err != nil {
return err
}
Expand All @@ -43,21 +44,22 @@ func (o output) Create(chartName string, templates []helmify.Template) error {
return err
}
}
cDir := filepath.Join(chartDir, chartName)
for filename, tpls := range files {
err = overwriteTemplateFile(filename, chartName, tpls)
err = overwriteTemplateFile(filename, cDir, tpls)
if err != nil {
return err
}
}
err = overwriteValuesFile(chartName, values)
err = overwriteValuesFile(cDir, values)
if err != nil {
return err
}
return nil
}

func overwriteTemplateFile(filename, chartName string, templates []helmify.Template) error {
file := filepath.Join(chartName, "templates", filename)
func overwriteTemplateFile(filename, chartDir string, templates []helmify.Template) error {
file := filepath.Join(chartDir, "templates", filename)
f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return errors.Wrap(err, "unable to open "+file)
Expand All @@ -80,12 +82,12 @@ func overwriteTemplateFile(filename, chartName string, templates []helmify.Templ
return nil
}

func overwriteValuesFile(chartName string, values helmify.Values) error {
func overwriteValuesFile(chartDir string, values helmify.Values) error {
res, err := yaml.Marshal(values)
if err != nil {
return errors.Wrap(err, "unable to write marshal values.yaml")
}
file := filepath.Join(chartName, "values.yaml")
file := filepath.Join(chartDir, "values.yaml")
err = ioutil.WriteFile(file, res, 0600)
if err != nil {
return errors.Wrap(err, "unable to write values.yaml")
Expand Down
18 changes: 10 additions & 8 deletions pkg/helm/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ var chartName = regexp.MustCompile("^[a-zA-Z0-9._-]+$")
const maxChartNameLength = 250

// initChartDir - creates Helm chart structure in chartName directory if not presented.
func initChartDir(chartName string) error {
func initChartDir(chartDir, chartName string) error {
if err := validateChartName(chartName); err != nil {
return err
}
_, err := os.Stat(filepath.Join(chartName, "Chart.yaml"))
cDir := filepath.Join(chartDir, chartName)
_, err := os.Stat(filepath.Join(cDir, "Chart.yaml"))
if os.IsNotExist(err) {
return createCommonFiles(chartName)
return createCommonFiles(chartDir, chartName)
}
logrus.Info("Skip creating Chart skeleton: Chart.yaml already exists.")
return err
Expand All @@ -146,8 +147,9 @@ func validateChartName(name string) error {
return nil
}

func createCommonFiles(chartName string) error {
err := os.MkdirAll(filepath.Join(chartName, "templates"), 0750)
func createCommonFiles(chartDir, chartName string) error {
cDir := filepath.Join(chartDir, chartName)
err := os.MkdirAll(filepath.Join(cDir, "templates"), 0750)
if err != nil {
return errors.Wrap(err, "unable create chart dir")
}
Expand All @@ -161,9 +163,9 @@ func createCommonFiles(chartName string) error {
logrus.WithField("file", file).Info("created")
}
}
createFile(chartYAML(chartName), chartName, "Chart.yaml")
createFile([]byte(helmIgnore), chartName, ".helmignore")
createFile(helpersYAML(chartName), chartName, "templates", "_helpers.tpl")
createFile(chartYAML(chartName), cDir, "Chart.yaml")
createFile([]byte(helmIgnore), cDir, ".helmignore")
createFile(helpersYAML(chartName), cDir, "templates", "_helpers.tpl")
return err
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/helmify/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helmify

import (
"io"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand All @@ -25,7 +26,7 @@ type Template interface {

// Output - converts Template into helm chart on disk.
type Output interface {
Create(chartName string, templates []Template) error
Create(chartName, chartDir string, templates []Template) error
}

// AppMetadata handle common information about K8s objects in the chart.
Expand Down
3 changes: 2 additions & 1 deletion pkg/helmify/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package helmify

import (
"fmt"
"strings"

"github.com/iancoleman/strcase"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"strings"
)

// Values - represents helm template values.yaml.
Expand Down
3 changes: 2 additions & 1 deletion pkg/helmify/values_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package helmify

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestValues_Add(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package metadata

import (
"fmt"
"strings"

"github.com/arttor/helmify/pkg/helmify"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"strings"
)

const nameTeml = `{{ include "%s.fullname" . }}-%s`
Expand Down
Loading

0 comments on commit 6233e66

Please sign in to comment.