Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String, String[]> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public String toJson(boolean compact) {
return propertyKeys;
}

@Override
public String toString() {
return toJson(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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<String, String[]> propertyKeys;

public ReplicationController(ModelNode node, IClient client, Map<String, String []> 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<ModelNode> containers = specContainers.asList();
if(!containers.isEmpty()) {
ModelNode var = new ModelNode();
set(var, NAME, name);
set(var, VALUE, value);

Optional<ModelNode> 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<IEnvironmentVariable> getEnvironmentVariables() {
return getEnvironmentVariables(null);
}



@Override
public Collection<IEnvironmentVariable> getEnvironmentVariables(String containerName) {
String name = StringUtils.defaultIfBlank(containerName, "");
ModelNode specContainers = get(SPEC_TEMPLATE_CONTAINERS);
if(specContainers.isDefined()) {
List<ModelNode> containers = specContainers.asList();
if(!containers.isEmpty()) {
Optional<ModelNode> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's correct, looking at a pod, I can see

"env" : [
                ...
                {
                    "name" : "OPENSHIFT_KUBE_PING_NAMESPACE",
                    "valueFrom" : {"fieldRef" : {
                        "apiVersion" : "v1",
                        "fieldPath" : "metadata.namespace"
                    }}
                }


/**
* The ref value or null if not defined
* @return
*/
IEnvVarSource getValueFrom();

/**
* Marker interface for sources of environment variables
* @author jeff.cantrill
*
*/
static interface IEnvVarSource{

}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<IEnvironmentVariable> 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<IEnvironmentVariable> getEnvironmentVariables(String containerName);

/**
* Returns the desired number of replicas
* @return
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
Loading