-
Notifications
You must be signed in to change notification settings - Fork 22
/
update.go
79 lines (62 loc) · 2.21 KB
/
update.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
package service
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/resource/crud"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (r *Resource) ApplyUpdateChange(ctx context.Context, obj, updateChange interface{}) error {
serviceToUpdate, err := toService(updateChange)
if err != nil {
return microerror.Mask(err)
}
if serviceToUpdate != nil && serviceToUpdate.Spec.ClusterIP != "" {
r.logger.Debugf(ctx, "updating services")
_, err := r.k8sClient.CoreV1().Services(serviceToUpdate.Namespace).Update(ctx, serviceToUpdate, metav1.UpdateOptions{})
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "updated services")
} else {
r.logger.Debugf(ctx, "did not update service")
}
return nil
}
func (r *Resource) NewUpdatePatch(ctx context.Context, obj, currentState, desiredState interface{}) (*crud.Patch, error) {
create, err := r.newCreateChange(ctx, obj, currentState, desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
update, err := r.newUpdateChange(ctx, obj, currentState, desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
patch := crud.NewPatch()
patch.SetCreateChange(create)
patch.SetUpdateChange(update)
return patch, nil
}
// Service resources are updated.
func (r *Resource) newUpdateChange(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)
}
r.logger.Debugf(ctx, "finding out if the service has to be updated")
if isServiceModified(desiredService, currentService) {
// Make a copy and set the resource version so the service can be updated.
serviceToUpdate := desiredService.DeepCopy()
if currentService != nil {
serviceToUpdate.ObjectMeta.ResourceVersion = currentService.ObjectMeta.ResourceVersion
serviceToUpdate.Spec.ClusterIP = currentService.Spec.ClusterIP
}
r.logger.Debugf(ctx, "the service has to be updated")
return serviceToUpdate, nil
}
r.logger.Debugf(ctx, "the service does not have to be updated")
return nil, nil
}