首先连接k8s客户端(略)
注入DefaultKubernetesClient
@Autowired
@Qualifier("defaultKubernetesClient")
private DefaultKubernetesClient client;
已查询istio的vs为例:
[root@k8s001 ~]# kubectl get vs gov-api-vs -ngov -oyaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
creationTimestamp: "2021-01-04T07:16:55Z"
generation: 1
managedFields:
- apiVersion: networking.istio.io/v1alpha3
fieldsType: FieldsV1
fieldsV1:
f:spec:
.: {}
f:gateways: {}
f:hosts: {}
f:http: {}
manager: Go-http-client
operation: Update
time: "2021-01-04T07:16:55Z"
name: gov-api-vs
namespace: gov
resourceVersion: "65412339"
selfLink: /apis/networking.istio.io/v1beta1/namespaces/gov/virtualservices/gov-api-vs
uid: 9bd8750b-0024-467c-bb7f-66809b8a54fb
spec:
gateways:
- xxx-gateway.istio-system
hosts:
- xxx.xxx.cn
http:
- route:
- destination:
host: gov-api-service
看了一下fabric8的deployment的实现,封装了好多类,为了简化,我把spec封装到了map里,后期可以优化
public class IstioCrdResourceDTO {
private HasMetadata objectMeta;
private HashMap<String,Object> spec;
public IstioCrdResourceDTO(HasMetadata objectMeta, HashMap<String, Object> spec) {
this.objectMeta = objectMeta;
this.spec = spec;
}
}
vsdto继承了上面的那个基类
public class VirtualServiceDTO extends IstioCrdResourceDTO{
public VirtualServiceDTO(HasMetadata objectMeta, HashMap<String, Object> spec) {
super(objectMeta, spec);
}
}
client调用的话调的就是k8s api,从kubecrl查询出来的也就是selfLink:/apis/networking.istio.io/v1beta1/namespaces/istio-system/gateways/biyi-gateway
只要client调用这个api没问题,就可以成功。
枚举类:
public enum IstioCrdType {
/* 例子:selfLink地址:
vs: /apis/networking.istio.io/v1beta1/namespaces/gov/virtualservices/gov-api-vs
dr: /apis/networking.istio.io/v1beta1/namespaces/gov/destinationrules/gov-api-dr
gw: /apis/networking.istio.io/v1beta1/namespaces/istio-system/gateways/biyi-gateway
*/
VirtualService("networking.istio.io","v1beta1","virtualservices","Namespaced"),
DestinationRule("networking.istio.io","v1beta1","destinationrules","Namespaced"),
Gateway("networking.istio.io","v1beta1","gateways","Namespaced");
private String group;
private String version;
private String plural;
private String scope;
private IstioCrdType(String group, String version, String plural, String scope) {
this.group = group;
this.version = version;
this.plural = plural;
this.scope = scope;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getPlural() {
return plural;
}
public void setPlural(String plural) {
this.plural = plural;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
}
实现类:
public IstioCrdResourceDTO getVirtualService(String namespace, String name) {
//构造vs的crd
CustomResourceDefinitionContext context = new CustomResourceDefinitionContext.Builder()
.withGroup(IstioCrdType.VirtualService.getGroup())
.withVersion(IstioCrdType.VirtualService.getVersion())
.withScope(IstioCrdType.VirtualService.getScope())
.withPlural(IstioCrdType.VirtualService.getPlural())
.build();
//获取crd的map资源
Map<String, Object> map = null;
try{
map = client.customResource(context).get(namespace, name);
}catch (Exception e) {
e.printStackTrace();
return null;
}
//通过JSON转成ObjectMeta对象
String metadataJson = JSON.toJSONString(map.get("metadata"));
ObjectMeta metadata = JSON.parseObject(metadataJson,ObjectMeta.class);
//使用构造器设置HasMetadata的属性
HasMetadata binding = new Binding(map.get("apiVersion").toString(),map.get("kind").toString(),metadata,null);
//设置spec,封装结果
VirtualServiceDTO virtualServiceDTO = new VirtualServiceDTO(binding, (HashMap<String,Object>)map.get("spec"));
return virtualServiceDTO;
}
如果打印出了404异常,一定是资源中的selfLink不对,请仔细查看或修改。我这里catch了一下,如果抛异常,则返回空即可。
测试类: