openshift/origin工作記錄(2)——RESTful程式設計介面使用
阿新 • • 發佈:2019-01-10
由於工作原因,需要對openshift進行二次開發,初步研究了一下RESTful程式設計介面使用。
程式碼如下:
import io.fabric8.kubernetes.api.model.NamespaceList;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.utils.URLUtils;
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfig;
import io.fabric8.openshift.client.OpenShiftConfigBuilder;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.net.URL;
public class openshiftDemo {
private static final String AUTHORIZATION = "Authorization" ;
private static final String LOCATION = "Location";
private static final String AUTHORIZE_PATH = "oauth/authorize?response_type=token&client_id=openshift-challenging-client";
private static final String BEFORE_TOKEN = "access_token=";
private static final String AFTER_TOKEN = "&expires" ;
public static void main(String[] args) {
OpenShiftConfig config = new OpenShiftConfigBuilder()
.withOpenShiftUrl("https://master.example.com:8443")
.withMasterUrl("https://master.example.com:8443")
.withUsername("dev")
.withPassword("dev")
.withTrustCerts(true).build();
DefaultOpenShiftClient client = new DefaultOpenShiftClient(config);
openshiftDemo openshiftDemo=new openshiftDemo();
//獲取使用者token
System.out.println(openshiftDemo.authorize(client.getHttpClient(), config));
//獲取工程列表,這裡必須是叢集管理員的賬號
NamespaceList myNs = client.namespaces().list();
//遍歷列印工程名
for (Namespace ns : myNs.getItems())
System.out.println(ns.getMetadata().getName());
}
//獲取token
public String authorize(OkHttpClient client, OpenShiftConfig config) {
try {
OkHttpClient.Builder builder = client.newBuilder();
builder.interceptors().remove(this);
OkHttpClient clone = builder.build();
String credential = Credentials.basic(config.getUsername(), new String(config.getPassword()));
URL url = new URL(URLUtils.join(config.getMasterUrl(), AUTHORIZE_PATH));
Response response = clone.newCall(new Request.Builder().get().url(url).header(AUTHORIZATION, credential).build()).execute();
response.body().close();
response = response.priorResponse() != null ? response.priorResponse() : response;
response = response.networkResponse() != null ? response.networkResponse() : response;
String token = response.header(LOCATION);
if (token == null || token.isEmpty()) {
throw new KubernetesClientException("Unexpected response (" + response.code() + " " + response.message() + "), to the authorization request. Missing header:[" + LOCATION + "]!");
}
token = token.substring(token.indexOf(BEFORE_TOKEN) + BEFORE_TOKEN.length());
token = token.substring(0, token.indexOf(AFTER_TOKEN));
return token;
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}
}
程式碼執行結果輸出如下:
這裡能夠獲取使用者賬號的token。
可以自己基於openshift官方RESTful程式設計介面進行二次開發,也可以直接使用開源專案fabric8io/kubernetes-client的jar包提供的功能進行開發,甚至可以修改開源專案fabric8io/kubernetes-client的原始碼。