Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support WorkflowMetadata in WorkflowTemplate and ClusterWorkflowTemplate #3364

Merged
merged 4 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4433,6 +4433,10 @@
"x-kubernetes-patch-merge-key": "name",
"x-kubernetes-patch-strategy": "merge"
},
"workflowMetadata": {
"description": "WorkflowMetadata contains some metadata of the workflow to be refer",
"$ref": "#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"
},
"workflowTemplateRef": {
"description": "WorkflowTemplateRef holds a reference to a WorkflowTemplate for execution",
"$ref": "#/definitions/io.argoproj.workflow.v1alpha1.WorkflowTemplateRef"
Expand Down
1 change: 1 addition & 0 deletions docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ WorkflowTemplateSpec is a spec of WorkflowTemplate.
|`ttlStrategy`|[`TTLStrategy`](#ttlstrategy)|TTLStrategy limits the lifetime of a Workflow that has finished execution depending on if it Succeeded or Failed. If this struct is set, once the Workflow finishes, it will be deleted after the time to live expires. If this field is unset, the controller config map will hold the default values.|
|`volumeClaimTemplates`|`Array<`[`PersistentVolumeClaim`](#persistentvolumeclaim)`>`|VolumeClaimTemplates is a list of claims that containers are allowed to reference. The Workflow controller will create the claims at the beginning of the workflow and delete the claims upon completion of the workflow|
|`volumes`|`Array<`[`Volume`](#volume)`>`|Volumes is a list of volumes that can be mounted by containers in a io.argoproj.workflow.v1alpha1.|
|`workflowMetadata`|[`ObjectMeta`](#objectmeta)|WorkflowMetadata contains some metadata of the workflow to be refer|
|`workflowTemplateRef`|[`WorkflowTemplateRef`](#workflowtemplateref)|WorkflowTemplateRef holds a reference to a WorkflowTemplate for execution|

## Arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6871,6 +6871,8 @@ spec:
- name
type: object
type: array
workflowMetadata:
type: object
workflowTemplateRef:
properties:
clusterScope:
Expand Down
2 changes: 2 additions & 0 deletions manifests/base/crds/full/argoproj.io_workflowtemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6870,6 +6870,8 @@ spec:
- name
type: object
type: array
workflowMetadata:
type: object
workflowTemplateRef:
properties:
clusterScope:
Expand Down
853 changes: 453 additions & 400 deletions pkg/apis/workflow/v1alpha1/generated.pb.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pkg/apis/workflow/v1alpha1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pkg/apis/workflow/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/apis/workflow/v1alpha1/workflow_template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var _ TemplateHolder = &WorkflowTemplate{}
// WorkflowTemplateSpec is a spec of WorkflowTemplate.
type WorkflowTemplateSpec struct {
WorkflowSpec `json:",inline" protobuf:"bytes,1,opt,name=workflowSpec"`
// WorkflowMetadata contains some metadata of the workflow to be refer
WorkflowMetadata *metav1.ObjectMeta `json:"workflowMetadata,omitempty" protobuf:"bytes,2,opt,name=workflowMeta"`
}

// GetTemplateByName retrieves a defined template by its name
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/workflow/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions server/workflow/workflow_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,17 @@ func (s *workflowServer) SubmitWorkflow(ctx context.Context, req *workflowpkg.Wo
}
wf = common.ConvertCronWorkflowToWorkflow(cronWf)
case workflow.WorkflowTemplateKind, workflow.WorkflowTemplateSingular, workflow.WorkflowTemplatePlural, workflow.WorkflowTemplateShortName:
wf = common.NewWorkflowFromWorkflowTemplate(req.ResourceName, false)
wfTmpl, err := wfClient.ArgoprojV1alpha1().WorkflowTemplates(req.Namespace).Get(req.ResourceName, metav1.GetOptions{})
if err != nil {
return nil, err
}
wf = common.NewWorkflowFromWorkflowTemplate(req.ResourceName, wfTmpl.Spec.WorkflowMetadata, false)
case workflow.ClusterWorkflowTemplateKind, workflow.ClusterWorkflowTemplateSingular, workflow.ClusterWorkflowTemplatePlural, workflow.ClusterWorkflowTemplateShortName:
wf = common.NewWorkflowFromWorkflowTemplate(req.ResourceName, true)
cwfTmpl, err := wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates().Get(req.ResourceName, metav1.GetOptions{})
if err != nil {
return nil, err
}
wf = common.NewWorkflowFromWorkflowTemplate(req.ResourceName, cwfTmpl.Spec.WorkflowMetadata, true)
default:
return nil, errors.Errorf(errors.CodeBadRequest, "Resource kind '%s' is not supported for submitting", req.ResourceKind)
}
Expand Down
20 changes: 20 additions & 0 deletions server/workflow/workflow_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ const workflowtmpl = `
"namespace": "workflows"
},
"spec": {
"workflowMetadata": {
"Labels": {
"labelTest": "test"
},
"annotations": {
"annotationTest": "test"
}
},
"entrypoint": "whalesay-template",
"arguments": {
"parameters": [
Expand Down Expand Up @@ -511,6 +519,14 @@ const clusterworkflowtmpl = `
"name": "cluster-workflow-template-whalesay-template"
},
"spec": {
"workflowMetadata": {
"Labels": {
"labelTest": "test"
},
"annotations": {
"annotationTest": "test"
}
},
"entrypoint": "whalesay-template",
"arguments": {
"parameters": [
Expand Down Expand Up @@ -870,6 +886,8 @@ func TestSubmitWorkflowFromResource(t *testing.T) {
if assert.NoError(t, err) {
assert.NotNil(t, wf)
assert.Contains(t, wf.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, wf.Labels, "labelTest")
assert.Contains(t, wf.Annotations, "annotationTest")
}
})
t.Run("SubmitFromCronWorkflow", func(t *testing.T) {
Expand All @@ -892,6 +910,8 @@ func TestSubmitWorkflowFromResource(t *testing.T) {
if assert.NoError(t, err) {
assert.NotNil(t, wf)
assert.Contains(t, wf.Labels, common.LabelKeyControllerInstanceID)
assert.Contains(t, wf.Labels, "labelTest")
assert.Contains(t, wf.Annotations, "annotationTest")
}
})
}
16 changes: 13 additions & 3 deletions workflow/common/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ func ConvertCronWorkflowToWorkflow(cronWf *wfv1.CronWorkflow) *wfv1.Workflow {

if len(cronWf.Spec.WorkflowMetadata.Annotations) > 0 {
wf.Annotations = make(map[string]string)
for key, label := range cronWf.Spec.WorkflowMetadata.Annotations {
wf.Annotations[key] = label
for key, annotation := range cronWf.Spec.WorkflowMetadata.Annotations {
wf.Annotations[key] = annotation
}
}
}
wf.SetOwnerReferences(append(wf.GetOwnerReferences(), *metav1.NewControllerRef(cronWf, wfv1.SchemeGroupVersion.WithKind(workflow.CronWorkflowKind))))
return wf
}

func NewWorkflowFromWorkflowTemplate(templateName string, clusterScope bool) *wfv1.Workflow {
func NewWorkflowFromWorkflowTemplate(templateName string, workflowMetadata *metav1.ObjectMeta, clusterScope bool) *wfv1.Workflow {

wf := &wfv1.Workflow{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -41,6 +41,16 @@ func NewWorkflowFromWorkflowTemplate(templateName string, clusterScope bool) *wf
},
},
}

if workflowMetadata != nil {
for key, label := range workflowMetadata.Labels {
wf.Labels[key] = label
}
for key, annotation := range workflowMetadata.Annotations {
wf.Annotations[key] = annotation
}
}

if clusterScope {
wf.Labels[LabelKeyClusterWorkflowTemplate] = templateName
} else {
Expand Down
48 changes: 41 additions & 7 deletions workflow/common/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
Expand Down Expand Up @@ -116,6 +117,11 @@ metadata:
labels:
argo-e2e: true
spec:
workflowMetadata:
labels:
label1: value1
annotations:
annotation1: value1
entrypoint: whalesay-template
arguments:
parameters:
Expand All @@ -139,12 +145,38 @@ func TestConvertWorkflowTemplateToWorkflow(t *testing.T) {
if err != nil {
panic(err)
}
wf := NewWorkflowFromWorkflowTemplate(wfTmpl.Name, false)
assert.NotNil(t, wf)
assert.Equal(t, "workflow-template-whalesay-template", wf.Labels["workflows.argoproj.io/workflow-template"])
assert.NotNil(t, wf.Spec.WorkflowTemplateRef)
assert.Equal(t, wfTmpl.Name, wf.Spec.WorkflowTemplateRef.Name)
assert.False(t, wf.Spec.WorkflowTemplateRef.ClusterScope)
t.Run("ConvertWorkflowFromWFT", func(t *testing.T) {
wf := NewWorkflowFromWorkflowTemplate(wfTmpl.Name, wfTmpl.Spec.WorkflowMetadata, false)
assert.NotNil(t, wf)
assert.Equal(t, "workflow-template-whalesay-template", wf.Labels["workflows.argoproj.io/workflow-template"])
assert.NotNil(t, wf.Spec.WorkflowTemplateRef)
assert.Equal(t, wfTmpl.Name, wf.Spec.WorkflowTemplateRef.Name)
assert.False(t, wf.Spec.WorkflowTemplateRef.ClusterScope)
assert.Contains(t, wf.Labels, "label1")
assert.Contains(t, wf.Annotations, "annotation1")
})
t.Run("ConvertWorkflowFromWFTWithNilWorkflowMetadata", func(t *testing.T) {

wf := NewWorkflowFromWorkflowTemplate(wfTmpl.Name, nil, false)
assert.NotNil(t, wf)
assert.Equal(t, "workflow-template-whalesay-template", wf.Labels["workflows.argoproj.io/workflow-template"])
assert.NotNil(t, wf.Spec.WorkflowTemplateRef)
assert.Equal(t, wfTmpl.Name, wf.Spec.WorkflowTemplateRef.Name)
assert.False(t, wf.Spec.WorkflowTemplateRef.ClusterScope)
})
t.Run("ConvertWorkflowFromWFTWithNilWorkflowMetadataLabels", func(t *testing.T) {
wfMetadata := &metav1.ObjectMeta{
Labels: nil,
Annotations: nil,
}
wf := NewWorkflowFromWorkflowTemplate(wfTmpl.Name, wfMetadata, false)
assert.NotNil(t, wf)
assert.Equal(t, "workflow-template-whalesay-template", wf.Labels["workflows.argoproj.io/workflow-template"])
assert.NotNil(t, wf.Spec.WorkflowTemplateRef)
assert.Equal(t, wfTmpl.Name, wf.Spec.WorkflowTemplateRef.Name)
assert.False(t, wf.Spec.WorkflowTemplateRef.ClusterScope)
})

}

func TestConvertClusterWorkflowTemplateToWorkflow(t *testing.T) {
Expand All @@ -153,10 +185,12 @@ func TestConvertClusterWorkflowTemplateToWorkflow(t *testing.T) {
if err != nil {
panic(err)
}
wf := NewWorkflowFromWorkflowTemplate(wfTmpl.Name, true)
wf := NewWorkflowFromWorkflowTemplate(wfTmpl.Name, wfTmpl.Spec.WorkflowMetadata, true)
assert.NotNil(t, wf)
assert.Equal(t, "workflow-template-whalesay-template", wf.Labels["workflows.argoproj.io/cluster-workflow-template"])
assert.NotNil(t, wf.Spec.WorkflowTemplateRef)
assert.Equal(t, wfTmpl.Name, wf.Spec.WorkflowTemplateRef.Name)
assert.True(t, wf.Spec.WorkflowTemplateRef.ClusterScope)
assert.Contains(t, wf.Labels, "label1")
assert.Contains(t, wf.Annotations, "annotation1")
}