Skip to content

Commit

Permalink
core: pass --extract-yaml flag from platform to component (#376)
Browse files Browse the repository at this point in the history
Previously holos render platform was not setting the --extract-yaml file
when calling holos render component, causing data file instances defined
in the Platform spec to be discarded.

This patch passes the value along using the flag.
  • Loading branch information
jeffmccune committed Dec 19, 2024
1 parent f693f04 commit 54efe3e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 42 deletions.
29 changes: 19 additions & 10 deletions api/core/v1alpha5/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ type Component struct {
// Injected as the tag variable "holos_component_path".
Path string `json:"path" yaml:"path"`
// Instances represents additional cue instance paths to unify with Path.
// Useful to unify data files into a component BuildPlan.
// Useful to unify data files into a component BuildPlan. Added in holos
// 0.101.7.
Instances []Instance `json:"instances,omitempty" yaml:"instances,omitempty"`
// WriteTo represents the holos render component --write-to flag. If empty,
// the default value for the --write-to flag is used.
Expand All @@ -323,18 +324,26 @@ type Component struct {
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
}

// Instance represents a data instance to unify with the configuration. Useful
// to unify json and yaml files with cue configuration files.
// Instance represents a data instance to unify with the configuration.
//
// Useful to unify json and yaml files with cue configuration files for
// integration with other tools. For example, executing holos render platform
// from a pull request workflow after [Kargo] executes the [yaml update] and
// [git wait for pr] promotion steps.
//
// [Kargo]: https://docs.kargo.io/
// [yaml update]: https://docs.kargo.io/references/promotion-steps#yaml-update
// [git wait for pr]: https://docs.kargo.io/references/promotion-steps#git-wait-for-pr
type Instance struct {
// Kind is a discriminator.
Kind string `json:"kind" yaml:"kind" cue:"\"extractYAML\""`
// Ignored unless kind is extractYAML.
ExtractYAML ExtractYAML `json:"extractYAML" yaml:"extractYAML"`
Kind string `json:"kind" yaml:"kind" cue:"\"ExtractYAML\""`
// Ignored unless kind is ExtractYAML.
ExtractYAML ExtractYAML `json:"extractYAML,omitempty" yaml:"extractYAML,omitempty"`
}

// ExtractYAML represents a cue data instance encoded as yaml. If Path refers to
// a directory all files in the directory are extracted non-recursively.
// Otherwise, path must refer to a file.
// ExtractYAML represents a cue data instance encoded as yaml or json. If Path
// refers to a directory all files in the directory are extracted
// non-recursively. Otherwise, path must refer to a file.
type ExtractYAML struct {
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Path string `json:"path" yaml:"path"`
}
19 changes: 10 additions & 9 deletions doc/md/api/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ type Component struct {
// Injected as the tag variable "holos_component_path".
Path string `json:"path" yaml:"path"`
// Instances represents additional cue instance paths to unify with Path.
// Useful to unify data files into a component BuildPlan.
// Useful to unify data files into a component BuildPlan. Added in holos
// 0.101.7.
Instances []Instance `json:"instances,omitempty" yaml:"instances,omitempty"`
// WriteTo represents the holos render component --write-to flag. If empty,
// the default value for the --write-to flag is used.
Expand All @@ -195,13 +196,11 @@ type Component struct {
<a name="ExtractYAML"></a>
## type ExtractYAML {#ExtractYAML}

ExtractYAML represents a cue data instance encoded as yaml. Holos extracts data of this kind using cue [encoding/yaml](<https://pkg.go.dev/encoding/yaml/>).

If Path refers to a directory, all files in the directory are extracted non\-recursively. Otherwise, path must refer to a file.
ExtractYAML represents a cue data instance encoded as yaml or json. If Path refers to a directory all files in the directory are extracted non\-recursively. Otherwise, path must refer to a file.

```go
type ExtractYAML struct {
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Path string `json:"path" yaml:"path"`
}
```

Expand Down Expand Up @@ -300,14 +299,16 @@ type Helm struct {
<a name="Instance"></a>
## type Instance {#Instance}

Instance represents a data instance to unify with the configuration. Useful to unify json and yaml files with cue configuration files.
Instance represents a data instance to unify with the configuration.

Useful to unify json and yaml files with cue configuration files for integration with other tools. For example, executing holos render platform from a pull request workflow after [Kargo](<https://docs.kargo.io/>) executes the [yaml update](<https://docs.kargo.io/references/promotion-steps#yaml-update>) and [git wait for pr](<https://docs.kargo.io/references/promotion-steps#git-wait-for-pr>) promotion steps.

```go
type Instance struct {
// Kind is a discriminator.
Kind string `json:"kind" yaml:"kind" cue:"\"extractYAML\""`
// Ignored unless kind is extractYAML.
ExtractYAML ExtractYAML `json:"extractYAML" yaml:"extractYAML"`
Kind string `json:"kind" yaml:"kind" cue:"\"ExtractYAML\""`
// Ignored unless kind is ExtractYAML.
ExtractYAML ExtractYAML `json:"extractYAML,omitempty" yaml:"extractYAML,omitempty"`
}
```

Expand Down
8 changes: 3 additions & 5 deletions internal/builder/v1alpha5/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,15 @@ func (c *Component) Path() string {
return util.DotSlash(c.Component.Path)
}

func (c *Component) Instances() ([]string, error) {
// ExtractYAML returns the path values for the --extract-yaml command line flag.
func (c *Component) ExtractYAML() ([]string, error) {
if c == nil {
return nil, nil
}
instances := make([]string, 0, len(c.Component.Instances))
for _, instance := range c.Component.Instances {
switch instance.Kind {
case "extractYAML":
if instance.Kind == "ExtractYAML" {
instances = append(instances, instance.ExtractYAML.Path)
default:
return nil, errors.Format("unsupported instance kind: %s", instance.Kind)
}
}
return instances, nil
Expand Down
9 changes: 8 additions & 1 deletion internal/cli/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ func makeComponentRenderFunc(w io.Writer, prefixArgs, cliTags []string) func(con
if err != nil {
return errors.Wrap(err)
}
args := make([]string, 0, 10+len(prefixArgs)+(len(tags)*2))
filepaths, err := component.ExtractYAML()
if err != nil {
return errors.Wrap(err)
}
args := make([]string, 0, 10+len(prefixArgs)+(len(tags)*2+len(filepaths)*2))
args = append(args, prefixArgs...)
args = append(args, "render", "component")
for _, tag := range cliTags {
Expand All @@ -159,6 +163,9 @@ func makeComponentRenderFunc(w io.Writer, prefixArgs, cliTags []string) func(con
for _, tag := range tags {
args = append(args, "--inject", tag)
}
for _, path := range filepaths {
args = append(args, "--extract-yaml", path)
}
args = append(args, component.Path())
if _, err := util.RunCmdA(ctx, w, "holos", args...); err != nil {
return errors.Format("could not render component: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ func makeBuildFunc(encoder holos.OrderedEncoder, opts holos.BuildOpts) func(cont
return errors.Wrap(err)
}
tags = append(tags, opts.Tags...)
instances, err := component.Instances()
filepaths, err := component.ExtractYAML()
if err != nil {
return errors.Wrap(err)
}
inst, err := builder.LoadInstance(component.Path(), instances, tags)
inst, err := builder.LoadInstance(component.Path(), filepaths, tags)
if err != nil {
return errors.Wrap(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ package core
path: string @go(Path)

// Instances represents additional cue instance paths to unify with Path.
// Useful to unify data files into a component BuildPlan.
// Useful to unify data files into a component BuildPlan. Added in holos
// 0.101.7.
instances?: [...#Instance] @go(Instances,[]Instance)

// WriteTo represents the holos render component --write-to flag. If empty,
Expand All @@ -358,23 +359,27 @@ package core
annotations?: {[string]: string} @go(Annotations,map[string]string)
}

// Instance represents a data instance to unify with the configuration. Useful
// to unify json and yaml files with cue configuration files.
// Instance represents a data instance to unify with the configuration.
//
// Useful to unify json and yaml files with cue configuration files for
// integration with other tools. For example, executing holos render platform
// from a pull request workflow after [Kargo] executes the [yaml update] and
// [git wait for pr] promotion steps.
//
// [Kargo]: https://docs.kargo.io/
// [yaml update]: https://docs.kargo.io/references/promotion-steps#yaml-update
// [git wait for pr]: https://docs.kargo.io/references/promotion-steps#git-wait-for-pr
#Instance: {
// Kind is a discriminator.
kind: string & "extractYAML" @go(Kind)
kind: string & "ExtractYAML" @go(Kind)

// Ignored unless kind is extractYAML.
extractYAML: #ExtractYAML @go(ExtractYAML)
// Ignored unless kind is ExtractYAML.
extractYAML?: #ExtractYAML @go(ExtractYAML)
}

// ExtractYAML represents a cue data instance encoded as yaml. Holos extracts data of
// this kind using cue [encoding/yaml].
//
// If Path refers to a directory, all files in the directory are extracted
// ExtractYAML represents a cue data instance encoded as yaml or json. If Path
// refers to a directory all files in the directory are extracted
// non-recursively. Otherwise, path must refer to a file.
//
// [yaml.Extract]: https://pkg.go.dev/cuelang.org/go@v0.11.0/encoding/yaml#Extract
#ExtractYAML: {
path?: string @go(Path)
path: string @go(Path)
}
3 changes: 2 additions & 1 deletion internal/holos/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type Platform interface {
type Component interface {
Describe() string
Path() string
Instances() ([]string, error)
// ExtractYAML represents the values of the --extract-yaml flag
ExtractYAML() ([]string, error)
Tags() ([]string, error)
WriteTo() string
Labels() Labels
Expand Down
2 changes: 1 addition & 1 deletion version/embedded/patch
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6
7

0 comments on commit 54efe3e

Please sign in to comment.