@@ -23,6 +23,7 @@ import (
2323 "testing"
2424
2525 "github.com/google/go-cmp/cmp"
26+ "github.com/google/go-cmp/cmp/cmpopts"
2627 "github.com/tektoncd/pipeline/pkg/apis/pipeline"
2728 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
2829 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
@@ -40,41 +41,46 @@ var (
4041 CredsImage : "override-with-creds:latest" ,
4142 ShellImage : "busybox" ,
4243 }
44+
45+ ignoreReleaseAnnotation = func (k string , v string ) bool {
46+ return k == ReleaseAnnotation
47+ }
4348)
4449
4550func TestMakePod (t * testing.T ) {
46- names .TestingSeed ()
47-
48- implicitEnvVars := []corev1.EnvVar {{
49- Name : "HOME" ,
50- Value : homeDir ,
51- }}
52- secretsVolumeMount := corev1.VolumeMount {
53- Name : "tekton-internal-secret-volume-multi-creds-9l9zj" ,
54- MountPath : "/tekton/creds-secrets/multi-creds" ,
55- }
56- secretsVolume := corev1.Volume {
57- Name : "tekton-internal-secret-volume-multi-creds-9l9zj" ,
58- VolumeSource : corev1.VolumeSource {Secret : & corev1.SecretVolumeSource {SecretName : "multi-creds" }},
59- }
51+ var (
52+ secretsVolumeMount = corev1.VolumeMount {
53+ Name : "tekton-internal-secret-volume-multi-creds-9l9zj" ,
54+ MountPath : "/tekton/creds-secrets/multi-creds" ,
55+ }
56+ secretsVolume = corev1.Volume {
57+ Name : "tekton-internal-secret-volume-multi-creds-9l9zj" ,
58+ VolumeSource : corev1.VolumeSource {Secret : & corev1.SecretVolumeSource {SecretName : "multi-creds" }},
59+ }
6060
61- placeToolsInit := corev1.Container {
62- Name : "place-tools" ,
63- Image : images .EntrypointImage ,
64- Command : []string {"cp" , "/ko-app/entrypoint" , "/tekton/tools/entrypoint" },
65- VolumeMounts : []corev1.VolumeMount {toolsMount },
66- }
61+ implicitEnvVars = []corev1.EnvVar {{
62+ Name : "HOME" ,
63+ Value : homeDir ,
64+ }}
6765
68- runtimeClassName := "gvisor"
69- automountServiceAccountToken := false
70- dnsPolicy := corev1 .DNSNone
71- enableServiceLinks := false
72- priorityClassName := "system-cluster-critical"
66+ placeToolsInit = corev1.Container {
67+ Name : "place-tools" ,
68+ Image : images .EntrypointImage ,
69+ Command : []string {"cp" , "/ko-app/entrypoint" , "/tekton/tools/entrypoint" },
70+ VolumeMounts : []corev1.VolumeMount {toolsMount },
71+ }
72+ runtimeClassName = "gvisor"
73+ automountServiceAccountToken = false
74+ dnsPolicy = corev1 .DNSNone
75+ enableServiceLinks = false
76+ priorityClassName = "system-cluster-critical"
77+ )
7378
7479 for _ , c := range []struct {
7580 desc string
7681 trs v1alpha1.TaskRunSpec
7782 ts v1alpha1.TaskSpec
83+ featureFlags map [string ]string
7884 want * corev1.PodSpec
7985 wantAnnotations map [string ]string
8086 }{{
@@ -113,6 +119,48 @@ func TestMakePod(t *testing.T) {
113119 }},
114120 Volumes : append (implicitVolumes , toolsVolume , downwardVolume ),
115121 },
122+ }, {
123+ desc : "simple with enable-ready-annotation-on-pod-create" ,
124+ ts : v1alpha1.TaskSpec {TaskSpec : v1beta1.TaskSpec {
125+ Steps : []v1alpha1.Step {{Container : corev1.Container {
126+ Name : "name" ,
127+ Image : "image" ,
128+ Command : []string {"cmd" }, // avoid entrypoint lookup.
129+ }}},
130+ }},
131+ featureFlags : map [string ]string {
132+ featureFlagSetReadyAnnotationOnPodCreate : "true" ,
133+ },
134+ want : & corev1.PodSpec {
135+ RestartPolicy : corev1 .RestartPolicyNever ,
136+ InitContainers : []corev1.Container {placeToolsInit },
137+ Containers : []corev1.Container {{
138+ Name : "step-name" ,
139+ Image : "image" ,
140+ Command : []string {"/tekton/tools/entrypoint" },
141+ Args : []string {
142+ "-wait_file" ,
143+ "/tekton/downward/ready" ,
144+ "-wait_file_content" ,
145+ "-post_file" ,
146+ "/tekton/tools/0" ,
147+ "-termination_path" ,
148+ "/tekton/termination" ,
149+ "-entrypoint" ,
150+ "cmd" ,
151+ "--" ,
152+ },
153+ Env : implicitEnvVars ,
154+ VolumeMounts : append ([]corev1.VolumeMount {toolsMount , downwardMount }, implicitVolumeMounts ... ),
155+ WorkingDir : pipeline .WorkspaceDir ,
156+ Resources : corev1.ResourceRequirements {Requests : allZeroQty ()},
157+ TerminationMessagePath : "/tekton/termination" ,
158+ }},
159+ Volumes : append (implicitVolumes , toolsVolume , downwardVolume ),
160+ },
161+ wantAnnotations : map [string ]string {
162+ readyAnnotation : readyAnnotationValue ,
163+ },
116164 }, {
117165 desc : "with service account" ,
118166 ts : v1alpha1.TaskSpec {TaskSpec : v1beta1.TaskSpec {
@@ -471,6 +519,58 @@ sidecar-script-heredoc-randomly-generated-mz4c7
471519 }},
472520 Volumes : append (implicitVolumes , scriptsVolume , toolsVolume , downwardVolume ),
473521 },
522+ }, {
523+ desc : "sidecar container with enable-ready-annotation-on-pod-create" ,
524+ ts : v1alpha1.TaskSpec {TaskSpec : v1beta1.TaskSpec {
525+ Steps : []v1alpha1.Step {{Container : corev1.Container {
526+ Name : "primary-name" ,
527+ Image : "primary-image" ,
528+ Command : []string {"cmd" }, // avoid entrypoint lookup.
529+ }}},
530+ Sidecars : []v1alpha1.Sidecar {{
531+ Container : corev1.Container {
532+ Name : "sc-name" ,
533+ Image : "sidecar-image" ,
534+ },
535+ }},
536+ }},
537+ featureFlags : map [string ]string {
538+ featureFlagSetReadyAnnotationOnPodCreate : "true" ,
539+ },
540+ wantAnnotations : map [string ]string {}, // no ready annotations on pod create since sidecars are present
541+ want : & corev1.PodSpec {
542+ RestartPolicy : corev1 .RestartPolicyNever ,
543+ InitContainers : []corev1.Container {placeToolsInit },
544+ Containers : []corev1.Container {{
545+ Name : "step-primary-name" ,
546+ Image : "primary-image" ,
547+ Command : []string {"/tekton/tools/entrypoint" },
548+ Args : []string {
549+ "-wait_file" ,
550+ "/tekton/downward/ready" ,
551+ "-wait_file_content" ,
552+ "-post_file" ,
553+ "/tekton/tools/0" ,
554+ "-termination_path" ,
555+ "/tekton/termination" ,
556+ "-entrypoint" ,
557+ "cmd" ,
558+ "--" ,
559+ },
560+ Env : implicitEnvVars ,
561+ VolumeMounts : append ([]corev1.VolumeMount {toolsMount , downwardMount }, implicitVolumeMounts ... ),
562+ WorkingDir : pipeline .WorkspaceDir ,
563+ Resources : corev1.ResourceRequirements {Requests : allZeroQty ()},
564+ TerminationMessagePath : "/tekton/termination" ,
565+ }, {
566+ Name : "sidecar-sc-name" ,
567+ Image : "sidecar-image" ,
568+ Resources : corev1.ResourceRequirements {
569+ Requests : nil ,
570+ },
571+ }},
572+ Volumes : append (implicitVolumes , toolsVolume , downwardVolume ),
573+ },
474574 }, {
475575 desc : "resource request" ,
476576 ts : v1alpha1.TaskSpec {TaskSpec : v1beta1.TaskSpec {
@@ -734,6 +834,10 @@ script-heredoc-randomly-generated-78c5n
734834 t .Run (c .desc , func (t * testing.T ) {
735835 names .TestingSeed ()
736836 kubeclient := fakek8s .NewSimpleClientset (
837+ & corev1.ConfigMap {
838+ ObjectMeta : metav1.ObjectMeta {Name : featureFlagConfigMapName , Namespace : system .GetNamespace ()},
839+ Data : c .featureFlags ,
840+ },
737841 & corev1.ServiceAccount {ObjectMeta : metav1.ObjectMeta {Name : "default" , Namespace : "default" }},
738842 & corev1.ServiceAccount {ObjectMeta : metav1.ObjectMeta {Name : "service-account" , Namespace : "default" },
739843 Secrets : []corev1.ObjectReference {{
@@ -780,6 +884,12 @@ script-heredoc-randomly-generated-78c5n
780884 if d := cmp .Diff (c .want , & got .Spec , resourceQuantityCmp ); d != "" {
781885 t .Errorf ("Diff(-want, +got):\n %s" , d )
782886 }
887+
888+ if c .wantAnnotations != nil {
889+ if d := cmp .Diff (c .wantAnnotations , got .ObjectMeta .Annotations , cmpopts .IgnoreMapEntries (ignoreReleaseAnnotation )); d != "" {
890+ t .Errorf ("Annotation Diff(-want, +got):\n %s" , d )
891+ }
892+ }
783893 })
784894 }
785895}
@@ -888,3 +998,82 @@ func TestShouldOverrideWorkingDir(t *testing.T) {
888998 })
889999 }
8901000}
1001+
1002+ func TestShouldAddReadyAnnotationonPodCreate (t * testing.T ) {
1003+ sd := v1beta1.Sidecar {
1004+ Container : corev1.Container {
1005+ Name : "a-sidecar" ,
1006+ },
1007+ }
1008+ tcs := []struct {
1009+ description string
1010+ sidecars []v1beta1.Sidecar
1011+ configMap * corev1.ConfigMap
1012+ expected bool
1013+ }{{
1014+ description : "Default behavior with sidecars present: Ready annotation not set on pod create" ,
1015+ sidecars : []v1beta1.Sidecar {sd },
1016+ configMap : & corev1.ConfigMap {
1017+ ObjectMeta : metav1.ObjectMeta {Name : featureFlagConfigMapName , Namespace : system .GetNamespace ()},
1018+ Data : map [string ]string {},
1019+ },
1020+ expected : false ,
1021+ }, {
1022+ description : "Default behavior with no sidecars present: Ready annotation not set on pod create" ,
1023+ sidecars : []v1beta1.Sidecar {},
1024+ configMap : & corev1.ConfigMap {
1025+ ObjectMeta : metav1.ObjectMeta {Name : featureFlagConfigMapName , Namespace : system .GetNamespace ()},
1026+ Data : map [string ]string {},
1027+ },
1028+ expected : false ,
1029+ }, {
1030+ description : "Setting enable-ready-annotation-on-pod-create to false with sidecars present results in false" ,
1031+ sidecars : []v1beta1.Sidecar {sd },
1032+ configMap : & corev1.ConfigMap {
1033+ ObjectMeta : metav1.ObjectMeta {Name : featureFlagConfigMapName , Namespace : system .GetNamespace ()},
1034+ Data : map [string ]string {
1035+ featureFlagSetReadyAnnotationOnPodCreate : "false" ,
1036+ },
1037+ },
1038+ expected : false ,
1039+ }, {
1040+ description : "Setting enable-ready-annotation-on-pod-create to false with no sidecars present results in false" ,
1041+ sidecars : []v1beta1.Sidecar {},
1042+ configMap : & corev1.ConfigMap {
1043+ ObjectMeta : metav1.ObjectMeta {Name : featureFlagConfigMapName , Namespace : system .GetNamespace ()},
1044+ Data : map [string ]string {
1045+ featureFlagSetReadyAnnotationOnPodCreate : "false" ,
1046+ },
1047+ },
1048+ expected : false ,
1049+ }, {
1050+ description : "Setting enable-ready-annotation-on-pod-create to true with sidecars present results in false" ,
1051+ sidecars : []v1beta1.Sidecar {sd },
1052+ configMap : & corev1.ConfigMap {
1053+ ObjectMeta : metav1.ObjectMeta {Name : featureFlagConfigMapName , Namespace : system .GetNamespace ()},
1054+ Data : map [string ]string {
1055+ featureFlagSetReadyAnnotationOnPodCreate : "true" ,
1056+ },
1057+ },
1058+ expected : false ,
1059+ }, {
1060+ description : "Setting enable-ready-annotation-on-pod-create to true with no sidecars present results in true" ,
1061+ sidecars : []v1beta1.Sidecar {},
1062+ configMap : & corev1.ConfigMap {
1063+ ObjectMeta : metav1.ObjectMeta {Name : featureFlagConfigMapName , Namespace : system .GetNamespace ()},
1064+ Data : map [string ]string {
1065+ featureFlagSetReadyAnnotationOnPodCreate : "true" ,
1066+ },
1067+ },
1068+ expected : true ,
1069+ }}
1070+
1071+ for _ , tc := range tcs {
1072+ t .Run (tc .description , func (t * testing.T ) {
1073+ kubclient := fakek8s .NewSimpleClientset (tc .configMap )
1074+ if result := shouldAddReadyAnnotationonPodCreate (tc .sidecars , kubclient ); result != tc .expected {
1075+ t .Errorf ("expected: %t Recieved: %t" , tc .expected , result )
1076+ }
1077+ })
1078+ }
1079+ }
0 commit comments