-
Notifications
You must be signed in to change notification settings - Fork 8
/
hook.go
122 lines (105 loc) · 3.44 KB
/
hook.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// Code generated by ack-generate. DO NOT EDIT.
package log_group
import (
"context"
svcapitypes "github.com/aws-controllers-k8s/cloudwatchlogs-controller/apis/v1alpha1"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
svcsdk "github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
func (rm *resourceManager) updateRetentionPeriod(
ctx context.Context,
desired *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.updateRetentionPeriod")
defer func(err error) {
exit(err)
}(err)
if desired.ko.Spec.RetentionDays != nil && *desired.ko.Spec.RetentionDays != 0 {
input := &svcsdk.PutRetentionPolicyInput{
RetentionInDays: desired.ko.Spec.RetentionDays,
LogGroupName: desired.ko.Spec.Name,
}
_, err = rm.sdkapi.PutRetentionPolicyWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "PutRetentionPolicy", err)
if err != nil {
return err
}
return nil
}
input := &svcsdk.DeleteRetentionPolicyInput{
LogGroupName: desired.ko.Spec.Name,
}
_, err = rm.sdkapi.DeleteRetentionPolicyWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "DeleteRetentionPolicy", err)
if err != nil {
return err
}
return nil
}
// customUpdateLogGroup patches each of the resource properties in the backend AWS
// service API and returns a new resource with updated fields.
func (rm *resourceManager) customUpdateLogGroup(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (updated *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.customUpdateLogGroup")
defer exit(err)
// Merge in the information we read from the API call above to the copy of
// the original Kubernetes object we passed to the function
ko := desired.ko.DeepCopy()
rm.setStatusDefaults(ko)
if delta.DifferentAt("Spec.RetentionDays") {
if err := rm.updateRetentionPeriod(ctx, desired); err != nil {
return &resource{ko}, err
}
}
if desired.ko.Spec.RetentionDays != nil {
ko.Status.RetentionInDays = desired.ko.Spec.RetentionDays
} else {
var retention int64 = 0
ko.Status.RetentionInDays = &retention
}
return &resource{ko}, nil
}
// addRetentionToSpec copies retention value from status to spec so as to
// enable comparison during sdkUpdate phase.
func (rm *resourceManager) addRetentionToSpec(
ctx context.Context,
r *resource,
ko *svcapitypes.LogGroup,
) (err error) {
ko.Spec.RetentionDays = ko.Status.RetentionInDays
return
}
// customPreCompare ensures that default values of types are initialised and
// server side defaults are excluded from the delta.
func customPreCompare(
delta *ackcompare.Delta,
a *resource,
b *resource,
) {
var retention int64 = 0
if a.ko.Spec.RetentionDays == nil {
a.ko.Spec.RetentionDays = &retention
}
if b.ko.Spec.RetentionDays == nil {
b.ko.Spec.RetentionDays = &retention
}
}