@@ -342,6 +342,11 @@ func TestDeploymentPatch(t *testing.T) {
342342 {
343343 Name : "nginx" ,
344344 Image : "nginx:1.14.2" ,
345+ Ports : []corev1.ContainerPort {
346+ {
347+ ContainerPort : 80 ,
348+ },
349+ },
345350 },
346351 },
347352 },
@@ -656,6 +661,126 @@ func TestDeploymentPatch(t *testing.T) {
656661 }
657662}
658663
664+ func TestDeploymentPodTemplateHashLabelPatch (t * testing.T ) {
665+ podTemplateHash := "user-defined-hash"
666+ rs := & appsv1.ReplicaSet {
667+ ObjectMeta : metav1.ObjectMeta {
668+ Name : "rs-1" ,
669+ UID : "123" ,
670+ OwnerReferences : []metav1.OwnerReference {
671+ {
672+ APIVersion : "apps/v1" ,
673+ Kind : "Deployment" ,
674+ Name : "deploy-1" ,
675+ UID : types .UID ("deploy-1" ),
676+ Controller : ptr .To (true ),
677+ BlockOwnerDeletion : ptr .To (true ),
678+ },
679+ },
680+ },
681+ Spec : appsv1.ReplicaSetSpec {
682+ Template : corev1.PodTemplateSpec {
683+ ObjectMeta : metav1.ObjectMeta {
684+ Labels : map [string ]string {
685+ "app" : "nginx" ,
686+ appsv1 .DefaultDeploymentUniqueLabelKey : podTemplateHash ,
687+ },
688+ },
689+ Spec : corev1.PodSpec {
690+ Containers : []corev1.Container {
691+ {
692+ Name : "nginx" ,
693+ Image : "nginx:1.14.2" ,
694+ Ports : []corev1.ContainerPort {
695+ {
696+ ContainerPort : 80 ,
697+ },
698+ },
699+ },
700+ },
701+ },
702+ },
703+ },
704+ }
705+ deploy := & appsv1.Deployment {
706+ ObjectMeta : metav1.ObjectMeta {
707+ Name : "deploy-1" ,
708+ },
709+ Spec : appsv1.DeploymentSpec {
710+ Replicas : ptr .To (int32 (10 )),
711+ Selector : & metav1.LabelSelector {
712+ MatchLabels : map [string ]string {
713+ "app" : "nginx" ,
714+ },
715+ },
716+ Template : corev1.PodTemplateSpec {
717+ ObjectMeta : metav1.ObjectMeta {
718+ Labels : map [string ]string {
719+ "app" : "nginx" ,
720+ appsv1 .DefaultDeploymentUniqueLabelKey : podTemplateHash ,
721+ },
722+ },
723+ Spec : corev1.PodSpec {
724+ Containers : []corev1.Container {
725+ {
726+ Name : "nginx" ,
727+ Image : "nginx:1.14.2" ,
728+ },
729+ },
730+ },
731+ },
732+ },
733+ }
734+
735+ revisionWithLabel := util .ComputeHash (& rs .Spec .Template , nil )
736+ templateWithoutLabel := rs .Spec .Template .DeepCopy ()
737+ delete (templateWithoutLabel .Labels , appsv1 .DefaultDeploymentUniqueLabelKey )
738+ revisionWithoutLabel := util .ComputeHash (templateWithoutLabel , nil )
739+ if revisionWithLabel == revisionWithoutLabel {
740+ t .Fatalf ("expected revisions to be different when %s label is removed" , appsv1 .DefaultDeploymentUniqueLabelKey )
741+ }
742+ updateRevision := util .ComputeHash (& deploy .Spec .Template , nil )
743+ if updateRevision != revisionWithLabel {
744+ t .Fatalf ("expected updateRevision %q to be equal to revisionWithLabel %q" , updateRevision , revisionWithLabel )
745+ }
746+
747+ ctx := & batchcontext.BatchContext {
748+ RolloutID : "rollout-1" ,
749+ UpdateRevision : updateRevision ,
750+ PlannedUpdatedReplicas : 5 ,
751+ Replicas : 10 ,
752+ }
753+ ctx .Pods = generateDeploymentPods (1 , ctx .Replicas , 0 , "" , "" )
754+ var objects []client.Object
755+ for _ , pod := range ctx .Pods {
756+ objects = append (objects , pod )
757+ }
758+ objects = append (objects , rs , deploy )
759+
760+ cli := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (objects ... ).Build ()
761+ patcher := NewLabelPatcher (cli , klog.ObjectRef {Name : "test" }, []v1beta1.ReleaseBatch {{CanaryReplicas : intstr .FromInt32 (5 )}})
762+ if err := patcher .patchPodBatchLabel (ctx .Pods , ctx ); err != nil {
763+ t .Fatalf ("failed to patch pods: %v" , err )
764+ }
765+
766+ podList := & corev1.PodList {}
767+ if err := cli .List (context .TODO (), podList ); err != nil {
768+ t .Fatalf ("failed to list pods: %v" , err )
769+ }
770+ var patched int
771+ for _ , pod := range podList .Items {
772+ if pod .Labels [appsv1 .ControllerRevisionHashLabelKey ] != updateRevision {
773+ t .Fatalf ("expected pod %s/%s to have revision %s, got %s" , pod .Namespace , pod .Name , updateRevision , pod .Labels [appsv1 .ControllerRevisionHashLabelKey ])
774+ }
775+ if pod .Labels [v1beta1 .RolloutIDLabel ] == ctx .RolloutID {
776+ patched ++
777+ }
778+ }
779+ if patched != 5 {
780+ t .Fatalf ("expected patched: %d, got: %d" , 5 , patched )
781+ }
782+ }
783+
659784func generateDeploymentPods (ordinalBegin , ordinalEnd , labeled int32 , rolloutID , batchID string ) []* corev1.Pod {
660785 podsWithLabel := generateLabeledPods (map [string ]string {
661786 v1beta1 .RolloutIDLabel : rolloutID ,
0 commit comments