This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelpers.go
90 lines (78 loc) · 2.6 KB
/
helpers.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
package client
import (
"context"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
"github.com/thoas/go-funk"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
type OwnerReferences struct {
ResourceUID types.UID
v1.OwnerReference
}
const (
ContextFieldName = "context"
ContextFieldDesc = "Name of the context from k8s configuration."
)
var CommonContextField = schema.Column{
Name: ContextFieldName,
Description: ContextFieldDesc,
Type: schema.TypeString,
Resolver: ResolveContext,
}
// ContextMultiplex returns a list of clients for each context from the cq config
func ContextMultiplex(meta schema.ClientMeta) []schema.ClientMeta {
client := meta.(*Client)
clients := make([]schema.ClientMeta, 0)
for _, ctxName := range client.contexts {
clients = append(clients, client.WithContext(ctxName))
}
return clients
}
// APIFilterContextMultiplex returns a list of clients for each context from the cq config
func APIFilterContextMultiplex(path string) func(meta schema.ClientMeta) []schema.ClientMeta {
return func(meta schema.ClientMeta) []schema.ClientMeta {
client := meta.(*Client)
// in kubernetes version below 1.4 paths is nil
if client.paths != nil {
if _, ok := client.paths[path]; !ok {
client.Logger().Warn("The resource is not supported by current version of k8s", "path", path)
return []schema.ClientMeta{}
}
}
clients := make([]schema.ClientMeta, 0, len(client.contexts))
for _, ctxName := range client.contexts {
clients = append(clients, client.WithContext(ctxName))
}
return clients
}
}
// DeleteContextFilter returns a delete filter that cleans up the data belonging to the k8s context.
func DeleteContextFilter(meta schema.ClientMeta, _ *schema.Resource) []interface{} {
client := meta.(*Client)
return []interface{}{ContextFieldName, client.Context}
}
// ResolveContext is a resolver that fills the k8s context field.
func ResolveContext(_ context.Context, meta schema.ClientMeta, r *schema.Resource, c schema.Column) error {
client := meta.(*Client)
return r.Set(c.Name, client.Context)
}
func OwnerReferenceResolver(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
v := funk.Get(parent.Item, "ObjectMeta")
if v == nil {
return nil
}
objMeta := v.(v1.ObjectMeta)
if len(objMeta.OwnerReferences) == 0 {
return nil
}
refs := make([]OwnerReferences, len(objMeta.OwnerReferences))
for i, o := range objMeta.OwnerReferences {
refs[i] = OwnerReferences{
ResourceUID: parent.Get("uid").(types.UID),
OwnerReference: o,
}
}
res <- refs
return nil
}