@@ -5,35 +5,79 @@ import (
5
5
"sync"
6
6
7
7
"sigs.k8s.io/structured-merge-diff/v4/typed"
8
+ "sigs.k8s.io/yaml"
8
9
9
10
_ "embed"
10
11
)
11
12
12
13
//go:embed kubernetes_builtin_schema.yaml
13
14
var kubernetesBuiltinSchemaYAML string
14
15
16
+ //go:embed kubernetes_builtin_schema.meta.yaml
17
+ var kubernetesBuiltinSchemaMetaYAML string
18
+
19
+ const BuiltinKey = "kubernetes_builtin_schema"
20
+
15
21
type Schema struct {
16
22
Parser * typed.Parser
23
+ Meta * SchemaMeta
17
24
}
18
25
19
- var cacheKubernetesBuiltinSchema * Schema
26
+ type SchemaMeta struct {
27
+ Resources []SchemaMetaResource `json:"resources"`
28
+ }
20
29
21
- var mutexKubernetesBuiltinSchema sync.Mutex
30
+ type SchemaMetaResource struct {
31
+ Key string `json:"key"`
32
+ Group string `json:"group"`
33
+ Version string `json:"version"`
34
+ Kind string `json:"kind"`
35
+ Resource string `json:"resource"`
36
+ Scope string `json:"scope"`
37
+ }
38
+
39
+ type schemaCache struct {
40
+ mutex sync.Mutex
41
+ schemas map [string ]* Schema
42
+ }
43
+
44
+ var globalSchemaCache schemaCache
22
45
23
46
func KubernetesBuiltInSchema () (* Schema , error ) {
24
- mutexKubernetesBuiltinSchema .Lock ()
25
- defer mutexKubernetesBuiltinSchema .Unlock ()
47
+ return globalSchemaCache .Get (BuiltinKey )
48
+ }
49
+
50
+ func (c * schemaCache ) Get (key string ) (* Schema , error ) {
51
+ c .mutex .Lock ()
52
+ defer c .mutex .Unlock ()
26
53
27
- if cacheKubernetesBuiltinSchema != nil {
28
- return cacheKubernetesBuiltinSchema , nil
54
+ if schema := c . schemas [ key ]; schema != nil {
55
+ return schema , nil
29
56
}
30
57
31
- schemaYAML := kubernetesBuiltinSchemaYAML
58
+ if key != BuiltinKey {
59
+ return nil , fmt .Errorf ("schema %q not known" , key )
60
+ }
32
61
62
+ schemaYAML := kubernetesBuiltinSchemaYAML
33
63
parser , err := typed .NewParser (typed .YAMLObject (schemaYAML ))
34
64
if err != nil {
35
65
return nil , fmt .Errorf ("error parsing schema: %w" , err )
36
66
}
37
- cacheKubernetesBuiltinSchema = & Schema {Parser : parser }
38
- return cacheKubernetesBuiltinSchema , nil
67
+
68
+ metaYAML := kubernetesBuiltinSchemaMetaYAML
69
+ meta := & SchemaMeta {}
70
+ if err := yaml .Unmarshal ([]byte (metaYAML ), meta ); err != nil {
71
+ return nil , fmt .Errorf ("error parsing schema metadata: %w" , err )
72
+ }
73
+
74
+ schema := & Schema {
75
+ Parser : parser ,
76
+ Meta : meta ,
77
+ }
78
+ if c .schemas == nil {
79
+ c .schemas = make (map [string ]* Schema )
80
+ }
81
+ c .schemas [key ] = schema
82
+ return schema , nil
39
83
}
0 commit comments