Skip to content

Commit 096112e

Browse files
committed
Use Patch to Scale Deployments
When scaling cluster Deployments, and Patch operation is now used instead of an Update. This prevents potential conflicts from occurring when attempting to scale the Deployments for a cluster (e.g. the Deployments for PG pods and/or the pgBackRest repository).
1 parent 07e7c6f commit 096112e

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

internal/operator/cluster/clusterlogic.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
kerrors "k8s.io/apimachinery/pkg/api/errors"
4242
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4343
"k8s.io/apimachinery/pkg/fields"
44+
"k8s.io/apimachinery/pkg/types"
4445
"k8s.io/client-go/kubernetes"
4546
"k8s.io/client-go/rest"
4647
)
@@ -762,11 +763,12 @@ func ScaleClusterDeployments(clientset kubernetes.Interface, cluster crv1.Pgclus
762763
log.Debugf("scaling deployment %s to %d for cluster %s", deployment.Name, replicas,
763764
clusterName)
764765

765-
// Scale the deployment accoriding to the number of replicas specified. If an error is
766-
// encountered, log it and move on to scaling the next deployment.
767-
deployment.Spec.Replicas = kubeapi.Int32(int32(replicas))
768-
if _, err = clientset.AppsV1().Deployments(deployment.Namespace).Update(&deployment); err != nil {
769-
log.Errorf("Error scaling deployment %s to %d: %w", deployment.Name, replicas, err)
766+
// Scale the deployment according to the number of replicas specified. If an error is
767+
// encountered, log it and move on to scaling the next deployment
768+
patchString := fmt.Sprintf(`{"spec":{"replicas":%d}}`, replicas)
769+
if _, err := clientset.AppsV1().Deployments(namespace).Patch(deployment.GetName(),
770+
types.MergePatchType, []byte(patchString)); err != nil {
771+
log.Errorf("Error scaling deployment %s to %d: %v", deployment.Name, replicas, err)
770772
}
771773
}
772774
return

0 commit comments

Comments
 (0)