@@ -30,6 +30,7 @@ import (
30
30
"k8s.io/apimachinery/pkg/runtime"
31
31
"k8s.io/apimachinery/pkg/types"
32
32
"k8s.io/client-go/kubernetes/scheme"
33
+ "k8s.io/utils/pointer"
33
34
"sigs.k8s.io/controller-runtime/pkg/client"
34
35
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
35
36
)
@@ -460,6 +461,18 @@ var _ = Describe("Controllerutil", func() {
460
461
ExpectWithOffset (1 , fetched .Status ).To (BeEquivalentTo (deploy .Status ))
461
462
}
462
463
464
+ assertLocalDeployStatusWasUpdated := func (fetched * appsv1.Deployment ) {
465
+ By ("local deploy object was updated during patch & has same spec, status, resource version as fetched" )
466
+ if fetched == nil {
467
+ fetched = & appsv1.Deployment {}
468
+ ExpectWithOffset (1 , c .Get (context .TODO (), deplKey , fetched )).To (Succeed ())
469
+ }
470
+ ExpectWithOffset (1 , fetched .ResourceVersion ).To (Equal (deploy .ResourceVersion ))
471
+ ExpectWithOffset (1 , * fetched .Spec .Replicas ).To (BeEquivalentTo (int32 (5 )))
472
+ ExpectWithOffset (1 , fetched .Status ).To (BeEquivalentTo (deploy .Status ))
473
+ ExpectWithOffset (1 , len (fetched .Status .Conditions )).To (BeEquivalentTo (1 ))
474
+ }
475
+
463
476
It ("creates a new object if one doesn't exists" , func () {
464
477
op , err := controllerutil .CreateOrPatch (context .TODO (), c , deploy , specr )
465
478
@@ -559,6 +572,46 @@ var _ = Describe("Controllerutil", func() {
559
572
assertLocalDeployWasUpdated (nil )
560
573
})
561
574
575
+ It ("patches resource and not empty status" , func () {
576
+ op , err := controllerutil .CreateOrPatch (context .TODO (), c , deploy , specr )
577
+
578
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationResultCreated ))
579
+ Expect (err ).NotTo (HaveOccurred ())
580
+
581
+ replicas := int32 (3 )
582
+ deployStatus := appsv1.DeploymentStatus {
583
+ ReadyReplicas : 1 ,
584
+ Replicas : replicas ,
585
+ }
586
+ op , err = controllerutil .CreateOrPatch (context .TODO (), c , deploy , func () error {
587
+ Expect (deploymentScaler (deploy , replicas )()).To (Succeed ())
588
+ return deploymentStatusr (deploy , deployStatus )()
589
+ })
590
+ By ("returning no error" )
591
+ Expect (err ).NotTo (HaveOccurred ())
592
+
593
+ By ("returning OperationResultUpdatedStatus" )
594
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationResultUpdatedStatus ))
595
+
596
+ assertLocalDeployWasUpdated (nil )
597
+
598
+ op , err = controllerutil .CreateOrPatch (context .TODO (), c , deploy , func () error {
599
+ deploy .Spec .Replicas = pointer .Int32Ptr (5 )
600
+ deploy .Status .Conditions = []appsv1.DeploymentCondition {{
601
+ Type : appsv1 .DeploymentProgressing ,
602
+ Status : corev1 .ConditionTrue ,
603
+ }}
604
+ return nil
605
+ })
606
+ By ("returning no error" )
607
+ Expect (err ).NotTo (HaveOccurred ())
608
+
609
+ By ("returning OperationResultUpdatedStatus" )
610
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationResultUpdatedStatus ))
611
+
612
+ assertLocalDeployStatusWasUpdated (nil )
613
+ })
614
+
562
615
It ("errors when MutateFn changes object name on creation" , func () {
563
616
op , err := controllerutil .CreateOrPatch (context .TODO (), c , deploy , func () error {
564
617
Expect (specr ()).To (Succeed ())
0 commit comments