Skip to content

Commit c351495

Browse files
committed
feat: integrate apply params to workspacebindings in taskrun
1 parent 39654b8 commit c351495

File tree

2 files changed

+303
-0
lines changed

2 files changed

+303
-0
lines changed

pkg/reconciler/taskrun/taskrun.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ func (c *Reconciler) reconcile(ctx context.Context, tr *v1.TaskRun, rtr *resourc
558558
tr.Spec.Workspaces = taskRunWorkspaces
559559
}
560560

561+
resources.ApplyParametersToWorkspaceBindings(rtr.TaskSpec, tr)
561562
// Get the randomized volume names assigned to workspace bindings
562563
workspaceVolumes := workspace.CreateVolumes(tr.Spec.Workspaces)
563564

pkg/reconciler/taskrun/taskrun_test.go

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,6 +2932,308 @@ spec:
29322932
}
29332933
}
29342934

2935+
func TestPopulateParamsToWorkspaceBindingsClaimName(t *testing.T) {
2936+
taskRun := parse.MustParseV1TaskRun(t, `
2937+
metadata:
2938+
name: test-taskrun-propagating-params-to-workspace-bindings
2939+
namespace: foo
2940+
spec:
2941+
params:
2942+
- name: myClaim
2943+
value: pvc-1
2944+
taskSpec:
2945+
steps:
2946+
- args:
2947+
- ""
2948+
command:
2949+
- echo
2950+
image: foo
2951+
name: simple-step
2952+
workspaces:
2953+
- name: ws-1
2954+
persistentVolumeClaim:
2955+
claimName: $(params.myClaim)
2956+
`)
2957+
d := test.Data{
2958+
TaskRuns: []*v1.TaskRun{taskRun},
2959+
}
2960+
testAssets, cancel := getTaskRunController(t, d)
2961+
defer cancel()
2962+
createServiceAccount(t, testAssets, "default", taskRun.Namespace)
2963+
c := testAssets.Controller
2964+
if err := c.Reconciler.Reconcile(testAssets.Ctx, getRunName(taskRun)); err == nil {
2965+
t.Fatalf("Could not reconcile the taskrun: %v", err)
2966+
}
2967+
2968+
pod, _ := testAssets.Clients.Kube.CoreV1().Pods(taskRun.Namespace).Get(testAssets.Ctx, taskRun.Name+"-pod", metav1.GetOptions{})
2969+
2970+
want := "pvc-1"
2971+
got := ""
2972+
for _, v := range pod.Spec.Volumes {
2973+
if v.PersistentVolumeClaim != nil {
2974+
got = v.PersistentVolumeClaim.ClaimName
2975+
break
2976+
}
2977+
}
2978+
if c := cmp.Diff(want, got); c != "" {
2979+
t.Errorf("TestPopulateParamsToWorkspaceBindings errored with: %s", diff.PrintWantGot(c))
2980+
}
2981+
}
2982+
2983+
func TestPopulateParamsToWorkspaceBindingsSubPath(t *testing.T) {
2984+
taskRun := parse.MustParseV1TaskRun(t, `
2985+
metadata:
2986+
name: test-taskrun-propagating-params-to-workspace-bindings
2987+
namespace: foo
2988+
spec:
2989+
params:
2990+
- name: myClaim
2991+
value: pvc-1
2992+
- name: mySubPath
2993+
value: sub-1
2994+
taskSpec:
2995+
steps:
2996+
- args:
2997+
- "$(workspaces.ws-1.volume)/foo"
2998+
command:
2999+
- echo
3000+
image: foo
3001+
name: simple-step
3002+
workspaces:
3003+
- name: ws-1
3004+
persistentVolumeClaim:
3005+
claimName: $(params.myClaim)
3006+
subPath: $(params.mySubPath)
3007+
`)
3008+
d := test.Data{
3009+
TaskRuns: []*v1.TaskRun{taskRun},
3010+
}
3011+
testAssets, cancel := getTaskRunController(t, d)
3012+
defer cancel()
3013+
createServiceAccount(t, testAssets, "default", taskRun.Namespace)
3014+
c := testAssets.Controller
3015+
if err := c.Reconciler.Reconcile(testAssets.Ctx, getRunName(taskRun)); err == nil {
3016+
t.Fatalf("Could not reconcile the taskrun: %v", err)
3017+
}
3018+
3019+
pod, _ := testAssets.Clients.Kube.CoreV1().Pods(taskRun.Namespace).Get(testAssets.Ctx, taskRun.Name+"-pod", metav1.GetOptions{})
3020+
3021+
want := "sub-1"
3022+
got := ""
3023+
for _, container := range pod.Spec.Containers {
3024+
for _, mount := range container.VolumeMounts {
3025+
if mount.SubPath != "" {
3026+
got = mount.SubPath
3027+
break
3028+
}
3029+
}
3030+
}
3031+
3032+
if c := cmp.Diff(want, got); c != "" {
3033+
t.Errorf("TestPopulateParamsToWorkspaceBindingsSubPath errored with: %s", diff.PrintWantGot(c))
3034+
}
3035+
}
3036+
3037+
func TestPopulateParamsToWorkspaceBindingsConfigMapName(t *testing.T) {
3038+
taskRun := parse.MustParseV1TaskRun(t, `
3039+
metadata:
3040+
name: test-taskrun-propagating-params-to-workspace-bindings
3041+
namespace: foo
3042+
spec:
3043+
params:
3044+
- name: myConfig
3045+
value: config-1
3046+
taskSpec:
3047+
steps:
3048+
- args:
3049+
- ""
3050+
command:
3051+
- echo
3052+
image: foo
3053+
name: simple-step
3054+
workspaces:
3055+
- name: ws-1
3056+
configMap:
3057+
name: $(params.myConfig)
3058+
`)
3059+
d := test.Data{
3060+
TaskRuns: []*v1.TaskRun{taskRun},
3061+
}
3062+
testAssets, cancel := getTaskRunController(t, d)
3063+
defer cancel()
3064+
createServiceAccount(t, testAssets, "default", taskRun.Namespace)
3065+
c := testAssets.Controller
3066+
if err := c.Reconciler.Reconcile(testAssets.Ctx, getRunName(taskRun)); err == nil {
3067+
t.Fatalf("Could not reconcile the taskrun: %v", err)
3068+
}
3069+
3070+
pod, _ := testAssets.Clients.Kube.CoreV1().Pods(taskRun.Namespace).Get(testAssets.Ctx, taskRun.Name+"-pod", metav1.GetOptions{})
3071+
3072+
want := "config-1"
3073+
got := ""
3074+
3075+
for _, v := range pod.Spec.Volumes {
3076+
if v.ConfigMap != nil {
3077+
got = v.ConfigMap.Name
3078+
break
3079+
}
3080+
}
3081+
3082+
if c := cmp.Diff(want, got); c != "" {
3083+
t.Errorf("TestPopulateParamsToWorkspaceBindingsConfigMapName errored with: %s", diff.PrintWantGot(c))
3084+
}
3085+
}
3086+
3087+
func TestPopulateParamsToWorkspaceBindingsSecretName(t *testing.T) {
3088+
taskRun := parse.MustParseV1TaskRun(t, `
3089+
metadata:
3090+
name: test-taskrun-propagating-params-to-workspace-bindings
3091+
namespace: foo
3092+
spec:
3093+
params:
3094+
- name: mySecret
3095+
value: secret-1
3096+
taskSpec:
3097+
steps:
3098+
- args:
3099+
- ""
3100+
command:
3101+
- echo
3102+
image: foo
3103+
name: simple-step
3104+
workspaces:
3105+
- name: ws-1
3106+
secret:
3107+
secretName: $(params.mySecret)
3108+
`)
3109+
d := test.Data{
3110+
TaskRuns: []*v1.TaskRun{taskRun},
3111+
}
3112+
testAssets, cancel := getTaskRunController(t, d)
3113+
defer cancel()
3114+
createServiceAccount(t, testAssets, "default", taskRun.Namespace)
3115+
c := testAssets.Controller
3116+
if err := c.Reconciler.Reconcile(testAssets.Ctx, getRunName(taskRun)); err == nil {
3117+
t.Fatalf("Could not reconcile the taskrun: %v", err)
3118+
}
3119+
3120+
pod, _ := testAssets.Clients.Kube.CoreV1().Pods(taskRun.Namespace).Get(testAssets.Ctx, taskRun.Name+"-pod", metav1.GetOptions{})
3121+
3122+
want := "secret-1"
3123+
got := ""
3124+
3125+
for _, v := range pod.Spec.Volumes {
3126+
if v.Secret != nil {
3127+
got = v.Secret.SecretName
3128+
break
3129+
}
3130+
}
3131+
3132+
if c := cmp.Diff(want, got); c != "" {
3133+
t.Errorf("TestPopulateParamsToWorkspaceBindingsSecretName errored with: %s", diff.PrintWantGot(c))
3134+
}
3135+
}
3136+
func TestPopulateParamsToWorkspaceBindingsProjectedSources(t *testing.T) {
3137+
tests := []struct {
3138+
description string
3139+
taskRun *v1.TaskRun
3140+
want []corev1.VolumeProjection
3141+
}{
3142+
{
3143+
description: "taskrun propagating params to workspace bindings projected secret",
3144+
taskRun: parse.MustParseV1TaskRun(t, `
3145+
metadata:
3146+
name: test-taskrun-propagating-params-to-workspace-bindings-projected-secret
3147+
namespace: foo
3148+
spec:
3149+
params:
3150+
- name: mySecret
3151+
value: secret-1
3152+
taskSpec:
3153+
steps:
3154+
- args:
3155+
- ""
3156+
command:
3157+
- echo
3158+
image: foo
3159+
name: simple-step
3160+
workspaces:
3161+
- name: ws-1
3162+
projected:
3163+
sources:
3164+
- secret:
3165+
name: $(params.mySecret)
3166+
`),
3167+
want: []corev1.VolumeProjection{
3168+
{Secret: &corev1.SecretProjection{LocalObjectReference: corev1.LocalObjectReference{Name: "secret-1"}}},
3169+
},
3170+
},
3171+
{
3172+
description: "taskrun propagating params to workspace bindings projected configMap",
3173+
taskRun: parse.MustParseV1TaskRun(t, `
3174+
metadata:
3175+
name: test-taskrun-propagating-params-to-workspace-bindings-projected-configMap
3176+
namespace: foo
3177+
spec:
3178+
params:
3179+
- name: myConfig
3180+
value: config-1
3181+
taskSpec:
3182+
steps:
3183+
- args:
3184+
- ""
3185+
command:
3186+
- echo
3187+
image: foo
3188+
name: simple-step
3189+
workspaces:
3190+
- name: ws-1
3191+
projected:
3192+
sources:
3193+
- configMap:
3194+
name: $(params.myConfig)
3195+
`),
3196+
want: []corev1.VolumeProjection{
3197+
{ConfigMap: &corev1.ConfigMapProjection{LocalObjectReference: corev1.LocalObjectReference{
3198+
Name: "config-1",
3199+
}}},
3200+
},
3201+
},
3202+
}
3203+
for _, tt := range tests {
3204+
t.Run(tt.description, func(t *testing.T) {
3205+
d := test.Data{
3206+
TaskRuns: []*v1.TaskRun{tt.taskRun},
3207+
}
3208+
testAssets, cancel := getTaskRunController(t, d)
3209+
defer cancel()
3210+
createServiceAccount(t, testAssets, "default", tt.taskRun.Namespace)
3211+
c := testAssets.Controller
3212+
if err := c.Reconciler.Reconcile(testAssets.Ctx, getRunName(tt.taskRun)); err == nil {
3213+
t.Fatalf("Could not reconcile the taskrun: %v", err)
3214+
}
3215+
3216+
list, _ := testAssets.Clients.Kube.CoreV1().Pods(tt.taskRun.Namespace).List(testAssets.Ctx, metav1.ListOptions{})
3217+
if len(list.Items) != 1 {
3218+
t.Fatalf("the result of list query shoud only contain 1 item.")
3219+
}
3220+
3221+
var got []corev1.VolumeProjection
3222+
3223+
for _, v := range list.Items[0].Spec.Volumes {
3224+
if v.Projected != nil {
3225+
got = v.Projected.Sources
3226+
break
3227+
}
3228+
}
3229+
3230+
if c := cmp.Diff(tt.want, got); c != "" {
3231+
t.Errorf("TestPopulateParamsToWorkspaceBindingsProjectedSources errored with: %s", diff.PrintWantGot(c))
3232+
}
3233+
})
3234+
}
3235+
}
3236+
29353237
func TestStepActionRef(t *testing.T) {
29363238
taskRun := parse.MustParseV1TaskRun(t, `
29373239
metadata:

0 commit comments

Comments
 (0)