-
Notifications
You must be signed in to change notification settings - Fork 109
/
store.go
133 lines (98 loc) · 3.02 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
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
/*
Copyright 2021 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package object_store
import (
"encoding/json"
"regexp"
"strings"
"github.com/tidwall/gjson"
yaml "gopkg.in/yaml.v2"
"github.com/deckhouse/deckhouse/testing/library"
)
type ErrObjectNotFound struct {
message string
}
func (e *ErrObjectNotFound) Error() string {
return e.message
}
type KubeObject map[string]interface{}
func (obj KubeObject) Field(path string) library.KubeResult {
jsonBytes, _ := json.Marshal(obj)
result := gjson.GetBytes(jsonBytes, path)
return library.KubeResult{Result: result}
}
func (obj KubeObject) Parse() library.KubeResult {
jsonBytes, _ := json.Marshal(obj)
result := gjson.ParseBytes(jsonBytes)
return library.KubeResult{Result: result}
}
func (obj KubeObject) ToYaml() string {
yamlBytes, _ := yaml.Marshal(obj)
return string(yamlBytes)
}
func (obj KubeObject) Exists() bool {
return len(obj) > 0
}
type MetaIndex struct {
Kind string
Namespace string
Name string
}
type ObjectStore map[MetaIndex]KubeObject
func (store ObjectStore) PutObject(object KubeObject, index MetaIndex) {
store[normalizeMetaIndex(index)] = object
}
func (store ObjectStore) GetObject(index MetaIndex) (KubeObject, bool) {
obj, ok := store[normalizeMetaIndex(index)]
return obj, ok
}
func (store ObjectStore) DeleteObject(index MetaIndex) {
delete(store, normalizeMetaIndex(index))
}
func (store ObjectStore) RetrieveObjectByMetaIndex(index MetaIndex) (object KubeObject, exists bool) {
object, exists = store[index]
return
}
func (store ObjectStore) KubernetesGlobalResource(kind, name string) KubeObject {
metaIndex := normalizeMetaIndex(NewMetaIndex(kind, "", name))
obj, _ := store.RetrieveObjectByMetaIndex(metaIndex)
return obj
}
func (store ObjectStore) KubernetesResource(kind, namespace, name string) KubeObject {
metaIndex := normalizeMetaIndex(NewMetaIndex(kind, namespace, name))
obj, _ := store.RetrieveObjectByMetaIndex(metaIndex)
return obj
}
func (store ObjectStore) ToYaml() string {
var buf strings.Builder
for _, object := range store {
buf.WriteString("---\n")
buf.WriteString(object.ToYaml())
}
return buf.String()
}
func NewMetaIndex(kind, namespace, name string) MetaIndex {
return MetaIndex{
Kind: kind,
Namespace: namespace,
Name: name,
}
}
func normalizeMetaIndex(index MetaIndex) MetaIndex {
r := regexp.MustCompile("(es|s)$")
return MetaIndex{
Kind: strings.ToLower(r.ReplaceAllString(index.Kind, "")),
Namespace: strings.ToLower(index.Namespace),
Name: strings.ToLower(index.Name),
}
}