@@ -22,16 +22,21 @@ package controller
2222import (
2323 "context"
2424 "encoding/json"
25+ "time"
2526
2627 corev1 "k8s.io/api/core/v1"
2728 "k8s.io/apimachinery/pkg/api/resource"
2829 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+ "k8s.io/client-go/tools/record"
2931 k8client "sigs.k8s.io/controller-runtime/pkg/client"
32+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
3033
3134 apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
3235 "github.com/cloudnative-pg/cloudnative-pg/internal/cnpi/plugin"
3336 pluginClient "github.com/cloudnative-pg/cloudnative-pg/internal/cnpi/plugin/client"
3437 "github.com/cloudnative-pg/cloudnative-pg/internal/configuration"
38+ rolloutManager "github.com/cloudnative-pg/cloudnative-pg/internal/controller/rollout"
39+ schemeBuilder "github.com/cloudnative-pg/cloudnative-pg/internal/scheme"
3540 "github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
3641 "github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
3742 "github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
@@ -919,3 +924,144 @@ var _ = Describe("checkPodSpec with plugins", Ordered, func() {
919924 "original and target PodSpec differ in containers: container postgres differs in environment" ))
920925 })
921926})
927+
928+ var _ = Describe ("Supervised primary update strategy and rollout slots" , func () {
929+ const namespace = "supervised-test"
930+
931+ var (
932+ reconciler * ClusterReconciler
933+ rm * rolloutManager.Manager
934+ k8sClient k8client.Client
935+ )
936+
937+ BeforeEach (func () {
938+ scheme := schemeBuilder .BuildWithAllKnownScheme ()
939+ k8sClient = fake .NewClientBuilder ().
940+ WithScheme (scheme ).
941+ WithStatusSubresource (& apiv1.Cluster {}).
942+ Build ()
943+
944+ // Create namespace
945+ ns := & corev1.Namespace {
946+ ObjectMeta : metav1.ObjectMeta {Name : namespace },
947+ }
948+ Expect (k8sClient .Create (context .Background (), ns )).To (Succeed ())
949+
950+ // Rollout manager with a large cluster delay so we can detect slot consumption
951+ rm = rolloutManager .New (time .Hour , 0 )
952+
953+ reconciler = & ClusterReconciler {
954+ Client : k8sClient ,
955+ Scheme : scheme ,
956+ Recorder : record .NewFakeRecorder (120 ),
957+ rolloutManager : rm ,
958+ }
959+
960+ configuration .Current = configuration .NewConfiguration ()
961+ })
962+
963+ // Helper to create a cluster in the fake client with the given primary update strategy
964+ createCluster := func (strategy apiv1.PrimaryUpdateStrategy ) * apiv1.Cluster {
965+ cluster := & apiv1.Cluster {
966+ ObjectMeta : metav1.ObjectMeta {
967+ Name : "test-cluster" ,
968+ Namespace : namespace ,
969+ },
970+ Spec : apiv1.ClusterSpec {
971+ Instances : 1 ,
972+ ImageName : "postgres:16.0" ,
973+ PrimaryUpdateStrategy : strategy ,
974+ PrimaryUpdateMethod : apiv1 .PrimaryUpdateMethodRestart ,
975+ StorageConfiguration : apiv1.StorageConfiguration {Size : "1Gi" },
976+ },
977+ }
978+ cluster .SetDefaults ()
979+ // Set status fields after SetDefaults() since it may clear them
980+ cluster .Status .CurrentPrimary = "test-cluster-1"
981+ cluster .Status .Image = "postgres:16.1"
982+ cluster .Status .Instances = 1
983+ Expect (k8sClient .Create (context .Background (), cluster )).To (Succeed ())
984+ Expect (k8sClient .Status ().Update (context .Background (), cluster )).To (Succeed ())
985+ return cluster
986+ }
987+
988+ // Helper to build a pod status list where the primary needs rollout (image mismatch)
989+ buildPodListWithPrimaryNeedingRollout := func (cluster * apiv1.Cluster ) * postgres.PostgresqlStatusList {
990+ pod := & corev1.Pod {
991+ ObjectMeta : metav1.ObjectMeta {
992+ Name : cluster .Status .CurrentPrimary ,
993+ Namespace : cluster .Namespace ,
994+ Annotations : map [string ]string {
995+ utils .ClusterSerialAnnotationName : "1" ,
996+ },
997+ },
998+ Spec : corev1.PodSpec {
999+ Containers : []corev1.Container {
1000+ {
1001+ Name : "postgres" ,
1002+ Image : "postgres:16.0" , // Different from cluster.Status.Image (16.1) -> triggers rollout
1003+ },
1004+ },
1005+ },
1006+ }
1007+ return & postgres.PostgresqlStatusList {
1008+ Items : []postgres.PostgresqlStatus {
1009+ {
1010+ Pod : pod ,
1011+ IsPodReady : true ,
1012+ ExecutableHash : "test_hash" ,
1013+ },
1014+ },
1015+ }
1016+ }
1017+
1018+ It ("supervised cluster does NOT consume a rollout slot" , func (ctx SpecContext ) {
1019+ cluster := createCluster (apiv1 .PrimaryUpdateStrategySupervised )
1020+ podList := buildPodListWithPrimaryNeedingRollout (cluster )
1021+
1022+ restarted , err := reconciler .rolloutRequiredInstances (ctx , cluster , podList )
1023+ Expect (err ).ToNot (HaveOccurred ())
1024+ Expect (restarted ).To (BeTrue ())
1025+
1026+ // Verify the rollout slot was NOT consumed: a second cluster should still be allowed
1027+ secondCluster := k8client.ObjectKey {Namespace : namespace , Name : "other-cluster" }
1028+ result := rm .CoordinateRollout (secondCluster , "other-pod" )
1029+ Expect (result .RolloutAllowed ).To (BeTrue (),
1030+ "supervised strategy should not consume the rollout slot" )
1031+ })
1032+
1033+ It ("supervised cluster returns true and sets PhaseWaitingForUser" , func (ctx SpecContext ) {
1034+ cluster := createCluster (apiv1 .PrimaryUpdateStrategySupervised )
1035+ podList := buildPodListWithPrimaryNeedingRollout (cluster )
1036+
1037+ restarted , err := reconciler .rolloutRequiredInstances (ctx , cluster , podList )
1038+ Expect (err ).ToNot (HaveOccurred ())
1039+ Expect (restarted ).To (BeTrue ())
1040+
1041+ // Re-fetch the cluster to see the updated status
1042+ var updatedCluster apiv1.Cluster
1043+ Expect (k8sClient .Get (ctx ,
1044+ k8client .ObjectKeyFromObject (cluster ),
1045+ & updatedCluster )).To (Succeed ())
1046+ Expect (updatedCluster .Status .Phase ).To (Equal (apiv1 .PhaseWaitingForUser ))
1047+ })
1048+
1049+ It ("unsupervised cluster DOES consume a rollout slot" , func (ctx SpecContext ) {
1050+ cluster := createCluster (apiv1 .PrimaryUpdateStrategyUnsupervised )
1051+ podList := buildPodListWithPrimaryNeedingRollout (cluster )
1052+
1053+ // Create the pod in the fake client so upgradePod (Delete) can find it
1054+ pod := podList .Items [0 ].Pod .DeepCopy ()
1055+ Expect (k8sClient .Create (ctx , pod )).To (Succeed ())
1056+
1057+ restarted , err := reconciler .rolloutRequiredInstances (ctx , cluster , podList )
1058+ Expect (err ).ToNot (HaveOccurred ())
1059+ Expect (restarted ).To (BeTrue ())
1060+
1061+ // Verify the rollout slot WAS consumed: a second cluster should be blocked
1062+ secondCluster := k8client.ObjectKey {Namespace : namespace , Name : "other-cluster" }
1063+ result := rm .CoordinateRollout (secondCluster , "other-pod" )
1064+ Expect (result .RolloutAllowed ).To (BeFalse (),
1065+ "unsupervised strategy should consume the rollout slot" )
1066+ })
1067+ })
0 commit comments