-
Notifications
You must be signed in to change notification settings - Fork 85
/
init_container.go
203 lines (172 loc) Β· 7.98 KB
/
init_container.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
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"
"fmt"
"stash.appscode.dev/apimachinery/apis"
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
"stash.appscode.dev/apimachinery/pkg/docker"
"stash.appscode.dev/stash/pkg/eventer"
stash_rbac "stash.appscode.dev/stash/pkg/rbac"
"stash.appscode.dev/stash/pkg/util"
"github.com/appscode/go/log"
stringz "github.com/appscode/go/strings"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
core_util "kmodules.xyz/client-go/core/v1"
"kmodules.xyz/client-go/meta"
wapi "kmodules.xyz/webhook-runtime/apis/workload/v1"
)
func (c *StashController) ensureRestoreInitContainer(w *wapi.Workload, rs *api_v1beta1.RestoreSession, caller string) error {
// if RBAC is enabled then ensure ServiceAccount and respective ClusterRole and RoleBinding
sa := stringz.Val(w.Spec.Template.Spec.ServiceAccountName, "default")
//Don't create RBAC stuff when the caller is webhook to make the webhooks side effect free.
if caller != apis.CallerWebhook {
owner, err := ownerWorkload(w)
if err != nil {
return err
}
err = stash_rbac.EnsureRestoreInitContainerRBAC(c.kubeClient, owner, rs.Namespace, sa, rs.OffshootLabels())
if err != nil {
return err
}
}
repository, err := c.stashClient.StashV1alpha1().Repositories(rs.Namespace).Get(context.TODO(), rs.Spec.Repository.Name, metav1.GetOptions{})
if err != nil {
log.Errorf("unable to get repository %s/%s: Reason: %v", rs.Namespace, rs.Spec.Repository.Name, err)
return err
}
if repository.Spec.Backend.StorageSecretName == "" {
err := fmt.Errorf("missing repository secret name %s/%s", repository.Namespace, repository.Name)
return err
}
// check if secret exist
_, err = c.kubeClient.CoreV1().Secrets(w.Namespace).Get(context.TODO(), repository.Spec.Backend.StorageSecretName, metav1.GetOptions{})
if err != nil {
return err
}
if w.Spec.Template.Annotations == nil {
w.Spec.Template.Annotations = map[string]string{}
}
// mark pods with RestoreSession spec hash. used to force restart pods for rc/rs
w.Spec.Template.Annotations[api_v1beta1.AppliedRestoreSessionSpecHash] = rs.GetSpecHash()
image := docker.Docker{
Registry: c.DockerRegistry,
Image: docker.ImageStash,
Tag: c.StashImageTag,
}
// insert restore init container
w.Spec.Template.Spec.InitContainers = core_util.UpsertContainer(
w.Spec.Template.Spec.InitContainers,
util.NewRestoreInitContainer(rs, repository, image),
)
// add an emptyDir volume for holding temporary files
w.Spec.Template.Spec.Volumes = util.UpsertTmpVolume(w.Spec.Template.Spec.Volumes, rs.Spec.TempDir)
// add downward volume to make some information of the workload accessible to the container
w.Spec.Template.Spec.Volumes = util.UpsertDownwardVolume(w.Spec.Template.Spec.Volumes)
// add storage secret as volume to the workload. this is mounted on the restore init container
w.Spec.Template.Spec.Volumes = util.UpsertSecretVolume(w.Spec.Template.Spec.Volumes, repository.Spec.Backend.StorageSecretName)
// if Repository uses local volume as backend, append this volume to workload
w.Spec.Template.Spec.Volumes = util.MergeLocalVolume(w.Spec.Template.Spec.Volumes, &repository.Spec.Backend)
// add RestoreSession definition as annotation of the workload
if w.Annotations == nil {
w.Annotations = make(map[string]string)
}
r := &api_v1beta1.RestoreSession{
TypeMeta: metav1.TypeMeta{
APIVersion: api_v1beta1.SchemeGroupVersion.String(),
Kind: api_v1beta1.ResourceKindRestoreSession,
},
ObjectMeta: rs.ObjectMeta,
Spec: rs.Spec,
}
data, _ := meta.MarshalToJson(r, api_v1beta1.SchemeGroupVersion)
w.Annotations[api_v1beta1.KeyLastAppliedRestoreSession] = string(data)
return nil
}
func (c *StashController) ensureRestoreInitContainerDeleted(w *wapi.Workload) {
// remove resource hash annotation
if w.Spec.Template.Annotations != nil {
delete(w.Spec.Template.Annotations, api_v1beta1.AppliedRestoreSessionSpecHash)
}
// remove init-container
w.Spec.Template.Spec.InitContainers = core_util.EnsureContainerDeleted(w.Spec.Template.Spec.InitContainers, apis.StashInitContainer)
// restore init-container has been removed but workload still may have backup sidecar
// so removed respective volumes that were added to the workload only if the workload does not have backup sidecar
if !util.HasStashContainer(w) {
// remove the helpers volumes added for init-container
w.Spec.Template.Spec.Volumes = util.EnsureVolumeDeleted(w.Spec.Template.Spec.Volumes, apis.ScratchDirVolumeName)
w.Spec.Template.Spec.Volumes = util.EnsureVolumeDeleted(w.Spec.Template.Spec.Volumes, apis.PodinfoVolumeName)
w.Spec.Template.Spec.Volumes = util.EnsureVolumeDeleted(w.Spec.Template.Spec.Volumes, apis.StashSecretVolume)
// if stash-local volume was added for local backend, remove it
w.Spec.Template.Spec.Volumes = util.EnsureVolumeDeleted(w.Spec.Template.Spec.Volumes, apis.LocalVolumeName)
}
// remove respective annotations
if w.Annotations != nil {
delete(w.Annotations, api_v1beta1.KeyLastAppliedRestoreSession)
}
}
func (c *StashController) handleInitContainerInjectionFailure(ref *core.ObjectReference, rs *api_v1beta1.RestoreSession, err error) error {
if ref == nil {
return errors.NewAggregate([]error{err, fmt.Errorf("failed to init-container injection failure event. Reason: provided ObjectReference is nil")})
}
log.Warningf("Failed to inject stash init-container into %s %s/%s. Reason: %v", ref.Kind, ref.Namespace, ref.Name, err)
// Set "StashInitContainerInjected" condition to "False"
cerr := c.setInitContainerInjectedConditionToFalse(rs, err)
// write event to respective resource
_, err2 := eventer.CreateEvent(
c.kubeClient,
eventer.EventSourceWorkloadController,
ref,
core.EventTypeWarning,
eventer.EventReasonInitContainerInjectionFailed,
fmt.Sprintf("Failed to inject stash init-container into %s %s/%s. Reason: %v", ref.Kind, ref.Namespace, ref.Name, err),
)
return errors.NewAggregate([]error{err2, cerr})
}
func (c *StashController) handleInitContainerInjectionSuccess(ref *core.ObjectReference, rs *api_v1beta1.RestoreSession) error {
if ref == nil {
return fmt.Errorf("failed to init-container injection success event. Reason: provided ObjectReference is nil")
}
log.Infof("Successfully injected stash init-container into %s %s/%s.", ref.Kind, ref.Namespace, ref.Name)
// Set "StashInitContainerInjected" condition to "True"
cerr := c.setInitContainerInjectedConditionToTrue(rs)
// write event to respective resource
_, err2 := eventer.CreateEvent(
c.kubeClient,
eventer.EventSourceWorkloadController,
ref,
core.EventTypeNormal,
eventer.EventReasonInitContainerInjectionSucceeded,
fmt.Sprintf("Successfully injected stash init-container into %s %s/%s.", ref.Kind, ref.Namespace, ref.Name),
)
return errors.NewAggregate([]error{err2, cerr})
}
func (c *StashController) handleInitContainerDeletionSuccess(ref *core.ObjectReference) error {
if ref == nil {
return fmt.Errorf("failed to init-container deletion success event. Reason: provided ObjectReference is nil")
}
log.Infof("Successfully removed stash init-container from %s %s/%s.", ref.Kind, ref.Namespace, ref.Name)
// write event to respective resource
_, err2 := eventer.CreateEvent(
c.kubeClient,
eventer.EventSourceWorkloadController,
ref,
core.EventTypeNormal,
eventer.EventReasonInitContainerDeletionSucceeded,
fmt.Sprintf("Successfully stash init-container from %s %s/%s.", ref.Kind, ref.Namespace, ref.Name),
)
return err2
}