Skip to content

Commit

Permalink
refactor: divide build
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Oct 8, 2024
1 parent 4fac10c commit 4e8c890
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 62 deletions.
66 changes: 57 additions & 9 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/siyul-park/uniflow/pkg/secret"
"github.com/siyul-park/uniflow/pkg/spec"
"github.com/siyul-park/uniflow/pkg/template"
"github.com/siyul-park/uniflow/pkg/types"
)

// Chart defines the structure that combines multiple nodes into a cluster node.
Expand Down Expand Up @@ -51,15 +52,15 @@ type Value struct {
var _ resource.Resource = (*Chart)(nil)

// IsBound checks whether any of the secrets are bound to the chart.
func IsBound(chrt *Chart, secrets ...*secret.Secret) bool {
for _, vals := range chrt.GetEnv() {
func (c *Chart) IsBound(secrets ...*secret.Secret) bool {
for _, vals := range c.GetEnv() {
for _, val := range vals {
examples := make([]*secret.Secret, 0, 2)
if val.ID != uuid.Nil {
examples = append(examples, &secret.Secret{ID: val.ID})
}
if val.Name != "" {
examples = append(examples, &secret.Secret{Namespace: chrt.GetNamespace(), Name: val.Name})
examples = append(examples, &secret.Secret{Namespace: c.GetNamespace(), Name: val.Name})
}

for _, sec := range secrets {
Expand All @@ -73,13 +74,13 @@ func IsBound(chrt *Chart, secrets ...*secret.Secret) bool {
}

// Bind binds the chart's environment variables to the provided secrets.
func Bind(chrt *Chart, secrets ...*secret.Secret) (*Chart, error) {
for _, vals := range chrt.GetEnv() {
func (c *Chart) Bind(secrets ...*secret.Secret) error {
for _, vals := range c.GetEnv() {
for i, val := range vals {
if val.ID != uuid.Nil || val.Name != "" {
example := &secret.Secret{
ID: val.ID,
Namespace: chrt.GetNamespace(),
Namespace: c.GetNamespace(),
Name: val.Name,
}

Expand All @@ -91,12 +92,12 @@ func Bind(chrt *Chart, secrets ...*secret.Secret) (*Chart, error) {
}
}
if sec == nil {
return nil, errors.WithStack(encoding.ErrUnsupportedValue)
return errors.WithStack(encoding.ErrUnsupportedValue)
}

v, err := template.Execute(val.Value, sec.Data)
if err != nil {
return nil, err
return err
}

val.ID = sec.GetID()
Expand All @@ -107,7 +108,54 @@ func Bind(chrt *Chart, secrets ...*secret.Secret) (*Chart, error) {
}
}
}
return chrt, nil
return nil
}

// Build constructs a specs based on the given spec.
func (c *Chart) Build(sp spec.Spec) ([]spec.Spec, error) {
doc, err := types.Marshal(sp)
if err != nil {
return nil, err
}

data := types.InterfaceOf(doc)

env := map[string][]spec.Value{}
for key, vals := range c.GetEnv() {
for _, val := range vals {
if val.ID == uuid.Nil && val.Name == "" {
v, err := template.Execute(val.Value, data)
if err != nil {
return nil, err
}
val.Value = v
}
env[key] = append(env[key], spec.Value{Value: val.Value})
}
}

specs := make([]spec.Spec, 0, len(c.GetSpecs()))
for _, sp := range c.GetSpecs() {
doc, err := types.Marshal(sp)
if err != nil {
return nil, err
}

unstructured := &spec.Unstructured{}
if err := types.Unmarshal(doc, unstructured); err != nil {
return nil, err
}

unstructured.SetEnv(env)

bind, err := spec.Bind(unstructured)
if err != nil {
return nil, err
}

specs = append(specs, bind)
}
return specs, nil
}

// GetID returns the chart's ID.
Expand Down
47 changes: 40 additions & 7 deletions pkg/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestIsBound(t *testing.T) {
func TestChart_IsBound(t *testing.T) {
sec1 := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
}
Expand All @@ -31,11 +31,11 @@ func TestIsBound(t *testing.T) {
},
}

assert.True(t, IsBound(chrt, sec1))
assert.False(t, IsBound(chrt, sec2))
assert.True(t, chrt.IsBound(sec1))
assert.False(t, chrt.IsBound(sec2))
}

func TestBind(t *testing.T) {
func TestChart_Bind(t *testing.T) {
sec := &secret.Secret{
ID: uuid.Must(uuid.NewV7()),
Data: "foo",
Expand All @@ -53,10 +53,43 @@ func TestBind(t *testing.T) {
},
}

bind, err := Bind(chrt, sec)
err := chrt.Bind(sec)
assert.NoError(t, err)
assert.Equal(t, "foo", bind.GetEnv()["FOO"][0].Value)
assert.True(t, IsBound(bind, sec))
assert.Equal(t, "foo", chrt.GetEnv()["FOO"][0].Value)
}

func TestChart_Build(t *testing.T) {
chrt := &Chart{
ID: uuid.Must(uuid.NewV7()),
Name: faker.UUIDHyphenated(),
Specs: []spec.Spec{
&spec.Unstructured{
Meta: spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: faker.UUIDHyphenated(),
},
Fields: map[string]any{
"foo": "{{ .FOO }}",
},
},
},
Env: map[string][]Value{
"FOO": {
{
Value: "foo",
},
},
},
}

meta := &spec.Meta{
Kind: chrt.GetName(),
Namespace: resource.DefaultNamespace,
}

specs, err := chrt.Build(meta)
assert.NoError(t, err)
assert.Len(t, specs, 1)
}

func TestChart_GetSet(t *testing.T) {
Expand Down
50 changes: 5 additions & 45 deletions pkg/chart/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package chart
import (
"slices"

"github.com/gofrs/uuid"
"github.com/siyul-park/uniflow/pkg/hook"
"github.com/siyul-park/uniflow/pkg/node"
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/siyul-park/uniflow/pkg/spec"
"github.com/siyul-park/uniflow/pkg/symbol"
"github.com/siyul-park/uniflow/pkg/template"
"github.com/siyul-park/uniflow/pkg/types"
)

// LinkerConfig holds the hook and scheme configuration.
Expand Down Expand Up @@ -43,58 +40,21 @@ func (l *Linker) Load(chrt *Chart) error {
}

codec := scheme.CodecFunc(func(sp spec.Spec) (node.Node, error) {
doc, err := types.Marshal(sp)
specs, err := chrt.Build(sp)
if err != nil {
return nil, err
}

env := map[string][]spec.Value{}
for key, vals := range chrt.GetEnv() {
for _, val := range vals {
if val.ID == uuid.Nil && val.Name == "" {
v, err := template.Execute(val.Value, types.InterfaceOf(doc))
if err != nil {
return nil, err
}
val.Value = v
}
env[key] = append(env[key], spec.Value{Value: val.Value})
}
}

symbols := make([]*symbol.Symbol, 0, len(chrt.GetSpecs()))
for _, sp := range chrt.GetSpecs() {
doc, err := types.Marshal(sp)
if err != nil {
return nil, err
}

unstructured := &spec.Unstructured{}
if err := types.Unmarshal(doc, unstructured); err != nil {
return nil, err
}

unstructured.SetEnv(env)

bind, err := spec.Bind(unstructured)
if err != nil {
return nil, err
}

decode, err := l.scheme.Decode(bind)
if err != nil {
return nil, err
}

n, err := l.scheme.Compile(decode)
symbols := make([]*symbol.Symbol, 0, len(specs))
for _, sp := range specs {
n, err := l.scheme.Compile(sp)
if err != nil {
for _, sb := range symbols {
sb.Close()
}
return nil, err
}

symbols = append(symbols, &symbol.Symbol{Spec: decode, Node: n})
symbols = append(symbols, &symbol.Symbol{Spec: sp, Node: n})
}

var loadHooks []symbol.LoadHook
Expand Down
2 changes: 1 addition & 1 deletion pkg/chart/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (l *Loader) Load(ctx context.Context, charts ...*Chart) error {

var errs []error
for _, chrt := range charts {
if chrt, err := Bind(chrt, secrets...); err != nil {
if err := chrt.Bind(secrets...); err != nil {
errs = append(errs, err)
} else if err := l.table.Insert(chrt); err != nil {
errs = append(errs, err)
Expand Down

0 comments on commit 4e8c890

Please sign in to comment.