fabric8 API操作ConfigMap


/**
* 构建ConfigMap对象
* @param ku8ConfigMap
*/
public void createConfigMap(Ku8ConfigMap ku8ConfigMap) {
k8sAPIService.createConfigMap(
ku8ConfigMap.getNamespace(),
new ConfigMapBuilder()
.withKind("ConfigMap")
.withApiVersion("v1")
.withNewMetadata()
.withName(ku8ConfigMap.getName())
.withNamespace(ku8ConfigMap.getNamespace())
.endMetadata()
.withData(ku8ConfigMap.getData())
.build()
);
}
/**
* 创建ConfigMap
* @param namespace
* @param configMap
* @return
*/
public ConfigMap createConfigMap(String namespace, ConfigMap configMap) {
return getClient().configMaps().inNamespace(namespace).create(configMap);
}
package com.***.entity;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
@XmlRootElement
public class Ku8ConfigMap implements Serializable {
private static final long serialVersionUID = -2929072646302058679L;
private Integer configMapId;
private String name;
private Integer namespaceId;
private Date createTime;
private Date updateTime;
private Map<String, String> data;
private String namespace;
public Integer getConfigMapId() {
return configMapId;
}
public void setConfigMapId(Integer configMapId) {
this.configMapId = configMapId;
}
public Integer getNamespaceId() {
return namespaceId;
}
public void setNamespaceId(Integer namespaceId) {
this.namespaceId = namespaceId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Map<String, String> getData() {
return data;
}
public void setData(Map<String, String> data) {
this.data = data;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
}


// 遍历需要挂载的ConfigMap列表
for (VolumeDto confMap : ku8ConfMapArray) {
ConfigMap f8ConfigMap = k8sAPIService.getConfigMapByName(namespace, confMap.getName());
if (f8ConfigMap == null) {
throw new BusinessException("cant find configMap:" + confMap.getName());
}
for (Container f8Container : f8Containers) { // 遍历需要创建的容器
List<VolumeMount> f8VolumeMounts = f8Container.getVolumeMounts();
if (f8VolumeMounts == null) {
f8VolumeMounts = new ArrayList<VolumeMount>();
}
// 在容器中挂载ConfigMap
VolumeMount f8VolumeMount = new VolumeMountBuilder().withName(confMap.getName()).withMountPath(confMap.getPath()).build();
f8VolumeMounts.add(f8VolumeMount);
f8Container.setVolumeMounts(f8VolumeMounts);
}
// 将ConfigMap挂载为pod的一个volume
List<KeyToPath> f8KeyToPathList = new ArrayList<KeyToPath>();
f8ConfigMap.getData().forEach((k, v) -> f8KeyToPathList.add(new KeyToPathBuilder().withKey(k).withPath(k).build())); // key既为键名称又作为挂载结果的文件名称
Volume f8Volume = new VolumeBuilder().withName(confMap.getName()).withNewConfigMap().withName(confMap.getName()).withItems(f8KeyToPathList).endConfigMap().build();
f8Volumes.add(f8Volume);
}
查看创建成功的pod:

