forked from libopenstorage/stork
/
pvcwatcher.go
178 lines (162 loc) · 5.07 KB
/
pvcwatcher.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package pvcwatcher
import (
"context"
"fmt"
"reflect"
"strings"
"time"
snapv1 "github.com/kubernetes-incubator/external-storage/snapshot/pkg/apis/crd/v1"
"github.com/libopenstorage/stork/drivers/volume"
storkv1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1"
"github.com/libopenstorage/stork/pkg/controller"
"github.com/operator-framework/operator-sdk/pkg/sdk"
"github.com/portworx/sched-ops/k8s"
"gopkg.in/yaml.v2"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
k8shelper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
)
const (
annotationPrefix = "stork.libopenstorage.org/"
snapshotSchedulePolicyAnnotationPrefix = "snapshotschedule." + annotationPrefix
scheduleCreatedAnnotation = annotationPrefix + "snapshot-schedule-created"
)
// PVCWatcher watches for changes in PVCs
type PVCWatcher struct {
Driver volume.Driver
Recorder record.EventRecorder
}
type policyInfo struct {
SchedulePolicyName string `yaml:"schedulePolicyName"`
ReclaimPolicy storkv1.ReclaimPolicyType `yaml:"reclaimPolicy"`
Annotations map[string]string `yaml:"annotations"`
}
// Start Starts the controller to watch updates on PVCs
func (p *PVCWatcher) Start() error {
return controller.Register(
&schema.GroupVersionKind{
Group: v1.GroupName,
Version: v1.SchemeGroupVersion.Version,
Kind: reflect.TypeOf(v1.PersistentVolumeClaim{}).Name(),
},
"",
1*time.Minute,
p)
}
// Handle updates for PVCs
func (p *PVCWatcher) Handle(ctx context.Context, event sdk.Event) error {
switch o := event.Object.(type) {
case *v1.PersistentVolumeClaim:
err := p.handleSnapshotScheduleUpdates(o, event)
if err != nil {
return err
}
}
return nil
}
func getPoliciesFromMap(options map[string]string, scheduleNamePrefix string) (map[string]*policyInfo, error) {
policyMap := make(map[string]*policyInfo)
for k, v := range options {
if strings.HasPrefix(k, snapshotSchedulePolicyAnnotationPrefix) {
scheduleName := strings.TrimPrefix(k, snapshotSchedulePolicyAnnotationPrefix)
var policy policyInfo
err := yaml.Unmarshal([]byte(v), &policy)
if err != nil {
return nil, err
}
if policy.ReclaimPolicy == "" {
policy.ReclaimPolicy = storkv1.ReclaimPolicyRetain
}
policyMap[scheduleNamePrefix+scheduleName] = &policy
}
}
return policyMap, nil
}
func (p *PVCWatcher) handleSnapshotScheduleUpdates(pvc *v1.PersistentVolumeClaim, event sdk.Event) error {
// Nothing to do for deletions
if event.Deleted {
return nil
}
// Do nothing if the driver doesn't own the PVC or if it isn't bound yet
if !p.Driver.OwnsPVC(pvc) || pvc.Status.Phase != v1.ClaimBound {
return nil
}
// Also skip if we've already configured the snapshot schedule for this PVC
if configured, ok := pvc.Annotations[scheduleCreatedAnnotation]; ok && configured == "yes" {
return nil
}
storageClassName := k8shelper.GetPersistentVolumeClaimClass(pvc)
if storageClassName == "" {
return nil
}
storageClass, err := k8s.Instance().GetStorageClass(storageClassName)
// Ignore if storageclass cannot be found
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
policiesMap, err := getPoliciesFromMap(storageClass.Parameters, pvc.Name+"-")
if err != nil {
return err
}
for snapshotScheduleName, policy := range policiesMap {
schedulePolicyName := policy.SchedulePolicyName
if _, err := k8s.Instance().GetSnapshotSchedule(snapshotScheduleName, pvc.Namespace); err == nil {
continue
}
snapshotSchedule := &storkv1.VolumeSnapshotSchedule{
ObjectMeta: metav1.ObjectMeta{
Name: snapshotScheduleName,
Namespace: pvc.Namespace,
Annotations: policy.Annotations,
// Set the owner reference so that the schedule gets deleted
// with the PVC
OwnerReferences: []metav1.OwnerReference{
{
Name: pvc.Name,
UID: pvc.UID,
Kind: pvc.GetObjectKind().GroupVersionKind().Kind,
APIVersion: pvc.GetObjectKind().GroupVersionKind().GroupVersion().String(),
},
},
},
Spec: storkv1.VolumeSnapshotScheduleSpec{
Template: storkv1.VolumeSnapshotTemplateSpec{
Spec: snapv1.VolumeSnapshotSpec{
PersistentVolumeClaimName: pvc.Name,
},
},
SchedulePolicyName: schedulePolicyName,
ReclaimPolicy: policy.ReclaimPolicy,
},
}
_, err = k8s.Instance().CreateSnapshotSchedule(snapshotSchedule)
if err != nil {
p.Recorder.Event(pvc,
v1.EventTypeWarning,
"Error",
fmt.Sprintf("Error creating snapshot schedule for PVC: %v", err))
return err
}
p.Recorder.Event(pvc,
v1.EventTypeNormal,
"Success",
fmt.Sprintf("Created volume snapshot schedule (%v) for PVC", snapshotScheduleName))
}
if len(policiesMap) > 0 {
if pvc.Annotations == nil {
pvc.Annotations = make(map[string]string)
}
pvc.Annotations[scheduleCreatedAnnotation] = "yes"
_, err = k8s.Instance().UpdatePersistentVolumeClaim(pvc)
if err != nil {
return err
}
}
return err
}