forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
64 lines (58 loc) · 1.73 KB
/
store.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
package secret
import (
"strings"
"context"
"github.com/rancher/norman/store/proxy"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/types/config"
)
type Store struct {
types.Store
}
func (s *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
t := convert.ToString(data["kind"])
t = strings.TrimPrefix(t, "namespaced")
t = convert.Uncapitalize(t)
data["kind"] = t
return s.Store.Create(apiContext, schema, data)
}
func NewNamespacedSecretStore(ctx context.Context, clientGetter proxy.ClientGetter) *Store {
secretsStore := proxy.NewProxyStore(ctx, clientGetter,
config.UserStorageContext,
[]string{"api"},
"",
"v1",
"Secret",
"secrets")
return &Store{
Store: &transform.Store{
Store: secretsStore,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
if data == nil {
return data, nil
}
anns, _ := data["annotations"].(map[string]interface{})
if anns["secret.user.cattle.io/secret"] == "true" {
return nil, nil
}
if data["projectId"] != nil {
fieldProjectID, _ := data["projectId"].(string)
projectID := strings.Split(fieldProjectID, ":")
id := ""
if len(projectID) == 2 {
id = projectID[1]
}
if id == data["namespaceId"] {
return nil, nil
}
}
parts := strings.Split(convert.ToString(data["type"]), "/")
parts[len(parts)-1] = "namespaced" + convert.Capitalize(parts[len(parts)-1])
data["type"] = strings.Join(parts, "/")
return data, nil
},
},
}
}