Skip to content

Commit ff23907

Browse files
authored
Merge pull request #24 from syntasso/helm-api
Generate CRD schema for Promise with `kratix init helm-promise`
2 parents 7f4fcaa + b3f698d commit ff23907

13 files changed

+675
-54
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test: # Run tests
1212
build: # Build the binary
1313
go build -o bin/kratix main.go
1414

15-
build-aspects: build-operator-promise-aspect # build all aspects
15+
build-aspects: build-operator-promise-aspect build-helm-promise-aspect # build all aspects
1616

1717
build-and-push-aspects: # build and push all aspects
1818
if ! docker buildx ls | grep -q "kratix-cli-image-builder"; then \

aspects/helm-promise/pipeline.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -eux
44

55
KRATIX_INPUT=${KRATIX_INPUT:-/kratix/input}
6-
KRATIX_OUTPUT=${KRATIX_OUTPUT:-/kratix/input}
6+
KRATIX_OUTPUT=${KRATIX_OUTPUT:-/kratix/output}
77
HELM_BINARY=${HELM_BINARY:-helm}
88
name=$(yq '.metadata.name' $KRATIX_INPUT/object.yaml)
99

cmd/init_helm_promise.go

+60-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package cmd
22

33
import (
44
"fmt"
5+
helmclient "github.com/mittwald/go-helm-client"
56
"github.com/spf13/cobra"
7+
"github.com/syntasso/kratix-cli/internal"
68
"github.com/syntasso/kratix/api/v1alpha1"
9+
"helm.sh/helm/v3/pkg/action"
10+
"helm.sh/helm/v3/pkg/registry"
711
corev1 "k8s.io/api/core/v1"
812
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
913
"sigs.k8s.io/yaml"
@@ -43,7 +47,12 @@ func InitHelmPromise(cmd *cobra.Command, args []string) error {
4347
return err
4448
}
4549

46-
templateValues := generateTemplateValues(promiseName, "helm-promise", resourceConfigure)
50+
crdSchema, err := schemaFromChart()
51+
if err != nil {
52+
return err
53+
}
54+
55+
templateValues := generateTemplateValues(promiseName, "helm-promise", resourceConfigure, crdSchema)
4756

4857
templates := map[string]string{
4958
resourceFileName: "templates/promise/example-resource.yaml.tpl",
@@ -94,7 +103,7 @@ func generateResourceConfigurePipeline() (string, error) {
94103
"containers": []interface{}{
95104
v1alpha1.Container{
96105
Name: "instance-configure",
97-
Image: "ghcr.io/syntasso/kratix-cli/helm-instance-configure:v0.1.0",
106+
Image: "ghcr.io/syntasso/kratix-cli/helm-resource-configure:v0.1.0",
98107
Env: envVars,
99108
},
100109
},
@@ -108,3 +117,52 @@ func generateResourceConfigurePipeline() (string, error) {
108117
}
109118
return string(pipelineBytes), nil
110119
}
120+
121+
func schemaFromChart() (string, error) {
122+
values, err := valuesFromChart()
123+
if err != nil {
124+
return "", err
125+
}
126+
schema, err := internal.HelmValuesToSchema(values)
127+
if err != nil {
128+
return "", fmt.Errorf("failed to convert helm values to schema: %w", err)
129+
}
130+
131+
bytes, err := yaml.Marshal(*schema)
132+
if err != nil {
133+
return "", err
134+
}
135+
136+
return string(bytes), nil
137+
}
138+
139+
func valuesFromChart() (map[string]interface{}, error) {
140+
client, err := helmclient.New(nil)
141+
if err != nil {
142+
return nil, fmt.Errorf("failed to create helm client: %w", err)
143+
}
144+
145+
install := action.NewInstall(&action.Configuration{})
146+
registryClient, _ := registry.NewClient()
147+
install.SetRegistryClient(registryClient)
148+
149+
if chartName != "" {
150+
install.RepoURL = chartURL
151+
}
152+
153+
chart, _, err := client.GetChart(getChartName(), &install.ChartPathOptions)
154+
if err != nil {
155+
return nil, fmt.Errorf("failed to fetch helm chart: %w", err)
156+
}
157+
158+
return chart.Values, nil
159+
}
160+
161+
// when provided --chart-url is a chart repo and --chart-name is provided, getChartName() returns chart-name
162+
// when provided --chart-url is OCI or a tar chart, getChartName() returns chart url
163+
func getChartName() string {
164+
if chartName != "" {
165+
return chartName
166+
}
167+
return chartURL
168+
}

cmd/init_promise.go

+4-28
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ type promiseTemplateValues struct {
4747
Singular string
4848
SubCommand string
4949
ResourceConfigure string
50+
CRDSchema string
5051
}
5152

5253
func InitPromise(cmd *cobra.Command, args []string) error {
5354
promiseName := args[0]
5455

55-
templateValues := generateTemplateValues(promiseName, "promise", "")
56+
templateValues := generateTemplateValues(promiseName, "promise", "", "")
5657

5758
templates := map[string]string{
5859
resourceFileName: "templates/promise/example-resource.yaml.tpl",
@@ -79,33 +80,7 @@ func InitPromise(cmd *cobra.Command, args []string) error {
7980

8081
}
8182

82-
func templatePromiseFiles(promiseName, subcommand string) error {
83-
templates := map[string]string{
84-
resourceFileName: "templates/promise/example-resource.yaml.tpl",
85-
"README.md": "templates/promise/README.md.tpl",
86-
}
87-
88-
if split {
89-
templates[apiFileName] = "templates/promise/api.yaml.tpl"
90-
templates[dependenciesFileName] = "templates/promise/dependencies.yaml"
91-
} else {
92-
templates[promiseFileName] = "templates/promise/promise.yaml.tpl"
93-
}
94-
95-
templateValues := generateTemplateValues(promiseName, subcommand, "")
96-
if err := templateFiles(promiseTemplates, outputDir, templates, templateValues); err != nil {
97-
return err
98-
}
99-
100-
dirName := "current"
101-
if outputDir != "." {
102-
dirName = outputDir
103-
}
104-
fmt.Printf("%s promise bootstrapped in the %s directory\n", promiseName, dirName)
105-
return nil
106-
}
107-
108-
func generateTemplateValues(promiseName, subCommand, resourceConfigure string) promiseTemplateValues {
83+
func generateTemplateValues(promiseName, subCommand, resourceConfigure, crdSchema string) promiseTemplateValues {
10984
if version == "" {
11085
version = "v1alpha1"
11186
}
@@ -123,5 +98,6 @@ func generateTemplateValues(promiseName, subCommand, resourceConfigure string) p
12398
Singular: strings.ToLower(kind),
12499
SubCommand: subCommand,
125100
ResourceConfigure: resourceConfigure,
101+
CRDSchema: crdSchema,
126102
}
127103
}

cmd/templates/promise/api.yaml.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ spec:
1717
properties:
1818
spec:
1919
type: object
20-
properties: {}
20+
{{ .CRDSchema | indent 14 }}
2121
served: true
2222
storage: true

cmd/templates/promise/promise.yaml.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
properties:
2424
spec:
2525
type: object
26-
properties: {}
26+
{{ .CRDSchema | indent 18 }}
2727
served: true
2828
storage: true
2929
workflows:

go.mod

+79-1
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,153 @@ go 1.22.4
55
require (
66
github.com/Masterminds/sprig/v3 v3.2.3
77
github.com/go-logr/logr v1.4.2
8+
github.com/mittwald/go-helm-client v0.12.10
89
github.com/onsi/ginkgo/v2 v2.19.0
910
github.com/onsi/gomega v1.33.1
1011
github.com/spf13/cobra v1.8.1
1112
github.com/syntasso/kratix v0.121.0
13+
helm.sh/helm/v3 v3.15.2
1214
k8s.io/api v0.30.2
1315
k8s.io/apiextensions-apiserver v0.30.2
1416
k8s.io/apimachinery v0.30.2
17+
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
1518
sigs.k8s.io/controller-runtime v0.18.4
1619
sigs.k8s.io/yaml v1.4.0
1720
)
1821

1922
require (
23+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
24+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
25+
github.com/BurntSushi/toml v1.3.2 // indirect
26+
github.com/MakeNowJust/heredoc v1.0.0 // indirect
2027
github.com/Masterminds/goutils v1.1.1 // indirect
2128
github.com/Masterminds/semver/v3 v3.2.1 // indirect
29+
github.com/Masterminds/squirrel v1.5.4 // indirect
30+
github.com/Microsoft/hcsshim v0.11.4 // indirect
31+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
2232
github.com/beorn7/perks v1.0.1 // indirect
2333
github.com/cespare/xxhash/v2 v2.3.0 // indirect
34+
github.com/chai2010/gettext-go v1.0.2 // indirect
35+
github.com/containerd/containerd v1.7.12 // indirect
36+
github.com/containerd/log v0.1.0 // indirect
37+
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
2438
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
39+
github.com/distribution/reference v0.5.0 // indirect
40+
github.com/docker/cli v25.0.1+incompatible // indirect
41+
github.com/docker/distribution v2.8.3+incompatible // indirect
42+
github.com/docker/docker v25.0.5+incompatible // indirect
43+
github.com/docker/docker-credential-helpers v0.8.0 // indirect
44+
github.com/docker/go-connections v0.5.0 // indirect
45+
github.com/docker/go-metrics v0.0.1 // indirect
2546
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
47+
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
2648
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
49+
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
50+
github.com/fatih/color v1.16.0 // indirect
51+
github.com/felixge/httpsnoop v1.0.4 // indirect
2752
github.com/fsnotify/fsnotify v1.7.0 // indirect
53+
github.com/go-errors/errors v1.5.1 // indirect
54+
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
55+
github.com/go-logr/stdr v1.2.2 // indirect
2856
github.com/go-openapi/jsonpointer v0.21.0 // indirect
2957
github.com/go-openapi/jsonreference v0.21.0 // indirect
3058
github.com/go-openapi/swag v0.23.0 // indirect
3159
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
60+
github.com/gobwas/glob v0.2.3 // indirect
3261
github.com/gogo/protobuf v1.3.2 // indirect
3362
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3463
github.com/golang/protobuf v1.5.4 // indirect
64+
github.com/google/btree v1.1.2 // indirect
3565
github.com/google/gnostic-models v0.6.8 // indirect
3666
github.com/google/go-cmp v0.6.0 // indirect
3767
github.com/google/gofuzz v1.2.0 // indirect
3868
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
69+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
3970
github.com/google/uuid v1.6.0 // indirect
71+
github.com/gorilla/mux v1.8.1 // indirect
72+
github.com/gorilla/websocket v1.5.1 // indirect
73+
github.com/gosuri/uitable v0.0.4 // indirect
74+
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
75+
github.com/hashicorp/errwrap v1.1.0 // indirect
76+
github.com/hashicorp/go-multierror v1.1.1 // indirect
4077
github.com/huandu/xstrings v1.4.0 // indirect
4178
github.com/imdario/mergo v0.3.16 // indirect
4279
github.com/inconshreveable/mousetrap v1.1.0 // indirect
80+
github.com/jmoiron/sqlx v1.3.5 // indirect
4381
github.com/josharian/intern v1.0.0 // indirect
4482
github.com/json-iterator/go v1.1.12 // indirect
83+
github.com/klauspost/compress v1.17.6 // indirect
84+
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
85+
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
86+
github.com/lib/pq v1.10.9 // indirect
87+
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
4588
github.com/mailru/easyjson v0.7.7 // indirect
89+
github.com/mattn/go-colorable v0.1.13 // indirect
90+
github.com/mattn/go-isatty v0.0.20 // indirect
91+
github.com/mattn/go-runewidth v0.0.15 // indirect
4692
github.com/mitchellh/copystructure v1.2.0 // indirect
93+
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
4794
github.com/mitchellh/reflectwalk v1.0.2 // indirect
95+
github.com/moby/locker v1.0.1 // indirect
96+
github.com/moby/spdystream v0.2.0 // indirect
97+
github.com/moby/term v0.5.0 // indirect
4898
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4999
github.com/modern-go/reflect2 v1.0.2 // indirect
100+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
50101
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
102+
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
103+
github.com/opencontainers/go-digest v1.0.0 // indirect
104+
github.com/opencontainers/image-spec v1.1.0-rc6 // indirect
105+
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
51106
github.com/pkg/errors v0.9.1 // indirect
52107
github.com/prometheus/client_golang v1.19.1 // indirect
53108
github.com/prometheus/client_model v0.6.1 // indirect
54109
github.com/prometheus/common v0.55.0 // indirect
55110
github.com/prometheus/procfs v0.15.1 // indirect
111+
github.com/rivo/uniseg v0.4.4 // indirect
112+
github.com/rubenv/sql-migrate v1.6.0 // indirect
113+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
56114
github.com/shopspring/decimal v1.3.1 // indirect
115+
github.com/sirupsen/logrus v1.9.3 // indirect
57116
github.com/spf13/cast v1.6.0 // indirect
58117
github.com/spf13/pflag v1.0.5 // indirect
118+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
119+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
120+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
121+
github.com/xlab/treeprint v1.2.0 // indirect
122+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
123+
go.opentelemetry.io/otel v1.21.0 // indirect
124+
go.opentelemetry.io/otel/metric v1.21.0 // indirect
125+
go.opentelemetry.io/otel/trace v1.21.0 // indirect
126+
go.starlark.net v0.0.0-20231121155337-90ade8b19d09 // indirect
59127
golang.org/x/crypto v0.24.0 // indirect
60128
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
61129
golang.org/x/net v0.26.0 // indirect
62130
golang.org/x/oauth2 v0.21.0 // indirect
131+
golang.org/x/sync v0.7.0 // indirect
63132
golang.org/x/sys v0.21.0 // indirect
64133
golang.org/x/term v0.21.0 // indirect
65134
golang.org/x/text v0.16.0 // indirect
66135
golang.org/x/time v0.5.0 // indirect
67136
golang.org/x/tools v0.22.0 // indirect
68137
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
138+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
139+
google.golang.org/grpc v1.60.1 // indirect
69140
google.golang.org/protobuf v1.34.2 // indirect
141+
gopkg.in/evanphx/json-patch.v5 v5.7.0 // indirect
70142
gopkg.in/inf.v0 v0.9.1 // indirect
71143
gopkg.in/yaml.v2 v2.4.0 // indirect
72144
gopkg.in/yaml.v3 v3.0.1 // indirect
145+
k8s.io/apiserver v0.30.2 // indirect
146+
k8s.io/cli-runtime v0.30.0 // indirect
73147
k8s.io/client-go v0.30.2 // indirect
148+
k8s.io/component-base v0.30.2 // indirect
74149
k8s.io/klog/v2 v2.130.1 // indirect
75150
k8s.io/kube-openapi v0.0.0-20240620174524-b456828f718b // indirect
76-
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
151+
k8s.io/kubectl v0.30.0 // indirect
152+
oras.land/oras-go v1.2.5 // indirect
77153
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
154+
sigs.k8s.io/kustomize/api v0.16.0 // indirect
155+
sigs.k8s.io/kustomize/kyaml v0.16.0 // indirect
78156
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
79157
)

0 commit comments

Comments
 (0)