@@ -929,30 +929,41 @@ func CreateCluster(request *msgs.CreateClusterRequest, ns, pgouser string) msgs.
929929 // the deployment template always tries to mount /sshd volume
930930 secretName := fmt .Sprintf ("%s-%s" , clusterName , config .LABEL_BACKREST_REPO_SECRET )
931931
932- if _ , err := apiserver .Clientset .
933- CoreV1 ().Secrets (request .Namespace ).
934- Get (ctx , secretName , metav1.GetOptions {}); kubeapi .IsNotFound (err ) {
935- // determine if a custom CA secret should be used
936- backrestS3CACert := []byte {}
937-
938- if request .BackrestS3CASecretName != "" {
939- backrestSecret , err := apiserver .Clientset .
940- CoreV1 ().Secrets (request .Namespace ).
941- Get (ctx , request .BackrestS3CASecretName , metav1.GetOptions {})
942- if err != nil {
943- log .Error (err )
944- resp .Status .Code = msgs .Error
945- resp .Status .Msg = fmt .Sprintf ("Error finding pgBackRest S3 CA secret \" %s\" : %s" ,
946- request .BackrestS3CASecretName , err .Error ())
947- return resp
948- }
932+ // determine if a custom CA secret should be used
933+ backrestS3CACert := []byte {}
949934
950- // attempt to retrieves the custom CA, assuming it has the name
951- // "aws-s3-ca.crt"
952- backrestS3CACert = backrestSecret .Data [util .BackRestRepoSecretKeyAWSS3KeyAWSS3CACert ]
935+ if request .BackrestS3CASecretName != "" {
936+ backrestSecret , err := apiserver .Clientset .
937+ CoreV1 ().Secrets (request .Namespace ).
938+ Get (ctx , request .BackrestS3CASecretName , metav1.GetOptions {})
939+ if err != nil {
940+ log .Error (err )
941+ resp .Status .Code = msgs .Error
942+ resp .Status .Msg = fmt .Sprintf ("Error finding pgBackRest S3 CA secret \" %s\" : %s" ,
943+ request .BackrestS3CASecretName , err .Error ())
944+ return resp
953945 }
954946
955- // set up the secret for the cluster that contains the pgBackRest
947+ // attempt to retrieves the custom CA, assuming it has the name
948+ // "aws-s3-ca.crt"
949+ backrestS3CACert = backrestSecret .Data [util .BackRestRepoSecretKeyAWSS3KeyAWSS3CACert ]
950+ }
951+
952+ // save the S3 credentials in a single map so it can be used to either create a new
953+ // secret or update an existing one
954+ s3Credentials := map [string ][]byte {
955+ util .BackRestRepoSecretKeyAWSS3KeyAWSS3CACert : backrestS3CACert ,
956+ util .BackRestRepoSecretKeyAWSS3KeyAWSS3Key : []byte (request .BackrestS3Key ),
957+ util .BackRestRepoSecretKeyAWSS3KeyAWSS3KeySecret : []byte (request .BackrestS3KeySecret ),
958+ }
959+
960+ _ , err = apiserver .Clientset .CoreV1 ().Secrets (request .Namespace ).
961+ Get (ctx , secretName , metav1.GetOptions {})
962+
963+ switch {
964+ case kubeapi .IsNotFound (err ):
965+ // The pgBackRest repo config secret was not found, create it.
966+ // Set up the secret for the cluster that contains the pgBackRest
956967 // information
957968 secret := & v1.Secret {
958969 ObjectMeta : metav1.ObjectMeta {
@@ -963,22 +974,30 @@ func CreateCluster(request *msgs.CreateClusterRequest, ns, pgouser string) msgs.
963974 config .LABEL_PGO_BACKREST_REPO : "true" ,
964975 },
965976 },
966- Data : map [string ][]byte {
967- util .BackRestRepoSecretKeyAWSS3KeyAWSS3CACert : backrestS3CACert ,
968- util .BackRestRepoSecretKeyAWSS3KeyAWSS3Key : []byte (request .BackrestS3Key ),
969- util .BackRestRepoSecretKeyAWSS3KeyAWSS3KeySecret : []byte (request .BackrestS3KeySecret ),
970- },
977+ Data : s3Credentials ,
971978 }
972979
973980 if _ , err := apiserver .Clientset .CoreV1 ().Secrets (ns ).Create (ctx , secret , metav1.CreateOptions {}); err != nil && ! kubeapi .IsAlreadyExists (err ) {
974981 resp .Status .Code = msgs .Error
975982 resp .Status .Msg = fmt .Sprintf ("could not create backrest repo secret: %s" , err )
976983 return resp
977984 }
978- } else if err != nil {
985+
986+ case err != nil :
987+ // An error occurred other than 'not found'. Log the error received when
988+ // attempting to get the pgBackRest repo config secret, then return.
979989 resp .Status .Code = msgs .Error
980990 resp .Status .Msg = fmt .Sprintf ("could not query if backrest repo secret exits: %s" , err )
981991 return resp
992+ default :
993+ // the pgBackRest repo config secret already exists, update any provided
994+ // S3 credential information
995+ err = updateRepoSecret (apiserver .Clientset , secretName , request .Namespace , s3Credentials )
996+ if err != nil {
997+ resp .Status .Code = msgs .Error
998+ resp .Status .Msg = fmt .Sprintf ("could not update backrest repo secret: %s" , err )
999+ return resp
1000+ }
9821001 }
9831002
9841003 // create a workflow for this new cluster
@@ -1011,6 +1030,30 @@ func CreateCluster(request *msgs.CreateClusterRequest, ns, pgouser string) msgs.
10111030 return resp
10121031}
10131032
1033+ // updateRepoSecret updates the existing pgBackRest repo config secret with any
1034+ // provided S3/GCS connection information.
1035+ func updateRepoSecret (clientset kubernetes.Interface , secretName ,
1036+ namespace string , connectionInfo map [string ][]byte ) error {
1037+ ctx := context .TODO ()
1038+
1039+ // Get the secret
1040+ secret , err := clientset .CoreV1 ().Secrets (namespace ).
1041+ Get (ctx , secretName , metav1.GetOptions {})
1042+ // The secret should already exist at this point. If there is any error,
1043+ // return.
1044+ if err != nil {
1045+ return err
1046+ }
1047+
1048+ // update the secret data
1049+ for k , v := range connectionInfo {
1050+ secret .Data [k ] = v
1051+ }
1052+ _ , err = clientset .CoreV1 ().Secrets (secret .Namespace ).Update (ctx , secret ,
1053+ metav1.UpdateOptions {})
1054+ return err
1055+ }
1056+
10141057func validateConfigPolicies (clusterName , PoliciesFlag , ns string ) error {
10151058 ctx := context .TODO ()
10161059 var err error
0 commit comments