forked from kubernetes-sigs/controller-tools
/
known_types.go
128 lines (114 loc) · 4.82 KB
/
known_types.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
/*
Copyright 2019 The Kubernetes Authors.
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 crd
import (
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/Diggs/controller-tools/pkg/loader"
)
// KnownPackages overrides types in some comment packages that have custom validation
// but don't have validation markers on them (since they're from core Kubernetes).
var KnownPackages = map[string]PackageOverride{
"k8s.io/api/core/v1": func(p *Parser, pkg *loader.Package) {
// Explicit defaulting for the corev1.Protocol type in lieu of https://github.com/kubernetes/enhancements/pull/1928
p.Schemata[TypeIdent{Name: "Protocol", Package: pkg}] = apiext.JSONSchemaProps{
Type: "string",
Default: &apiext.JSON{Raw: []byte(`"TCP"`)},
}
p.AddPackage(pkg)
},
"k8s.io/apimachinery/pkg/apis/meta/v1": func(p *Parser, pkg *loader.Package) {
// ObjectMeta is managed by the Kubernetes API server, so no need to
// generate validation for it.
p.Schemata[TypeIdent{Name: "ObjectMeta", Package: pkg}] = apiext.JSONSchemaProps{
Type: "object",
}
p.Schemata[TypeIdent{Name: "Time", Package: pkg}] = apiext.JSONSchemaProps{
Type: "string",
Format: "date-time",
}
p.Schemata[TypeIdent{Name: "MicroTime", Package: pkg}] = apiext.JSONSchemaProps{
Type: "string",
Format: "date-time",
}
p.Schemata[TypeIdent{Name: "Duration", Package: pkg}] = apiext.JSONSchemaProps{
// TODO(directxman12): regexp validation for this (or get kube to support it as a format value)
Type: "string",
}
p.Schemata[TypeIdent{Name: "Fields", Package: pkg}] = apiext.JSONSchemaProps{
// this is a recursive structure that can't be flattened or, for that matter, properly generated.
// so just treat it as an arbitrary map
Type: "object",
AdditionalProperties: &apiext.JSONSchemaPropsOrBool{Allows: true},
}
p.AddPackage(pkg) // get the rest of the types
},
"k8s.io/apimachinery/pkg/api/resource": func(p *Parser, pkg *loader.Package) {
p.Schemata[TypeIdent{Name: "Quantity", Package: pkg}] = apiext.JSONSchemaProps{
// TODO(directxman12): regexp validation for this (or get kube to support it as a format value)
XIntOrString: true,
AnyOf: []apiext.JSONSchemaProps{
{Type: "integer"},
{Type: "string"},
},
Pattern: "^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$",
}
// No point in calling AddPackage, this is the sole inhabitant
},
"k8s.io/apimachinery/pkg/runtime": func(p *Parser, pkg *loader.Package) {
p.Schemata[TypeIdent{Name: "RawExtension", Package: pkg}] = apiext.JSONSchemaProps{
// TODO(directxman12): regexp validation for this (or get kube to support it as a format value)
Type: "object",
}
p.AddPackage(pkg) // get the rest of the types
},
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured": func(p *Parser, pkg *loader.Package) {
p.Schemata[TypeIdent{Name: "Unstructured", Package: pkg}] = apiext.JSONSchemaProps{
Type: "object",
}
p.AddPackage(pkg) // get the rest of the types
},
"k8s.io/apimachinery/pkg/util/intstr": func(p *Parser, pkg *loader.Package) {
p.Schemata[TypeIdent{Name: "IntOrString", Package: pkg}] = apiext.JSONSchemaProps{
XIntOrString: true,
AnyOf: []apiext.JSONSchemaProps{
{Type: "integer"},
{Type: "string"},
},
}
// No point in calling AddPackage, this is the sole inhabitant
},
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1": func(p *Parser, pkg *loader.Package) {
p.Schemata[TypeIdent{Name: "JSON", Package: pkg}] = apiext.JSONSchemaProps{
XPreserveUnknownFields: boolPtr(true),
}
p.AddPackage(pkg) // get the rest of the types
},
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1": func(p *Parser, pkg *loader.Package) {
p.Schemata[TypeIdent{Name: "JSON", Package: pkg}] = apiext.JSONSchemaProps{
XPreserveUnknownFields: boolPtr(true),
}
p.AddPackage(pkg) // get the rest of the types
},
}
func boolPtr(b bool) *bool {
return &b
}
// AddKnownTypes registers the packages overrides in KnownPackages with the given parser.
func AddKnownTypes(parser *Parser) {
// ensure everything is there before adding to PackageOverrides
// TODO(directxman12): this is a bit of a hack, maybe just use constructors?
parser.init()
for pkgName, override := range KnownPackages {
parser.PackageOverrides[pkgName] = override
}
}