forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owner.go
137 lines (112 loc) · 2.99 KB
/
owner.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
package pod
import (
"fmt"
"time"
"strings"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/controllers/user/workload"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/project.cattle.io/v3/schema"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/cache"
)
var (
ownerCache = cache.NewLRUExpireCache(1000)
)
type key struct {
SubContext string
Namespace string
Kind string
Name string
}
type value struct {
Kind string
Name string
}
func getOwnerWithKind(apiContext *types.APIContext, namespace, ownerKind, name string) (string, string, error) {
subContext := apiContext.SubContext["/v3/schemas/project"]
if subContext == "" {
subContext = apiContext.SubContext["/v3/schemas/cluster"]
}
if subContext == "" {
logrus.Warnf("failed to find subcontext to lookup replicaSet owner")
return "", "", nil
}
key := key{
SubContext: subContext,
Namespace: namespace,
Kind: strings.ToLower(ownerKind),
Name: name,
}
val, ok := ownerCache.Get(key)
if ok {
value, _ := val.(value)
return value.Kind, value.Name, nil
}
data := map[string]interface{}{}
if err := access.ByID(apiContext, &schema.Version, ownerKind, ref.FromStrings(namespace, name), &data); err != nil {
return "", "", err
}
kind, name := getOwner(data)
ownerCache.Add(key, value{
Kind: kind,
Name: name,
}, time.Hour)
return kind, name, nil
}
func getOwner(data map[string]interface{}) (string, string) {
ownerReferences, ok := values.GetSlice(data, "ownerReferences")
if !ok {
return "", ""
}
for _, ownerReference := range ownerReferences {
controller, _ := ownerReference["controller"].(bool)
if !controller {
continue
}
kind, _ := ownerReference["kind"].(string)
name, _ := ownerReference["name"].(string)
return kind, name
}
return "", ""
}
func SaveOwner(apiContext *types.APIContext, kind, name string, data map[string]interface{}) {
parentKind, parentName := getOwner(data)
namespace, _ := data["namespaceId"].(string)
subContext := apiContext.SubContext["/v3/schemas/project"]
if subContext == "" {
subContext = apiContext.SubContext["/v3/schemas/cluster"]
}
if subContext == "" {
return
}
key := key{
SubContext: subContext,
Namespace: namespace,
Kind: strings.ToLower(kind),
Name: name,
}
ownerCache.Add(key, value{
Kind: parentKind,
Name: parentName,
}, time.Hour)
}
func resolveWorkloadID(apiContext *types.APIContext, data map[string]interface{}) string {
kind, name := getOwner(data)
if kind == "" {
return ""
}
namespace, _ := data["namespaceId"].(string)
if ownerKind := strings.ToLower(kind); ownerKind == workload.ReplicaSetType || ownerKind == workload.JobType {
k, n, err := getOwnerWithKind(apiContext, namespace, ownerKind, name)
if err != nil {
return ""
}
if k != "" {
kind, name = k, n
}
}
return strings.ToLower(fmt.Sprintf("%s:%s:%s", kind, namespace, name))
}