当前位置: 首页 > 工具软件 > fabric8 > 使用案例 >

使用Fabric8 kubernetes-client java client 操作kuberntes 自定义资源CR

慕容劲
2023-12-01

kubernetes-client (fabric8) to interact with kubernetes custom resources

kubernetes中的一切东西都叫做 resource,k8s 默认的提供了很多 resource,比如 pod/deployment… 而 custom resource 允许用户基于已有resource,创建新resource来扩展k8s;

在这里并不打算深入介绍CR/CRD;而着重介绍下如何通过 fabric8·kubernetes-client 来操作CR。

相信有很多场景需要 使用 java/golang sdk 去操作 k8s CR 资源;写这篇blog的起因也是因为需要去操作 seldon 的CR 来部署 machine leaning model

dependency

最好使用 > 4.6.0 版本

	<dependency>
		<groupId>io.fabric8</groupId>
		<artifactId>kubernetes-client</artifactId>
		<version>4.8.0</version>
	</dependency>
	<dependency>
		<groupId>io.fabric8</groupId>
		<artifactId>kubernetes-model</artifactId>
		<version>4.8.0</version>
	</dependency>

CRD

这里并不打算使用CRD新创建一个CR,直接来看Seldon的CRD

Rex@xxx:~/Rex$ kubectl get crd seldondeployments.machinelearning.seldon.io -o yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  annotations:
    cert-manager.io/inject-ca-from: seldon-system/seldon-serving-cert
  creationTimestamp: "2020-03-27T02:44:08Z"
  generation: 1
  labels:
    app: seldon
    app.kubernetes.io/instance: seldon-core
    app.kubernetes.io/name: seldon-core-operator
    app.kubernetes.io/version: 1.0.2
  name: seldondeployments.machinelearning.seldon.io
  resourceVersion: "247082"
  selfLink:
  uid:
spec:
  conversion:
    strategy: None
  group: machinelearning.seldon.io
  names:
    kind: SeldonDeployment
    listKind: SeldonDeploymentList
    plural: seldondeployments
    shortNames:
    - sdep
    singular: seldondeployment
  preserveUnknownFields: true
  scope: Namespaced
  subresources:
    status: {}
  validation:
    ... (省略万字validation...)
  versions:
  - name: v1
    served: true
    storage: true
  - name: v1alpha2
    served: true
    storage: false
  - name: v1alpha3
    served: true
    storage: false
status:
  acceptedNames:
    kind: SeldonDeployment
    listKind: SeldonDeploymentList
    plural: seldondeployments
    shortNames:
    - sdep
    singular: seldondeployment
  conditions:
  - lastTransitionTime: "2020-03-27T02:44:08Z"
    message: no conflicts found
    reason: NoConflicts
    status: "True"
    type: NamesAccepted
  - lastTransitionTime: null
    message: the initial names have been accepted
    reason: InitialNamesAccepted
    status: "True"
    type: Established
  storedVersions:
  - v1

查看此CR的实例

Rex@xxx:~/Rex$ kubectl get sdep -n seldon-system
NAME                  AGE
model-wines-quality   23h

fabric8·kubernetes-client

使用fabric8的kubernetes-client去操作CR,首先需要创建 CustomResourceDefinitionContext

在定义CRD context的时候,需要从CRD中获取相应信息

@Bean
public CustomResourceDefinitionContext seldonCrdCxt(){
	return new CustomResourceDefinitionContext
			.Builder()
			.withGroup("machinelearning.seldon.io")
			.withScope("Namespaced")
			.withVersion("v1alpha2")
			.withPlural("seldondeployments")
			.build();

拿到了CustomResourceDefinitionContext之后,只需要简单的调用 KubernetesClient.customResource(CustomResourceDefinitionContext customResourceDefinition)

/**
* Typeless API for interacting with CustomResources. You can do basic operations with CustomResources
* without having any model. You just need to pass an object providing basic information of
* CustomResource. CustomResource objects are parsed as HashMaps.
*
* @param customResourceDefinition CustomResourceDefinitionContext - information about CustomResource like versioning, namespaced or not and group etc
* @return a RawCustomResourceOperations object which offers several functions for creating, deleting, updating, watching CustomResources.
*/
RawCustomResourceOperationsImpl customResource(CustomResourceDefinitionContext customResourceDefinition);

for example:

Map<String, Object> deployments = k8sClient.customResource(seldonCrd).list(namespace);
 类似资料: