-
Notifications
You must be signed in to change notification settings - Fork 22
/
create.go
66 lines (55 loc) · 1.81 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
package service
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/resourcecanceledcontext"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/aws-operator/v14/service/controller/key"
)
func (r *Resource) ApplyCreateChange(ctx context.Context, obj, createChange interface{}) error {
cr, err := key.ToCluster(ctx, obj)
if err != nil {
return microerror.Mask(err)
}
serviceToCreate, err := toService(createChange)
if err != nil {
return microerror.Mask(err)
}
if serviceToCreate != nil {
r.logger.Debugf(ctx, "creating service")
namespace := key.ClusterNamespace(cr)
_, err = r.k8sClient.CoreV1().Services(namespace).Create(ctx, serviceToCreate, metav1.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
// fall through
} else if apierrors.IsNotFound(err) {
r.logger.Debugf(ctx, "did not create service")
r.logger.Debugf(ctx, "namespace not found yet")
r.logger.Debugf(ctx, "canceling resource")
resourcecanceledcontext.SetCanceled(ctx)
return nil
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "created service")
} else {
r.logger.Debugf(ctx, "did not create service")
}
return nil
}
func (r *Resource) newCreateChange(ctx context.Context, obj, currentState, desiredState interface{}) (interface{}, error) {
currentService, err := toService(currentState)
if err != nil {
return nil, microerror.Mask(err)
}
desiredService, err := toService(desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
var serviceToCreate *corev1.Service
if currentService == nil || desiredService.Name != currentService.Name {
serviceToCreate = desiredService
}
return serviceToCreate, nil
}