From 81f1ee33301d5317d89a77d5c183847d33209b1b Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Tue, 8 Oct 2024 09:59:01 +0200 Subject: [PATCH 1/2] feat: use custom chart --- .golangci.yml | 3 ++ internal/config/chart.go | 62 ++++++++++++++++++++++++++++++++++++++++ internal/config/unit.go | 3 +- squadron.schema.json | 52 +++++++++++++++++---------------- 4 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 internal/config/chart.go diff --git a/.golangci.yml b/.golangci.yml index a2a2d5f..69a71c2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -28,6 +28,9 @@ linters-settings: testifylint: disable: - float-compare + # https://golangci-lint.run/usage/linters/#goconst + goconst: + ignore-strings: '!!.+' # https://golangci-lint.run/usage/linters/#gosec gosec: confidence: medium diff --git a/internal/config/chart.go b/internal/config/chart.go new file mode 100644 index 0000000..9aed233 --- /dev/null +++ b/internal/config/chart.go @@ -0,0 +1,62 @@ +package config + +import ( + "context" + "fmt" + "os" + "path" + + "github.com/foomo/squadron/internal/template" + "github.com/pkg/errors" + "gopkg.in/yaml.v3" +) + +type Chart struct { + Name string `json:"name,omitempty" yaml:"name,omitempty"` + Repository string `json:"repository,omitempty" yaml:"repository,omitempty"` + Version string `json:"version,omitempty" yaml:"version,omitempty"` + Alias string `json:"alias,omitempty" yaml:"alias,omitempty"` +} + +func (d *Chart) UnmarshalYAML(value *yaml.Node) error { + switch value.Tag { + case "!!map": + type wrapper Chart + return value.Decode((*wrapper)(d)) + case "!!str": + var vString string + if err := value.Decode(&vString); err != nil { + return err + } + vBytes, err := template.ExecuteFileTemplate(context.Background(), vString, nil, true) + if err != nil { + return errors.Wrap(err, "failed to render chart string") + } + localChart, err := loadChart(path.Join(string(vBytes), "Chart.yaml")) + if err != nil { + return errors.New("failed to load local chart: " + vString) + } + d.Name = localChart.Name + d.Repository = fmt.Sprintf("file://%v", vString) + d.Version = localChart.Version + return nil + default: + return fmt.Errorf("unsupported node tag type for %T: %q", d, value.Tag) + } +} + +func (d *Chart) String() string { + return fmt.Sprintf("%s/%s:%s", d.Repository, d.Name, d.Version) +} + +func loadChart(name string) (*Chart, error) { + c := Chart{} + file, err := os.ReadFile(name) + if err != nil { + return nil, errors.Wrap(err, "error while opening file") + } + if err := yaml.Unmarshal(file, &c); err != nil { + return nil, errors.Wrap(err, "error while unmarshalling template file") + } + return &c, nil +} diff --git a/internal/config/unit.go b/internal/config/unit.go index 71e7b8b..f5ef0b3 100644 --- a/internal/config/unit.go +++ b/internal/config/unit.go @@ -8,14 +8,13 @@ import ( "sort" "strings" - "github.com/foomo/squadron/internal/helm" "github.com/foomo/squadron/internal/util" "github.com/pkg/errors" yamlv2 "gopkg.in/yaml.v2" ) type Unit struct { - Chart helm.Dependency `json:"chart,omitempty" yaml:"chart,omitempty"` + Chart Chart `json:"chart,omitempty" yaml:"chart,omitempty" jsonschema:"anyof_type=string;Chart"` Kustomize string `json:"kustomize,omitempty" yaml:"kustomize,omitempty"` Tags Tags `json:"tags,omitempty" yaml:"tags,omitempty"` Builds map[string]Build `json:"builds,omitempty" yaml:"builds,omitempty"` diff --git a/squadron.schema.json b/squadron.schema.json index 2a302f3..f209b49 100644 --- a/squadron.schema.json +++ b/squadron.schema.json @@ -120,6 +120,24 @@ "additionalProperties": false, "type": "object" }, + "Chart": { + "properties": { + "name": { + "type": "string" + }, + "repository": { + "type": "string" + }, + "version": { + "type": "string" + }, + "alias": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, "Config": { "properties": { "version": { @@ -153,30 +171,6 @@ "version" ] }, - "Dependency": { - "properties": { - "Name": { - "type": "string" - }, - "Repository": { - "type": "string" - }, - "Version": { - "type": "string" - }, - "Alias": { - "type": "string" - } - }, - "additionalProperties": false, - "type": "object", - "required": [ - "Name", - "Repository", - "Version", - "Alias" - ] - }, "Tags": { "items": { "type": "string" @@ -186,7 +180,15 @@ "Unit": { "properties": { "chart": { - "$ref": "#/$defs/Dependency" + "$ref": "#/$defs/Chart", + "anyOf": [ + { + "type": "string" + }, + { + "type": "Chart" + } + ] }, "kustomize": { "type": "string" From 2523e807e6a1d8ea542abbf31a2fd58dbe268f34 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Tue, 8 Oct 2024 10:12:15 +0200 Subject: [PATCH 2/2] fix: ref --- internal/config/unit.go | 11 ++++++++++- squadron.schema.json | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/config/unit.go b/internal/config/unit.go index f5ef0b3..00ea908 100644 --- a/internal/config/unit.go +++ b/internal/config/unit.go @@ -14,7 +14,7 @@ import ( ) type Unit struct { - Chart Chart `json:"chart,omitempty" yaml:"chart,omitempty" jsonschema:"anyof_type=string;Chart"` + Chart Chart `json:"chart,omitempty" yaml:"chart,omitempty" jsonschema:"anyof_type=string,anyof_ref=#/$defs/Chart"` Kustomize string `json:"kustomize,omitempty" yaml:"kustomize,omitempty"` Tags Tags `json:"tags,omitempty" yaml:"tags,omitempty"` Builds map[string]Build `json:"builds,omitempty" yaml:"builds,omitempty"` @@ -25,6 +25,15 @@ type Unit struct { // ~ Public methods // ------------------------------------------------------------------------------------------------ +// JSONSchemaProperty type workaround +func (Unit) JSONSchemaProperty(prop string) any { + var x any + if prop == "chart" { + return x + } + return nil +} + func (u *Unit) ValuesYAML(global, vars map[string]any) ([]byte, error) { values := u.Values if values == nil { diff --git a/squadron.schema.json b/squadron.schema.json index f209b49..4e5755a 100644 --- a/squadron.schema.json +++ b/squadron.schema.json @@ -180,13 +180,12 @@ "Unit": { "properties": { "chart": { - "$ref": "#/$defs/Chart", "anyOf": [ { "type": "string" }, { - "type": "Chart" + "$ref": "#/$defs/Chart" } ] },