Skip to content

Commit

Permalink
GetOutput takes no parameters
Browse files Browse the repository at this point in the history
Templates have different requirements for fulfilling
GetOutput. Ensure that these are set before calling.
  • Loading branch information
waciumawanjohi committed Nov 10, 2021
1 parent 259535d commit a80c83f
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 50 deletions.
5 changes: 4 additions & 1 deletion pkg/realizer/deliverable/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ func (r *resourceRealizer) Do(ctx context.Context, resource *v1alpha1.ClusterDel
}
}

output, err := template.GetOutput(stampedObject, templatingContext)
template.SetTemplatingContext(templatingContext)
template.SetStampedObject(stampedObject)

output, err := template.GetOutput()
if err != nil {
return stampedObject, nil, RetrieveOutputError{
Err: err,
Expand Down
1 change: 1 addition & 0 deletions pkg/realizer/runnable/realizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"

. "github.com/MakeNowJust/heredoc/dot"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo"
Expand Down
4 changes: 3 additions & 1 deletion pkg/realizer/workload/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ func (r *resourceRealizer) Do(ctx context.Context, resource *v1alpha1.SupplyChai
}
}

output, err := template.GetOutput(stampedObject, workloadTemplatingContext)
template.SetStampedObject(stampedObject)

output, err := template.GetOutput()
if err != nil {
return stampedObject, nil, RetrieveOutputError{
Err: err,
Expand Down
23 changes: 15 additions & 8 deletions pkg/templates/cluster_config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,31 @@ import (
)

type clusterConfigTemplate struct {
template *v1alpha1.ClusterConfigTemplate
evaluator evaluator
template *v1alpha1.ClusterConfigTemplate
evaluator evaluator
stampedObject *unstructured.Unstructured
}

func (t clusterConfigTemplate) GetKind() string {
func (t *clusterConfigTemplate) GetKind() string {
return t.template.Kind
}

func NewClusterConfigTemplateModel(template *v1alpha1.ClusterConfigTemplate, eval evaluator) *clusterConfigTemplate {
return &clusterConfigTemplate{template: template, evaluator: eval}
}

func (t clusterConfigTemplate) GetName() string {
func (t *clusterConfigTemplate) GetName() string {
return t.template.Name
}

func (t clusterConfigTemplate) GetOutput(stampedObject *unstructured.Unstructured, templatingContext map[string]interface{}) (*Output, error) {
config, err := t.evaluator.EvaluateJsonPath(t.template.Spec.ConfigPath, stampedObject.UnstructuredContent())
func (t *clusterConfigTemplate) SetTemplatingContext(_ map[string]interface{}) {}

func (t *clusterConfigTemplate) SetStampedObject(stampedObject *unstructured.Unstructured) {
t.stampedObject = stampedObject
}

func (t *clusterConfigTemplate) GetOutput() (*Output, error) {
config, err := t.evaluator.EvaluateJsonPath(t.template.Spec.ConfigPath, t.stampedObject.UnstructuredContent())
if err != nil {
return nil, JsonPathError{
Err: fmt.Errorf("evaluate config url json path: %w", err),
Expand All @@ -53,10 +60,10 @@ func (t clusterConfigTemplate) GetOutput(stampedObject *unstructured.Unstructure
}, nil
}

func (t clusterConfigTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
func (t *clusterConfigTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
return t.template.Spec.TemplateSpec
}

func (t clusterConfigTemplate) GetDefaultParams() v1alpha1.DefaultParams {
func (t *clusterConfigTemplate) GetDefaultParams() v1alpha1.DefaultParams {
return t.template.Spec.Params
}
3 changes: 2 additions & 1 deletion pkg/templates/cluster_config_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ var _ = Describe("ClusterConfigTemplate", func() {

JustBeforeEach(func() {
clusterConfigTemplateModel := templates.NewClusterConfigTemplateModel(configTemplate, evaluator)
output, err = clusterConfigTemplateModel.GetOutput(stampedObject, nil)
clusterConfigTemplateModel.SetStampedObject(stampedObject)
output, err = clusterConfigTemplateModel.GetOutput()
})

When("passed a stamped object for which the evaluator can return a value at the configPath", func() {
Expand Down
36 changes: 23 additions & 13 deletions pkg/templates/cluster_deployment_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,42 @@ import (
)

type clusterDeploymentTemplate struct {
template *v1alpha1.ClusterDeploymentTemplate
evaluator evaluator
template *v1alpha1.ClusterDeploymentTemplate
evaluator evaluator
templatingContext map[string]interface{}
stampedObject *unstructured.Unstructured
}

func (t clusterDeploymentTemplate) GetKind() string {
func (t *clusterDeploymentTemplate) GetKind() string {
return t.template.Kind
}

func NewClusterDeploymentTemplateModel(template *v1alpha1.ClusterDeploymentTemplate, eval evaluator) *clusterDeploymentTemplate {
return &clusterDeploymentTemplate{template: template, evaluator: eval}
}

func (t clusterDeploymentTemplate) GetName() string {
func (t *clusterDeploymentTemplate) GetName() string {
return t.template.Name
}

func (t clusterDeploymentTemplate) GetOutput(stampedObject *unstructured.Unstructured, templatingContext map[string]interface{}) (*Output, error) {
if err := t.outputReady(stampedObject); err != nil {
func (t *clusterDeploymentTemplate) SetTemplatingContext(templatingContext map[string]interface{}) {
t.templatingContext = templatingContext
}

func (t *clusterDeploymentTemplate) SetStampedObject(stampedObject *unstructured.Unstructured) {
t.stampedObject = stampedObject
}

func (t *clusterDeploymentTemplate) GetOutput() (*Output, error) {
if err := t.outputReady(t.stampedObject); err != nil {
return nil, err
}

output := &Output{Source: &Source{}}

originalSource, ok := templatingContext["source"].(*SourceInput)
originalSource, ok := t.templatingContext["source"].(*SourceInput)
if !ok {
return nil, fmt.Errorf("original source not found in context: %v", templatingContext)
return nil, fmt.Errorf("original source not found in context: %v", t.templatingContext)
}

output.Source.URL = originalSource.URL
Expand All @@ -57,23 +67,23 @@ func (t clusterDeploymentTemplate) GetOutput(stampedObject *unstructured.Unstruc
return output, nil
}

func (t clusterDeploymentTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
func (t *clusterDeploymentTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
return t.template.Spec.TemplateSpec
}

func (t clusterDeploymentTemplate) GetDefaultParams() v1alpha1.DefaultParams {
func (t *clusterDeploymentTemplate) GetDefaultParams() v1alpha1.DefaultParams {
return t.template.Spec.Params
}

func (t clusterDeploymentTemplate) outputReady(stampedObject *unstructured.Unstructured) error {
func (t *clusterDeploymentTemplate) outputReady(stampedObject *unstructured.Unstructured) error {
if t.template.Spec.ObservedCompletion != nil {
return t.observedCompletionReady(stampedObject)
} else {
return t.observedMatchesReady(stampedObject)
}
}

func (t clusterDeploymentTemplate) observedMatchesReady(stampedObject *unstructured.Unstructured) error {
func (t *clusterDeploymentTemplate) observedMatchesReady(stampedObject *unstructured.Unstructured) error {
for _, match := range t.template.Spec.ObservedMatches {
input, err := t.evaluator.EvaluateJsonPath(match.Input, stampedObject.UnstructuredContent())
if err != nil {
Expand All @@ -99,7 +109,7 @@ func (t clusterDeploymentTemplate) observedMatchesReady(stampedObject *unstructu
return nil
}

func (t clusterDeploymentTemplate) observedCompletionReady(stampedObject *unstructured.Unstructured) error {
func (t *clusterDeploymentTemplate) observedCompletionReady(stampedObject *unstructured.Unstructured) error {
generation, err := t.evaluator.EvaluateJsonPath("metadata.generation", stampedObject.UnstructuredContent())
if err != nil {
return JsonPathError{
Expand Down
4 changes: 3 additions & 1 deletion pkg/templates/cluster_deployment_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ var _ = Describe("ClusterDeploymentTemplate", func() {

JustBeforeEach(func() {
clusterDeploymentTemplateModel := templates.NewClusterDeploymentTemplateModel(deploymentTemplate, evaluator)
output, err = clusterDeploymentTemplateModel.GetOutput(stampedObject, templatingContext)
clusterDeploymentTemplateModel.SetStampedObject(stampedObject)
clusterDeploymentTemplateModel.SetTemplatingContext(templatingContext)
output, err = clusterDeploymentTemplateModel.GetOutput()
})

Context("observedCompletion", func() {
Expand Down
23 changes: 15 additions & 8 deletions pkg/templates/cluster_image_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,31 @@ import (
)

type clusterImageTemplate struct {
template *v1alpha1.ClusterImageTemplate
evaluator evaluator
template *v1alpha1.ClusterImageTemplate
evaluator evaluator
stampedObject *unstructured.Unstructured
}

func (t clusterImageTemplate) GetKind() string {
func (t *clusterImageTemplate) GetKind() string {
return t.template.Kind
}

func NewClusterImageTemplateModel(template *v1alpha1.ClusterImageTemplate, eval evaluator) *clusterImageTemplate {
return &clusterImageTemplate{template: template, evaluator: eval}
}

func (t clusterImageTemplate) GetName() string {
func (t *clusterImageTemplate) GetName() string {
return t.template.Name
}

func (t clusterImageTemplate) GetOutput(stampedObject *unstructured.Unstructured, templatingContext map[string]interface{}) (*Output, error) {
image, err := t.evaluator.EvaluateJsonPath(t.template.Spec.ImagePath, stampedObject.UnstructuredContent())
func (t *clusterImageTemplate) SetTemplatingContext(_ map[string]interface{}) {}

func (t *clusterImageTemplate) SetStampedObject(stampedObject *unstructured.Unstructured) {
t.stampedObject = stampedObject
}

func (t *clusterImageTemplate) GetOutput() (*Output, error) {
image, err := t.evaluator.EvaluateJsonPath(t.template.Spec.ImagePath, t.stampedObject.UnstructuredContent())
if err != nil {
return nil, JsonPathError{
Err: fmt.Errorf("evaluate image json path: %w", err),
Expand All @@ -55,10 +62,10 @@ func (t clusterImageTemplate) GetOutput(stampedObject *unstructured.Unstructured
}, nil
}

func (t clusterImageTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
func (t *clusterImageTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
return t.template.Spec.TemplateSpec
}

func (t clusterImageTemplate) GetDefaultParams() v1alpha1.DefaultParams {
func (t *clusterImageTemplate) GetDefaultParams() v1alpha1.DefaultParams {
return t.template.Spec.Params
}
3 changes: 2 additions & 1 deletion pkg/templates/cluster_image_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ var _ = Describe("ClusterImageTemplate", func() {

JustBeforeEach(func() {
clusterImageTemplateModel := templates.NewClusterImageTemplateModel(imageTemplate, evaluator)
output, err = clusterImageTemplateModel.GetOutput(stampedObject, nil)
clusterImageTemplateModel.SetStampedObject(stampedObject)
output, err = clusterImageTemplateModel.GetOutput()
})

When("passed a stamped object for which the evaluator can return a value at the imagePath", func() {
Expand Down
25 changes: 16 additions & 9 deletions pkg/templates/cluster_source_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,39 @@ import (
)

type clusterSourceTemplate struct {
template *v1alpha1.ClusterSourceTemplate
evaluator evaluator
template *v1alpha1.ClusterSourceTemplate
evaluator evaluator
stampedObject *unstructured.Unstructured
}

func (t clusterSourceTemplate) GetKind() string {
func (t *clusterSourceTemplate) GetKind() string {
return t.template.Kind
}

func NewClusterSourceTemplateModel(template *v1alpha1.ClusterSourceTemplate, eval evaluator) *clusterSourceTemplate {
return &clusterSourceTemplate{template: template, evaluator: eval}
}

func (t clusterSourceTemplate) GetName() string {
func (t *clusterSourceTemplate) GetName() string {
return t.template.Name
}

func (t clusterSourceTemplate) GetOutput(stampedObject *unstructured.Unstructured, templatingContext map[string]interface{}) (*Output, error) {
url, err := t.evaluator.EvaluateJsonPath(t.template.Spec.URLPath, stampedObject.UnstructuredContent())
func (t *clusterSourceTemplate) SetTemplatingContext(_ map[string]interface{}) {}

func (t *clusterSourceTemplate) SetStampedObject(stampedObject *unstructured.Unstructured) {
t.stampedObject = stampedObject
}

func (t *clusterSourceTemplate) GetOutput() (*Output, error) {
url, err := t.evaluator.EvaluateJsonPath(t.template.Spec.URLPath, t.stampedObject.UnstructuredContent())
if err != nil {
return nil, JsonPathError{
Err: fmt.Errorf("evaluate source url json path: %w", err),
expression: t.template.Spec.URLPath,
}
}

revision, err := t.evaluator.EvaluateJsonPath(t.template.Spec.RevisionPath, stampedObject.UnstructuredContent())
revision, err := t.evaluator.EvaluateJsonPath(t.template.Spec.RevisionPath, t.stampedObject.UnstructuredContent())
if err != nil {
return nil, JsonPathError{
Err: fmt.Errorf("evaluate source revision json path: %w", err),
Expand All @@ -63,10 +70,10 @@ func (t clusterSourceTemplate) GetOutput(stampedObject *unstructured.Unstructure
}, nil
}

func (t clusterSourceTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
func (t *clusterSourceTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
return t.template.Spec.TemplateSpec
}

func (t clusterSourceTemplate) GetDefaultParams() v1alpha1.DefaultParams {
func (t *clusterSourceTemplate) GetDefaultParams() v1alpha1.DefaultParams {
return t.template.Spec.Params
}
3 changes: 2 additions & 1 deletion pkg/templates/cluster_source_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ var _ = Describe("ClusterSourceTemplate", func() {

JustBeforeEach(func() {
clusterSourceTemplateModel := templates.NewClusterSourceTemplateModel(sourceTemplate, evaluator)
output, err = clusterSourceTemplateModel.GetOutput(stampedObject, nil)
clusterSourceTemplateModel.SetStampedObject(stampedObject)
output, err = clusterSourceTemplateModel.GetOutput()
})

When("passed a stamped object for which the evaluator can return a value at the urlPath and revisionPath", func() {
Expand Down
14 changes: 9 additions & 5 deletions pkg/templates/cluster_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,33 @@ type clusterTemplate struct {
template *v1alpha1.ClusterTemplate
}

func (t clusterTemplate) GetKind() string {
func (t *clusterTemplate) GetKind() string {
return t.template.Kind
}

func NewClusterTemplateModel(template *v1alpha1.ClusterTemplate) *clusterTemplate {
return &clusterTemplate{template: template}
}

func (t clusterTemplate) GetName() string {
func (t *clusterTemplate) GetName() string {
return t.template.Name
}

func (t clusterTemplate) GetOutput(_ *unstructured.Unstructured, templatingContext map[string]interface{}) (*Output, error) {
func (t *clusterTemplate) SetTemplatingContext(_ map[string]interface{}) {}

func (t *clusterTemplate) SetStampedObject(_ *unstructured.Unstructured) {}

func (t *clusterTemplate) GetOutput() (*Output, error) {
return &Output{}, nil
}

func (t clusterTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
func (t *clusterTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
return v1alpha1.TemplateSpec{
Template: t.template.Spec.Template,
Ytt: t.template.Spec.Ytt,
}
}

func (t clusterTemplate) GetDefaultParams() v1alpha1.DefaultParams {
func (t *clusterTemplate) GetDefaultParams() v1alpha1.DefaultParams {
return t.template.Spec.Params
}
4 changes: 3 additions & 1 deletion pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
type Template interface {
GetResourceTemplate() v1alpha1.TemplateSpec
GetDefaultParams() v1alpha1.DefaultParams
GetOutput(stampedObject *unstructured.Unstructured, templatingContext map[string]interface{}) (*Output, error)
GetOutput() (*Output, error)
SetTemplatingContext(templatingContext map[string]interface{})
SetStampedObject(stampedObject *unstructured.Unstructured)
GetName() string
GetKind() string
}
Expand Down

0 comments on commit a80c83f

Please sign in to comment.