Skip to content

Commit

Permalink
Merge pull request #73 from foomo/schema
Browse files Browse the repository at this point in the history
feat: use custom chart
  • Loading branch information
franklinkim authored Oct 8, 2024
2 parents e2a87e1 + 2523e80 commit 45c90a1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions internal/config/chart.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 10 additions & 2 deletions internal/config/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,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"`
Expand All @@ -26,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 {
Expand Down
51 changes: 26 additions & 25 deletions squadron.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
Expand All @@ -186,7 +180,14 @@
"Unit": {
"properties": {
"chart": {
"$ref": "#/$defs/Dependency"
"anyOf": [
{
"type": "string"
},
{
"$ref": "#/$defs/Chart"
}
]
},
"kustomize": {
"type": "string"
Expand Down

0 comments on commit 45c90a1

Please sign in to comment.