|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package backupcontroller |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + |
| 11 | + strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1" |
| 12 | +) |
| 13 | + |
| 14 | +// cnpgArtifactSizeBytes sums the objects barman-cloud uploaded for one base |
| 15 | +// backup, <destinationPath>/<serverName>/base/<backupID>/. The WAL archive |
| 16 | +// is not counted: it is one continuous stream shared by every base backup of |
| 17 | +// the server, so no part of it belongs to a single Backup. Returns 0 - |
| 18 | +// leaving Artifact.SizeBytes absent - when the backup ID is unknown, the |
| 19 | +// bucket cannot be read, or nothing is stored under the prefix. |
| 20 | +func (r *BackupJobReconciler) cnpgArtifactSizeBytes( |
| 21 | + ctx context.Context, |
| 22 | + namespace string, |
| 23 | + store *strategyv1alpha1.BarmanObjectStoreTemplate, |
| 24 | + serverName, backupID string, |
| 25 | +) int64 { |
| 26 | + logger := getLogger(ctx) |
| 27 | + if backupID == "" { |
| 28 | + return 0 |
| 29 | + } |
| 30 | + ctx, cancel := context.WithTimeout(ctx, artifactSizeProbeTimeout) |
| 31 | + defer cancel() |
| 32 | + |
| 33 | + bucket, path, err := parseBarmanS3DestinationPath(store.DestinationPath) |
| 34 | + if err != nil { |
| 35 | + logger.Debug("cannot measure CNPG backup size", "backupID", backupID, "error", err) |
| 36 | + return 0 |
| 37 | + } |
| 38 | + conn, err := r.cnpgS3Connection(ctx, namespace, store) |
| 39 | + if err != nil { |
| 40 | + logger.Debug("cannot resolve S3 credentials to measure CNPG backup size", "backupID", backupID, "error", err) |
| 41 | + return 0 |
| 42 | + } |
| 43 | + lister, err := r.s3Lister(conn) |
| 44 | + if err != nil { |
| 45 | + logger.Debug("cannot build S3 client to measure CNPG backup size", "backupID", backupID, "error", err) |
| 46 | + return 0 |
| 47 | + } |
| 48 | + prefix := barmanBaseBackupPrefix(path, serverName, backupID) |
| 49 | + objects, err := lister.ListObjects(ctx, bucket, prefix) |
| 50 | + if err != nil { |
| 51 | + logger.Debug("cannot list CNPG base backup objects to measure size", "backupID", backupID, "error", err) |
| 52 | + return 0 |
| 53 | + } |
| 54 | + var total int64 |
| 55 | + for _, obj := range objects { |
| 56 | + total += obj.Size |
| 57 | + } |
| 58 | + if total == 0 { |
| 59 | + logger.Debug("no CNPG base backup objects found; leaving size unset", "backupID", backupID, "prefix", prefix) |
| 60 | + } |
| 61 | + return total |
| 62 | +} |
| 63 | + |
| 64 | +func (r *BackupJobReconciler) cnpgS3Connection(ctx context.Context, namespace string, store *strategyv1alpha1.BarmanObjectStoreTemplate) (s3Connection, error) { |
| 65 | + var conn s3Connection |
| 66 | + if store.S3Credentials == nil { |
| 67 | + return conn, errors.New("barmanObjectStore has no s3Credentials") |
| 68 | + } |
| 69 | + if store.EndpointURL == "" { |
| 70 | + conn.Endpoint, conn.Secure = "s3.amazonaws.com", true |
| 71 | + } else { |
| 72 | + u, err := url.Parse(store.EndpointURL) |
| 73 | + if err != nil || u.Host == "" { |
| 74 | + return conn, fmt.Errorf("invalid endpointURL %q", store.EndpointURL) |
| 75 | + } |
| 76 | + conn.Endpoint, conn.Secure = u.Host, u.Scheme == "https" |
| 77 | + } |
| 78 | + |
| 79 | + creds := store.S3Credentials |
| 80 | + accessKeyKey, secretKeyKey := creds.AccessKeyIDKey, creds.SecretAccessKeyKey |
| 81 | + if accessKeyKey == "" { |
| 82 | + accessKeyKey = defaultS3AccessKeyIDKey |
| 83 | + } |
| 84 | + if secretKeyKey == "" { |
| 85 | + secretKeyKey = defaultS3SecretAccessKeyKey |
| 86 | + } |
| 87 | + var err error |
| 88 | + if conn.AccessKey, err = r.secretValue(ctx, namespace, creds.SecretRef.Name, accessKeyKey); err != nil { |
| 89 | + return conn, err |
| 90 | + } |
| 91 | + if conn.SecretKey, err = r.secretValue(ctx, namespace, creds.SecretRef.Name, secretKeyKey); err != nil { |
| 92 | + return conn, err |
| 93 | + } |
| 94 | + if ca := store.EndpointCA; ca != nil && conn.Secure { |
| 95 | + key := ca.Key |
| 96 | + if key == "" { |
| 97 | + key = defaultEndpointCAKey |
| 98 | + } |
| 99 | + pem, err := r.secretValue(ctx, namespace, ca.SecretRef.Name, key) |
| 100 | + if err != nil { |
| 101 | + return conn, err |
| 102 | + } |
| 103 | + conn.CAPEM = []byte(pem) |
| 104 | + } |
| 105 | + return conn, nil |
| 106 | +} |
| 107 | + |
| 108 | +// parseBarmanS3DestinationPath splits s3://bucket/path into the bucket and |
| 109 | +// the key path barman-cloud prefixes every object with. |
| 110 | +func parseBarmanS3DestinationPath(destinationPath string) (bucket, path string, err error) { |
| 111 | + u, err := url.Parse(destinationPath) |
| 112 | + if err != nil || u.Scheme != "s3" || u.Host == "" { |
| 113 | + return "", "", fmt.Errorf("destinationPath %q is not an s3:// URL", destinationPath) |
| 114 | + } |
| 115 | + return u.Host, strings.Trim(u.Path, "/"), nil |
| 116 | +} |
| 117 | + |
| 118 | +// barmanBaseBackupPrefix mirrors barman-cloud's |
| 119 | +// os.path.join(path, server_name, "base", backup_id), with a trailing slash |
| 120 | +// so one backup ID never matches another that extends it. |
| 121 | +func barmanBaseBackupPrefix(path, serverName, backupID string) string { |
| 122 | + parts := make([]string, 0, 4) |
| 123 | + if path != "" { |
| 124 | + parts = append(parts, path) |
| 125 | + } |
| 126 | + parts = append(parts, serverName, "base", backupID) |
| 127 | + return strings.Join(parts, "/") + "/" |
| 128 | +} |
0 commit comments