Skip to content

Commit 23ac001

Browse files
committed
fix(backups): report sizeBytes for CNPG backup artifacts
CNPG backups arrived without Backup.status.artifact.sizeBytes: cnpg.io/Backup status carries no size, and the data lives in barman's object-store layout rather than in a single object. barman-cloud uploads every base backup under <destinationPath>/<serverName>/base/<backupID>/ (backup.info plus the data and tablespace tarballs, possibly split into parts). On completion the driver now lists that prefix with the credentials and endpoint CA of the rendered barmanObjectStore and records the total. The WAL archive is not counted: it is one stream shared by every base backup of the server. The field stays unset when the backup ID is unknown, the bucket cannot be read, or nothing is stored under the prefix. The S3 client and Secret lookup are now shared with the MariaDB driver. Refs #4316 Assisted-by: LLM Signed-off-by: Andrey Kolkov <[email protected]>
1 parent 9edcd3d commit 23ac001

8 files changed

Lines changed: 564 additions & 144 deletions
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package backupcontroller
3+
4+
import (
5+
"context"
6+
"crypto/tls"
7+
"crypto/x509"
8+
"errors"
9+
"fmt"
10+
"net/http"
11+
"time"
12+
13+
"github.com/minio/minio-go/v7"
14+
"github.com/minio/minio-go/v7/pkg/credentials"
15+
corev1 "k8s.io/api/core/v1"
16+
"k8s.io/apimachinery/pkg/types"
17+
)
18+
19+
// artifactSizeProbeTimeout bounds the object-store round trips a driver
20+
// makes to measure Backup.status.artifact.sizeBytes; the probe runs inside
21+
// the reconcile that creates the Backup.
22+
const artifactSizeProbeTimeout = 30 * time.Second
23+
24+
type s3ObjectInfo struct {
25+
Key string
26+
Size int64
27+
}
28+
29+
type s3ObjectLister interface {
30+
ListObjects(ctx context.Context, bucket, prefix string) ([]s3ObjectInfo, error)
31+
}
32+
33+
// s3Connection is what a driver resolves from its rendered strategy to
34+
// read the bucket with the same identity the backup was written with.
35+
type s3Connection struct {
36+
// Endpoint is host[:port], without a scheme.
37+
Endpoint string
38+
Secure bool
39+
Region string
40+
AccessKey string
41+
SecretKey string
42+
SessionToken string
43+
// CAPEM is trusted on top of the system pool when set.
44+
CAPEM []byte
45+
}
46+
47+
func (r *BackupJobReconciler) s3Lister(conn s3Connection) (s3ObjectLister, error) {
48+
if r.newS3ObjectLister != nil {
49+
return r.newS3ObjectLister(conn)
50+
}
51+
return newMinioObjectLister(conn)
52+
}
53+
54+
type minioObjectLister struct {
55+
client *minio.Client
56+
}
57+
58+
func newMinioObjectLister(conn s3Connection) (s3ObjectLister, error) {
59+
opts := &minio.Options{
60+
Creds: credentials.NewStaticV4(conn.AccessKey, conn.SecretKey, conn.SessionToken),
61+
Region: conn.Region,
62+
Secure: conn.Secure,
63+
}
64+
if conn.Secure && len(conn.CAPEM) > 0 {
65+
pool, err := x509.SystemCertPool()
66+
if err != nil {
67+
pool = x509.NewCertPool()
68+
}
69+
if !pool.AppendCertsFromPEM(conn.CAPEM) {
70+
return nil, errors.New("no PEM certificates in the endpoint CA bundle")
71+
}
72+
opts.Transport = &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}}
73+
}
74+
client, err := minio.New(conn.Endpoint, opts)
75+
if err != nil {
76+
return nil, err
77+
}
78+
return &minioObjectLister{client: client}, nil
79+
}
80+
81+
func (l *minioObjectLister) ListObjects(ctx context.Context, bucket, prefix string) ([]s3ObjectInfo, error) {
82+
var out []s3ObjectInfo
83+
for obj := range l.client.ListObjects(ctx, bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
84+
if obj.Err != nil {
85+
return nil, obj.Err
86+
}
87+
out = append(out, s3ObjectInfo{Key: obj.Key, Size: obj.Size})
88+
}
89+
return out, nil
90+
}
91+
92+
func (r *BackupJobReconciler) secretValue(ctx context.Context, namespace, name, key string) (string, error) {
93+
secret := &corev1.Secret{}
94+
if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, secret); err != nil {
95+
return "", err
96+
}
97+
v, ok := secret.Data[key]
98+
if !ok {
99+
return "", fmt.Errorf("Secret %s/%s has no key %q", namespace, name, key)
100+
}
101+
return string(v), nil
102+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package backupcontroller
3+
4+
import (
5+
"context"
6+
"testing"
7+
8+
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
)
12+
13+
type fakeS3Lister struct {
14+
objects []s3ObjectInfo
15+
err error
16+
17+
conn s3Connection
18+
bucket, prefix string
19+
}
20+
21+
func (f *fakeS3Lister) ListObjects(_ context.Context, bucket, prefix string) ([]s3ObjectInfo, error) {
22+
f.bucket, f.prefix = bucket, prefix
23+
return f.objects, f.err
24+
}
25+
26+
func s3CredsSecret(namespace, name string) *corev1.Secret {
27+
return &corev1.Secret{
28+
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name},
29+
Data: map[string][]byte{
30+
"AWS_ACCESS_KEY_ID": []byte("AKID"),
31+
"AWS_SECRET_ACCESS_KEY": []byte("secret"),
32+
},
33+
}
34+
}
35+
36+
// reconcilerWithLister returns a reconciler whose S3 reads go to l, backed
37+
// by a fake client seeded with objs (typically the credentials Secret).
38+
func reconcilerWithLister(t *testing.T, l *fakeS3Lister, objs ...client.Object) *BackupJobReconciler {
39+
t.Helper()
40+
c := newMariaDBStrategyTestClient(t, objs...)
41+
return &BackupJobReconciler{
42+
Client: c,
43+
Scheme: c.Scheme(),
44+
newS3ObjectLister: func(conn s3Connection) (s3ObjectLister, error) {
45+
l.conn = conn
46+
return l, nil
47+
},
48+
}
49+
}

‎internal/backupcontroller/backupjob_controller.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type BackupJobReconciler struct {
4242

4343
// newS3ObjectLister overrides the S3 client used to measure artifact
4444
// sizes; nil means a real minio client. Set by unit tests.
45-
newS3ObjectLister func(ctx context.Context, namespace string, s3 *strategyv1alpha1.MariaDBS3Template) (s3ObjectLister, error)
45+
newS3ObjectLister func(conn s3Connection) (s3ObjectLister, error)
4646
}
4747

4848
func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)