This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
/
create.go
68 lines (54 loc) · 1.83 KB
/
create.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
package pvc
import (
"context"
"github.com/giantswarm/microerror"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/kvm-operator/service/controller/key"
)
func (r *Resource) ApplyCreateChange(ctx context.Context, obj, createChange interface{}) error {
customObject, err := key.ToCustomObject(obj)
if err != nil {
return microerror.Mask(err)
}
pvcsToCreate, err := toPVCs(createChange)
if err != nil {
return microerror.Mask(err)
}
if len(pvcsToCreate) != 0 {
r.logger.Debugf(ctx, "creating the PVCs in the Kubernetes API")
namespace := key.ClusterNamespace(customObject)
for _, persistentVolumeClaim := range pvcsToCreate {
_, err := r.k8sClient.CoreV1().PersistentVolumeClaims(namespace).Create(ctx, persistentVolumeClaim.DeepCopy(), v1.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
// fall through
} else if err != nil {
return microerror.Mask(err)
}
}
r.logger.Debugf(ctx, "created the PVCs in the Kubernetes API")
} else {
r.logger.Debugf(ctx, "the PVCs do not need to be created in the Kubernetes API")
}
return nil
}
func (r *Resource) newCreateChange(ctx context.Context, obj, currentState, desiredState interface{}) (interface{}, error) {
currentPVCs, err := toPVCs(currentState)
if err != nil {
return nil, microerror.Mask(err)
}
desiredPVCs, err := toPVCs(desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
r.logger.Debugf(ctx, "finding out which PVCs have to be created")
var pvcsToCreate []corev1.PersistentVolumeClaim
for _, desiredPVC := range desiredPVCs {
if !containsPVC(currentPVCs, desiredPVC) {
pvcsToCreate = append(pvcsToCreate, desiredPVC)
}
}
r.logger.Debugf(ctx, "found %d PVCs that have to be created", len(pvcsToCreate))
return pvcsToCreate, nil
}