/
update.go
126 lines (106 loc) · 3.49 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
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
package s3object
import (
"bytes"
"context"
"io"
"strings"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/resource/crud"
"github.com/giantswarm/aws-operator/v15/service/controller/controllercontext"
)
func (r *Resource) ApplyUpdateChange(ctx context.Context, obj, updateChange interface{}) error {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
s3Objects, err := toS3Objects(updateChange)
if err != nil {
return microerror.Mask(err)
}
if len(s3Objects) != 0 {
for _, s3Object := range s3Objects {
r.logger.Debugf(ctx, "updating S3 object %#q", *s3Object.Key)
_, err = cc.Client.TenantCluster.AWS.S3.PutObject(s3Object)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "updated S3 object %#q", *s3Object.Key)
}
} else {
r.logger.Debugf(ctx, "did not update any S3 object")
}
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
}
func (r *Resource) newUpdateChange(ctx context.Context, obj, currentState, desiredState interface{}) (interface{}, error) {
currentS3Objects, err := toS3Objects(currentState)
if err != nil {
return nil, microerror.Mask(err)
}
desiredS3Objects, err := toS3Objects(desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
if len(currentS3Objects) == 0 {
// In case there is no current state we need to create first and cannot
// update.
return nil, nil
}
// We do a poor man's comparison here to figure out if we have to deal with a
// change between current and desired state. The first and most straight
// forward thing to do at this point is to simply check how many S3 Objects we
// have. When current and desired state do not have the same number of items,
// we simply apply all of the desired state without being smarter about it.
var num int
{
if len(currentS3Objects) != len(desiredS3Objects) {
return desiredS3Objects, nil
}
num = len(currentS3Objects)
}
// The passed resource state defines the actual Cloud Config content as
// io.ReadSeaker. In order to compare the current and desired state we need to
// read and re-apply the byte stream once we read it. Otherwise we would flush
// content and it would not be available anymore for create or update calls.
// Note that we apply the same primitive comparison here as described above.
// In case one item of current state does equal the desired item at the same
// position in the list, we simply apply all of the desired state without
// being smarter about it.
for i := 0; i < num; i++ {
currentS3Object := currentS3Objects[i]
desiredS3Object := desiredS3Objects[i]
var c []byte
{
c, err = io.ReadAll(currentS3Object.Body)
if err != nil {
return nil, microerror.Mask(err)
}
currentS3Object.Body = strings.NewReader(string(c))
}
var d []byte
{
d, err = io.ReadAll(desiredS3Object.Body)
if err != nil {
return nil, microerror.Mask(err)
}
desiredS3Object.Body = strings.NewReader(string(d))
}
if !bytes.Equal(c, d) {
return desiredS3Objects, nil
}
}
return nil, nil
}