Skip to content

Commit

Permalink
feat: #9 generate api schema from chart
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Jul 11, 2024
1 parent 933c09e commit b3f698d
Show file tree
Hide file tree
Showing 8 changed files with 520 additions and 52 deletions.
62 changes: 60 additions & 2 deletions cmd/init_helm_promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package cmd

import (
"fmt"
helmclient "github.com/mittwald/go-helm-client"
"github.com/spf13/cobra"
"github.com/syntasso/kratix-cli/internal"
"github.com/syntasso/kratix/api/v1alpha1"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/registry"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -43,7 +47,12 @@ func InitHelmPromise(cmd *cobra.Command, args []string) error {
return err
}

templateValues := generateTemplateValues(promiseName, "helm-promise", resourceConfigure)
crdSchema, err := schemaFromChart()
if err != nil {
return err
}

templateValues := generateTemplateValues(promiseName, "helm-promise", resourceConfigure, crdSchema)

templates := map[string]string{
resourceFileName: "templates/promise/example-resource.yaml.tpl",
Expand Down Expand Up @@ -94,7 +103,7 @@ func generateResourceConfigurePipeline() (string, error) {
"containers": []interface{}{
v1alpha1.Container{
Name: "instance-configure",
Image: "ghcr.io/syntasso/kratix-cli/helm-instance-configure:v0.1.0",
Image: "ghcr.io/syntasso/kratix-cli/helm-resource-configure:v0.1.0",
Env: envVars,
},
},
Expand All @@ -108,3 +117,52 @@ func generateResourceConfigurePipeline() (string, error) {
}
return string(pipelineBytes), nil
}

func schemaFromChart() (string, error) {
values, err := valuesFromChart()
if err != nil {
return "", err
}
schema, err := internal.HelmValuesToSchema(values)
if err != nil {
return "", fmt.Errorf("failed to convert helm values to schema: %w", err)
}

bytes, err := yaml.Marshal(*schema)
if err != nil {
return "", err
}

return string(bytes), nil
}

func valuesFromChart() (map[string]interface{}, error) {
client, err := helmclient.New(nil)
if err != nil {
return nil, fmt.Errorf("failed to create helm client: %w", err)
}

install := action.NewInstall(&action.Configuration{})
registryClient, _ := registry.NewClient()
install.SetRegistryClient(registryClient)

if chartName != "" {
install.RepoURL = chartURL
}

chart, _, err := client.GetChart(getChartName(), &install.ChartPathOptions)
if err != nil {
return nil, fmt.Errorf("failed to fetch helm chart: %w", err)
}

return chart.Values, nil
}

// when provided --chart-url is a chart repo and --chart-name is provided, getChartName() returns chart-name
// when provided --chart-url is OCI or a tar chart, getChartName() returns chart url
func getChartName() string {
if chartName != "" {
return chartName
}
return chartURL
}
32 changes: 4 additions & 28 deletions cmd/init_promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ type promiseTemplateValues struct {
Singular string
SubCommand string
ResourceConfigure string
CRDSchema string
}

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

templateValues := generateTemplateValues(promiseName, "promise", "")
templateValues := generateTemplateValues(promiseName, "promise", "", "")

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

}

func templatePromiseFiles(promiseName, subcommand string) error {
templates := map[string]string{
resourceFileName: "templates/promise/example-resource.yaml.tpl",
"README.md": "templates/promise/README.md.tpl",
}

if split {
templates[apiFileName] = "templates/promise/api.yaml.tpl"
templates[dependenciesFileName] = "templates/promise/dependencies.yaml"
} else {
templates[promiseFileName] = "templates/promise/promise.yaml.tpl"
}

templateValues := generateTemplateValues(promiseName, subcommand, "")
if err := templateFiles(promiseTemplates, outputDir, templates, templateValues); err != nil {
return err
}

dirName := "current"
if outputDir != "." {
dirName = outputDir
}
fmt.Printf("%s promise bootstrapped in the %s directory\n", promiseName, dirName)
return nil
}

func generateTemplateValues(promiseName, subCommand, resourceConfigure string) promiseTemplateValues {
func generateTemplateValues(promiseName, subCommand, resourceConfigure, crdSchema string) promiseTemplateValues {
if version == "" {
version = "v1alpha1"
}
Expand All @@ -123,5 +98,6 @@ func generateTemplateValues(promiseName, subCommand, resourceConfigure string) p
Singular: strings.ToLower(kind),
SubCommand: subCommand,
ResourceConfigure: resourceConfigure,
CRDSchema: crdSchema,
}
}
2 changes: 1 addition & 1 deletion cmd/templates/promise/api.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ spec:
properties:
spec:
type: object
properties: {}
{{ .CRDSchema | indent 14 }}
served: true
storage: true
2 changes: 1 addition & 1 deletion cmd/templates/promise/promise.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
properties:
spec:
type: object
properties: {}
{{ .CRDSchema | indent 18 }}
served: true
storage: true
workflows:
Expand Down
80 changes: 79 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,153 @@ go 1.22.4
require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/go-logr/logr v1.4.2
github.com/mittwald/go-helm-client v0.12.10
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/spf13/cobra v1.8.1
github.com/syntasso/kratix v0.121.0
helm.sh/helm/v3 v3.15.2
k8s.io/api v0.30.2
k8s.io/apiextensions-apiserver v0.30.2
k8s.io/apimachinery v0.30.2
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
sigs.k8s.io/controller-runtime v0.18.4
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/squirrel v1.5.4 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/containerd/containerd v1.7.12 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/cli v25.0.1+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v25.0.5+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc6 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rubenv/sql-migrate v1.6.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.starlark.net v0.0.0-20231121155337-90ade8b19d09 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.22.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/evanphx/json-patch.v5 v5.7.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiserver v0.30.2 // indirect
k8s.io/cli-runtime v0.30.0 // indirect
k8s.io/client-go v0.30.2 // indirect
k8s.io/component-base v0.30.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240620174524-b456828f718b // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
k8s.io/kubectl v0.30.0 // indirect
oras.land/oras-go v1.2.5 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.16.0 // indirect
sigs.k8s.io/kustomize/kyaml v0.16.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
Loading

0 comments on commit b3f698d

Please sign in to comment.