forked from percona/percona-server-mongodb-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbm.go
140 lines (124 loc) · 3.85 KB
/
pbm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package backup
import (
"context"
"fmt"
"strings"
"github.com/percona/percona-backup-mongodb/pbm"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
client "sigs.k8s.io/controller-runtime/pkg/client"
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb"
)
const (
agentContainerName = "backup-agent"
awsAccessKeySecretKey = "AWS_ACCESS_KEY_ID"
awsSecretAccessKeySecretKey = "AWS_SECRET_ACCESS_KEY"
)
type PBM struct {
C *pbm.PBM
k8c client.Client
namespace string
}
// NewPBM creates a new connection to PBM.
// It should be closed after the last use with.
func NewPBM(c client.Client, cluster *api.PerconaServerMongoDB) (*PBM, error) {
rs := cluster.Spec.Replsets[0]
pods := &corev1.PodList{}
err := c.List(context.TODO(),
&client.ListOptions{
Namespace: cluster.Namespace,
LabelSelector: labels.SelectorFromSet(map[string]string{
"app.kubernetes.io/name": "percona-server-mongodb",
"app.kubernetes.io/instance": cluster.Name,
"app.kubernetes.io/replset": rs.Name,
"app.kubernetes.io/managed-by": "percona-server-mongodb-operator",
"app.kubernetes.io/part-of": "percona-server-mongodb",
}),
},
pods,
)
if err != nil {
return nil, errors.Wrapf(err, "get pods list for replset %s", rs.Name)
}
scr, err := secret(c, cluster.Namespace, cluster.Spec.Secrets.Users)
if err != nil {
return nil, errors.Wrap(err, "get secrets")
}
if len(cluster.Spec.ClusterServiceDNSSuffix) == 0 {
cluster.Spec.ClusterServiceDNSSuffix = api.DefaultDNSSuffix
}
addrs, err := psmdb.GetReplsetAddrs(c, cluster, rs, pods.Items)
if err != nil {
return nil, errors.Wrap(err, "get mongo addr")
}
murl := fmt.Sprintf("mongodb://%s:%s@%s/",
scr.Data["MONGODB_BACKUP_USER"],
scr.Data["MONGODB_BACKUP_PASSWORD"],
strings.Join(addrs, ","),
)
pbmc, err := pbm.New(context.Background(), murl, "operator-pbm-ctl")
if err != nil {
return nil, errors.Wrapf(err, "create PBM connection to %s", strings.Join(addrs, ","))
}
return &PBM{
C: pbmc,
k8c: c,
namespace: cluster.Namespace,
}, nil
}
// SetConfig sets the pbm config with storage defined in the cluster CR
// by given storageName
func (b *PBM) SetConfig(stg api.BackupStorageSpec) error {
switch stg.Type {
case pbm.StorageS3:
if stg.S3.CredentialsSecret == "" {
return errors.New("no credentials specified for the secret name")
}
s3secret, err := secret(b.k8c, b.namespace, stg.S3.CredentialsSecret)
if err != nil {
return errors.Wrap(err, "getting s3 credentials secret name")
}
conf := pbm.Config{
Storage: pbm.Storage{
Type: pbm.StorageS3,
S3: pbm.S3{
Region: stg.S3.Region,
EndpointURL: stg.S3.EndpointURL,
Bucket: stg.S3.Bucket,
Prefix: stg.S3.Prefix,
Credentials: pbm.Credentials{
AccessKeyID: string(s3secret.Data[awsAccessKeySecretKey]),
SecretAccessKey: string(s3secret.Data[awsSecretAccessKeySecretKey]),
},
},
},
}
err = b.C.SetConfig(conf)
if err != nil {
return errors.Wrap(err, "write config")
}
case pbm.StorageFilesystem:
return errors.New("filesystem backup storage not supported yet, skipping storage name")
default:
return errors.New("unsupported backup storage type")
}
return nil
}
// Close close the PBM connection
func (b *PBM) Close() error {
return b.C.Conn.Disconnect(context.Background())
}
func secret(cl client.Client, namespace, secretName string) (*corev1.Secret, error) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: namespace,
},
}
err := cl.Get(context.TODO(), types.NamespacedName{Name: secretName, Namespace: namespace}, secret)
return secret, err
}