|
18 | 18 | import java.io.IOException;
|
19 | 19 | import java.io.Reader;
|
20 | 20 | import java.io.StringReader;
|
21 |
| -import java.util.HashMap; |
22 |
| -import java.util.Map; |
23 |
| -import java.util.Set; |
| 21 | +import java.util.*; |
24 | 22 | import org.slf4j.Logger;
|
25 | 23 | import org.slf4j.LoggerFactory;
|
26 | 24 |
|
27 | 25 | public class Yaml {
|
28 | 26 | private static org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml();
|
29 | 27 | private static Map<String, Class<?>> classes = new HashMap<>();
|
| 28 | + private static Map<String, String> apiGroups = new HashMap<>(); |
| 29 | + private static List<String> apiVersions = new ArrayList<>(); |
30 | 30 |
|
31 | 31 | static final Logger logger = LoggerFactory.getLogger(Yaml.class);
|
32 | 32 |
|
33 |
| - public static String getApiGroupVersion(String name) { |
34 |
| - if (name.startsWith("AppsV1beta2")) { |
35 |
| - return "apps/v1beta2"; |
36 |
| - } |
37 |
| - if (name.startsWith("AppsV1beta1")) { |
38 |
| - return "apps/v1beta1"; |
39 |
| - } |
40 |
| - if (name.startsWith("AppsV1")) { |
41 |
| - return "apps/v1"; |
42 |
| - } |
43 |
| - if (name.startsWith("ExtensionsV1beta1")) { |
44 |
| - return "extensions/v1beta1"; |
45 |
| - } |
46 |
| - if (name.startsWith("ExtensionsV1")) { |
47 |
| - return "extensions/v1"; |
48 |
| - } |
49 |
| - if (name.startsWith("V1beta1")) { |
50 |
| - return "v1beta1"; |
51 |
| - } |
52 |
| - if (name.startsWith("V1beta2")) { |
53 |
| - return "v1beta2"; |
54 |
| - } |
55 |
| - if (name.startsWith("V1alpha1")) { |
56 |
| - return "v1alpha1"; |
57 |
| - } |
58 |
| - if (name.startsWith("V2beta1")) { |
59 |
| - return "v2beta1"; |
60 |
| - } |
61 |
| - if (name.startsWith("V2alpha1")) { |
62 |
| - return "v2alpha1"; |
63 |
| - } |
64 |
| - if (name.startsWith("V1")) { |
65 |
| - return "v1"; |
66 |
| - } |
67 |
| - return name; |
| 33 | + private static void initApiGroupMap() { |
| 34 | + apiGroups.put("Admissionregistration", "admissionregistration.k8s.io"); |
| 35 | + apiGroups.put("Apiextensions", "apiextensions.k8s.io"); |
| 36 | + apiGroups.put("Apiregistration", "apiregistration.k8s.io"); |
| 37 | + apiGroups.put("Apps", "apps"); |
| 38 | + apiGroups.put("Authentication", "authentication.k8s.io"); |
| 39 | + apiGroups.put("Authorization", "authorization.k8s.io"); |
| 40 | + apiGroups.put("Autoscaling", "autoscaling"); |
| 41 | + apiGroups.put("Extensions", "extensions"); |
| 42 | + apiGroups.put("Batch", "batch"); |
| 43 | + apiGroups.put("Certificates", "certificates.k8s.io"); |
| 44 | + apiGroups.put("Networking", "networking.k8s.io"); |
| 45 | + apiGroups.put("Policy", "policy"); |
| 46 | + apiGroups.put("RbacAuthorization", "rbac.authorization.k8s.io"); |
| 47 | + apiGroups.put("Scheduling", "scheduling.k8s.io"); |
| 48 | + apiGroups.put("Settings", "settings.k8s.io"); |
| 49 | + apiGroups.put("Storage", "storage.k8s.io"); |
| 50 | + } |
| 51 | + |
| 52 | + private static void initApiVersionList() { |
| 53 | + // Order important |
| 54 | + apiVersions.add("V2beta1"); |
| 55 | + apiVersions.add("V2alpha1"); |
| 56 | + apiVersions.add("V1beta2"); |
| 57 | + apiVersions.add("V1beta1"); |
| 58 | + apiVersions.add("V1alpha1"); |
| 59 | + apiVersions.add("V1"); |
| 60 | + } |
| 61 | + |
| 62 | + private static String[] getApiGroup(String name) { |
| 63 | + String[] parts = new String[2]; |
| 64 | + |
| 65 | + for (String prefix : apiGroups.keySet()) { |
| 66 | + if (name.startsWith(prefix)) { |
| 67 | + parts[0] = apiGroups.get(prefix); |
| 68 | + parts[1] = name.substring(prefix.length()); |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + if (parts[0] == null) parts[1] = name; |
| 73 | + |
| 74 | + return parts; |
| 75 | + } |
| 76 | + |
| 77 | + private static String[] getApiVersion(String name) { |
| 78 | + String[] parts = new String[2]; |
| 79 | + for (String version : apiVersions) { |
| 80 | + if (name.startsWith(version)) { |
| 81 | + parts[0] = version.toLowerCase(); |
| 82 | + parts[1] = name.substring(version.length()); |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + if (parts[0] == null) parts[1] = name; |
| 87 | + |
| 88 | + return parts; |
68 | 89 | }
|
69 | 90 |
|
70 | 91 | private static void initModelMap() throws IOException {
|
| 92 | + initApiGroupMap(); |
| 93 | + initApiVersionList(); |
| 94 | + |
71 | 95 | ClassPath cp = ClassPath.from(ClassLoader.getSystemClassLoader());
|
72 | 96 | Set<ClassPath.ClassInfo> allClasses = cp.getTopLevelClasses("io.kubernetes.client.models");
|
73 | 97 |
|
74 | 98 | for (ClassPath.ClassInfo clazz : allClasses) {
|
75 |
| - String groupVersion = getApiGroupVersion(clazz.getSimpleName()); |
76 |
| - int len = groupVersion.replace("/", "").length(); |
77 |
| - String name = clazz.getSimpleName().substring(len); |
78 |
| - classes.put(groupVersion + "/" + name, clazz.load()); |
| 99 | + String modelName = ""; |
| 100 | + String[] nameParts = getApiGroup(clazz.getSimpleName()); |
| 101 | + modelName += nameParts[0] == null ? "" : nameParts[0] + "/"; |
| 102 | + |
| 103 | + nameParts = getApiVersion(nameParts[1]); |
| 104 | + modelName += nameParts[0] == null ? "" : nameParts[0] + "/"; |
| 105 | + modelName += nameParts[1]; |
| 106 | + |
| 107 | + classes.put(modelName, clazz.load()); |
79 | 108 | }
|
80 | 109 | }
|
81 | 110 |
|
|
0 commit comments