From f5fb7530a004fd2cd70c052743bb9bbcdb3cf46b Mon Sep 17 00:00:00 2001 From: Ricardo Zanini Date: Mon, 17 Jun 2019 21:33:59 -0300 Subject: [PATCH] Parsing kube config file without reflection. Fixes #1591 --- .../client/internal/KubeConfigUtils.java | 18 +++-- .../internal/mappers/AuthInfoMapper.java | 49 +++++++++++++ .../internal/mappers/AuthProviderMapper.java | 33 +++++++++ .../internal/mappers/ClusterMapper.java | 37 ++++++++++ .../client/internal/mappers/ConfigMapper.java | 44 ++++++++++++ .../internal/mappers/ContextMapper.java | 36 ++++++++++ .../internal/mappers/ExecConfigMapper.java | 36 ++++++++++ .../internal/mappers/ExecEnvVarMapper.java | 32 +++++++++ .../internal/mappers/NamedAuthInfoMapper.java | 34 +++++++++ .../internal/mappers/NamedClusterMapper.java | 33 +++++++++ .../internal/mappers/NamedContextMapper.java | 34 +++++++++ .../mappers/NamedExtensionsMapper.java | 31 ++++++++ .../internal/mappers/PreferencesMapper.java | 34 +++++++++ .../internal/mappers/PropertyMapper.java | 72 +++++++++++++++++++ 14 files changed, 516 insertions(+), 7 deletions(-) create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/AuthInfoMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/AuthProviderMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ClusterMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ConfigMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ContextMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ExecConfigMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ExecEnvVarMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedAuthInfoMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedClusterMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedContextMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedExtensionsMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/PreferencesMapper.java create mode 100644 kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/PropertyMapper.java diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/KubeConfigUtils.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/KubeConfigUtils.java index cc2df1f1115..a2e10d348a3 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/KubeConfigUtils.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/KubeConfigUtils.java @@ -15,8 +15,6 @@ */ package io.fabric8.kubernetes.client.internal; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.fabric8.kubernetes.api.model.AuthInfo; import io.fabric8.kubernetes.api.model.Cluster; import io.fabric8.kubernetes.api.model.Config; @@ -24,25 +22,31 @@ import io.fabric8.kubernetes.api.model.NamedAuthInfo; import io.fabric8.kubernetes.api.model.NamedCluster; import io.fabric8.kubernetes.api.model.NamedContext; +import io.fabric8.kubernetes.client.internal.mappers.ConfigMapper; +import org.yaml.snakeyaml.Yaml; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.util.List; /** * Helper class for working with the YAML config file thats located in * ~/.kube/config which is updated when you use commands - * like osc login and osc project myproject + * like oc login and oc project myproject */ public class KubeConfigUtils { + public static Config parseConfig(File file) throws IOException { - ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); - return mapper.readValue(file, Config.class); + final Yaml yamlMapper = new Yaml(); + final ConfigMapper configMapper = new ConfigMapper(); + return configMapper.map(yamlMapper.load(new FileInputStream(file))); } public static Config parseConfigFromString(String contents) throws IOException { - ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); - return mapper.readValue(contents, Config.class); + final Yaml yamlMapper = new Yaml(); + final ConfigMapper configMapper = new ConfigMapper(); + return configMapper.map(yamlMapper.load(contents)); } /** diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/AuthInfoMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/AuthInfoMapper.java new file mode 100644 index 00000000000..ac52cac2d59 --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/AuthInfoMapper.java @@ -0,0 +1,49 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.AuthInfo; + +public class AuthInfoMapper extends PropertyMapper { + + private final NamedExtensionsMapper namedExtensionsMapper = new NamedExtensionsMapper(); + private final AuthProviderMapper authProviderConfigMapper = new AuthProviderMapper(); + private final ExecConfigMapper execConfigMapper = new ExecConfigMapper(); + + @SuppressWarnings("unchecked") + @Override + public AuthInfo map(Map mappedBean) { + final AuthInfo authInfo = new AuthInfo(); + authInfo.setAs(get("as", mappedBean)); + authInfo.setAsGroups(getList("as-groups", mappedBean)); + authInfo.setAsUserExtra(get("as-user-extra", mappedBean, Map.class)); + authInfo.setAuthProvider(get("auth-provider", mappedBean, authProviderConfigMapper)); + authInfo.setClientCertificate(get("client-certificate", mappedBean)); + authInfo.setClientCertificateData(get("client-certificate-data", mappedBean)); + authInfo.setClientKey(get("client-key", mappedBean)); + authInfo.setClientKeyData(get("client-key-data", mappedBean)); + authInfo.setExec(get("exec", mappedBean, execConfigMapper)); + authInfo.setExtensions(getList("extensions", mappedBean, namedExtensionsMapper)); + authInfo.setPassword(get("password", mappedBean)); + authInfo.setToken(get("token", mappedBean)); + authInfo.setTokenFile(get("tokenFile", mappedBean)); + authInfo.setUsername(get("username", mappedBean)); + return authInfo; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/AuthProviderMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/AuthProviderMapper.java new file mode 100644 index 00000000000..da8ea3a99dd --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/AuthProviderMapper.java @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.AuthProviderConfig; + +@SuppressWarnings("unchecked") +public class AuthProviderMapper extends PropertyMapper { + + @Override + public AuthProviderConfig map(Map mappedBean) { + final AuthProviderConfig bean = new AuthProviderConfig(); + bean.setConfig(get("config", mappedBean, Map.class)); + bean.setName(get("name", mappedBean)); + return bean; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ClusterMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ClusterMapper.java new file mode 100644 index 00000000000..a5061b500ae --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ClusterMapper.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.Cluster; + +public class ClusterMapper extends PropertyMapper { + + private final NamedExtensionsMapper namedExtensionsMapper = new NamedExtensionsMapper(); + + @Override + public Cluster map(Map mappedBean) { + final Cluster cluster = new Cluster(); + cluster.setCertificateAuthority(get("certificate-authority", mappedBean, String.class)); + cluster.setCertificateAuthorityData(get("certificate-authority-data", mappedBean, String.class)); + cluster.setExtensions(getList("extensions", mappedBean, namedExtensionsMapper)); + cluster.setInsecureSkipTlsVerify(get("insecure-skip-tls-verify", mappedBean, Boolean.class)); + cluster.setServer(get("server", mappedBean, String.class)); + return cluster; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ConfigMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ConfigMapper.java new file mode 100644 index 00000000000..7cd4f3c81b9 --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ConfigMapper.java @@ -0,0 +1,44 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.Config; + +public class ConfigMapper extends PropertyMapper { + + private final NamedClusterMapper namedClusterMapper = new NamedClusterMapper(); + private final NamedContextMapper namedContextMapper = new NamedContextMapper(); + private final NamedExtensionsMapper namedExtensionsMapper = new NamedExtensionsMapper(); + private final PreferencesMapper preferencesMapper = new PreferencesMapper(); + private final NamedAuthInfoMapper namedAuthInfoMapper = new NamedAuthInfoMapper(); + + @Override + public Config map(Map mappedBean) { + final Config config = new Config(); + config.setApiVersion(get("apiVersion", mappedBean, String.class)); + config.setClusters(getList("clusters", mappedBean, namedClusterMapper)); + config.setContexts(getList("contexts", mappedBean, namedContextMapper)); + config.setCurrentContext(get("current-context", mappedBean, String.class)); + config.setExtensions(getList("extensions", mappedBean, namedExtensionsMapper)); + config.setKind(get("kind", mappedBean, String.class)); + config.setPreferences(get("preferences", mappedBean, preferencesMapper)); + config.setUsers(getList("users", mappedBean, namedAuthInfoMapper)); + return config; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ContextMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ContextMapper.java new file mode 100644 index 00000000000..9ac0753c885 --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ContextMapper.java @@ -0,0 +1,36 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.Context; + +public class ContextMapper extends PropertyMapper { + + private final NamedExtensionsMapper namedExtensionsMapper = new NamedExtensionsMapper(); + + @Override + public Context map(Map mappedBean) { + final Context bean = new Context(); + bean.setCluster(get("cluster", mappedBean, String.class)); + bean.setUser(get("user", mappedBean, String.class)); + bean.setNamespace(get("namespace", mappedBean, String.class)); + bean.setExtensions(getList("extensions", mappedBean, namedExtensionsMapper)); + return bean; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ExecConfigMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ExecConfigMapper.java new file mode 100644 index 00000000000..c7ae09987a0 --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ExecConfigMapper.java @@ -0,0 +1,36 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.ExecConfig; + +public class ExecConfigMapper extends PropertyMapper { + + private final ExecEnvVarMapper execEnvVarMapper = new ExecEnvVarMapper(); + + @Override + public ExecConfig map(Map mappedBean) { + final ExecConfig bean = new ExecConfig(); + bean.setApiVersion(get("apiVersion", mappedBean)); + bean.setArgs(getList("args", mappedBean)); + bean.setCommand(get("command", mappedBean)); + bean.setEnv(getList("env", mappedBean, execEnvVarMapper)); + return bean; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ExecEnvVarMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ExecEnvVarMapper.java new file mode 100644 index 00000000000..772c2167792 --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/ExecEnvVarMapper.java @@ -0,0 +1,32 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.ExecEnvVar; + +public class ExecEnvVarMapper extends PropertyMapper { + + @Override + public ExecEnvVar map(Map mappedBean) { + final ExecEnvVar bean = new ExecEnvVar(); + bean.setName(get("name", mappedBean)); + bean.setValue(get("value", mappedBean)); + return bean; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedAuthInfoMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedAuthInfoMapper.java new file mode 100644 index 00000000000..e73bb73b15e --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedAuthInfoMapper.java @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.NamedAuthInfo; + +public class NamedAuthInfoMapper extends PropertyMapper { + + private final AuthInfoMapper authInfoMapper = new AuthInfoMapper(); + + @Override + public NamedAuthInfo map(Map mappedBean) { + final NamedAuthInfo bean = new NamedAuthInfo(); + bean.setName(get("name", mappedBean, String.class)); + bean.setUser(get("user", mappedBean, authInfoMapper)); + return bean; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedClusterMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedClusterMapper.java new file mode 100644 index 00000000000..cda07adcd99 --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedClusterMapper.java @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.NamedCluster; + +public class NamedClusterMapper extends PropertyMapper { + + private final ClusterMapper clusterMapper = new ClusterMapper(); + + @Override + public NamedCluster map(Map mappedBean) { + final NamedCluster bean = new NamedCluster(); + bean.setName(get("name", mappedBean, String.class)); + bean.setCluster(get("cluster", mappedBean, clusterMapper)); + return bean; + } +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedContextMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedContextMapper.java new file mode 100644 index 00000000000..3f834ab14d7 --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedContextMapper.java @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.NamedContext; + +public class NamedContextMapper extends PropertyMapper { + + private final ContextMapper contextMapper = new ContextMapper(); + + @Override + public NamedContext map(Map mappedBean) { + final NamedContext bean = new NamedContext(); + bean.setName(get("name", mappedBean)); + bean.setContext(get("context", mappedBean, contextMapper)); + return bean; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedExtensionsMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedExtensionsMapper.java new file mode 100644 index 00000000000..69c74380669 --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/NamedExtensionsMapper.java @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.NamedExtension; + +public class NamedExtensionsMapper extends PropertyMapper { + + @Override + public NamedExtension map(Map mappedBean) { + final NamedExtension bean = new NamedExtension(); + bean.setName(get("name", mappedBean)); + return bean; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/PreferencesMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/PreferencesMapper.java new file mode 100644 index 00000000000..3cc7e3f3b4a --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/PreferencesMapper.java @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.Map; + +import io.fabric8.kubernetes.api.model.Preferences; + +public class PreferencesMapper extends PropertyMapper { + + private final NamedExtensionsMapper namedExtensionsMapper = new NamedExtensionsMapper(); + + @Override + public Preferences map(Map mappedBean) { + final Preferences bean = new Preferences(); + bean.setColors(get("colors", mappedBean, Boolean.class)); + bean.setExtensions(getList("extensions", mappedBean, namedExtensionsMapper)); + return bean; + } + +} diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/PropertyMapper.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/PropertyMapper.java new file mode 100644 index 00000000000..77d8d3eaebb --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/internal/mappers/PropertyMapper.java @@ -0,0 +1,72 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 io.fabric8.kubernetes.client.internal.mappers; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@SuppressWarnings("unchecked") +public abstract class PropertyMapper { + + public abstract T map(Map mappedBean); + + protected String get(final String prop, final Map mappedBean) { + final Object value = mappedBean.get(prop); + if (value instanceof String && value != null) { + return (String) value; + } + return null; + } + + protected V get(final String prop, final Map mappedBean, final Class clazz) { + final Object value = mappedBean.get(prop); + if (clazz.isInstance(value) && value != null) { + return (V) value; + } + return null; + } + + protected V get(final String prop, final Map mappedBean, final PropertyMapper mapper) { + final Object value = mappedBean.get(prop); + if (value instanceof Map) { + return (V) mapper.map((Map) value); + } + return null; + } + + protected List getList(final String prop, final Map mappedBean) { + final Object value = mappedBean.get(prop); + if (value instanceof List && value != null) { + return (List) value; + } + return null; + } + + protected List getList(final String prop, final Map mappedBean, final PropertyMapper mapper) { + final Object value = mappedBean.get(prop); + List mappedList = new ArrayList<>(); + if (value instanceof List && value != null) { + ((List) value).forEach(v -> { + if (v instanceof Map && v != null) { + mappedList.add(mapper.map((Map) v)); + } + }); + } + return mappedList; + } + +}