From 06bb51c70d8b6cfcbae82aa2e615be8ff7afa050 Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Thu, 8 Dec 2022 13:53:18 -0500 Subject: [PATCH] fix #3972 load should not be overloaded --- CHANGELOG.md | 1 + .../resourcelock/ConfigMapLock.java | 23 ++-- .../kubernetes/client/utils/IOHelpers.java | 6 +- .../resourcelock/ConfigMapLockTest.java | 10 +- .../client/dsl/internal/OperationSupport.java | 1 - .../client/mock/LeaderElectionTest.java | 1 + .../server/mock/LoadAsTemplateTest.java | 2 +- .../client/server/mock/OpenShiftLoadTest.java | 15 ++- .../openshift/client/OpenShiftClient.java | 33 ++++++ .../internal/core/TemplateOperationsImpl.java | 50 --------- .../client/impl/OpenShiftClientImpl.java | 18 --- ...hDeleteRecreateWaitApplicableListImpl.java | 106 ------------------ 12 files changed, 63 insertions(+), 203 deletions(-) delete mode 100644 openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6df2a34947b..1593fa8e45a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ * Fix #4579: the implicit registration of resource and list types that happens when using the resource(class) methods has been removed. This makes the behavior of the client more predictable as that was an undocumented side-effect. If you expect to see instances of a custom type from an untyped api call - typically KubernetesClient.load, KubernetesClient.resourceList, KubernetesClient.resource(InputStream|String), then you must either create a META-INF/services/io.fabric8.kubernetes.api.model.KubernetesResource file (see above #3923), or make calls to KubernetesDeserializer.registerCustomKind - however since KubernetesDeserializer is an internal class that mechanism is not preferred. * Fix #4597: remove the deprecated support for `javax.validation.constraints.NotNull` in the `crd-generator`, to mark a property as `required` it needs to be annotated with `io.fabric8.generator.annotation.Required` * Fix #3973: removed support for Parameterizable - that was only needed as a workaround for non-string parameters. You should instead include those parameter values in the map passed to processLocally. +* Fix #3973: openShiftClient.templates().load and openShiftClient.load will no longer automatically process / create templates. Use the method openshiftClient.templateFrom as a helper to get a template from any stream. ### 6.2.0 (2022-10-20) diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLock.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLock.java index 0395ea26d69..576b262b339 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLock.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLock.java @@ -15,13 +15,11 @@ */ package io.fabric8.kubernetes.client.extended.leaderelection.resourcelock; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.KubernetesClientException; import io.fabric8.kubernetes.client.utils.Serialization; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,13 +34,11 @@ public class ConfigMapLock implements Lock { private final String configMapNamespace; private final String configMapName; private final String identity; - private final ObjectMapper objectMapper; public ConfigMapLock(String configMapNamespace, String configMapName, String identity) { this.configMapNamespace = Objects.requireNonNull(configMapNamespace, "configMapNamespace is required"); this.configMapName = Objects.requireNonNull(configMapName, "configMapName is required"); this.identity = Objects.requireNonNull(identity, "identity is required"); - objectMapper = Serialization.jsonMapper(); } /** @@ -58,9 +54,8 @@ public LeaderElectionRecord get(KubernetesClient client) { .map(annotations -> annotations.get(LEADER_ELECTION_RECORD_ANNOTATION_KEY)) .map(annotation -> { try { - return objectMapper.readValue(annotation, new TypeReference() { - }); - } catch (JsonProcessingException ex) { + return Serialization.unmarshal(annotation, LeaderElectionRecord.class); + } catch (KubernetesClientException ex) { LOGGER.error("Error deserializing LeaderElectionRecord from ConfigMap", ex); return null; } @@ -80,11 +75,11 @@ public void create( KubernetesClient client, LeaderElectionRecord leaderElectionRecord) throws LockException { try { - client.configMaps().inNamespace(configMapNamespace).withName(configMapName).create(new ConfigMapBuilder() + client.configMaps().inNamespace(configMapNamespace).resource(new ConfigMapBuilder() .editOrNewMetadata().withNamespace(configMapNamespace).withName(configMapName) - .addToAnnotations(LEADER_ELECTION_RECORD_ANNOTATION_KEY, objectMapper.writeValueAsString(leaderElectionRecord)) + .addToAnnotations(LEADER_ELECTION_RECORD_ANNOTATION_KEY, Serialization.asJson(leaderElectionRecord)) .endMetadata() - .build()); + .build()).create(); } catch (Exception e) { throw new LockException("Unable to create ConfigMapLock", e); } @@ -100,11 +95,11 @@ public void update( try { final ConfigMap toReplace = client.configMaps().inNamespace(configMapNamespace).withName(configMapName).get(); toReplace.getMetadata().getAnnotations() - .put(LEADER_ELECTION_RECORD_ANNOTATION_KEY, objectMapper.writeValueAsString(leaderElectionRecord)); + .put(LEADER_ELECTION_RECORD_ANNOTATION_KEY, Serialization.asJson(leaderElectionRecord)); // Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version - client.configMaps().inNamespace(configMapNamespace).withName(configMapName) + client.configMaps().inNamespace(configMapNamespace).resource(toReplace) .lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion())) - .replace(toReplace); + .replace(); } catch (Exception e) { throw new LockException("Unable to update ConfigMapLock", e); } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/IOHelpers.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/IOHelpers.java index a700165c9d7..e51978245ad 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/IOHelpers.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/IOHelpers.java @@ -75,7 +75,11 @@ public static boolean isJSONValid(String json) { } public static String convertYamlToJson(String yaml) throws IOException { - return Serialization.asJson(Serialization.unmarshal(yaml)); + ObjectMapper yamlReader = Serialization.yamlMapper(); + Object obj = yamlReader.readValue(yaml, Object.class); + + ObjectMapper jsonWriter = Serialization.jsonMapper(); + return jsonWriter.writeValueAsString(obj); } public static String convertToJson(String jsonOrYaml) throws IOException { diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLockTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLockTest.java index a6f2bb8bc47..31848cbcde6 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLockTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLockTest.java @@ -21,13 +21,11 @@ import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.dsl.MixedOperation; -import io.fabric8.kubernetes.client.dsl.ReplaceDeletable; import io.fabric8.kubernetes.client.dsl.Resource; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; -import org.mockito.Answers; import org.mockito.ArgumentMatchers; import org.mockito.Mockito; @@ -42,7 +40,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -128,15 +125,13 @@ void createWithValidLeaderElectionRecordShouldSendPostRequest() throws Exception // When lock.create(kc, record); // Then - verify(configMaps.withName("name"), times(1)).create(any(ConfigMap.class)); + verify(configMaps.resource(any(ConfigMap.class)), times(1)).create(); } @Test void updateWithValidLeaderElectionRecordShouldSendPutRequest() throws Exception { // Given final Resource configMapResource = configMaps.withName("name"); - final ReplaceDeletable replaceable = mock(ReplaceDeletable.class, Answers.RETURNS_DEEP_STUBS); - when(configMapResource.lockResourceVersion(any())).thenReturn(replaceable); final ConfigMap configMapInTheCluster = new ConfigMap(); configMapInTheCluster.setMetadata(new ObjectMetaBuilder().withAnnotations(new HashMap<>()).build()); when(configMapResource.get()).thenReturn(configMapInTheCluster); @@ -144,10 +139,11 @@ void updateWithValidLeaderElectionRecordShouldSendPutRequest() throws Exception "1337", Duration.ofSeconds(1), ZonedDateTime.now(), ZonedDateTime.now(), 0); record.setVersion("313373"); final ConfigMapLock lock = new ConfigMapLock("namespace", "name", "1337"); + // When lock.update(kc, record); // Then - verify(replaceable, times(1)).replace(eq(configMapInTheCluster)); + verify(configMaps.resource(any()).lockResourceVersion("313373")).replace(); } @Test diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/OperationSupport.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/OperationSupport.java index 71964f3acf4..1b909b9fd5c 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/OperationSupport.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/OperationSupport.java @@ -474,7 +474,6 @@ protected Status handleDeploymentRollback(String resourceUrl, DeploymentRollback * @param template argument provided * * @return returns a deserialized object as api server response of provided type. - * @throws InterruptedException Interrupted Exception * @throws IOException IOException */ protected T handleGet(URL resourceUrl, Class type) throws IOException { diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java index a3b2e7c4afc..63df1237701 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java @@ -64,6 +64,7 @@ public void singleLeaderConfigMapLockUpdateTest() throws Exception { server.expect().get().withPath("/api/v1/namespaces/namespace/configmaps/name") .andReturn(200, new ConfigMapBuilder() .withNewMetadata() + .withName("name") .withResourceVersion("1") .withAnnotations(Collections.singletonMap( "control-plane.alpha.kubernetes.io/leader", diff --git a/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/LoadAsTemplateTest.java b/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/LoadAsTemplateTest.java index a0006cdac01..1427b632e8f 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/LoadAsTemplateTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/LoadAsTemplateTest.java @@ -55,7 +55,7 @@ void tearDown() { @Test void shouldLoadPodAsTemplate() { - KubernetesList list = client.templates().load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")) + KubernetesList list = client.templateFrom(getClass().getResourceAsStream("/test-pod-create-from-load.yml")) .processLocally(); assertNotNull(list); assertNotNull(list.getItems()); diff --git a/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftLoadTest.java b/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftLoadTest.java index 2bc3582bc22..ecaf5243834 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftLoadTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftLoadTest.java @@ -16,31 +16,35 @@ package io.fabric8.openshift.client.server.mock; import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.openshift.client.OpenShiftClient; import org.junit.jupiter.api.Test; +import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -@EnableOpenShiftMockClient +@EnableKubernetesMockClient class OpenShiftLoadTest { - OpenShiftMockServer server; + KubernetesMockServer server; OpenShiftClient client; @Test void testResourceGetFromLoadWhenSingleDocumentsWithStartingDelimiter() { // when - List result = client.load(getClass().getResourceAsStream("/test-template.yml")).get(); + List result = client.templateFrom(getClass().getResourceAsStream("/test-template.yml")) + .processLocally(Collections.emptyMap()).getItems(); // then assertNotNull(result); assertEquals(5, result.size()); HasMetadata deploymentResource = result.get(2); - assertEquals("image.openshift.io/v1", deploymentResource.getApiVersion()); + assertEquals("v1", deploymentResource.getApiVersion()); assertEquals("ImageStream", deploymentResource.getKind()); assertEquals("eap-app", deploymentResource.getMetadata().getName()); } @@ -49,7 +53,8 @@ void testResourceGetFromLoadWhenSingleDocumentsWithStartingDelimiter() { void testResourceGetFromLoadWhenSingleDocumentsWithoutDelimiter() { // when - List result = client.load(getClass().getResourceAsStream("/template-with-params.yml")).get(); + List result = client.templateFrom(getClass().getResourceAsStream("/template-with-params.yml")).get() + .getObjects(); // then assertNotNull(result); diff --git a/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftClient.java b/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftClient.java index d8ff1c277f1..6441cbd1b7d 100644 --- a/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftClient.java +++ b/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftClient.java @@ -16,12 +16,14 @@ package io.fabric8.openshift.client; +import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.RequestConfig; import io.fabric8.kubernetes.client.VersionInfo; import io.fabric8.kubernetes.client.dsl.*; import io.fabric8.kubernetes.client.extension.SupportTestingClient; +import io.fabric8.kubernetes.client.utils.Utils; import io.fabric8.openshift.api.model.*; import io.fabric8.openshift.api.model.miscellaneous.apiserver.v1.APIRequestCount; import io.fabric8.openshift.api.model.miscellaneous.apiserver.v1.APIRequestCountList; @@ -37,7 +39,9 @@ import io.fabric8.openshift.api.model.miscellaneous.network.operator.v1.OperatorPKIList; import io.fabric8.openshift.client.dsl.*; +import java.io.InputStream; import java.net.URL; +import java.util.List; public interface OpenShiftClient extends KubernetesClient, SupportTestingClient { @@ -450,6 +454,35 @@ public interface OpenShiftClient extends KubernetesClient, SupportTestingClient */ MixedOperation> templates(); + /** + * Create a template from the given stream. If it is already a template, that will be returned unchanged. + * Any other HasMetadata will be returned as items in a Template with a randomly generated name. + * + * @param is + * @return the Template + */ + default TemplateResource templateFrom(InputStream is) { + Template template = null; + // TODO: should be item + List items = load(is).get(); + Object item = items; + if (items.size() == 1) { + item = items.get(0); + } + if (item instanceof Template) { + template = (Template) item; + } else { + String generatedName = "template-" + Utils.randomString(5); + template = new TemplateBuilder() + .withNewMetadata() + .withName(generatedName) + .endMetadata() + .withObjects(items).build(); + } + + return templates().resource(template); + } + /** * API entrypoint for TemplateInstance(template.openshift.io/v1) * diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/core/TemplateOperationsImpl.java b/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/core/TemplateOperationsImpl.java index 745e0e6b1ed..01b8457add6 100644 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/core/TemplateOperationsImpl.java +++ b/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/core/TemplateOperationsImpl.java @@ -20,7 +20,6 @@ import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; -import io.fabric8.kubernetes.api.model.KubernetesResourceList; import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.KubernetesClientException; import io.fabric8.kubernetes.client.dsl.MixedOperation; @@ -28,11 +27,9 @@ import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperationsImpl; import io.fabric8.kubernetes.client.dsl.internal.OperationContext; import io.fabric8.kubernetes.client.http.HttpRequest; -import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.kubernetes.client.utils.Utils; import io.fabric8.openshift.api.model.Parameter; import io.fabric8.openshift.api.model.Template; -import io.fabric8.openshift.api.model.TemplateBuilder; import io.fabric8.openshift.api.model.TemplateList; import io.fabric8.openshift.client.ParameterValue; import io.fabric8.openshift.client.dsl.TemplateResource; @@ -45,8 +42,6 @@ import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -202,49 +197,4 @@ private URL getProcessUrl() throws MalformedURLException { return getNamespacedUrl(getNamespace(), "processedtemplates"); } - @Override - public TemplateResource load(InputStream is) { - String generatedName = "template-" + Utils.randomString(5); - Template template = null; - Object item = Serialization.unmarshal(is); - if (item instanceof Template) { - template = (Template) item; - } else if (item instanceof HasMetadata) { - HasMetadata h = (HasMetadata) item; - template = new TemplateBuilder() - .withNewMetadata() - .withName(generatedName) - .withNamespace(h.getMetadata() != null ? h.getMetadata().getNamespace() : null) - .endMetadata() - .withObjects(h).build(); - } else if (item instanceof KubernetesResourceList) { - List list = ((KubernetesResourceList) item).getItems(); - template = new TemplateBuilder() - .withNewMetadata() - .withName(generatedName) - .endMetadata() - .withObjects(list.toArray(new HasMetadata[list.size()])).build(); - } else if (item instanceof HasMetadata[]) { - template = new TemplateBuilder() - .withNewMetadata() - .withName(generatedName) - .endMetadata() - .withObjects((HasMetadata[]) item).build(); - } else if (item instanceof Collection) { - List items = new ArrayList<>(); - for (Object o : (Collection) item) { - if (o instanceof HasMetadata) { - items.add((HasMetadata) o); - } - } - template = new TemplateBuilder() - .withNewMetadata() - .withName(generatedName) - .endMetadata() - .withObjects(items.toArray(new HasMetadata[items.size()])).build(); - } - - return newInstance(context.withItem(template)); - } - } diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java b/openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java index de5ac80c229..810992e98f8 100644 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java +++ b/openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java @@ -15,7 +15,6 @@ */ package io.fabric8.openshift.client.impl; -import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.KubernetesResourceList; import io.fabric8.kubernetes.api.model.RootPaths; @@ -32,7 +31,6 @@ import io.fabric8.kubernetes.client.dsl.InOutCreateable; import io.fabric8.kubernetes.client.dsl.MixedOperation; import io.fabric8.kubernetes.client.dsl.Nameable; -import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable; import io.fabric8.kubernetes.client.dsl.Namespaceable; import io.fabric8.kubernetes.client.dsl.NamespacedInOutCreateable; import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; @@ -167,7 +165,6 @@ import io.fabric8.openshift.client.dsl.internal.build.BuildOperationsImpl; import io.fabric8.openshift.client.dsl.internal.core.TemplateOperationsImpl; import io.fabric8.openshift.client.dsl.internal.project.ProjectOperationsImpl; -import io.fabric8.openshift.client.internal.OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl; import io.fabric8.openshift.client.internal.OpenShiftOAuthInterceptor; import java.io.InputStream; @@ -267,21 +264,6 @@ public OpenShiftOperatorHubAPIGroupDSL operatorHub() { return adapt(OpenShiftOperatorHubAPIGroupClient.class); } - @Override - public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable load(InputStream is) { - return new OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(this, is); - } - - @Override - public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable resourceList(KubernetesResourceList item) { - return new OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(this, item); - } - - @Override - public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable resourceList(String s) { - return new OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(this, s); - } - @Override public MixedOperation builds() { return new BuildOperationsImpl(this); diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java b/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java deleted file mode 100644 index ca1f1b76ed4..00000000000 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java +++ /dev/null @@ -1,106 +0,0 @@ -/** - * 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.openshift.client.internal; - -import com.mifmif.common.regex.Generex; -import io.fabric8.kubernetes.api.model.HasMetadata; -import io.fabric8.kubernetes.api.model.KubernetesList; -import io.fabric8.kubernetes.api.model.KubernetesListBuilder; -import io.fabric8.kubernetes.client.Client; -import io.fabric8.kubernetes.client.KubernetesClientException; -import io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl; -import io.fabric8.kubernetes.client.dsl.internal.OperationContext; -import io.fabric8.kubernetes.client.utils.Utils; -import io.fabric8.openshift.api.model.Parameter; -import io.fabric8.openshift.api.model.Template; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl - extends NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl { - - public OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(Client client, Object item) { - super(client, item); - } - - public OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(OperationContext context) { - super(context); - } - - @Override - protected List asHasMetadata(Object item) { - if (item instanceof Template) { - return processTemplateList((Template) item, true); - } - return super.asHasMetadata(item); - } - - private static List processTemplate(Template template, Boolean failOnMissing) { - List parameters = template != null ? template.getParameters() : null; - List objects = template != null ? template.getObjects() : Collections.emptyList(); - KubernetesList list = new KubernetesListBuilder() - .withItems(objects) - .build(); - - try { - String json = OBJECT_MAPPER.writeValueAsString(list); - if (parameters != null && !parameters.isEmpty()) { - // lets make a few passes in case there's expressions in values - for (int i = 0; i < 5; i++) { - for (Parameter parameter : parameters) { - String name = parameter.getName(); - String regex = "${" + name + "}"; - String value; - if (Utils.isNotNullOrEmpty(parameter.getValue())) { - value = parameter.getValue(); - } else if (EXPRESSION.equals(parameter.getGenerate())) { - Generex generex = new Generex(parameter.getFrom()); - value = generex.random(); - } else if (Boolean.TRUE.equals(failOnMissing)) { - throw new IllegalArgumentException("No value available for parameter name: " + name); - } else { - value = ""; - } - json = json.replace(regex, value); - } - } - } - - list = OBJECT_MAPPER.readValue(json, KubernetesList.class); - } catch (IOException e) { - throw KubernetesClientException.launderThrowable(e); - } - return list.getItems(); - } - - private static List processTemplateList(Template item, Boolean enableProccessing) { - List result = new ArrayList<>(); - if (Boolean.FALSE.equals(enableProccessing)) { - result.addAll(item.getObjects()); - } else { - result.addAll(processTemplate(item, false)); - } - return result; - } - - @Override - public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl newInstance(OperationContext context) { - return new OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(context); - } -}