-
Notifications
You must be signed in to change notification settings - Fork 86
/
statefulsets.go
172 lines (152 loc) Β· 5.6 KB
/
statefulsets.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
/*
Copyright The Stash Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"context"
"stash.appscode.dev/apimachinery/apis"
stash_rbac "stash.appscode.dev/stash/pkg/rbac"
"stash.appscode.dev/stash/pkg/util"
"github.com/golang/glog"
appsv1 "k8s.io/api/apps/v1"
kerr "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/client-go/tools/cache"
apps_util "kmodules.xyz/client-go/apps/v1"
"kmodules.xyz/client-go/tools/queue"
"kmodules.xyz/webhook-runtime/admission"
hooks "kmodules.xyz/webhook-runtime/admission/v1beta1"
webhook "kmodules.xyz/webhook-runtime/admission/v1beta1/workload"
wapi "kmodules.xyz/webhook-runtime/apis/workload/v1"
wcs "kmodules.xyz/webhook-runtime/client/workload/v1"
)
func (c *StashController) NewStatefulSetWebhook() hooks.AdmissionHook {
return webhook.NewWorkloadWebhook(
schema.GroupVersionResource{
Group: "admission.stash.appscode.com",
Version: "v1alpha1",
Resource: "statefulsetmutators",
},
"statefulsetmutator",
"StatefulSetMutator",
nil,
&admission.ResourceHandlerFuncs{
CreateFunc: func(obj runtime.Object) (runtime.Object, error) {
w := obj.(*wapi.Workload)
// apply stash backup/restore logic on this workload
modified, err := c.applyStashLogic(w, apis.CallerWebhook)
if err != nil {
return w, err
}
if modified {
err := setRollingUpdate(w)
if err != nil {
return w, err
}
}
return w, err
},
UpdateFunc: func(oldObj, newObj runtime.Object) (runtime.Object, error) {
w := newObj.(*wapi.Workload)
// apply stash backup/restore logic on this workload
modified, err := c.applyStashLogic(w, apis.CallerWebhook)
if err != nil {
return w, err
}
if modified {
err := setRollingUpdate(w)
if err != nil {
return w, err
}
}
return w, err
},
},
)
}
func (c *StashController) initStatefulSetWatcher() {
c.ssInformer = c.kubeInformerFactory.Apps().V1().StatefulSets().Informer()
c.ssQueue = queue.New("StatefulSet", c.MaxNumRequeues, c.NumThreads, c.runStatefulSetInjector)
c.ssInformer.AddEventHandler(queue.DefaultEventHandler(c.ssQueue.GetQueue()))
c.ssLister = c.kubeInformerFactory.Apps().V1().StatefulSets().Lister()
}
// syncToStdout is the business logic of the controller. In this controller it simply prints
// information about the deployment to stdout. In case an error happened, it has to simply return the error.
// The retry logic should not be part of the business logic.
func (c *StashController) runStatefulSetInjector(key string) error {
obj, exists, err := c.ssInformer.GetIndexer().GetByKey(key)
if err != nil {
glog.Errorf("Fetching object with key %s from store failed with %v", key, err)
return err
}
if !exists {
// Below we will warm up our cache with a StatefulSet, so that we will see a delete for one d
glog.Warningf("StatefulSet %s does not exist anymore\n", key)
ns, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
// workload does not exist anymore. so delete respective ConfigMapLocks if exist
err = util.DeleteAllConfigMapLocks(c.kubeClient, ns, name, apis.KindStatefulSet)
if err != nil && !kerr.IsNotFound(err) {
return err
}
} else {
glog.Infof("Sync/Add/Update for StatefulSet %s", key)
ss := obj.(*appsv1.StatefulSet).DeepCopy()
ss.GetObjectKind().SetGroupVersionKind(appsv1.SchemeGroupVersion.WithKind(apis.KindStatefulSet))
// convert StatefulSet into a common object (Workload type) so that
// we don't need to re-write stash logic for StatefulSet separately
w, err := wcs.ConvertToWorkload(ss.DeepCopy())
if err != nil {
glog.Errorf("failed to convert StatefulSet %s/%s to workload type. Reason: %v", ss.Namespace, ss.Name, err)
return err
}
// apply stash backup/restore logic on this workload
modified, err := c.applyStashLogic(w, apis.CallerController)
if err != nil {
glog.Errorf("failed to apply stash logic on StatefulSet %s/%s. Reason: %v", ss.Namespace, ss.Name, err)
return err
}
if modified {
// set update strategy RollingUpdate so that pods automatically restart after patch
err := setRollingUpdate(w)
if err != nil {
return err
}
// workload has been modified. patch the workload so that respective pods start with the updated spec
_, _, err = apps_util.PatchStatefulSetObject(context.TODO(), c.kubeClient, ss, w.Object.(*appsv1.StatefulSet), metav1.PatchOptions{})
if err != nil {
glog.Errorf("failed to update statefulset %s/%s. Reason: %v", ss.Namespace, ss.Name, err)
return err
}
// wait until newly patched StatefulSet pods are ready
err = util.WaitUntilStatefulSetReady(c.kubeClient, ss.ObjectMeta)
if err != nil {
return err
}
}
// if the workload does not have any stash sidecar/init-container then
// delete respective ConfigMapLock and RBAC stuffs if exist
err = c.ensureUnnecessaryConfigMapLockDeleted(w)
if err != nil {
return err
}
err = stash_rbac.EnsureUnnecessaryWorkloadRBACDeleted(c.kubeClient, w)
if err != nil {
return err
}
}
return nil
}