-
Notifications
You must be signed in to change notification settings - Fork 52
/
restore_controller.go
159 lines (137 loc) · 4.44 KB
/
restore_controller.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package controllers
import (
"context"
"fmt"
"github.com/infinispan/infinispan-operator/api/v2alpha1"
"github.com/infinispan/infinispan-operator/pkg/infinispan/client/api"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// +kubebuilder:rbac:groups=infinispan.org,namespace=infinispan-operator-system,resources=restores;restores/status;restores/finalizers,verbs=get;list;watch;create;update;patch
// RestoreReconciler reconciles a Restore object
type RestoreReconciler struct {
client.Client
}
type restore struct {
instance *v2alpha1.Restore
client client.Client
scheme *runtime.Scheme
ctx context.Context
}
// SetupWithManager sets up the controller with the Manager.
func (r *RestoreReconciler) SetupWithManager(mgr ctrl.Manager) error {
return newZeroCapacityController("Restore", &RestoreReconciler{mgr.GetClient()}, mgr)
}
func (r *RestoreReconciler) ResourceInstance(ctx context.Context, name types.NamespacedName, ctrl *zeroCapacityController) (zeroCapacityResource, error) {
instance := &v2alpha1.Restore{}
if err := ctrl.Get(ctx, name, instance); err != nil {
return nil, err
}
restore := &restore{
instance: instance,
client: r.Client,
scheme: ctrl.Scheme,
ctx: ctx,
}
return restore, nil
}
func (r *RestoreReconciler) Type() client.Object {
return &v2alpha1.Restore{}
}
func (r *restore) AsMeta() metav1.Object {
return r.instance
}
func (r *restore) Cluster() string {
return r.instance.Spec.Cluster
}
func (r *restore) Phase() zeroCapacityPhase {
return zeroCapacityPhase(string(r.instance.Status.Phase))
}
func (r *restore) UpdatePhase(phase zeroCapacityPhase, phaseErr error) error {
_, err := r.update(func() {
restore := r.instance
var reason string
if phaseErr != nil {
reason = phaseErr.Error()
}
restore.Status.Phase = v2alpha1.RestorePhase(phase)
restore.Status.Reason = reason
})
return err
}
func (r *restore) update(mutate func()) (bool, error) {
restore := r.instance
res, err := controllerutil.CreateOrPatch(r.ctx, r.client, restore, func() error {
if restore.CreationTimestamp.IsZero() {
return errors.NewNotFound(schema.ParseGroupResource("restore.infinispan.org"), restore.Name)
}
mutate()
return nil
})
return res != controllerutil.OperationResultNone, err
}
func (r *restore) Init() (*zeroCapacitySpec, error) {
backup := &v2alpha1.Backup{}
backupKey := types.NamespacedName{
Namespace: r.instance.Namespace,
Name: r.instance.Spec.Backup,
}
if err := r.client.Get(r.ctx, backupKey, backup); err != nil {
return nil, fmt.Errorf("unable to load Infinispan Backup '%s': %w", backupKey.Name, err)
}
return &zeroCapacitySpec{
Container: r.instance.Spec.Container,
PodLabels: map[string]string{"restore_cr": r.instance.Name},
Volume: zeroCapacityVolumeSpec{
MountPath: BackupDataMountPath,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: backup.Name,
ReadOnly: true,
},
},
},
}, nil
}
func (r *restore) Exec(client api.Infinispan) error {
instance := r.instance
var resources api.BackupRestoreResources
if instance.Spec.Resources == nil {
resources = api.BackupRestoreResources{}
} else {
resources = api.BackupRestoreResources{
Caches: instance.Spec.Resources.Caches,
Counters: instance.Spec.Resources.Counters,
ProtoSchemas: instance.Spec.Resources.ProtoSchemas,
Templates: instance.Spec.Resources.Templates,
Tasks: instance.Spec.Resources.Tasks,
}
}
config := &api.RestoreConfig{
Location: fmt.Sprintf("%[1]s/%[2]s/%[2]s.zip", BackupDataMountPath, instance.Spec.Backup),
Resources: resources,
}
status, err := client.Container().Restores().Status(instance.Name)
if err != nil {
return fmt.Errorf("unable to obtain Restore status: %w", err)
}
if status == api.StatusNotFound {
return client.Container().Restores().Create(instance.Name, config)
}
return nil
}
func (r *restore) ExecStatus(client api.Infinispan) (zeroCapacityPhase, error) {
name := r.instance.Name
status, err := client.Container().Restores().Status(name)
if err != nil {
return ZeroUnknown, err
}
return zeroCapacityPhase(status), nil
}