坑,fabric8资料太少,创建Secret就更少了,
参考地址:https://github.com/Azure-Samples/aks-java-deploy-image-from-acr-to-kubernetes/blob/master/src/main/java/com/microsoft/azure/management/kubernetescluster/samples/DeployImageFromContainerRegistryToKubernetes.java
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.PodResource;
import org.apache.commons.codec.binary.Base64;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
/**
* @Author:
* @Description
* @Date: 2019/3/1
*/
public class K8sUtils {
private static final Object lock = new Object();
private static KubernetesClient client = null;
private K8sUtils() {
}
// 单例优化
public static KubernetesClient getClient(String masterUrl) {
Config config = new ConfigBuilder().withMasterUrl(masterUrl).build();
synchronized (lock) {
if (client == null) {
client = new DefaultKubernetesClient(config);
}
}
return client;
}
public static void main(String[] args) {
KubernetesClient client = K8sUtils.getClient("http://172.16.1.73:8080/");
MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods = client.pods();
String namespace = client.getNamespace();
Config configuration = client.getConfiguration();
client.namespaces().list();
client.secrets().list();
client.services().list();
System.out.println("..");
try {
String s = new String(Base64.decodeBase64("YWRtaW46SGFyYm9yMTIzNDU="),
"UTF-8");
System.out.printf("s");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//kubectl create secret docker-registry registry-secret --docker-server=172.16.1.57 --docker-username=admin
// --docker-email =123@163.com --docker-password=Harbor12345 -n ysj-dev
String registryUrl = "172.16.1.57", name = "admin", email = "123@163.com", password = "Harbor12345",
nameSpace="ysj-dev", secretName="-docker-secret";
Secret secret = client.secrets().inNamespace("ysj-dev").withName("docker-registry1").get();
if (secret == null) {
try {
new String("{ \"%s\": { \"auth\": \"%s\", \"email\": \"%s\" } }");
HashMap<String, String> secretData = new HashMap(1);
String basicAuth = new String(Base64.encodeBase64((name + ":" + password).getBytes()));
String dockerCfg = String.format("{ " +
" \"auths\": { " +
" \"%s\": { " +
" \"username\": \"%s\", " +
" \"password\": \"%s\", " +
" \"email\": \"%s\", " +
" \"auth\": \"%s\" " +
" } " +
" } " +
"}",
registryUrl,
name,
password,
email,
basicAuth
);
System.out.println("dockerCfg--before-->:" + dockerCfg);
dockerCfg = new String(Base64.encodeBase64(dockerCfg.getBytes("UTF-8")), "UTF-8");
System.out.println("dockerCfg--after-->:" + dockerCfg);
secretData.put(".dockerconfigjson", dockerCfg);
SecretBuilder secretBuilder = new SecretBuilder();
secretBuilder
.withNewMetadata()
.withName(nameSpace + secretName)
.withNamespace(nameSpace)
.endMetadata()
.withData(secretData)
.withType("kubernetes.io/dockerconfigjson");
System.out.println("" + secretBuilder.toString());
Secret secret1 = null;
try {
secret1 = client.secrets().inNamespace("ysj-dev").create(secretBuilder.build());
} catch (Exception e) {
e.printStackTrace();
System.out.printf(e.getMessage());
}
System.out.printf("Creating new secret: " + secret1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
网友评论