generated from kyma-project/template-repository
/
pvc.go
64 lines (56 loc) · 1.98 KB
/
pvc.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
package chart
import (
"context"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
dockerRegistryPVCName = "serverless-docker-registry"
pvcKind = "PersistentVolumeClaim"
pvcVersion = "v1"
pvcGroup = ""
)
func AdjustDockerRegToClusterPVCSize(ctx context.Context, c client.Client, obj unstructured.Unstructured) (unstructured.Unstructured, error) {
if obj.GetName() != dockerRegistryPVCName {
return obj, nil
}
clusterPVC := corev1.PersistentVolumeClaim{}
objKey := client.ObjectKey{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}
if err := c.Get(ctx, objKey, &clusterPVC); err != nil {
if k8serrors.IsNotFound(err) {
return obj, nil
}
return obj, errors.Wrap(err, "while getting pvc from cluster")
}
objPVC := corev1.PersistentVolumeClaim{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &objPVC); err != nil {
return obj, errors.Wrap(err, "while converting unstructured to pvc")
}
storage := clusterPVC.Spec.Resources.Requests.Storage()
if storage.Equal(*objPVC.Spec.Resources.Requests.Storage()) {
return obj, nil
}
objPVCcopy := objPVC.DeepCopy()
objPVCcopy.Spec.Resources.Requests[corev1.ResourceStorage] = *clusterPVC.Spec.Resources.Requests.Storage()
out, err := runtime.DefaultUnstructuredConverter.ToUnstructured(objPVCcopy)
if err != nil {
return obj, errors.Wrap(err, "while converting copied pvc object to unstructured")
}
return unstructured.Unstructured{Object: out}, nil
}
func IsPVC(objKind schema.GroupVersionKind) bool {
expected := schema.GroupVersionKind{
Group: pvcGroup,
Version: pvcVersion,
Kind: pvcKind,
}
return expected.Group == objKind.Group && expected.Kind == objKind.Kind && expected.Version == objKind.Version
}