-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfn.go
More file actions
164 lines (131 loc) · 5.16 KB
/
fn.go
File metadata and controls
164 lines (131 loc) · 5.16 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"context"
"strings"
"github.com/crossplane-contrib/function-tag-manager/filters"
"github.com/crossplane-contrib/function-tag-manager/input/v1beta1"
fncontext "github.com/crossplane/function-sdk-go/context"
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"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
"github.com/crossplane/crossplane-runtime/v2/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
)
// Function manages tags on composed resources based on configuration rules.
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{}
err := request.GetInput(req, in)
if 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
}
env := &unstructured.Unstructured{}
if v, ok := request.GetContextKey(req, fncontext.KeyEnvironment); ok {
err := resource.AsObject(v.GetStructValue(), env)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get Composition environment from %T context key %q", req, fncontext.KeyEnvironment))
return rsp, nil
}
f.log.Debug("Loaded Composition environment from Function context", "context-key", fncontext.KeyEnvironment)
}
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, env)
// 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 := filters.NewResourceFilter()
// Resolve tags to remove once for all resources (doesn't depend on individual resources)
removeTags := f.ResolveRemoveTags(in.RemoveTags, oxr, env)
for name, desired := range desiredComposed {
if IgnoreResource(desired) {
f.log.Debug("skipping resource due to ignore annotation or label", "resource", string(name), "gvk", desired.Resource.GroupVersionKind().String())
continue
}
if !SupportedManagedResource(desired, resourceFilter) {
f.log.Debug("skipping resource that doesn't support tags", "resource", string(name), "gvk", desired.Resource.GroupVersionKind().String())
continue
}
err := MergeTags(desired, additionalTags)
if err != nil {
f.log.Debug("error adding tags", "resource", string(name), "error", 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, env)
if ignoreTags != nil {
err := MergeTags(desired, *ignoreTags)
if err != nil {
f.log.Debug("error adding tags to ignore", "resource", string(name), "error", err.Error())
}
}
}
// Remove tags
if len(removeTags) > 0 {
err := RemoveTags(desired, removeTags)
if err != nil {
f.log.Debug("error removing tags", "resource", string(name), "error", err.Error())
}
}
}
err = response.SetDesiredComposedResources(rsp, desiredComposed)
if 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 or annotation set to ignore.
// If the annotation is present, it takes precedence over the label.
func IgnoreResource(dc *resource.DesiredComposed) bool {
if dc == nil {
return true
}
annotations := dc.Resource.GetAnnotations()
// Check annotation first - if present, it takes precedence
aval, ok := annotations[IgnoreResourceAnnotation]
if ok {
return strings.EqualFold(aval, "true")
}
// Fall back to label for backward compatibility
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
}