Skip to content

Commit

Permalink
fix: add workflow nnd project name info for pods created by cyclone (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujian7 committed Sep 28, 2020
1 parent 15c7958 commit 47c09e5
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/workflow/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
EnvOutputResourcesInfo = "OUTPUT_RESOURCES_INFO"
// EnvMetadataNamespace is an environment which represents namespace of workflows/workflowRuns/stages.
EnvMetadataNamespace = "METADATA_NAMESPACE"
// EnvProjectName is an environment which represents project name.
EnvProjectName = "PROJECT_NAME"
// EnvWorkflowName is an environment which represents workflow name.
EnvWorkflowName = "WORKFLOW_NAME"
// EnvWorkflowrunName is an environment which represents workflowrun name.
Expand Down
44 changes: 44 additions & 0 deletions pkg/workflow/common/owner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package common

import (
"reflect"

"github.com/caicloud/cyclone/pkg/apis/cyclone/v1alpha1"
"github.com/caicloud/cyclone/pkg/meta"
)

// ResolveWorkflowName finds workflow name in the workflowRun's filed in order
// - WorkflowRef
// - OwnerReference
// - Label workflow
// if found returns the value, otherwise returns empty.
func ResolveWorkflowName(wfr v1alpha1.WorkflowRun) string {
workflowKind := reflect.TypeOf(v1alpha1.Workflow{}).Name()
if wfr.Spec.WorkflowRef != nil && wfr.Spec.WorkflowRef.Kind == workflowKind {
return wfr.Spec.WorkflowRef.Name
}

for _, or := range wfr.OwnerReferences {
if or.Kind == workflowKind {
return or.Name
}
}

if wfr.Labels != nil {
if n, ok := wfr.Labels[meta.LabelWorkflowName]; ok {
return n
}
}
return ""
}

// ResolveProjectName finds project name in the workflowRun's label,
// if found returns the value, otherwise returns empty.
func ResolveProjectName(wfr v1alpha1.WorkflowRun) string {
if wfr.Labels != nil {
if n, ok := wfr.Labels[meta.LabelProjectName]; ok {
return n
}
}
return ""
}
89 changes: 89 additions & 0 deletions pkg/workflow/common/owner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package common

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/caicloud/cyclone/pkg/apis/cyclone/v1alpha1"
"github.com/caicloud/cyclone/pkg/meta"
)

func TestResolveWorkflowName(t *testing.T) {

cases := map[string]struct {
wfr v1alpha1.WorkflowRun
expect string
}{
"wfrRef": {
wfr: v1alpha1.WorkflowRun{
Spec: v1alpha1.WorkflowRunSpec{
WorkflowRef: &corev1.ObjectReference{
Kind: "Workflow",
Name: "w1",
},
},
},
expect: "w1",
},
"wfrOwner": {
wfr: v1alpha1.WorkflowRun{
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{
{
Kind: "Workflow",
Name: "w1",
}},
},
},
expect: "w1",
},
"label": {
wfr: v1alpha1.WorkflowRun{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
meta.LabelWorkflowName: "w1",
},
},
},
expect: "w1",
},
"none": {
wfr: v1alpha1.WorkflowRun{},
expect: "",
},
}

for _, c := range cases {
assert.Equal(t, c.expect, ResolveWorkflowName(c.wfr))
}
}

func TestResolveProjectName(t *testing.T) {

cases := map[string]struct {
wfr v1alpha1.WorkflowRun
expect string
}{
"label": {
wfr: v1alpha1.WorkflowRun{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
meta.LabelProjectName: "p1",
},
},
},
expect: "p1",
},
"none": {
wfr: v1alpha1.WorkflowRun{},
expect: "",
},
}

for _, c := range cases {
assert.Equal(t, c.expect, ResolveProjectName(c.wfr))
}
}
2 changes: 2 additions & 0 deletions pkg/workflow/workflowrun/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ func (o *operator) GC(lastTry, wfrDeletion bool) error {
Name: GCPodName(o.wfr.Name),
Namespace: executionContext.Namespace,
Labels: map[string]string{
meta.LabelProjectName: common.ResolveProjectName(*o.wfr),
meta.LabelWorkflowName: common.ResolveWorkflowName(*o.wfr),
meta.LabelWorkflowRunName: o.wfr.Name,
meta.LabelPodKind: meta.PodKindGC.String(),
meta.LabelPodCreatedBy: meta.CycloneCreator,
Expand Down
10 changes: 8 additions & 2 deletions pkg/workflow/workload/pod/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func (m *Builder) Prepare() error {
Name: Name(m.wf.Name, m.stage),
Namespace: m.executionContext.Namespace,
Labels: map[string]string{
meta.LabelProjectName: common.ResolveProjectName(*m.wfr),
meta.LabelWorkflowName: common.ResolveWorkflowName(*m.wfr),
meta.LabelWorkflowRunName: m.wfr.Name,
meta.LabelPodCreatedBy: meta.CycloneCreator,
meta.LabelPodKind: meta.PodKindWorkload.String(),
Expand Down Expand Up @@ -844,11 +846,15 @@ func (m *Builder) InjectEnvs() error {
envs := []corev1.EnvVar{
{
Name: common.EnvMetadataNamespace,
Value: m.wf.Namespace,
Value: m.wfr.Namespace,
},
{
Name: common.EnvProjectName,
Value: common.ResolveProjectName(*m.wfr),
},
{
Name: common.EnvWorkflowName,
Value: m.wf.Name,
Value: common.ResolveWorkflowName(*m.wfr),
},
{
Name: common.EnvWorkflowrunName,
Expand Down
29 changes: 29 additions & 0 deletions pkg/workflow/workload/pod/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ var tmp2 = "v1"
var wfr = &v1alpha1.WorkflowRun{
ObjectMeta: metav1.ObjectMeta{
Name: "wfr",
Labels: map[string]string{
meta.LabelProjectName: "p1",
meta.LabelWorkflowName: "wf",
},
},
Spec: v1alpha1.WorkflowRunSpec{
Stages: []v1alpha1.ParameterConfig{
Expand Down Expand Up @@ -621,6 +625,31 @@ func (suite *PodBuilderSuite) TestAddCommonVolumes() {
}
}

func (suite *PodBuilderSuite) TestInjectEnvs() {
builder := NewBuilder(suite.client, wf, wfr, getStage(suite.client, "simple"))
err := builder.Prepare()
assert.Nil(suite.T(), err)
err = builder.ResolveArguments()
assert.Nil(suite.T(), err)
err = builder.InjectEnvs()
assert.Nil(suite.T(), err)
assert.NotNil(suite.T(), builder.pod.Spec.Containers[0].Env)
for _, kv := range builder.pod.Spec.Containers[0].Env {
switch kv.Name {
case common.EnvMetadataNamespace:
assert.Equal(suite.T(), "", kv.Value)
case common.EnvProjectName:
assert.Equal(suite.T(), "p1", kv.Value)
case common.EnvWorkflowName:
assert.Equal(suite.T(), "wf", kv.Value)
case common.EnvWorkflowrunName:
assert.Equal(suite.T(), "wfr", kv.Value)
case common.EnvStageName:
assert.Equal(suite.T(), "simple", kv.Value)
}
}
}

func TestPodBuilderSuite(t *testing.T) {
suite.Run(t, new(PodBuilderSuite))
}
Expand Down

0 comments on commit 47c09e5

Please sign in to comment.