Skip to content

Commit 94a5361

Browse files
tjmoore4andrewlecuyer
authored andcommitted
Allow backup config change when recreated cluster
Previously, when attempting to change the backup configuration on an existing cluster, stanza-create job will error out due to missing S3 configuration parameters despite being provided as part of the pgo create cluster command. This update corrects that issue by ensuring the existing pgBackRest repo config secret is updated to include the necessary configuration values.
1 parent a324d0e commit 94a5361

File tree

1 file changed

+81
-38
lines changed

1 file changed

+81
-38
lines changed

internal/apiserver/clusterservice/clusterimpl.go

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -940,43 +940,55 @@ func CreateCluster(request *msgs.CreateClusterRequest, ns, pgouser string) msgs.
940940
// the deployment template always tries to mount /sshd volume
941941
secretName := fmt.Sprintf("%s-%s", clusterName, config.LABEL_BACKREST_REPO_SECRET)
942942

943-
if _, err := apiserver.Clientset.
944-
CoreV1().Secrets(request.Namespace).
945-
Get(ctx, secretName, metav1.GetOptions{}); kubeapi.IsNotFound(err) {
946-
// determine if a custom CA secret should be used
947-
backrestS3CACert := []byte{}
948-
949-
if request.BackrestS3CASecretName != "" {
950-
backrestSecret, err := apiserver.Clientset.
951-
CoreV1().Secrets(request.Namespace).
952-
Get(ctx, request.BackrestS3CASecretName, metav1.GetOptions{})
953-
if err != nil {
954-
log.Error(err)
955-
resp.Status.Code = msgs.Error
956-
resp.Status.Msg = fmt.Sprintf("Error finding pgBackRest S3 CA secret \"%s\": %s",
957-
request.BackrestS3CASecretName, err.Error())
958-
return resp
959-
}
943+
// determine if a custom CA secret should be used
944+
backrestS3CACert := []byte{}
960945

961-
// attempt to retrieves the custom CA, assuming it has the name
962-
// "aws-s3-ca.crt"
963-
backrestS3CACert = backrestSecret.Data[util.BackRestRepoSecretKeyAWSS3KeyAWSS3CACert]
946+
if request.BackrestS3CASecretName != "" {
947+
backrestSecret, err := apiserver.Clientset.
948+
CoreV1().Secrets(request.Namespace).
949+
Get(ctx, request.BackrestS3CASecretName, metav1.GetOptions{})
950+
if err != nil {
951+
log.Error(err)
952+
resp.Status.Code = msgs.Error
953+
resp.Status.Msg = fmt.Sprintf("Error finding pgBackRest S3 CA secret \"%s\": %s",
954+
request.BackrestS3CASecretName, err.Error())
955+
return resp
964956
}
965957

966-
// if a GCS key is provided, we need to base64 decode it
967-
backrestGCSKey := []byte{}
968-
if request.BackrestGCSKey != "" {
969-
// try to decode the string
970-
backrestGCSKey, err = base64.StdEncoding.DecodeString(request.BackrestGCSKey)
958+
// attempt to retrieves the custom CA, assuming it has the name
959+
// "aws-s3-ca.crt"
960+
backrestS3CACert = backrestSecret.Data[util.BackRestRepoSecretKeyAWSS3KeyAWSS3CACert]
961+
}
971962

972-
if err != nil {
973-
resp.Status.Code = msgs.Error
974-
resp.Status.Msg = fmt.Sprintf("could not decode GCS key: %s", err.Error())
975-
return resp
976-
}
963+
// if a GCS key is provided, we need to base64 decode it
964+
backrestGCSKey := []byte{}
965+
if request.BackrestGCSKey != "" {
966+
// try to decode the string
967+
backrestGCSKey, err = base64.StdEncoding.DecodeString(request.BackrestGCSKey)
968+
969+
if err != nil {
970+
resp.Status.Code = msgs.Error
971+
resp.Status.Msg = fmt.Sprintf("could not decode GCS key: %s", err.Error())
972+
return resp
977973
}
974+
}
975+
976+
// save the S3 credentials in a single map so it can be used to either create a new
977+
// secret or update an existing one
978+
s3Credentials := map[string][]byte{
979+
util.BackRestRepoSecretKeyAWSS3KeyAWSS3CACert: backrestS3CACert,
980+
util.BackRestRepoSecretKeyAWSS3KeyAWSS3Key: []byte(request.BackrestS3Key),
981+
util.BackRestRepoSecretKeyAWSS3KeyAWSS3KeySecret: []byte(request.BackrestS3KeySecret),
982+
util.BackRestRepoSecretKeyAWSS3KeyGCSKey: backrestGCSKey,
983+
}
978984

979-
// set up the secret for the cluster that contains the pgBackRest
985+
_, err = apiserver.Clientset.CoreV1().Secrets(request.Namespace).
986+
Get(ctx, secretName, metav1.GetOptions{})
987+
988+
switch {
989+
case kubeapi.IsNotFound(err):
990+
// The pgBackRest repo config secret was not found, create it.
991+
// Set up the secret for the cluster that contains the pgBackRest
980992
// information
981993
secret := &v1.Secret{
982994
ObjectMeta: metav1.ObjectMeta{
@@ -987,12 +999,7 @@ func CreateCluster(request *msgs.CreateClusterRequest, ns, pgouser string) msgs.
987999
config.LABEL_PGO_BACKREST_REPO: "true",
9881000
},
9891001
},
990-
Data: map[string][]byte{
991-
util.BackRestRepoSecretKeyAWSS3KeyAWSS3CACert: backrestS3CACert,
992-
util.BackRestRepoSecretKeyAWSS3KeyAWSS3Key: []byte(request.BackrestS3Key),
993-
util.BackRestRepoSecretKeyAWSS3KeyAWSS3KeySecret: []byte(request.BackrestS3KeySecret),
994-
util.BackRestRepoSecretKeyAWSS3KeyGCSKey: backrestGCSKey,
995-
},
1002+
Data: s3Credentials,
9961003
}
9971004

9981005
for k, v := range util.GetCustomLabels(newInstance) {
@@ -1004,10 +1011,22 @@ func CreateCluster(request *msgs.CreateClusterRequest, ns, pgouser string) msgs.
10041011
resp.Status.Msg = fmt.Sprintf("could not create backrest repo secret: %s", err)
10051012
return resp
10061013
}
1007-
} else if err != nil {
1014+
1015+
case err != nil:
1016+
// An error occurred other than 'not found'. Log the error received when
1017+
// attempting to get the pgBackRest repo config secret, then return.
10081018
resp.Status.Code = msgs.Error
10091019
resp.Status.Msg = fmt.Sprintf("could not query if backrest repo secret exits: %s", err)
10101020
return resp
1021+
default:
1022+
// the pgBackRest repo config secret already exists, update any provided
1023+
// S3 credential information
1024+
err = updateRepoSecret(apiserver.Clientset, secretName, request.Namespace, s3Credentials)
1025+
if err != nil {
1026+
resp.Status.Code = msgs.Error
1027+
resp.Status.Msg = fmt.Sprintf("could not update backrest repo secret: %s", err)
1028+
return resp
1029+
}
10111030
}
10121031

10131032
// create a workflow for this new cluster
@@ -1040,6 +1059,30 @@ func CreateCluster(request *msgs.CreateClusterRequest, ns, pgouser string) msgs.
10401059
return resp
10411060
}
10421061

1062+
// updateRepoSecret updates the existing pgBackRest repo config secret with any
1063+
// provided S3/GCS connection information.
1064+
func updateRepoSecret(clientset kubernetes.Interface, secretName,
1065+
namespace string, connectionInfo map[string][]byte) error {
1066+
ctx := context.TODO()
1067+
1068+
// Get the secret
1069+
secret, err := clientset.CoreV1().Secrets(namespace).
1070+
Get(ctx, secretName, metav1.GetOptions{})
1071+
// The secret should already exist at this point. If there is any error,
1072+
// return.
1073+
if err != nil {
1074+
return err
1075+
}
1076+
1077+
// update the secret data
1078+
for k, v := range connectionInfo {
1079+
secret.Data[k] = v
1080+
}
1081+
_, err = clientset.CoreV1().Secrets(secret.Namespace).Update(ctx, secret,
1082+
metav1.UpdateOptions{})
1083+
return err
1084+
}
1085+
10431086
func validateConfigPolicies(clusterName, PoliciesFlag, ns string) error {
10441087
ctx := context.TODO()
10451088
var err error

0 commit comments

Comments
 (0)