Skip to content

Commit

Permalink
Merge branch 'main' into ci-cd-example-workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleyscholl authored Jan 7, 2025
2 parents 1a41809 + 2ece85c commit e825754
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 45 deletions.
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/evanphx/json-patch v5.8.0+incompatible
github.com/expr-lang/expr v1.16.9
github.com/gavv/httpexpect/v2 v2.16.0
github.com/go-git/go-git/v5 v5.11.0
github.com/go-git/go-git/v5 v5.13.1
github.com/go-jose/go-jose/v3 v3.0.3
github.com/go-openapi/jsonreference v0.20.4
github.com/go-sql-driver/mysql v1.7.1
Expand Down Expand Up @@ -49,7 +49,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
github.com/tidwall/gjson v1.17.0
github.com/upper/db/v4 v4.7.0
github.com/valyala/fasttemplate v1.2.2
Expand All @@ -62,7 +62,7 @@ require (
go.opentelemetry.io/otel/sdk v1.32.0
go.opentelemetry.io/otel/sdk/metric v1.32.0
golang.org/x/crypto v0.31.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
golang.org/x/oauth2 v0.21.0
golang.org/x/sync v0.10.0
golang.org/x/time v0.5.0
Expand Down Expand Up @@ -93,7 +93,7 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/cyphar/filepath-securejoin v0.3.6 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/evilmonkeyinc/jsonpath v0.8.1 // indirect
github.com/fatih/color v1.15.0 // indirect
Expand Down Expand Up @@ -132,7 +132,7 @@ require (
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/segmentio/fasthash v1.0.3 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/skeema/knownhosts v1.3.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/vbatts/tar-split v0.11.3 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand All @@ -141,8 +141,8 @@ require (
go.opentelemetry.io/otel/trace v1.32.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
Expand Down Expand Up @@ -174,7 +174,7 @@ require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/ProtonMail/go-crypto v1.1.3 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/awalterschulze/gographviz v0.0.0-20200901124122-0eecad45bd71 // indirect
Expand Down Expand Up @@ -215,7 +215,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-billy/v5 v5.6.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.6 // indirect
Expand Down Expand Up @@ -278,7 +278,7 @@ require (
github.com/rs/xid v1.6.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down
57 changes: 26 additions & 31 deletions go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions pkg/apis/workflow/v1alpha1/cluster_workflow_template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (cwftmpl *ClusterWorkflowTemplate) GetResourceScope() ResourceScope {
return ResourceScopeCluster
}

// GetPodMetadata returns the PodMetadata of cluster workflow template.
func (cwftmpl *ClusterWorkflowTemplate) GetPodMetadata() *Metadata {
return cwftmpl.Spec.PodMetadata
}

// GetWorkflowSpec returns the WorkflowSpec of cluster workflow template.
func (cwftmpl *ClusterWorkflowTemplate) GetWorkflowSpec() *WorkflowSpec {
return &cwftmpl.Spec
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/workflow/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type TemplateHolder interface {
GroupVersionKind() schema.GroupVersionKind
GetTemplateByName(name string) *Template
GetResourceScope() ResourceScope
GetPodMetadata() *Metadata
}

// WorkflowSpecHolder is an object that holds a WorkflowSpec; e.g., WorkflowTemplate, and ClusterWorkflowTemplate
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/workflow/v1alpha1/workflow_template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func (wftmpl *WorkflowTemplate) GetResourceScope() ResourceScope {
return ResourceScopeNamespaced
}

// GetPodMetadata returns the PodMetadata of workflow template.
func (wftmpl *WorkflowTemplate) GetPodMetadata() *Metadata {
return wftmpl.Spec.PodMetadata
}

// GetWorkflowSpec returns the WorkflowSpec of workflow template.
func (wftmpl *WorkflowTemplate) GetWorkflowSpec() *WorkflowSpec {
return &wftmpl.Spec
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/workflow/v1alpha1/workflow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3410,6 +3410,11 @@ func (wf *Workflow) GetResourceScope() ResourceScope {
return ResourceScopeLocal
}

// GetPodMetadata returns the PodMetadata of a workflow.
func (wf *Workflow) GetPodMetadata() *Metadata {
return wf.Spec.PodMetadata
}

// GetWorkflowSpec returns the Spec of a workflow.
func (wf *Workflow) GetWorkflowSpec() WorkflowSpec {
return wf.Spec
Expand Down
68 changes: 68 additions & 0 deletions workflow/controller/operator_workflow_template_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,71 @@ func TestWorkflowTemplateWithDynamicRef(t *testing.T) {
woc.operate(ctx)
assert.Equal(t, wfv1.WorkflowSucceeded, woc.wf.Status.Phase)
}

const wfTemplateWithPodMetadata = `
apiVersion: argoproj.io/v1alpha1
kind: ClusterWorkflowTemplate
metadata:
name: workflow-template
spec:
entrypoint: whalesay-template
podMetadata:
labels:
workflow-template-label: hello
annotations:
all-pods-should-have-this: value
arguments:
parameters:
- name: message
value: hello world
templates:
- name: whalesay-template
inputs:
parameters:
- name: message
container:
image: docker/whalesay
command: [cowsay]
args: ["{{inputs.parameters.message}}"]`

const wfWithTemplateRef = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: test-workflow
namespace: argo-workflows-system
spec:
podMetadata:
labels:
caller-label: hello
entrypoint: start
templates:
- name: start
steps:
- - name: hello
templateRef:
name: workflow-template
template: whalesay-template
clusterScope: true
arguments:
parameters:
- name: message
value: Hello Bug`

func TestWorkflowTemplateWithPodMetadata(t *testing.T) {
cancel, controller := newController(wfv1.MustUnmarshalWorkflow(wfWithTemplateRef), wfv1.MustUnmarshalClusterWorkflowTemplate(wfTemplateWithPodMetadata))
defer cancel()

ctx := context.Background()
woc := newWorkflowOperationCtx(wfv1.MustUnmarshalWorkflow(wfWithTemplateRef), controller)
woc.operate(ctx)
assert.Equal(t, wfv1.WorkflowRunning, woc.wf.Status.Phase)
pods, err := listPods(woc)
require.NoError(t, err)
assert.NotEmpty(t, len(pods.Items) > 0, "pod was not created successfully")
pod := pods.Items[0]
assert.Contains(t, pod.Labels, "caller-label")
assert.Contains(t, pod.Labels, "workflow-template-label")
assert.Contains(t, pod.Annotations, "all-pods-should-have-this")
}
25 changes: 25 additions & 0 deletions workflow/templateresolution/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func (ctx *Context) GetTemplateByName(name string) (*wfv1.Template, error) {
ctx.log.Debugf("Getting the template by name: %s", name)

tmpl := ctx.tmplBase.GetTemplateByName(name)

podMetadata := ctx.tmplBase.GetPodMetadata()
ctx.addPodMetadata(podMetadata, tmpl)

if tmpl == nil {
return nil, errors.Errorf(errors.CodeNotFound, "template %s not found", name)
}
Expand Down Expand Up @@ -138,6 +142,9 @@ func (ctx *Context) GetTemplateFromRef(tmplRef *wfv1.TemplateRef) (*wfv1.Templat

template = wftmpl.GetTemplateByName(tmplRef.Template)

podMetadata := wftmpl.GetPodMetadata()
ctx.addPodMetadata(podMetadata, template)

if template == nil {
return nil, errors.Errorf(errors.CodeNotFound, "template %s not found in workflow template %s", tmplRef.Template, tmplRef.Name)
}
Expand Down Expand Up @@ -268,3 +275,21 @@ func (ctx *Context) WithClusterWorkflowTemplate(name string) (*Context, error) {
}
return ctx.WithTemplateBase(cwftmpl), nil
}

// addPodMetadata add podMetadata in workflow template level to template
func (ctx *Context) addPodMetadata(podMetadata *wfv1.Metadata, tmpl *wfv1.Template) {
if podMetadata != nil {
if tmpl.Metadata.Annotations == nil {
tmpl.Metadata.Annotations = make(map[string]string)
}
for k, v := range podMetadata.Annotations {
tmpl.Metadata.Annotations[k] = v
}
if tmpl.Metadata.Labels == nil {
tmpl.Metadata.Labels = make(map[string]string)
}
for k, v := range podMetadata.Labels {
tmpl.Metadata.Labels[k] = v
}
}
}
20 changes: 16 additions & 4 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,14 @@ func (ctx *templateValidationCtx) validateTemplate(tmpl *wfv1.Template, tmplCtx

}

templateScope := tmplCtx.GetTemplateScope()
tmplID := getTemplateID(tmpl)
_, ok := ctx.results[tmplID]
_, ok := ctx.results[templateScope+tmplID]
if ok {
// we can skip the rest since it has been validated.
return nil
}
ctx.results[tmplID] = true
ctx.results[templateScope+tmplID] = true

for globalVar, val := range ctx.globalParams {
scope[globalVar] = val
Expand Down Expand Up @@ -962,7 +963,12 @@ func (ctx *templateValidationCtx) validateSteps(scope map[string]interface{}, tm
if err != nil {
return err
}
resolvedTmpl, err := ctx.validateTemplateHolder(&step, tmplCtx, &FakeArguments{}, workflowTemplateValidation)
var args wfv1.ArgumentsProvider
args = &FakeArguments{}
if step.TemplateRef != nil {
args = &step.Arguments
}
resolvedTmpl, err := ctx.validateTemplateHolder(&step, tmplCtx, args, workflowTemplateValidation)
if err != nil {
return errors.Errorf(errors.CodeBadRequest, "templates.%s.steps[%d].%s %s", tmpl.Name, i, step.Name, err.Error())
}
Expand Down Expand Up @@ -1355,7 +1361,13 @@ func (ctx *templateValidationCtx) validateDAG(scope map[string]interface{}, tmpl
return errors.Errorf(errors.CodeBadRequest, "templates.%s cannot use 'continueOn' when using 'depends'. Instead use 'dep-task.Failed'/'dep-task.Errored'", tmpl.Name)
}

resolvedTmpl, err := ctx.validateTemplateHolder(&task, tmplCtx, &FakeArguments{}, workflowTemplateValidation)
var args wfv1.ArgumentsProvider
args = &FakeArguments{}
if task.TemplateRef != nil {
args = &task.Arguments
}

resolvedTmpl, err := ctx.validateTemplateHolder(&task, tmplCtx, args, workflowTemplateValidation)

if err != nil {
return errors.Errorf(errors.CodeBadRequest, "templates.%s.tasks.%s %s", tmpl.Name, task.Name, err.Error())
Expand Down
Loading

0 comments on commit e825754

Please sign in to comment.