-
Notifications
You must be signed in to change notification settings - Fork 1
/
fn.go
130 lines (109 loc) · 3.98 KB
/
fn.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
127
128
129
130
package main
import (
"context"
"strings"
"github.com/crossplane-contrib/function-tag-manager/input/v1beta1"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/resource"
"github.com/crossplane/function-sdk-go/response"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/pkg/logging"
)
// Function returns whatever response you ask it to.
type Function struct {
fnv1.FunctionRunnerServiceServer
log logging.Logger
}
// RunFunction runs the Function.
func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) (*fnv1.RunFunctionResponse, error) {
f.log.Info("Running Function", "tag-manager", req.GetMeta().GetTag())
rsp := response.To(req, response.DefaultTTL)
in := &v1beta1.ManagedTags{}
if err := request.GetInput(req, in); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
return rsp, nil
}
oxr, err := request.GetObservedCompositeResource(req)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get observed composite resource from %T", req))
return rsp, nil
}
f.log.WithValues(
"xr-d", oxr.Resource.GetAPIVersion(),
"xr-kind", oxr.Resource.GetKind(),
"xr-name", oxr.Resource.GetName(),
)
// Process all the AddTags into 2 groups based on Policy: Replace or Retain
// we also need to resolve any tags coming from a Composite fieldpath
additionalTags := f.ResolveAddTags(in.AddTags, oxr)
// The composed resources that actually exist.
observedComposed, err := request.GetObservedComposedResources(req)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get observed composed resources from %T", req))
return rsp, nil
}
// The composed resources desired by any previous Functions in the pipeline.
desiredComposed, err := request.GetDesiredComposedResources(req)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get desired composed resources from %T", req))
return rsp, nil
}
resourceFilter := NewAWSResourceFilter()
for name, desired := range desiredComposed {
desired.Resource.GetObjectKind()
if IgnoreResource(desired) {
f.log.Debug("skipping resource due to label", string(name), desired.Resource.GroupVersionKind().String())
continue
}
if !SupportedManagedResource(desired, resourceFilter) {
f.log.Debug("skipping resource that doesn't support tags", string(name), desired.Resource.GroupVersionKind().String())
continue
}
err := MergeTags(desired, additionalTags)
if err != nil {
f.log.Debug("error adding tags", string(name), err.Error())
}
// Ignore tags only if there is an existing Composed resource with tags in the status
if observed, ok := observedComposed[name]; ok {
ignoreTags := f.ResolveIgnoreTags(in.IgnoreTags, oxr, &observed)
if ignoreTags != nil {
err := MergeTags(desired, *ignoreTags)
if err != nil {
f.log.Debug("error adding tags to ignore", string(name), err.Error())
}
}
}
removeTags := f.ResolveRemoveTags(in.RemoveTags, oxr)
// Remove tags
if len(removeTags) > 0 {
err := RemoveTags(desired, removeTags)
if err != nil {
f.log.Debug("error removing tags", string(name), err.Error())
}
}
}
if err := response.SetDesiredComposedResources(rsp, desiredComposed); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot set desired composed resources in %T", rsp))
return rsp, nil
}
response.Normalf(rsp, "Successfully Processed tags")
return rsp, nil
}
// IgnoreResource whether this resource has a label set to ignore.
func IgnoreResource(dc *resource.DesiredComposed) bool {
if dc == nil {
return true
}
var labels map[string]any
err := fieldpath.Pave(dc.Resource.Object).GetValueInto("metadata.labels", &labels)
if err != nil {
return false
}
val, ok := labels[IgnoreResourceLabel].(string)
if ok && strings.EqualFold(val, "true") {
return true
}
return false
}