Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 9905c8d

Browse files
committed
Retry s3 connection multiple times before failing
1 parent 086cee8 commit 9905c8d

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

operator/controllers/execution/scan_controller.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,7 @@ func (r *ScanReconciler) ensureServiceAccountExists(namespace, serviceAccountNam
747747
return nil
748748
}
749749

750-
// SetupWithManager sets up the controller and initializes every thing it needs
751-
func (r *ScanReconciler) SetupWithManager(mgr ctrl.Manager) error {
750+
func (r *ScanReconciler) initS3Connection() *minio.Client {
752751
endpoint := os.Getenv("S3_ENDPOINT")
753752
accessKeyID := os.Getenv("S3_ACCESS_KEY")
754753
secretAccessKey := os.Getenv("S3_SECRET_KEY")
@@ -763,20 +762,36 @@ func (r *ScanReconciler) SetupWithManager(mgr ctrl.Manager) error {
763762
}
764763

765764
// Initialize minio client object.
766-
minioClient, err := minio.New(fmt.Sprintf("%s:%s", endpoint, port), accessKeyID, secretAccessKey, useSSL)
767-
if err != nil {
768-
r.Log.Error(err, "Could not create minio client to communicate with s3 or compatible storage provider")
769-
panic(err)
765+
var minioClient *minio.Client
766+
const maxRetries = 5
767+
for i := 0; i < 5; i++ {
768+
client, err := minio.New(fmt.Sprintf("%s:%s", endpoint, port), accessKeyID, secretAccessKey, useSSL)
769+
if err != nil && i < maxRetries-1 {
770+
r.Log.Info("Could not create minio client to communicate to s3 endpoint", "retiresLeft", maxRetries-i)
771+
time.Sleep(5 * time.Second)
772+
} else if err != nil {
773+
r.Log.Error(err, "S3 Client init failed repeatedly. Process will exit.")
774+
panic(err)
775+
} else {
776+
minioClient = client
777+
}
770778
}
771-
r.MinioClient = *minioClient
779+
772780
bucketName := os.Getenv("S3_BUCKET")
773781

774-
bucketExists, err := r.MinioClient.BucketExists(bucketName)
782+
bucketExists, err := minioClient.BucketExists(bucketName)
775783
if err != nil || bucketExists == false {
776784
r.Log.Error(err, "Could not communicate with s3 or compatible storage provider")
777785
panic(err)
778786
}
779787

788+
return minioClient
789+
}
790+
791+
// SetupWithManager sets up the controller and initializes every thing it needs
792+
func (r *ScanReconciler) SetupWithManager(mgr ctrl.Manager) error {
793+
r.MinioClient = *r.initS3Connection()
794+
780795
// Todo: Better config management
781796

782797
if err := mgr.GetFieldIndexer().IndexField(&batch.Job{}, ownerKey, func(rawObj runtime.Object) []string {

0 commit comments

Comments
 (0)