2020-07-14 21:47:00 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 the Velero contributors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-09-02 17:12:37 +00:00
|
|
|
package storage
|
2020-07-14 21:47:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-05 06:43:40 +00:00
|
|
|
"fmt"
|
2020-07-14 21:47:00 +00:00
|
|
|
"time"
|
|
|
|
|
2020-09-02 17:12:37 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-07-14 21:47:00 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
|
|
|
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
|
|
)
|
|
|
|
|
2020-09-02 17:12:37 +00:00
|
|
|
// DefaultBackupLocationInfo holds server default backup storage location information
|
|
|
|
type DefaultBackupLocationInfo struct {
|
2020-12-09 17:38:31 +00:00
|
|
|
// StorageLocation is the name of the backup storage location designated as the default on the server side.
|
|
|
|
// Deprecated TODO(2.0)
|
2020-09-02 17:12:37 +00:00
|
|
|
StorageLocation string
|
2020-12-09 17:38:31 +00:00
|
|
|
// ServerValidationFrequency is the server default validation frequency for all backup storage locations
|
|
|
|
ServerValidationFrequency time.Duration
|
2020-07-14 21:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsReadyToValidate calculates if a given backup storage location is ready to be validated.
|
|
|
|
//
|
|
|
|
// Rules:
|
Fix various typos found by codespell (#3057)
By running the following command:
codespell -S .git,*.png,*.jpg,*.woff,*.ttf,*.gif,*.ico -L \
iam,aks,ist,bridget,ue
Signed-off-by: Mateusz Gozdek <mgozdekof@gmail.com>
2020-11-10 16:48:35 +00:00
|
|
|
// Users can choose a validation frequency per location. This will override the server's default value
|
2020-07-14 21:47:00 +00:00
|
|
|
// To skip/stop validation, set the frequency to zero
|
|
|
|
// This will always return "true" for the first attempt at validating a location, regardless of its validation frequency setting
|
|
|
|
// Otherwise, it returns "ready" only when NOW is equal to or after the next validation time
|
|
|
|
// (next validation time: last validation time + validation frequency)
|
2020-12-09 17:38:31 +00:00
|
|
|
func IsReadyToValidate(bslValidationFrequency *metav1.Duration, lastValidationTime *metav1.Time, serverValidationFrequency time.Duration, log logrus.FieldLogger) bool {
|
|
|
|
validationFrequency := serverValidationFrequency
|
2020-07-14 21:47:00 +00:00
|
|
|
// If the bsl validation frequency is not specifically set, skip this block and continue, using the server's default
|
2020-09-02 17:12:37 +00:00
|
|
|
if bslValidationFrequency != nil {
|
|
|
|
validationFrequency = bslValidationFrequency.Duration
|
2020-07-14 21:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if validationFrequency < 0 {
|
2021-08-24 14:50:45 +00:00
|
|
|
log.Debugf("Validation period must be non-negative, changing from %d to %d", validationFrequency, serverValidationFrequency)
|
2020-12-09 17:38:31 +00:00
|
|
|
validationFrequency = serverValidationFrequency
|
2020-07-14 21:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 17:12:37 +00:00
|
|
|
lastValidation := lastValidationTime
|
2020-12-03 15:52:36 +00:00
|
|
|
if lastValidation == nil {
|
|
|
|
// Regardless of validation frequency, we want to validate all BSLs at least once.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if validationFrequency == 0 {
|
|
|
|
// Validation was disabled so return false.
|
|
|
|
log.Debug("Validation period for this backup location is set to 0, skipping validation")
|
|
|
|
return false
|
2020-07-14 21:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 15:52:36 +00:00
|
|
|
// We want to validate BSL only if the set validation frequency/ interval has elapsed.
|
|
|
|
nextValidation := lastValidation.Add(validationFrequency) // next validation time: last validation time + validation frequency
|
2023-01-19 08:39:38 +00:00
|
|
|
return !time.Now().UTC().Before(nextValidation) // ready only when NOW is equal to or after the next validation time
|
2020-07-14 21:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListBackupStorageLocations verifies if there are any backup storage locations.
|
|
|
|
// For all purposes, if either there is an error while attempting to fetch items or
|
|
|
|
// if there are no items an error would be returned since the functioning of the system
|
|
|
|
// would be haulted.
|
2020-09-02 17:12:37 +00:00
|
|
|
func ListBackupStorageLocations(ctx context.Context, kbClient client.Client, namespace string) (velerov1api.BackupStorageLocationList, error) {
|
2020-07-14 21:47:00 +00:00
|
|
|
var locations velerov1api.BackupStorageLocationList
|
|
|
|
if err := kbClient.List(ctx, &locations, &client.ListOptions{
|
|
|
|
Namespace: namespace,
|
|
|
|
}); err != nil {
|
|
|
|
return velerov1api.BackupStorageLocationList{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(locations.Items) == 0 {
|
|
|
|
return velerov1api.BackupStorageLocationList{}, errors.New("no backup storage locations found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return locations, nil
|
|
|
|
}
|
2023-09-05 06:43:40 +00:00
|
|
|
|
|
|
|
func GetDefaultBackupStorageLocations(ctx context.Context, kbClient client.Client, namespace string) (*velerov1api.BackupStorageLocationList, error) {
|
|
|
|
locations := new(velerov1api.BackupStorageLocationList)
|
|
|
|
defaultLocations := new(velerov1api.BackupStorageLocationList)
|
|
|
|
if err := kbClient.List(context.Background(), locations, &client.ListOptions{Namespace: namespace}); err != nil {
|
|
|
|
return defaultLocations, errors.Wrapf(err, fmt.Sprintf("failed to list backup storage locations in namespace %s", namespace))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, location := range locations.Items {
|
|
|
|
if location.Spec.Default {
|
|
|
|
defaultLocations.Items = append(defaultLocations.Items, location)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultLocations, nil
|
|
|
|
}
|