From bf02be5c267244d596dcb7099fc4ebebc2b463c8 Mon Sep 17 00:00:00 2001 From: Jeff Cantrill Date: Tue, 16 Feb 2016 16:18:07 -0500 Subject: [PATCH] [OSJC-240] Set deploymentconfig env vars --- .../restclient/model/EnvironmentVariable.java | 87 +++++++++++++++++++ .../restclient/model/ModelNodeAdapter.java | 5 ++ .../model/ReplicationController.java | 68 ++++++++++++++- .../model/IConfigMapKeySelector.java | 21 +++++ .../model/IEnvironmentVariable.java | 52 +++++++++++ .../model/IObjectFieldSelector.java | 20 +++++ .../model/IReplicationController.java | 34 ++++++++ .../restclient/model/ISecretKeySelector.java | 21 +++++ .../model/v1/ReplicationControllerTest.java | 57 ++++++++++++ 9 files changed, 364 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/openshift/internal/restclient/model/EnvironmentVariable.java create mode 100644 src/main/java/com/openshift/restclient/model/IConfigMapKeySelector.java create mode 100644 src/main/java/com/openshift/restclient/model/IEnvironmentVariable.java create mode 100644 src/main/java/com/openshift/restclient/model/IObjectFieldSelector.java create mode 100644 src/main/java/com/openshift/restclient/model/ISecretKeySelector.java diff --git a/src/main/java/com/openshift/internal/restclient/model/EnvironmentVariable.java b/src/main/java/com/openshift/internal/restclient/model/EnvironmentVariable.java new file mode 100644 index 00000000..ad24b03f --- /dev/null +++ b/src/main/java/com/openshift/internal/restclient/model/EnvironmentVariable.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.restclient.model; + +import static com.openshift.internal.util.JBossDmrExtentions.*; + +import java.util.Map; + +import org.jboss.dmr.ModelNode; + +import com.openshift.internal.restclient.model.properties.ResourcePropertyKeys; +import com.openshift.restclient.model.IConfigMapKeySelector; +import com.openshift.restclient.model.IEnvironmentVariable; +import com.openshift.restclient.model.IObjectFieldSelector; +import com.openshift.restclient.model.ISecretKeySelector; + +public class EnvironmentVariable extends ModelNodeAdapter implements IEnvironmentVariable, ResourcePropertyKeys { + + public EnvironmentVariable(ModelNode node, Map propertyKeys) { + super(node, propertyKeys); + } + + @Override + public String getName() { + return asString(getNode(), getPropertyKeys(), NAME); + } + + @Override + public String getValue() { + return asString(getNode(), getPropertyKeys(), VALUE); + } + + @Override + public IEnvVarSource getValueFrom() { + if(getNode().hasDefined("fieldRef")) { + return new IObjectFieldSelector(){ + @Override + public String getApiVersion() { + return asString(getNode(), getPropertyKeys(), "fieldRef.apiVersion"); + } + + @Override + public String getFieldPath() { + return asString(getNode(), getPropertyKeys(), "fieldRef.fieldPath"); + } + + }; + }else if(getNode().hasDefined("configMapKeyRef")) { + return new IConfigMapKeySelector() { + + @Override + public String getName() { + return asString(getNode(), getPropertyKeys(), "configMapKeyRef.name"); + } + + @Override + public String getKey() { + return asString(getNode(), getPropertyKeys(), "configMapKeyRef.key"); + } + }; + + }else if(getNode().hasDefined("secretKeyRef")) { + return new ISecretKeySelector() { + + @Override + public String getName() { + return asString(getNode(), getPropertyKeys(), "secretKeyRef.name"); + } + + @Override + public String getKey() { + return asString(getNode(), getPropertyKeys(), "secretKeyRef.key"); + } + }; + } + return null; + } + +} diff --git a/src/main/java/com/openshift/internal/restclient/model/ModelNodeAdapter.java b/src/main/java/com/openshift/internal/restclient/model/ModelNodeAdapter.java index 763c45a7..15d3b2c7 100644 --- a/src/main/java/com/openshift/internal/restclient/model/ModelNodeAdapter.java +++ b/src/main/java/com/openshift/internal/restclient/model/ModelNodeAdapter.java @@ -49,4 +49,9 @@ public String toJson(boolean compact) { return propertyKeys; } + @Override + public String toString() { + return toJson(false); + } + } diff --git a/src/main/java/com/openshift/internal/restclient/model/ReplicationController.java b/src/main/java/com/openshift/internal/restclient/model/ReplicationController.java index 05357524..071633b3 100644 --- a/src/main/java/com/openshift/internal/restclient/model/ReplicationController.java +++ b/src/main/java/com/openshift/internal/restclient/model/ReplicationController.java @@ -10,12 +10,16 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; import org.jboss.dmr.ModelNode; import org.jboss.dmr.ModelType; @@ -24,9 +28,9 @@ import com.openshift.restclient.IClient; import com.openshift.restclient.images.DockerImageURI; import com.openshift.restclient.model.IContainer; +import com.openshift.restclient.model.IEnvironmentVariable; import com.openshift.restclient.model.IPort; import com.openshift.restclient.model.IReplicationController; -import com.openshift.restclient.model.volume.IVolume; import com.openshift.restclient.model.volume.IVolumeMount; import com.openshift.restclient.model.volume.IVolumeSource; @@ -44,10 +48,72 @@ public class ReplicationController extends KubernetesResource implements IReplic protected static final String IMAGE = "image"; protected static final String ENV = "env"; + private Map propertyKeys; public ReplicationController(ModelNode node, IClient client, Map propertyKeys) { super(node, client, propertyKeys); + this.propertyKeys = propertyKeys; } + + @Override + public void setEnvironmentVariable(String name, String value) { + setEnvironmentVariable(null, name, value); + } + + + + @Override + public void setEnvironmentVariable(String containerName, String name, String value) { + String defaultedContainerName = StringUtils.defaultIfBlank(containerName, ""); + ModelNode specContainers = get(SPEC_TEMPLATE_CONTAINERS); + if(specContainers.isDefined()) { //should ALWAYS exist + List containers = specContainers.asList(); + if(!containers.isEmpty()) { + ModelNode var = new ModelNode(); + set(var, NAME, name); + set(var, VALUE, value); + + Optional opt = containers.stream().filter(n->defaultedContainerName.equals(asString(n, NAME))).findFirst(); + ModelNode node = opt.isPresent() ? opt.get() : containers.get(0); + ModelNode envNode = get(node, ENV); + + envNode.add(var); + + } + } + } + + + + @Override + public Collection getEnvironmentVariables() { + return getEnvironmentVariables(null); + } + + + + @Override + public Collection getEnvironmentVariables(String containerName) { + String name = StringUtils.defaultIfBlank(containerName, ""); + ModelNode specContainers = get(SPEC_TEMPLATE_CONTAINERS); + if(specContainers.isDefined()) { + List containers = specContainers.asList(); + if(!containers.isEmpty()) { + Optional opt = containers.stream().filter(n->name.equals(asString(n, NAME))).findFirst(); + ModelNode node = opt.isPresent() ? opt.get() : containers.get(0); + ModelNode envNode = get(node, ENV); + if(envNode.isDefined()) { + return envNode.asList() + .stream() + .map(n-> new EnvironmentVariable(n, propertyKeys)) + .collect(Collectors.toList()); + } + } + } + return Collections.emptyList(); + } + + @Override public int getDesiredReplicaCount() { diff --git a/src/main/java/com/openshift/restclient/model/IConfigMapKeySelector.java b/src/main/java/com/openshift/restclient/model/IConfigMapKeySelector.java new file mode 100644 index 00000000..e1a86bc6 --- /dev/null +++ b/src/main/java/com/openshift/restclient/model/IConfigMapKeySelector.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.openshift.restclient.model; + +import com.openshift.restclient.model.IEnvironmentVariable.IEnvVarSource; + +public interface IConfigMapKeySelector extends IEnvVarSource { + + String getName(); + + String getKey(); + +} diff --git a/src/main/java/com/openshift/restclient/model/IEnvironmentVariable.java b/src/main/java/com/openshift/restclient/model/IEnvironmentVariable.java new file mode 100644 index 00000000..72db28a1 --- /dev/null +++ b/src/main/java/com/openshift/restclient/model/IEnvironmentVariable.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.openshift.restclient.model; + +/** + * Environment variable representation to + * allow more complex values then + * name/value pairs. An environmentVariable + * will have either a value or valueFrom + * but not both. + * + * @author jeff.cantrill + * + */ +public interface IEnvironmentVariable { + + /** + * The name of the env var + * @return + */ + String getName(); + + /** + * The value of the environment variable or null if not + * defined. + * @return + */ + String getValue(); + + /** + * The ref value or null if not defined + * @return + */ + IEnvVarSource getValueFrom(); + + /** + * Marker interface for sources of environment variables + * @author jeff.cantrill + * + */ + static interface IEnvVarSource{ + + } +} diff --git a/src/main/java/com/openshift/restclient/model/IObjectFieldSelector.java b/src/main/java/com/openshift/restclient/model/IObjectFieldSelector.java new file mode 100644 index 00000000..afb33039 --- /dev/null +++ b/src/main/java/com/openshift/restclient/model/IObjectFieldSelector.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.openshift.restclient.model; + +import com.openshift.restclient.model.IEnvironmentVariable.IEnvVarSource; + +public interface IObjectFieldSelector extends IEnvVarSource { + + String getApiVersion(); + + String getFieldPath(); +} diff --git a/src/main/java/com/openshift/restclient/model/IReplicationController.java b/src/main/java/com/openshift/restclient/model/IReplicationController.java index 1ca345e8..2f9c278f 100644 --- a/src/main/java/com/openshift/restclient/model/IReplicationController.java +++ b/src/main/java/com/openshift/restclient/model/IReplicationController.java @@ -23,6 +23,40 @@ public interface IReplicationController extends IResource{ static final String DEPLOYMENT_PHASE = "openshift.io/deployment.phase"; + /** + * Set an environment variable to the given name and + * value on the first container in the list of containers + * + * @param name + * @param value + */ + void setEnvironmentVariable(String name, String value); + + /** + * Set an environment variable to the given name and + * value on the given container. Returns silently + * if the containerName is not found + * + * @param containerName + * @param name + * @param value + */ + void setEnvironmentVariable(String containerName, String name, String value); + + /** + * Return the list of env vars of the first container + * @return + */ + Collection getEnvironmentVariables(); + + /** + * Return the list of env vars for the given container or an empty list + * if the container is not found + * @param containerName + * @return + */ + Collection getEnvironmentVariables(String containerName); + /** * Returns the desired number of replicas * @return diff --git a/src/main/java/com/openshift/restclient/model/ISecretKeySelector.java b/src/main/java/com/openshift/restclient/model/ISecretKeySelector.java new file mode 100644 index 00000000..6a2c2373 --- /dev/null +++ b/src/main/java/com/openshift/restclient/model/ISecretKeySelector.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.openshift.restclient.model; + +import com.openshift.restclient.model.IEnvironmentVariable.IEnvVarSource; + +public interface ISecretKeySelector extends IEnvVarSource { + + String getName(); + + String getKey(); + +} diff --git a/src/test/java/com/openshift/internal/restclient/model/v1/ReplicationControllerTest.java b/src/test/java/com/openshift/internal/restclient/model/v1/ReplicationControllerTest.java index 71d4ef74..5472df6c 100644 --- a/src/test/java/com/openshift/internal/restclient/model/v1/ReplicationControllerTest.java +++ b/src/test/java/com/openshift/internal/restclient/model/v1/ReplicationControllerTest.java @@ -14,10 +14,12 @@ import static org.mockito.Mockito.when; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import org.jboss.dmr.ModelNode; @@ -32,9 +34,14 @@ import com.openshift.restclient.IClient; import com.openshift.restclient.ResourceKind; import com.openshift.restclient.images.DockerImageURI; +import com.openshift.restclient.model.IConfigMapKeySelector; import com.openshift.restclient.model.IContainer; +import com.openshift.restclient.model.IEnvironmentVariable; +import com.openshift.restclient.model.IEnvironmentVariable.IEnvVarSource; +import com.openshift.restclient.model.IObjectFieldSelector; import com.openshift.restclient.model.IPort; import com.openshift.restclient.model.IReplicationController; +import com.openshift.restclient.model.ISecretKeySelector; import com.openshift.restclient.model.volume.IVolume; import com.openshift.restclient.model.volume.IVolumeSource; import com.openshift.restclient.utils.Samples; @@ -56,6 +63,56 @@ public void setup(){ rc = new ReplicationController(node, client, ResourcePropertiesRegistry.getInstance().get(VERSION, ResourceKind.REPLICATION_CONTROLLER)); } + public void testGetEnvironmentVariablesWithValueFrom() { + + Collection envVars = rc.getEnvironmentVariables(); + + //fieldref + Optional envVar = envVars.stream().filter(e->"OPENSHIFT_KUBE_PING_NAMESPACE".equals(e.getName())).findFirst(); + assertTrue("Exp. to find env var", envVar.isPresent()); + IEnvVarSource from = envVar.get().getValueFrom(); + assertTrue(from instanceof IObjectFieldSelector); + IObjectFieldSelector selector = (IObjectFieldSelector)from; + assertEquals("v1",selector.getApiVersion()); + assertEquals("metadata.namespace",selector.getFieldPath()); + + //configmapkeyref + envVar = envVars.stream().filter(e->"OPENSHIFT_CONFIGMAP_KEY_REF".equals(e.getName())).findFirst(); + assertTrue("Exp. to find env var", envVar.isPresent()); + from = envVar.get().getValueFrom(); + assertTrue(from instanceof IConfigMapKeySelector); + IConfigMapKeySelector configSelector = (IConfigMapKeySelector) from; + assertEquals("xyz",configSelector.getName()); + assertEquals("abc123",configSelector.getKey()); + + //secretkeyref + envVar = envVars.stream().filter(e->"OPENSHIFT_SECRET_KEY_REF".equals(e.getName())).findFirst(); + assertTrue("Exp. to find env var", envVar.isPresent()); + from = envVar.get().getValueFrom(); + assertTrue(from instanceof ISecretKeySelector); + ISecretKeySelector secretKeySelector = (ISecretKeySelector) from; + assertEquals("bar",secretKeySelector.getName()); + assertEquals("foo",secretKeySelector.getKey()); + } + + @Test + public void testEnvironmentVariable() { + rc.setEnvironmentVariable("foo", "bar"); + Collection envVars = rc.getEnvironmentVariables(); + Optional envVar = envVars.stream().filter(e->"foo".equals(e.getName())).findFirst(); + assertTrue("Exp. to find env var", envVar.isPresent()); + assertEquals("bar", envVar.get().getValue()); + } + + @Test + public void testEnvironmentVariableForANamedContainer() { + rc.setEnvironmentVariable("ruby-helloworld-database", "fooz", "balls"); + Collection envVars = rc.getEnvironmentVariables("ruby-helloworld-database"); + Optional envVar = envVars.stream().filter(e->"fooz".equals(e.getName())).findFirst(); + assertTrue("Exp. to find env var", envVar.isPresent()); + assertEquals("balls", envVar.get().getValue()); + } + @Test public void setReplicaSelector() { Map exp = new HashMap();