Skip to content
This repository has been archived by the owner on Oct 5, 2022. It is now read-only.

Commit

Permalink
fixes #3974 so we parse the configuration properly using the generate…
Browse files Browse the repository at this point in the history
…d DTOs
  • Loading branch information
jstrachan committed May 21, 2015
1 parent 48abdf1 commit ea7c8dd
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 136 deletions.
5 changes: 5 additions & 0 deletions components/kubernetes-api/pom.xml
Expand Up @@ -48,6 +48,11 @@
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
Expand Down
Expand Up @@ -16,14 +16,14 @@
package io.fabric8.kubernetes.api;

import io.fabric8.kubernetes.api.builds.Builds;
import io.fabric8.kubernetes.api.extensions.Configs;
import io.fabric8.kubernetes.api.model.EndpointSubset;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.EndpointsList;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.NamespaceList;
import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.NodeList;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.ReplicationController;
Expand All @@ -35,6 +35,8 @@
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.api.model.ServiceSpec;
import io.fabric8.kubernetes.api.model.config.Config;
import io.fabric8.kubernetes.api.model.config.Context;
import io.fabric8.openshift.api.model.Build;
import io.fabric8.openshift.api.model.BuildConfig;
import io.fabric8.openshift.api.model.BuildConfigList;
Expand Down Expand Up @@ -89,7 +91,6 @@
import java.util.TreeMap;
import java.util.concurrent.Callable;

import static io.fabric8.kubernetes.api.KubernetesFactory.getOpenShiftConfigFile;
import static io.fabric8.kubernetes.api.KubernetesHelper.defaultOsApiVersion;
import static io.fabric8.kubernetes.api.KubernetesHelper.filterLabels;
import static io.fabric8.kubernetes.api.KubernetesHelper.getName;
Expand Down Expand Up @@ -129,75 +130,16 @@ public static String defaultNamespace() {
}

public static String findDefaultOpenShiftNamespace() {
File file = getOpenShiftConfigFile();
String namePrefix = "name:";
String namespacePrefix = "namespace:";

String answer = null;
if (file.exists() && file.isFile()) {
LOG.debug("Parsing OpenShift configuration: " + file);
String context = findDefaultOpenShiftContext(file);
if (Strings.isNotBlank(context)) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
boolean inUsers = false;
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
line = line.trim();
String namespaceValue = yamlValue(namespacePrefix, line);
if (namespaceValue != null) {
answer = namespaceValue;
} else {
String nameValue = yamlValue(namePrefix, line);
if (nameValue != null && nameValue.equals(context)) {
return answer;
}
}
}
} catch (Exception e) {
LOG.warn("Could not parse OpenShift configuration file: " + file);
}

}
}
return answer;
}

protected static String yamlValue(String prefix, String text) {
if (text.startsWith(prefix)) {
String value = text.substring(prefix.length());
value = value.trim();
if (Strings.isNotBlank(value)) {
return value;
Config config = Configs.parseConfigs();
if (config != null) {
Context context = Configs.getCurrentContext(config);
if (context != null) {
return context.getNamespace();
}
}
return null;
}

protected static String findDefaultOpenShiftContext(File file) {
String tokenPrefix = "current-context:";
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
boolean inUsers = false;
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
if (line.startsWith(tokenPrefix)) {
String token = line.substring(tokenPrefix.length()).trim();
if (Strings.isNotBlank(token)) {
return token;
}
}
}
} catch (Exception e) {
LOG.warn("Could not parse OpenShift configuration file: " + file);
}
return null;
}

