-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
46 lines (37 loc) · 1.05 KB
/
namespace.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
package namespace
import (
"time"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/types"
"github.com/rancher/types/apis/cluster.cattle.io/v3/schema"
"github.com/rancher/types/client/cluster/v3"
"k8s.io/apimachinery/pkg/util/cache"
)
var (
namespaceOwnerMap = cache.NewLRUExpireCache(1000)
)
func updateNamespaceOwnerMap(apiContext *types.APIContext) error {
var namespaces []client.Namespace
if err := access.List(apiContext, &schema.Version, client.NamespaceType, &types.QueryOptions{}, &namespaces); err != nil {
return err
}
for _, namespace := range namespaces {
namespaceOwnerMap.Add(namespace.Name, namespace.ProjectID, time.Hour)
}
return nil
}
func ProjectMap(apiContext *types.APIContext, refresh bool) (map[string]string, error) {
if refresh {
err := updateNamespaceOwnerMap(apiContext)
if err != nil {
return nil, err
}
}
data := map[string]string{}
for _, key := range namespaceOwnerMap.Keys() {
if val, ok := namespaceOwnerMap.Get(key); ok {
data[key.(string)] = val.(string)
}
}
return data, nil
}