19
19
import java .io .IOException ;
20
20
import java .io .Reader ;
21
21
import java .io .StringReader ;
22
+ import java .util .ArrayList ;
22
23
import java .util .HashMap ;
24
+ import java .util .List ;
23
25
import java .util .Map ;
24
26
import java .util .Set ;
27
+ import org .apache .commons .lang3 .tuple .MutablePair ;
28
+ import org .apache .commons .lang3 .tuple .Pair ;
25
29
import org .slf4j .Logger ;
26
30
import org .slf4j .LoggerFactory ;
27
31
import org .yaml .snakeyaml .constructor .Constructor ;
@@ -32,52 +36,85 @@ public class Yaml {
32
36
private static org .yaml .snakeyaml .Yaml yaml =
33
37
new org .yaml .snakeyaml .Yaml (new CustomConstructor ());
34
38
private static Map <String , Class <?>> classes = new HashMap <>();
39
+ private static Map <String , String > apiGroups = new HashMap <>();
40
+ private static List <String > apiVersions = new ArrayList <>();
35
41
36
42
static final Logger logger = LoggerFactory .getLogger (Yaml .class );
37
43
38
- public static String getApiGroupVersion (String name ) {
39
- if (name .startsWith ("AppsV1" )) {
40
- return "apps/v1" ;
41
- }
42
- if (name .startsWith ("AppsV1beta1" )) {
43
- return "apps/v1beta1" ;
44
- }
45
- if (name .startsWith ("ExtensionsV1beta1" )) {
46
- return "extensions/v1beta1" ;
47
- }
48
- if (name .startsWith ("ExtensionsV1" )) {
49
- return "extensions/v1" ;
50
- }
51
- if (name .startsWith ("V1beta1" )) {
52
- return "v1beta1" ;
53
- }
54
- if (name .startsWith ("V1beta2" )) {
55
- return "v1beta2" ;
56
- }
57
- if (name .startsWith ("V1alpha1" )) {
58
- return "v1alpha1" ;
59
- }
60
- if (name .startsWith ("V2beta1" )) {
61
- return "v2beta1" ;
62
- }
63
- if (name .startsWith ("V2alpha1" )) {
64
- return "v2alpha1" ;
44
+ private static void initApiGroupMap () {
45
+ apiGroups .put ("Admissionregistration" , "admissionregistration.k8s.io" );
46
+ apiGroups .put ("Apiextensions" , "apiextensions.k8s.io" );
47
+ apiGroups .put ("Apiregistration" , "apiregistration.k8s.io" );
48
+ apiGroups .put ("Apps" , "apps" );
49
+ apiGroups .put ("Authentication" , "authentication.k8s.io" );
50
+ apiGroups .put ("Authorization" , "authorization.k8s.io" );
51
+ apiGroups .put ("Autoscaling" , "autoscaling" );
52
+ apiGroups .put ("Extensions" , "extensions" );
53
+ apiGroups .put ("Batch" , "batch" );
54
+ apiGroups .put ("Certificates" , "certificates.k8s.io" );
55
+ apiGroups .put ("Networking" , "networking.k8s.io" );
56
+ apiGroups .put ("Policy" , "policy" );
57
+ apiGroups .put ("RbacAuthorization" , "rbac.authorization.k8s.io" );
58
+ apiGroups .put ("Scheduling" , "scheduling.k8s.io" );
59
+ apiGroups .put ("Settings" , "settings.k8s.io" );
60
+ apiGroups .put ("Storage" , "storage.k8s.io" );
61
+ }
62
+
63
+ private static void initApiVersionList () {
64
+ // Order important
65
+ apiVersions .add ("V2beta1" );
66
+ apiVersions .add ("V2alpha1" );
67
+ apiVersions .add ("V1beta2" );
68
+ apiVersions .add ("V1beta1" );
69
+ apiVersions .add ("V1alpha1" );
70
+ apiVersions .add ("V1" );
71
+ }
72
+
73
+ private static Pair <String , String > getApiGroup (String name ) {
74
+ MutablePair <String , String > parts = new MutablePair <>();
75
+ for (String prefix : apiGroups .keySet ()) {
76
+ if (name .startsWith (prefix )) {
77
+ parts .left = apiGroups .get (prefix );
78
+ parts .right = name .substring (prefix .length ());
79
+ break ;
80
+ }
65
81
}
66
- if (name .startsWith ("V1" )) {
67
- return "v1" ;
82
+ if (parts .left == null ) parts .right = name ;
83
+
84
+ return parts ;
85
+ }
86
+
87
+ private static Pair <String , String > getApiVersion (String name ) {
88
+ MutablePair <String , String > parts = new MutablePair <>();
89
+ for (String version : apiVersions ) {
90
+ if (name .startsWith (version )) {
91
+ parts .left = version .toLowerCase ();
92
+ parts .right = name .substring (version .length ());
93
+ break ;
94
+ }
68
95
}
69
- return name ;
96
+ if (parts .left == null ) parts .right = name ;
97
+
98
+ return parts ;
70
99
}
71
100
72
101
private static void initModelMap () throws IOException {
102
+ initApiGroupMap ();
103
+ initApiVersionList ();
104
+
73
105
ClassPath cp = ClassPath .from (ClassLoader .getSystemClassLoader ());
74
106
Set <ClassPath .ClassInfo > allClasses = cp .getTopLevelClasses ("io.kubernetes.client.models" );
75
107
76
108
for (ClassPath .ClassInfo clazz : allClasses ) {
77
- String groupVersion = getApiGroupVersion (clazz .getSimpleName ());
78
- int len = groupVersion .replace ("/" , "" ).length ();
79
- String name = clazz .getSimpleName ().substring (len );
80
- classes .put (groupVersion + "/" + name , clazz .load ());
109
+ String modelName = "" ;
110
+ Pair <String , String > nameParts = getApiGroup (clazz .getSimpleName ());
111
+ modelName += nameParts .getLeft () == null ? "" : nameParts .getLeft () + "/" ;
112
+
113
+ nameParts = getApiVersion (nameParts .getRight ());
114
+ modelName += nameParts .getLeft () == null ? "" : nameParts .getLeft () + "/" ;
115
+ modelName += nameParts .getRight ();
116
+
117
+ classes .put (modelName , clazz .load ());
81
118
}
82
119
}
83
120
@@ -141,6 +178,12 @@ public static Object load(Reader reader) throws IOException {
141
178
}
142
179
143
180
Class <?> clazz = (Class <?>) classes .get (apiVersion + "/" + kind );
181
+ if (clazz == null ) {
182
+ // Attempt to detect class from version and kind alone
183
+ if (apiVersion .contains ("/" )) {
184
+ clazz = (Class <?>) classes .get (apiVersion .split ("/" )[1 ] + "/" + kind );
185
+ }
186
+ }
144
187
if (clazz == null ) {
145
188
throw new IOException (
146
189
"Unknown apiVersionKind: "
0 commit comments