public KubernetesClient() {
this(new KubernetesFactory());
}
Expand Down
Expand Up @@ -20,7 +20,10 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.jaxrs.cfg.Annotations;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import io.fabric8.kubernetes.api.extensions.Configs;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.config.Config;
import io.fabric8.kubernetes.api.model.config.Context;
import io.fabric8.kubernetes.api.support.KindToClassMapping;
import io.fabric8.kubernetes.api.support.KubernetesDeserializer;
import io.fabric8.openshift.api.model.OAuthClient;
Expand All @@ -47,7 +50,6 @@
*/
public class KubernetesFactory {
public static final String KUBERNETES_SCHEMA_JSON = "schema/kube-schema.json";
public static final String OPENSHIFT_CONFIG_FILE_PROPERTY = "openshift.config.file";
private final Logger log = LoggerFactory.getLogger(getClass());

public static final String DEFAULT_KUBERNETES_MASTER = "http://localhost:8080";
Expand Down Expand Up @@ -234,55 +236,14 @@ public WebClient createWebClient(String serviceAddress) {
}

protected String findOpenShiftToken() {
File file = getOpenShiftConfigFile();
String answer = null;
if (file.exists() && file.isFile()) {
log.debug("Parsing OpenShift configuration: " + file);
String tokenPrefix = "token:";
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
boolean inUsers = false;
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
if (line.isEmpty()) {
continue;
}
if (line.startsWith("users:")) {
inUsers = true;
} else {
char ch = line.charAt(0);
if (Character.isWhitespace(ch) || ch == '-') {
if (inUsers) {
String text = line.trim();
if (text.startsWith(tokenPrefix)) {
String token = text.substring(tokenPrefix.length()).trim();
if (Strings.isNotBlank(token)) {
answer = token;
}
}
}
} else {
inUsers = false;
}
}

}
} catch (Exception e) {
log.warn("Could not parse OpenShift configuration file: " + file);
Config config = Configs.parseConfigs();
if (config != null) {
Context context = Configs.getCurrentContext(config);
if (context != null) {
return Configs.getUserToken(config, context);
}
}
return answer;
}

public static File getOpenShiftConfigFile() {
String file = System.getProperty(OPENSHIFT_CONFIG_FILE_PROPERTY);
if (file != null) {
return new File(file);
}
String homeDir = System.getProperty("user.home", ".");
return new File(homeDir, ".config/openshift/config");
return null;
}

protected List<Object> createProviders() {
Expand Down
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.fabric8.kubernetes.api.extensions.Templates;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerPort;
Expand All @@ -25,11 +26,8 @@
import io.fabric8.kubernetes.api.model.ContainerStateTerminated;
import io.fabric8.kubernetes.api.model.ContainerStateWaiting;
import io.fabric8.kubernetes.api.model.ContainerStatus;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
Expand All @@ -45,10 +43,6 @@
import io.fabric8.kubernetes.api.model.ServicePort;
import io.fabric8.kubernetes.api.model.ServiceSpec;
import io.fabric8.kubernetes.api.model.util.IntOrString;
import io.fabric8.openshift.api.model.Build;
import io.fabric8.openshift.api.model.BuildConfig;
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.ImageStream;
import io.fabric8.openshift.api.model.OAuthClient;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.api.model.RouteSpec;
Expand Down Expand Up @@ -215,6 +209,26 @@ public static String getNamespace(HasMetadata entity) {
}
}

public static Map<String, String> getOrCreateAnnotations(HasMetadata entity) {
ObjectMeta metadata = getOrCreateMetadata(entity);
Map<String, String> answer = metadata.getAnnotations();
if (answer == null) {
answer = new HashMap<>();
metadata.setAnnotations(answer);
}
return answer;
}

public static Map<String, String> getOrCreateLabels(HasMetadata entity) {
ObjectMeta metadata = getOrCreateMetadata(entity);
Map<String, String> answer = metadata.getLabels();
if (answer == null) {
answer = new HashMap<>();
metadata.setLabels(answer);
}
return answer;
}


/**
* Returns the labels of the given metadata object or an empty map if the metadata or labels are null
Expand Down Expand Up @@ -383,6 +397,30 @@ public static Object loadJson(byte[] json) throws IOException {
}


/**
* Loads the YAML file for the given DTO class
*/
public static <T> T loadYaml(InputStream in, Class<T> clazz) throws IOException {
byte[] data = Files.readBytes(in);
return loadYaml(data, clazz);
}

/**
* Loads the YAML file for the given DTO class
*/
public static <T> T loadYaml(File file, Class<T> clazz) throws IOException {
byte[] data = Files.readBytes(file);
return loadYaml(data, clazz);
}

/**
* Loads the YAML file for the given DTO class
*/
public static <T> T loadYaml(byte[] data, Class<T> clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
return mapper.readValue(data, clazz);
}

/**
* Loads the Kubernetes JSON and converts it to a list of entities
*/
Expand Down Expand Up @@ -1614,5 +1652,4 @@ public static KubernetesList asKubernetesList(Object dto) throws IOException {
return answer;
}
}

}

0 comments on commit ea7c8dd

Please sign in to comment.