Skip to content

Commit

Permalink
feat(render): decouple version and kind
Browse files Browse the repository at this point in the history
  • Loading branch information
sh0rez committed May 4, 2020
1 parent 3012984 commit 349da20
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
type strSliceFlag []string

func (a *strSliceFlag) Set(value string) error {
if elems := strings.Split(value, " "); len(elems) > 0 {
*a = append(*a, elems...)
return nil
}

*a = append(*a, value)
return nil
}
Expand Down
35 changes: 28 additions & 7 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ func Main(adds []string) j.Type {
return j.Add("", elems...)
}

// Objects is a collection of Jsonnet objects, indexed by their expected path on
// the filesystem
type Objects map[string]j.ObjectType

func (o Objects) Add(set Objects, prefix string) {
for k, v := range set {
o[filepath.Join("/"+prefix, k)] = v
}
}

// Group renders the entire given group, returning e.g.:
// - /main.libsonnet, the group index
// - /v1/main.libsonnet, the version v1 index
// - /v1/deployment.libsonnet, Deployment
// - /v1/daemonset.libsonnet, DaemonSet
func Group(name string, g model.Group) j.ObjectType {
fields := []j.Type{}
for name, ver := range g {
Expand All @@ -59,12 +74,16 @@ func Group(name string, g model.Group) j.ObjectType {
return j.Object(name, fields...)
}

// Version renders the entire given object, returning e.g.:
// - /main.libsonnet, the version index
// - /deployment.libsonnet, Deployment
// - /daemonset.libsonnet, DaemonSet
func Version(name string, v model.Version) j.ObjectType {
fields := []j.Type{
j.Local(j.ConciseObject(LocalApiVersion, j.String("apiVersion", v.ApiVersion))),
// j.Local(j.ConciseObject(LocalApiVersion, j.String("apiVersion", v.ApiVersion))),
}
for name, kind := range v.Kinds {
k := Kind(name, kind)
k := Kind(name, kind, v.ApiVersion)
docs := j.Comment(k, kind.Help)
fields = append(fields, docs)
}
Expand All @@ -74,7 +93,7 @@ func Version(name string, v model.Version) j.ObjectType {
return j.Object(name, fields...)
}

func Kind(name string, k model.Kind) j.Type {
func Kind(name string, k model.Kind, apiVersion string) j.Type {
fields := []j.Type{}

for k, m := range k.Modifiers {
Expand All @@ -89,7 +108,7 @@ func Kind(name string, k model.Kind) j.Type {
// prepend new() function, so it is at top
if k.New != nil {
fn := j.Comment(
constructor(*k.New, strings.Title(name)),
constructor(*k.New, strings.Title(name), apiVersion),
k.New.Help,
)

Expand All @@ -101,10 +120,12 @@ func Kind(name string, k model.Kind) j.Type {
return j.Object(name, fields...)
}

func constructor(c model.Constructor, kind string) j.FuncType {
func constructor(c model.Constructor, kind, apiVersion string) j.FuncType {
ret := j.Add("",
j.Ref("", LocalApiVersion),
j.ConciseObject("", j.String("kind", kind)),
j.Object("",
j.String("apiVersion", apiVersion),
j.String("kind", kind),
),
j.Call("", "self.metadata.withName", j.Args(j.Ref("name", "name"))),
)

Expand Down

0 comments on commit 349da20

Please sign in to comment.