@@ -5,9 +5,14 @@ import (
55 "context"
66 "strconv"
77 "strings"
8+ "time"
89
10+ "github.com/percona/percona-server-mongodb-operator/pkg/naming"
911 "github.com/pkg/errors"
1012 corev1 "k8s.io/api/core/v1"
13+ "k8s.io/apimachinery/pkg/util/wait"
14+ "k8s.io/client-go/util/retry"
15+ logf "sigs.k8s.io/controller-runtime/pkg/log"
1116
1217 "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/config"
1318)
@@ -25,6 +30,23 @@ func (r *ReconcilePerconaServerMongoDB) getPVCUsageFromMetrics(
2530 pod * corev1.Pod ,
2631 pvcName string ,
2732) (* PVCUsage , error ) {
33+ log := logf .FromContext (ctx ).WithName ("StorageAutoscaling" ).WithValues ("pvc" , pvcName )
34+
35+ if pod == nil {
36+ return nil , errors .New ("pod is nil" )
37+ }
38+
39+ if ! isContainerAndPodRunning (* pod , naming .ComponentMongod ) {
40+ log .V (1 ).Info ("skipping PVC metrics check: container and pod not running" , "phase" , pod .Status .Phase )
41+ return nil , nil
42+ }
43+
44+ backoff := wait.Backoff {
45+ Steps : 5 ,
46+ Duration : 5 * time .Second ,
47+ Factor : 2.0 ,
48+ }
49+
2850 // Execute df command in the mongod container to get disk usage
2951 // df -B1 /data/db outputs in bytes
3052 // Example output:
@@ -33,9 +55,18 @@ func (r *ReconcilePerconaServerMongoDB) getPVCUsageFromMetrics(
3355 var stdout , stderr bytes.Buffer
3456 command := []string {"df" , "-B1" , config .MongodContainerDataDir }
3557
36- err := r .clientcmd .Exec (ctx , pod , "mongod" , command , nil , & stdout , & stderr , false )
58+ err := retry .OnError (backoff , func (err error ) bool { return true }, func () error {
59+ stdout .Reset ()
60+ stderr .Reset ()
61+
62+ err := r .clientcmd .Exec (ctx , pod , naming .ComponentMongod , command , nil , & stdout , & stderr , false )
63+ if err != nil {
64+ return errors .Wrapf (err , "failed to execute df in pod %s: %s" , pod .Name , stderr .String ())
65+ }
66+ return nil
67+ })
3768 if err != nil {
38- return nil , errors .Wrapf (err , "failed to execute df in pod %s: %s" , pod . Name , stderr . String () )
69+ return nil , errors .Wrap (err , "wait for df execution" )
3970 }
4071
4172 lines := strings .Split (strings .TrimSpace (stdout .String ()), "\n " )
0 commit comments