-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfn.go
363 lines (309 loc) · 11.6 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph"
"github.com/upbound/function-azresourcegraph/input/v1beta1"
"google.golang.org/protobuf/types/known/structpb"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/response"
)
// AzureQueryInterface defines the methods required for querying Azure resources.
type AzureQueryInterface interface {
azQuery(ctx context.Context, azureCreds map[string]string, in *v1beta1.Input) (armresourcegraph.ClientResourcesResponse, error)
}
// Function returns whatever response you ask it to.
type Function struct {
fnv1.UnimplementedFunctionRunnerServiceServer
azureQuery AzureQueryInterface
log logging.Logger
}
// RunFunction runs the Function.
func (f *Function) RunFunction(ctx context.Context, req *fnv1.RunFunctionRequest) (*fnv1.RunFunctionResponse, error) {
f.log.Info("Running function", "tag", req.GetMeta().GetTag())
rsp := response.To(req, response.DefaultTTL)
in := &v1beta1.Input{}
if err := request.GetInput(req, in); err != nil {
// You can set a custom status condition on the claim. This allows you to
// communicate with the user. See the link below for status condition
// guidance.
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
response.ConditionFalse(rsp, "FunctionSuccess", "InternalError").
WithMessage("Something went wrong.").
TargetCompositeAndClaim()
// You can emit an event regarding the claim. This allows you to communicate
// with the user. Note that events should be used sparingly and are subject
// to throttling; see the issue below for more information.
// https://github.com/crossplane/crossplane/issues/5802
response.Warning(rsp, errors.New("something went wrong")).
TargetCompositeAndClaim()
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
return rsp, nil
}
azureCreds, err := getCreds(req)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}
if f.azureQuery == nil {
f.azureQuery = &AzureQuery{}
}
switch {
case in.QueryRef == nil:
case strings.HasPrefix(*in.QueryRef, "status."):
err := getQueryFromStatus(req, in)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}
case strings.HasPrefix(*in.QueryRef, "context."):
functionContext := req.GetContext().AsMap()
if queryFromContext, ok := GetNestedKey(functionContext, strings.TrimPrefix(*in.QueryRef, "context.")); ok {
in.Query = queryFromContext
}
default:
response.Fatal(rsp, errors.Errorf("Unrecognized QueryRef field: %s", *in.QueryRef))
return rsp, nil
}
if in.Query == "" {
response.Warning(rsp, errors.New("Query is empty"))
f.log.Info("WARNING: ", "query is empty", in.Query)
return rsp, nil
}
results, err := f.azureQuery.azQuery(ctx, azureCreds, in)
if err != nil {
response.Fatal(rsp, err)
f.log.Info("FAILURE: ", "failure", fmt.Sprint(err))
return rsp, nil
}
// Print the obtained query results
f.log.Info("Query:", "query", in.Query)
f.log.Info("Results:", "results", fmt.Sprint(results.Data))
response.Normalf(rsp, "Query: %q", in.Query)
switch {
case strings.HasPrefix(in.Target, "status."):
err = putQueryResultToStatus(req, rsp, in, results, f)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}
case strings.HasPrefix(in.Target, "context."):
err = putQueryResultToContext(req, rsp, in, results, f)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}
default:
response.Fatal(rsp, errors.Errorf("Unrecognized target field: %s", in.Target))
return rsp, nil
}
// You can set a custom status condition on the claim. This allows you to
// communicate with the user. See the link below for status condition
// guidance.
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
response.ConditionTrue(rsp, "FunctionSuccess", "Success").
TargetCompositeAndClaim()
return rsp, nil
}
func getCreds(req *fnv1.RunFunctionRequest) (map[string]string, error) {
var azureCreds map[string]string
rawCreds := req.GetCredentials()
if credsData, ok := rawCreds["azure-creds"]; ok {
credsData := credsData.GetCredentialData().GetData()
if credsJSON, ok := credsData["credentials"]; ok {
err := json.Unmarshal(credsJSON, &azureCreds)
if err != nil {
return nil, errors.Wrap(err, "cannot parse json credentials")
}
}
} else {
return nil, errors.New("failed to get azure-creds credentials")
}
return azureCreds, nil
}
// AzureQuery is a concrete implementation of the AzureQueryInterface
// that interacts with Azure Resource Graph API.
type AzureQuery struct{}
func (a *AzureQuery) azQuery(ctx context.Context, azureCreds map[string]string, in *v1beta1.Input) (armresourcegraph.ClientResourcesResponse, error) {
tenantID := azureCreds["tenantId"]
clientID := azureCreds["clientId"]
clientSecret := azureCreds["clientSecret"]
subscriptionID := azureCreds["subscriptionId"]
// To configure DefaultAzureCredential to authenticate a user-assigned managed identity,
// set the environment variable AZURE_CLIENT_ID to the identity's client ID.
cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, nil)
if err != nil {
return armresourcegraph.ClientResourcesResponse{}, errors.Wrap(err, "failed to obtain credentials")
}
// Create and authorize a ResourceGraph client
client, err := armresourcegraph.NewClient(cred, nil)
if err != nil {
return armresourcegraph.ClientResourcesResponse{}, errors.Wrap(err, "failed to create client")
}
queryRequest := armresourcegraph.QueryRequest{
Query: to.Ptr(in.Query),
}
if len(subscriptionID) > 0 {
queryRequest.Subscriptions = []*string{to.Ptr(subscriptionID)}
}
if len(in.ManagementGroups) > 0 {
queryRequest.ManagementGroups = in.ManagementGroups
}
// Create the query request, Run the query and get the results.
results, err := client.Resources(ctx, queryRequest, nil)
if err != nil {
return armresourcegraph.ClientResourcesResponse{}, errors.Wrap(err, "failed to finish the request")
}
return results, nil
}
// ParseNestedKey enables the bracket and dot notation to key reference
func ParseNestedKey(key string) ([]string, error) {
var parts []string
// Regular expression to extract keys, supporting both dot and bracket notation
regex := regexp.MustCompile(`\[([^\[\]]+)\]|([^.\[\]]+)`)
matches := regex.FindAllStringSubmatch(key, -1)
for _, match := range matches {
if match[1] != "" {
parts = append(parts, match[1]) // Bracket notation
} else if match[2] != "" {
parts = append(parts, match[2]) // Dot notation
}
}
if len(parts) == 0 {
return nil, errors.New("invalid key")
}
return parts, nil
}
// GetNestedKey retrieves a nested string value from a map using dot notation keys.
func GetNestedKey(context map[string]interface{}, key string) (string, bool) {
parts, err := ParseNestedKey(key)
if err != nil {
return "", false
}
currentValue := interface{}(context)
for _, k := range parts {
// Check if the current value is a map
if nestedMap, ok := currentValue.(map[string]interface{}); ok {
// Get the next value in the nested map
if nextValue, exists := nestedMap[k]; exists {
currentValue = nextValue
} else {
return "", false
}
} else {
return "", false
}
}
// Convert the final value to a string
if result, ok := currentValue.(string); ok {
return result, true
}
return "", false
}
// SetNestedKey sets a value to a nested key from a map using dot notation keys.
func SetNestedKey(root map[string]interface{}, key string, value interface{}) error {
parts, err := ParseNestedKey(key)
if err != nil {
return err
}
current := root
for i, part := range parts {
if i == len(parts)-1 {
// Set the value at the final key
current[part] = value
return nil
}
// Traverse into nested maps or create them if they don't exist
if next, exists := current[part]; exists {
if nextMap, ok := next.(map[string]interface{}); ok {
current = nextMap
} else {
return fmt.Errorf("key %q exists but is not a map", part)
}
} else {
// Create a new map if the path doesn't exist
newMap := make(map[string]interface{})
current[part] = newMap
current = newMap
}
}
return nil
}
func getQueryFromStatus(req *fnv1.RunFunctionRequest, in *v1beta1.Input) error {
oxr, err := request.GetObservedCompositeResource(req)
if err != nil {
return errors.Wrap(err, "cannot get observed composite resource")
}
xrStatus := make(map[string]interface{})
err = oxr.Resource.GetValueInto("status", &xrStatus)
if err != nil {
return errors.Wrap(err, "cannot get XR status")
}
if queryFromXRStatus, ok := GetNestedKey(xrStatus, strings.TrimPrefix(*in.QueryRef, "status.")); ok {
in.Query = queryFromXRStatus
}
return nil
}
func putQueryResultToStatus(req *fnv1.RunFunctionRequest, rsp *fnv1.RunFunctionResponse, in *v1beta1.Input, results armresourcegraph.ClientResourcesResponse, f *Function) error {
oxr, err := request.GetObservedCompositeResource(req)
if err != nil {
return errors.Wrap(err, "cannot get observed composite resource")
}
// The composite resource desired by previous functions in the pipeline.
dxr, err := request.GetDesiredCompositeResource(req)
if err != nil {
return errors.Wrap(err, "cannot get desired composite resource")
}
dxr.Resource.SetAPIVersion(oxr.Resource.GetAPIVersion())
dxr.Resource.SetKind(oxr.Resource.GetKind())
xrStatus := make(map[string]interface{})
err = oxr.Resource.GetValueInto("status", &xrStatus)
if err != nil {
f.log.Debug("Cannot get status from XR")
}
// Update the specific status field using the reusable function
statusField := strings.TrimPrefix(in.Target, "status.")
err = SetNestedKey(xrStatus, statusField, results.Data)
if err != nil {
return errors.Wrapf(err, "cannot set status field %s to %v", statusField, results.Data)
}
// Write the updated status field back into the composite resource
if err := dxr.Resource.SetValue("status", xrStatus); err != nil {
return errors.Wrap(err, "cannot write updated status back into composite resource")
}
// Save the updated desired composite resource
if err := response.SetDesiredCompositeResource(rsp, dxr); err != nil {
return errors.Wrapf(err, "cannot set desired composite resource in %T", rsp)
}
return nil
}
func putQueryResultToContext(req *fnv1.RunFunctionRequest, rsp *fnv1.RunFunctionResponse, in *v1beta1.Input, results armresourcegraph.ClientResourcesResponse, f *Function) error {
contextField := strings.TrimPrefix(in.Target, "context.")
data, err := structpb.NewValue(results.Data)
if err != nil {
return errors.Wrap(err, "cannot convert results data to structpb.Value")
}
// Convert existing context into a map[string]interface{}
contextMap := req.GetContext().AsMap()
err = SetNestedKey(contextMap, contextField, data.AsInterface())
if err != nil {
return errors.Wrap(err, "failed to update context key")
}
f.log.Debug("Updating Composition Pipeline Context", "key", contextField, "data", &results.Data)
// Convert the updated context back into structpb.Struct
updatedContext, err := structpb.NewStruct(contextMap)
if err != nil {
return errors.Wrap(err, "failed to serialize updated context")
}
// Set the updated context
rsp.Context = updatedContext
return nil
}