-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.go
84 lines (64 loc) · 2.38 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
package chart
import (
"context"
"fmt"
"github.com/giantswarm/apiextensions/pkg/apis/application/v1alpha1"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/resource/crud"
"github.com/giantswarm/app-operator/service/controller/app/controllercontext"
)
func (r *Resource) ApplyUpdateChange(ctx context.Context, obj, updateChange interface{}) error {
chart, err := toChart(updateChange)
if err != nil {
return microerror.Mask(err)
}
if chart.Name != "" {
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("updating Chart CR %#q in namespace %#q", chart.Name, chart.Namespace))
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
_, err = cc.Clients.K8s.G8sClient().ApplicationV1alpha1().Charts(chart.Namespace).Update(chart)
if err != nil {
return microerror.Mask(err)
}
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("updated Chart CR %#q in namespace %#q", chart.Name, chart.Namespace))
}
return nil
}
func (r *Resource) NewUpdatePatch(ctx context.Context, obj, currentChart, desiredChart interface{}) (*crud.Patch, error) {
create, err := r.newCreateChange(ctx, currentChart, desiredChart)
if err != nil {
return nil, microerror.Mask(err)
}
update, err := r.newUpdateChange(ctx, currentChart, desiredChart)
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, currentResource, desiredResource interface{}) (interface{}, error) {
currentChart, err := toChart(currentResource)
if err != nil {
return nil, microerror.Mask(err)
}
desiredChart, err := toChart(desiredResource)
if err != nil {
return nil, microerror.Mask(err)
}
r.logger.LogCtx(ctx, "level", "debug", "message", "finding out if the chart has to be updated")
updateChart := &v1alpha1.Chart{}
isModified := !isEmpty(currentChart) && !equals(currentChart, desiredChart)
if isModified {
r.logger.LogCtx(ctx, "level", "debug", "message", "the chart has to be updated")
updateChart = desiredChart.DeepCopy()
updateChart.ObjectMeta.ResourceVersion = currentChart.ObjectMeta.ResourceVersion
return updateChart, nil
} else {
r.logger.LogCtx(ctx, "level", "debug", "message", "the chart does not have to be updated")
}
return updateChart, nil
}