-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add workflow nnd project name info for pods created by cyclone (#…
- Loading branch information
Showing
6 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters