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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
</build>

<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-dmr</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* 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.kubeclient;

import java.util.HashMap;
import java.util.Map;
import com.openshift.restclient.model.kubeclient.ICluster;

public class Cluster implements ICluster {

private static final String SERVER = "server";
private static final String INSECURE_SKIP_TLS_VERIFY = "insecure-skip-tls-verify";
private String name;
private Map<String, Object> cluster = new HashMap<>();

public void setCluster(Map<String, Object> cluster) {
this.cluster.clear();
this.cluster.putAll(cluster);
}

@Override
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String getServer() {
return (String) cluster.get(SERVER);
}

@Override
public boolean isInsecureSkipTLSVerify() {
if(cluster.containsKey(INSECURE_SKIP_TLS_VERIFY)) {
return (Boolean) cluster.get(INSECURE_SKIP_TLS_VERIFY);
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* 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.kubeclient;

import java.util.HashMap;
import java.util.Map;

import com.openshift.internal.restclient.model.properties.ResourcePropertyKeys;
import com.openshift.restclient.model.kubeclient.IContext;

/**
* KubeConfig context
* @author jeff.cantrill
*
*/
public class Context implements IContext, ResourcePropertyKeys{

private static final String USER = "user";
private static final String CLUSTER = "cluster";
private Map<String, String> context = new HashMap<>();
private String name;

public void setContext(Map<String, String> context) {
this.context.clear();
this.context.putAll(context);
}
@Override
public String getCluster() {
return context.get(CLUSTER);
}

@Override
public String getUser() {
return context.get(USER);
}

@Override
public String getNamespace() {
return context.get(NAMESPACE);
}
@Override
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* 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.kubeclient;

import java.util.ArrayList;
import java.util.Collection;

import com.openshift.restclient.model.kubeclient.ICluster;
import com.openshift.restclient.model.kubeclient.IContext;
import com.openshift.restclient.model.kubeclient.IKubeClientConfig;
import com.openshift.restclient.model.kubeclient.IUser;

/**
* Kube Client config impl
*
* @author jeff.cantrill
*
*/
public class KubeClientConfig implements IKubeClientConfig {

private Collection<ICluster> clusters = new ArrayList<>();
private Collection<IContext> contexts = new ArrayList<>();
private String currentContext = "";
private Collection<IUser> users = new ArrayList<>();

public void setApiVersion(String apiVersion) {

}

@Override
public Collection< ICluster> getClusters() {
return clusters;
}

public void setClusters(Collection<ICluster> clusters) {
this.clusters = clusters;
}

@Override
public Collection<IContext> getContexts() {
return contexts;
}
public void setContexts(Collection<IContext> contexts) {
this.contexts = contexts;
}

@Override
public String getCurrentContext() {
// TODO Auto-generated method stub
return currentContext;
}
public void setCurrentContext(String currentContext) {
this.currentContext = currentContext;
}
@Override
public Collection<IUser> getUsers() {
return users;
}

public void setUsers(Collection<IUser> users) {
this.users = users;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* 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.kubeclient;

import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.introspector.PropertyUtils;

public class KubeClientConfigConstructor extends Constructor {

private static final String USERS = "users";
private static final String CLUSTERS = "clusters";
private static final String CONTEXTS = "contexts";

public KubeClientConfigConstructor(PropertyUtils propertyUtils) {
super(KubeClientConfig.class);

TypeDescription configTypeDesc = new TypeDescription(KubeClientConfig.class);
configTypeDesc.putListPropertyType(CONTEXTS, Context.class);
configTypeDesc.putListPropertyType(CLUSTERS, Cluster.class);
configTypeDesc.putListPropertyType(USERS, User.class);
addTypeDescription(configTypeDesc);

setPropertyUtils(propertyUtils);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* 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.kubeclient;

import java.util.Map;

import com.openshift.restclient.model.kubeclient.IUser;

public class User implements IUser {

private String name;
private Map<String, String> user;

public void setUser(Map<String, String> user) {
this.user = user;
}
@Override
public String getToken() {
return user.get("token");
}

@Override
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}


}
39 changes: 39 additions & 0 deletions src/main/java/com/openshift/restclient/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class ClientBuilder {
private String certificateAlias;
private IResourceFactory resourceFactory;
private IAuthorizationStrategy authStrategy;
private String withUserName;
private Object token;

public ClientBuilder() {
this(null);
}

public ClientBuilder(String baseUrl) {
this.baseUrl = baseUrl;
Expand All @@ -52,11 +58,44 @@ public ClientBuilder resourceFactory(IResourceFactory factory) {
return this;
}

@Deprecated
public ClientBuilder resourceFactory(IAuthorizationStrategy authStrategy) {
this.authStrategy = authStrategy;
return this;
}

public ClientBuilder authorizationStrategy(IAuthorizationStrategy authStrategy) {
this.authStrategy = authStrategy;
return this;
}

public ClientBuilder toCluster(String baseUrl) {
this.baseUrl = baseUrl;
return this;
}

public ClientBuilder withUserName(String userName) {
this.withUserName = userName;
return this;
}

public ClientBuilder usingToken(String userName) {
this.token = token;
return this;
}

/**
* Build a client using the config loading rules defined http://janetkuo.github.io/kubernetes/v1.0/docs/user-guide/kubeconfig-file.html. Brief summary
* of loading order:
*
* 1. use explicit values set in builder
* a. username/token
* b. authStrategy
* 2. currentContext of config file located at $KUBECONFIG
* 3. currentContext of config file located at ~/.kube/config
*
* @return
*/
public IClient build() {
try {
ISSLCertificateCallback sslCallback = defaultIfNull(this.sslCertificateCallback, new NoopSSLCertificateCallback());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* 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.kubeclient;

public interface ICluster {

String getName();
/**
* The server url
* @return
*/
String getServer();

boolean isInsecureSkipTLSVerify();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* 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.kubeclient;

public interface IContext {

/**
* The name of the cluster
* @return
*/
String getCluster();

/**
* Returns the user info in the form of:
* <username>/<url>
* @return the user info
*/
String getUser();

/**
* default namespace to use
* on unspecified requests
* @return
*/
String getNamespace();

/**
* The name of the context
* @return
*/
String getName();

}
Loading