Skip to content

Commit

Permalink
feat: use custom chart
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Oct 8, 2024
1 parent e2a87e1 commit 81f1ee3
Show file tree
Hide file tree
Showing 4 changed files with 93 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
}
3 changes: 1 addition & 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;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 Down
52 changes: 27 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,15 @@
"Unit": {
"properties": {
"chart": {
"$ref": "#/$defs/Dependency"
"$ref": "#/$defs/Chart",
"anyOf": [
{
"type": "string"
},
{
"type": "Chart"
}
]
},
"kustomize": {
"type": "string"
Expand Down

0 comments on commit 81f1ee3

Please sign in to comment.