kubectl apply -f equivalent in fabric8 java

2019-08-23 01:21发布

I was trying to use io.fabric8 api to create a few resources in kubernetes using a pod-spec.yaml.

Config config = new ConfigBuilder()
                  .withNamespace("ag")
                  .withMasterUrl(K8_URL)
                  .build();
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {

            LOGGER.info("Master: " + client.getMasterUrl());

            LOGGER.info("Loading File : " + args[0]);
            Pod pod = client.pods().load(new FileInputStream(args[0])).get();
            LOGGER.info("Pod created with name : " + pod.toString());

} catch (Exception e) {

            LOGGER.error(e.getMessage(), e);

}

The above code works if the resource type is of POD. Similarly for other resource type it is working fine. But if the yaml has multiple resource type like POD and service in the same file, how to use fabric8 Api ?

I was trying to use client.load(new FileInputStream(args[0])).createOrReplace(); but it is crashing with the below exception:

java.lang.NullPointerException
    at java.net.URI$Parser.parse(URI.java:3042)
    at java.net.URI.<init>(URI.java:588)
    at io.fabric8.kubernetes.client.utils.URLUtils.join(URLUtils.java:48)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.getMandatory(BaseOperation.java:208)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.get(BaseOperation.java:177)
    at io.fabric8.kubernetes.client.handlers.PodHandler.reload(PodHandler.java:53)
    at io.fabric8.kubernetes.client.handlers.PodHandler.reload(PodHandler.java:32)
    at io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.createOrReplace(NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java:202)
    at io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.createOrReplace(NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java:62)
    at com.nokia.k8s.InterpreterLanuch.main(InterpreterLanuch.java:66)

Yaml file used

apiVersion: v1
kind: Pod
metadata:
  generateName: zep-ag-pod
  annotations:
    kubernetes.io/psp: restricted
    spark-app-name: Zeppelin-spark-shared-process
  namespace: ag
  labels:
    app: zeppelin
    int-app-selector: shell-123

spec:
  containers:
    - name: ag-csf-zep
      image: bcmt-registry:5000/zep-spark2.2:9
      imagePullPolicy: IfNotPresent
      command: ["/bin/bash"]
      args: ["-c","echo Hi && sleep 60 && echo Done"]
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          drop:
          - ALL
        runAsNonRoot: true
  securityContext:
    fsGroup: 2000
    runAsUser: 1510
  serviceAccount: csfzeppelin
  serviceAccountName: csfzeppelin
---
apiVersion: v1
kind: Service
metadata:
  name: zeppelin-service
  namespace: ag
  labels:
    app: zeppelin
spec:
  type: NodePort
  ports:
  - name: zeppelin-service
    port: 30099
    protocol: TCP
    targetPort: 8080
  selector:
    app: zeppelin

1条回答
迷人小祖宗
2楼-- · 2019-08-23 02:05

You don't need to specify resource type whenever loading a file with multiple documents. You simply need to do:

  // Load Yaml into Kubernetes resources
  List<HasMetadata> result = client.load(new FileInputStream(args[0])).get();
  // Apply Kubernetes Resources
  client.resourceList(result).inNamespace(namespace).createOrReplace() 
查看更多
登录 后发表回答