-
Notifications
You must be signed in to change notification settings - Fork 3
/
volumes.go
350 lines (279 loc) · 10.8 KB
/
volumes.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright © 2022 FORTH-ICS
//
// 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 podhandler
import (
"context"
"io/fs"
"os"
"path/filepath"
"time"
"github.com/carv-ics-forth/hpk/compute/endpoint"
"github.com/carv-ics-forth/hpk/compute/volume/configmap"
"github.com/carv-ics-forth/hpk/compute/volume/emptydir"
"github.com/carv-ics-forth/hpk/compute/volume/hostpath"
"github.com/carv-ics-forth/hpk/compute/volume/projected"
"github.com/carv-ics-forth/hpk/compute/volume/secret"
"github.com/carv-ics-forth/hpk/compute/volume/util"
"github.com/pkg/errors"
mounter "k8s.io/utils/mount"
"github.com/carv-ics-forth/hpk/compute"
"github.com/carv-ics-forth/hpk/pkg/fieldpath"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
k8errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
)
// NotFoundBackoff is the recommended backoff for a resource that is required,
// but is not created yet. For instance, when mounting configmap volumes to pods.
// TODO: in future version, the backoff can be self-modified depending on the load of the controller.
var NotFoundBackoff = wait.Backoff{
Steps: 10,
Duration: 2 * time.Second,
Factor: 5.0,
Jitter: 0.1,
}
// mountVolumeSource prepares the volumes into the local pod directory.
// Critical errors related to the HPK fail directly.
// Misconfigurations (like wrong hostpaths), are returned as errors
func (h *podHandler) mountVolumeSource(ctx context.Context, vol corev1.Volume) error {
switch {
case vol.VolumeSource.EmptyDir != nil:
/*---------------------------------------------------
* EmptyDir
*---------------------------------------------------*/
emptyDir := filepath.Join(h.podDirectory.VolumeDir(), vol.Name)
if err := os.MkdirAll(emptyDir, endpoint.PodGlobalDirectoryPermissions); err != nil {
compute.SystemPanic(err, "cannot create dir '%s'", emptyDir)
}
mounter := emptydir.VolumeMounter{
Volume: vol,
Pod: *h.Pod,
Logger: h.logger,
}
if err := mounter.SetUpAt(ctx, emptyDir); err != nil {
compute.SystemPanic(err, "mount emptyDir volume to dir '%s' has failed", emptyDir)
}
h.logger.Info(" * EmptyDir Volume is mounted", "name", vol.Name)
return nil
case vol.VolumeSource.ConfigMap != nil:
/*---------------------------------------------------
* ConfigMap
*---------------------------------------------------*/
configMapDir := filepath.Join(h.podDirectory.VolumeDir(), vol.Name)
if err := os.MkdirAll(configMapDir, endpoint.PodGlobalDirectoryPermissions); err != nil {
compute.SystemPanic(err, "cannot create dir '%s'", configMapDir)
}
mounter := configmap.VolumeMounter{
Volume: vol,
Pod: *h.Pod,
Logger: h.logger,
}
if err := mounter.SetUpAt(ctx, configMapDir); err != nil {
compute.DefaultLogger.Info("mount configMap volume has failed",
"volume", vol.Name,
"dir", configMapDir,
)
return errors.Wrapf(err, "failed to mount ConfigMap '%s'", vol.Name)
}
h.logger.Info(" * ConfigMap Volume is mounted", "name", vol.Name)
return nil
case vol.VolumeSource.Secret != nil:
/*---------------------------------------------------
* Secret
*---------------------------------------------------*/
secretDir := filepath.Join(h.podDirectory.VolumeDir(), vol.Name)
if err := os.MkdirAll(secretDir, endpoint.PodGlobalDirectoryPermissions); err != nil {
compute.SystemPanic(err, "cannot create dir '%s'", secretDir)
}
mounter := secret.VolumeMounter{
Volume: vol,
Pod: *h.Pod,
Logger: h.logger,
}
if err := mounter.SetUpAt(ctx, secretDir); err != nil {
compute.DefaultLogger.Info("mount secret volume has failed",
"volume", vol.Name,
"dir", secretDir,
)
return errors.Wrapf(err, "failed to mount Secret '%s'", vol.Name)
}
h.logger.Info(" * Secret Volume is mounted", "name", vol.Name)
return nil
case vol.VolumeSource.DownwardAPI != nil:
/*---------------------------------------------------
* Downward API
*---------------------------------------------------*/
h.DownwardAPIVolumeSource(ctx, vol)
h.logger.Info(" * DownwardAPI Volume is mounted", "name", vol.Name)
return nil
case vol.VolumeSource.HostPath != nil:
/*---------------------------------------------------
* HostPath
*---------------------------------------------------*/
if vol.VolumeSource.HostPath.Type == nil || *vol.VolumeSource.HostPath.Type == corev1.HostPathUnset {
// ensure that the references host path exists
exists, err := mounter.PathExists(vol.VolumeSource.HostPath.Path)
if err != nil {
compute.SystemPanic(err, "failed to inspect HostPath at path '%s'", vol.VolumeSource.HostPath.Path)
}
if !exists {
return errors.Errorf("HostPath '%s' does not exist", vol.VolumeSource.HostPath.Path)
}
// Empty string (default) is for backward compatibility,
// which means that no checks will be performed before mounting the hostPath volume.
dstFullPath := filepath.Join(h.podDirectory.VolumeDir(), vol.Name)
if err := os.Symlink(vol.VolumeSource.HostPath.Path, dstFullPath); err != nil {
compute.SystemPanic(err, "cannot link symlink at path '%s'", dstFullPath)
}
// nothing to do
return nil
}
mounter := hostpath.VolumeMounter{
Volume: vol,
Pod: *h.Pod,
Logger: h.logger,
}
if err := mounter.SetUpAt(ctx); err != nil {
compute.SystemPanic(err, "mount hostpath volume has failed")
}
h.logger.Info(" * HostPath Volume is mounted", "name", vol.Name)
return nil
case vol.VolumeSource.PersistentVolumeClaim != nil:
/*---------------------------------------------------
* Persistent Volume Claim
*---------------------------------------------------*/
h.PersistentVolumeClaimSource(ctx, vol)
h.logger.Info(" * PersistentVolumeClaim Volume is mounted", "name", vol.Name)
return nil
case vol.VolumeSource.Projected != nil:
/*---------------------------------------------------
* Projected
*---------------------------------------------------*/
projectedDir := filepath.Join(h.podDirectory.VolumeDir(), vol.Name)
if err := os.MkdirAll(projectedDir, endpoint.PodGlobalDirectoryPermissions); err != nil {
compute.SystemPanic(err, "cannot create dir '%s'", projectedDir)
}
mounter := projected.VolumeMounter{
Volume: vol,
Pod: *h.Pod,
Logger: h.logger,
}
if err := mounter.SetUpAt(ctx, projectedDir); err != nil {
return errors.Wrapf(err, "failed to mount ProjectedVolume '%s'", vol.Name)
}
return nil
default:
logrus.Warn(vol)
panic("It seems I have missed a Volume type")
}
}
func (h *podHandler) DownwardAPIVolumeSource(ctx context.Context, vol corev1.Volume) {
downApiDir := filepath.Join(h.podDirectory.VolumeDir(), vol.Name)
if err := os.MkdirAll(downApiDir, endpoint.PodGlobalDirectoryPermissions); err != nil {
compute.SystemPanic(err, "cannot create dir '%s'", downApiDir)
}
for _, item := range vol.DownwardAPI.Items {
itemPath := filepath.Join(downApiDir, item.Path)
value, err := fieldpath.ExtractFieldPathAsString(h.Pod, item.FieldRef.FieldPath)
if err != nil {
compute.PodError(h.Pod, compute.ReasonSpecError, err.Error())
return
}
if err := os.WriteFile(itemPath, []byte(value), fs.FileMode(*vol.Projected.DefaultMode)); err != nil {
compute.SystemPanic(err, "cannot write config map file '%s'", itemPath)
}
}
}
func (h *podHandler) PersistentVolumeClaimSource(ctx context.Context, vol corev1.Volume) {
/*---------------------------------------------------
* Get the Referenced PVC from Volume
*---------------------------------------------------*/
var pvc corev1.PersistentVolumeClaim
{
source := vol.VolumeSource.PersistentVolumeClaim
key := types.NamespacedName{Namespace: h.Pod.GetNamespace(), Name: source.ClaimName}
if errPVC := retry.OnError(NotFoundBackoff,
func(err error) bool { // retry condition
return k8errors.IsNotFound(err) || errors.Is(err, compute.ErrUnboundedPVC)
},
func() error { // execution
if err := compute.K8SClient.Get(ctx, key, &pvc); err != nil {
compute.DefaultLogger.Info("Failed to get PVC", "pvcName", pvc.GetName())
return err
}
// filter-out unsupported pvc
if util.CheckPersistentVolumeClaimModeBlock(&pvc) {
compute.DefaultLogger.Info("Unsupported PVC mode", "pvcName", pvc.GetName())
return compute.ErrUnsupportedClaimMode
}
// ensure that pvc is bounded to a pv
if pvc.Status.Phase != corev1.ClaimBound {
compute.DefaultLogger.Info("Waiting for PVC to become bounded to a PV",
"pvcName", pvc.GetName(),
)
return compute.ErrUnboundedPVC
}
return nil
},
); errPVC != nil { // error cehcking
compute.PodError(h.Pod, "PVCError", "PVC (%s) has failed. error:'%W'", pvc.GetName(), errPVC)
return
}
}
/*---------------------------------------------------
* Get the referenced PV from PVC
*---------------------------------------------------*/
var pv corev1.PersistentVolume
{
key := types.NamespacedName{Namespace: h.Pod.GetNamespace(), Name: pvc.Spec.VolumeName}
if errPV := retry.OnError(NotFoundBackoff,
func(err error) bool { // retry condition
return k8errors.IsNotFound(err)
},
func() error { // execution
return compute.K8SClient.Get(ctx, key, &pv)
},
); errPV != nil { // error checking
compute.PodError(h.Pod, "PVError", "PV (%s) has failed. err:'%W'",
pv.GetName(),
errPV,
)
}
}
h.logger.Info("PVC bounding info", "pvc", pvc.Spec, "pv", pv.Spec)
/*---------------------------------------------------
* Link the Referenced PV to the Pod's Volumes
*---------------------------------------------------*/
switch {
// // DEBUG ONLY
// case pv.Spec.HostPath != nil:
// dstFullPath := filepath.Join(h.podDirectory.VolumeDir(), vol.Name)
// if err := os.Symlink(pv.Spec.Local.Path, dstFullPath); err != nil {
// compute.SystemPanic(err, "cannot link symlink at path '%s'", dstFullPath)
// }
// h.logger.Info(" * HostPath Volume is mounted", "name", vol.Name)
// break
case pv.Spec.Local != nil:
dstFullPath := filepath.Join(h.podDirectory.VolumeDir(), vol.Name)
if err := os.Symlink(pv.Spec.Local.Path, dstFullPath); err != nil {
compute.SystemPanic(err, "cannot link symlink at path '%s'", dstFullPath)
}
h.logger.Info(" * Local Volume is mounted", "name", vol.Name)
default:
logrus.Warn(vol)
panic("It seems I have missed a PersistentVolume type")
}
}