-
Notifications
You must be signed in to change notification settings - Fork 9
/
op_applyrolebinding.go
70 lines (58 loc) · 2.09 KB
/
op_applyrolebinding.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
// Copyright 2022 Namespace Labs Inc; 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.
package kubeops
import (
"context"
"encoding/json"
rbacv1 "k8s.io/client-go/applyconfigurations/rbac/v1"
"namespacelabs.dev/foundation/framework/kubernetes/kubeblueprint"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/kubernetes/kubetool"
"namespacelabs.dev/foundation/internal/fnerrors"
fnschema "namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/execution"
)
func registerApplyRoleBinding() {
execution.Compile[*kubedef.OpApplyRoleBinding](func(ctx context.Context, inputs []*fnschema.SerializedInvocation) ([]*fnschema.SerializedInvocation, error) {
ns, err := kubedef.InjectedKubeClusterNamespace(ctx)
if err != nil {
return nil, err
}
var res []*fnschema.SerializedInvocation
for _, input := range inputs {
spec := &kubedef.OpApplyRoleBinding{}
if err := input.Impl.UnmarshalTo(spec); err != nil {
return nil, err
}
scope := kubeblueprint.ClusterScope
if spec.Namespaced {
scope = kubeblueprint.NamespaceScope
}
var rules []*rbacv1.PolicyRuleApplyConfiguration
if err := json.Unmarshal([]byte(spec.RulesJson), &rules); err != nil {
return nil, fnerrors.InternalError("failed to unmarshal rules: %w", err)
}
invocations := kubeblueprint.MakeInvocations(input.Description, scope, &kubetool.ContextualEnv{Namespace: ns.KubeConfig().Namespace},
spec.RoleName, spec.RoleBindingName, makeMap(spec.Label), makeMap(spec.Annotation), spec.ServiceAccount, rules)
for _, inv := range invocations {
compiled, err := inv.ToDefinition(fnschema.PackageNames(input.Scope...)...)
if err != nil {
return nil, err
}
res = append(res, compiled)
}
}
return res, nil
})
}
func makeMap(kvs []*kubedef.OpApplyRoleBinding_KeyValue) map[string]string {
if len(kvs) == 0 {
return nil
}
m := map[string]string{}
for _, kv := range kvs {
m[kv.Key] = kv.Value
}
return m
}