Skip to content

Commit

Permalink
fix(backup): modify updating backup target from ConfigMap
Browse files Browse the repository at this point in the history
And add missing changes when fixing 7159.

ref: longhorn/longhorn 5411, 7159, 10043

Signed-off-by: James Lu <[email protected]>
  • Loading branch information
mantissahz committed Dec 26, 2024
1 parent 9ff2b0a commit 17bec8c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
20 changes: 17 additions & 3 deletions datastore/longhorn.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ func (s *DataStore) syncDefaultBackupTargetResourceWithCustomizedSettings(custom
PollInterval: metav1.Duration{Duration: types.DefaultBackupstorePollInterval},
},
}
backupTargetURL := customizedDefaultBackupTargetSettings[string(types.SettingNameBackupTarget)]
backupTargetCredentialSecret := customizedDefaultBackupTargetSettings[string(types.SettingNameBackupTargetCredentialSecret)]
pollIntervalStr := customizedDefaultBackupTargetSettings[string(types.SettingNameBackupstorePollInterval)]
backupTargetURL, urlExists := customizedDefaultBackupTargetSettings[string(types.SettingNameBackupTarget)]
backupTargetCredentialSecret, secretExists := customizedDefaultBackupTargetSettings[string(types.SettingNameBackupTargetCredentialSecret)]
pollIntervalStr, pollInterExists := customizedDefaultBackupTargetSettings[string(types.SettingNameBackupstorePollInterval)]

if backupTargetURL != "" {
backupTarget.Spec.BackupTargetURL = backupTargetURL
Expand Down Expand Up @@ -255,6 +255,20 @@ func (s *DataStore) syncDefaultBackupTargetResourceWithCustomizedSettings(custom
return err
}

// If the settings are not in the longhorn-default-setting ConfigMap, use the existing values.
if !urlExists {
backupTarget.Spec.BackupTargetURL = existingBackupTarget.Spec.BackupTargetURL
}
if !secretExists {
backupTarget.Spec.CredentialSecret = existingBackupTarget.Spec.CredentialSecret
}
if !pollInterExists {
backupTarget.Spec.PollInterval = existingBackupTarget.Spec.PollInterval
}
syncTime := metav1.Time{Time: time.Now().UTC()}
backupTarget.Spec.SyncRequestedAt = syncTime
existingBackupTarget.Spec.SyncRequestedAt = syncTime

if reflect.DeepEqual(existingBackupTarget.Spec, backupTarget.Spec) {
return nil // No changes, no need to update
}
Expand Down
11 changes: 11 additions & 0 deletions webhook/resources/backuptarget/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ func (b *backupTargetValidator) validateCredentialSecret(secretName string) erro
for _, checkKey := range checkKeyList {
if value, ok := secret.Data[checkKey]; ok {
if strings.TrimSpace(string(value)) != string(value) {
//ref: longhorn/longhorn#7159: appropriate warning messages for credential data
switch {
case strings.TrimLeft(string(value), " ") != string(value):
return fmt.Errorf("invalid leading white space in %s", checkKey)
case strings.TrimRight(string(value), " ") != string(value):
return fmt.Errorf("invalid trailing white space in %s", checkKey)
case strings.TrimLeft(string(value), "\n") != string(value):
return fmt.Errorf("invalid leading new line in %s", checkKey)
case strings.TrimRight(string(value), "\n") != string(value):
return fmt.Errorf("invalid trailing new line in %s", checkKey)
}
return fmt.Errorf("there is space or new line in %s", checkKey)
}
}
Expand Down
5 changes: 5 additions & 0 deletions webhook/resources/systembackup/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ func (v *systemBackupValidator) Create(request *admission.Request, newObj runtim
return werror.NewBadRequest(fmt.Sprintf("cannot access %s without credential secret", backupType))
}
}

if !backupTarget.Status.Available {
return werror.NewBadRequest("default backup target is not available")
}

return nil
}

0 comments on commit 17bec8c

Please sign in to comment.