Skip to content

Commit 40dcbb6

Browse files
committed
Set .Status.BackupUri if .Status.BackupUri not specified. Fixes #48
1 parent c60392a commit 40dcbb6

File tree

4 files changed

+112
-60
lines changed

4 files changed

+112
-60
lines changed

pkg/apis/mysql/v1alpha1/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ type BackupStatus struct {
216216
// Complete marks the backup in final state
217217
Completed bool `json:"completed"`
218218

219+
// BackupUri represent the fully uri to the backup location
220+
BackupUri string `json:"backupUri,omitempty"`
221+
219222
Conditions []BackupCondition `json:"conditions"`
220223
}
221224

pkg/backupfactory/backupfactory.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ func (f *bFactory) Sync(ctx context.Context) error {
6868
f.backup.AsOwnerReference(),
6969
},
7070
}
71+
72+
if len(f.GetBackupUri()) == 0 {
73+
glog.Errorf("The backupUri for backupt: '%s' and cluster: '%s' not specified,"+
74+
" neither in backup or cluster!", f.backup.Name, f.cluster.Name)
75+
return fmt.Errorf("backupUri not specified")
76+
}
77+
7178
_, _, err := kbatch.CreateOrPatchJob(f.k8Client, meta, func(in *batch.Job) *batch.Job {
7279
if len(in.Spec.Template.Spec.Containers) == 0 {
7380
in.Spec.Template.Spec = f.ensurePodSpec(in.Spec.Template.Spec)
@@ -97,15 +104,15 @@ func (f *bFactory) ensurePodSpec(in core.PodSpec) core.PodSpec {
97104
in.Containers[0].Args = []string{
98105
"take-backup-to",
99106
f.cluster.GetBackupCandidate(),
100-
f.backup.Spec.BackupUri,
107+
f.GetBackupUri(),
101108
}
102109

103-
if len(f.backup.Spec.BackupSecretName) != 0 {
110+
if len(f.GetBackupSecretName()) != 0 {
104111
in.Containers[0].EnvFrom = []core.EnvFromSource{
105112
core.EnvFromSource{
106113
SecretRef: &core.SecretEnvSource{
107114
LocalObjectReference: core.LocalObjectReference{
108-
Name: f.backup.Spec.BackupSecretName,
115+
Name: f.GetBackupSecretName(),
109116
},
110117
},
111118
},
@@ -124,23 +131,29 @@ func (f *bFactory) SetDefaults() error {
124131
f.backup.UpdateStatusCondition(api.BackupComplete, core.ConditionUnknown, "set defaults",
125132
"First initialization of backup")
126133

127-
if len(f.backup.Spec.BackupUri) == 0 {
128-
if len(f.cluster.Spec.BackupUri) > 0 {
129-
f.backup.Spec.BackupUri = getBucketUri(
130-
f.cluster.Name, f.cluster.Spec.BackupUri)
131-
} else {
132-
return fmt.Errorf("backupUri not specified, neither in cluster")
133-
}
134+
// mark backup as not in final state
135+
f.backup.Status.Completed = false
136+
137+
// update status backup uri
138+
f.backup.Status.BackupUri = f.GetBackupUri()
139+
140+
return nil
141+
}
142+
143+
func (f *bFactory) GetBackupUri() string {
144+
if len(f.backup.Status.BackupUri) > 0 {
145+
return f.backup.Status.BackupUri
134146
}
135147

136-
if len(f.backup.Spec.BackupSecretName) == 0 {
137-
f.backup.Spec.BackupSecretName = f.cluster.Spec.BackupSecretName
148+
if len(f.backup.Spec.BackupUri) > 0 {
149+
return f.backup.Spec.BackupUri
138150
}
139151

140-
// mark backup as not in final state
141-
f.backup.Status.Completed = false
152+
if len(f.cluster.Spec.BackupUri) > 0 {
153+
return getBucketUri(f.cluster.Name, f.cluster.Spec.BackupUri)
154+
}
142155

143-
return nil
156+
return ""
144157
}
145158

146159
func getBucketUri(cluster, bucket string) string {
@@ -153,6 +166,14 @@ func getBucketUri(cluster, bucket string) string {
153166
)
154167
}
155168

169+
func (f *bFactory) GetBackupSecretName() string {
170+
if len(f.backup.Spec.BackupSecretName) > 0 {
171+
return f.backup.Spec.BackupSecretName
172+
}
173+
174+
return f.cluster.Spec.BackupSecretName
175+
}
176+
156177
func (f *bFactory) updateStatus(job *batch.Job) {
157178
glog.V(2).Infof("Updating status of '%s' backup", f.backup.Name)
158179

pkg/backupfactory/backupfactory_test.go

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ package backupfactory
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"testing"
2322

23+
. "github.com/onsi/ginkgo"
24+
. "github.com/onsi/gomega"
2425
// core "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/client-go/kubernetes/fake"
@@ -31,16 +32,18 @@ import (
3132
tutil "github.com/presslabs/mysql-operator/pkg/util/test"
3233
)
3334

35+
func TestBackupFactory(t *testing.T) {
36+
RegisterFailHandler(Fail)
37+
RunSpecs(t, "Test backups factory")
38+
}
39+
3440
func getFakeFactory(backup *api.MysqlBackup, k8Client *fake.Clientset,
3541
myClient *fakeMyClient.Clientset) *bFactory {
3642

3743
cluster, err := myClient.MysqlV1alpha1().MysqlClusters(backup.Namespace).Get(
3844
backup.Spec.ClusterName, metav1.GetOptions{})
3945

40-
if err != nil {
41-
fmt.Println("Failed to get cluster:", err)
42-
}
43-
46+
Expect(err).To(Succeed())
4447
return &bFactory{
4548
backup: backup,
4649
cluster: cluster,
@@ -49,40 +52,67 @@ func getFakeFactory(backup *api.MysqlBackup, k8Client *fake.Clientset,
4952
}
5053
}
5154

52-
// TestSync
53-
// Test: sync a backup for a cluster
54-
// Expect: sync successful, job created
55-
func TestSync(t *testing.T) {
56-
client := fake.NewSimpleClientset()
57-
myClient := fakeMyClient.NewSimpleClientset()
58-
59-
cluster := tutil.NewFakeCluster("test-2")
60-
_, err := myClient.MysqlV1alpha1().MysqlClusters(tutil.Namespace).Create(cluster)
61-
if err != nil {
62-
fmt.Println("Failed to create cluster:", err)
63-
}
64-
backup := tutil.NewFakeBackup("test-1", cluster.Name)
65-
f := getFakeFactory(backup, client, myClient)
66-
67-
err = f.SetDefaults()
68-
if err != nil {
69-
t.Fail()
70-
}
71-
72-
ctx := context.TODO()
73-
err = f.Sync(ctx)
74-
if err != nil {
75-
t.Fail()
76-
}
77-
78-
_, err = client.BatchV1().Jobs(tutil.Namespace).Get(f.getJobName(), metav1.GetOptions{})
79-
if err != nil {
80-
t.Fail()
81-
return
82-
}
83-
84-
err = f.Sync(ctx)
85-
if err != nil {
86-
t.Fail()
87-
}
88-
}
55+
var _ = Describe("Test backup factory", func() {
56+
var (
57+
client *fake.Clientset
58+
myClient *fakeMyClient.Clientset
59+
60+
ctx context.Context
61+
)
62+
63+
BeforeEach(func() {
64+
client = fake.NewSimpleClientset()
65+
myClient = fakeMyClient.NewSimpleClientset()
66+
67+
ctx = context.TODO()
68+
})
69+
70+
Describe("Test a simple sync", func() {
71+
Context("A backup is created but not initialized", func() {
72+
It("sync should be successful", func() {
73+
cluster := tutil.NewFakeCluster("test-cluster-1")
74+
backup := tutil.NewFakeBackup("test-backup-1", cluster.Name)
75+
76+
cluster.Spec.BackupUri = "gs://some-bucket/"
77+
_, err := myClient.MysqlV1alpha1().MysqlClusters(tutil.Namespace).Create(cluster)
78+
Expect(err).To(Succeed())
79+
80+
f := getFakeFactory(backup, client, myClient)
81+
Expect(f.SetDefaults()).To(Succeed())
82+
Expect(backup.Status.BackupUri).To(ContainSubstring("gs://some-bucket"))
83+
Expect(f.Sync(ctx)).To(Succeed())
84+
85+
_, err = client.BatchV1().Jobs(tutil.Namespace).Get(
86+
f.getJobName(), metav1.GetOptions{})
87+
Expect(err).To(Succeed())
88+
Expect(f.Sync(ctx)).To(Succeed())
89+
})
90+
91+
It("sync a backup with init bucket uri", func() {
92+
cluster := tutil.NewFakeCluster("test-cluster-1")
93+
backup := tutil.NewFakeBackup("test-backup-1", cluster.Name)
94+
95+
backup.Spec.BackupUri = "gs://other-backup/test.gz"
96+
cluster.Spec.BackupUri = "gs://some-bucket/"
97+
_, err := myClient.MysqlV1alpha1().MysqlClusters(tutil.Namespace).Create(cluster)
98+
Expect(err).To(Succeed())
99+
100+
f := getFakeFactory(backup, client, myClient)
101+
Expect(f.SetDefaults()).To(Succeed())
102+
Expect(backup.Status.BackupUri).To(ContainSubstring("gs://other-backup"))
103+
Expect(f.Sync(ctx)).To(Succeed())
104+
})
105+
It("sync a backup with no bucket uri", func() {
106+
cluster := tutil.NewFakeCluster("test-cluster-1")
107+
backup := tutil.NewFakeBackup("test-backup-1", cluster.Name)
108+
109+
_, err := myClient.MysqlV1alpha1().MysqlClusters(tutil.Namespace).Create(cluster)
110+
Expect(err).To(Succeed())
111+
112+
f := getFakeFactory(backup, client, myClient)
113+
Expect(f.SetDefaults()).To(Succeed())
114+
Expect(f.Sync(ctx)).ToNot(Succeed())
115+
})
116+
})
117+
})
118+
})

pkg/util/test/test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ func NewFakeBackup(name, clName string) *api.MysqlBackup {
5959
Namespace: Namespace,
6060
},
6161
Spec: api.BackupSpec{
62-
ClusterName: clName,
63-
BackupUri: "gs://bucket/a.xb.gz",
64-
BackupSecretName: name,
62+
ClusterName: clName,
6563
},
6664
}
6765
}

0 commit comments

Comments
 (0)