在创建Pod集群时获取ApiException

时间:2020-06-09 10:38:21

标签: java spring-boot kubernetes google-cloud-platform google-kubernetes-engine

我的应用程序使用client-java-8.0.2.jar和Java 1.8.0_252版本。使用以下代码创建Pod时出现以下错误,

代码:

[
  [
    "7"
  ],
  [
    "32"
  ],
  [
  "7",
  "32"
  ]
]

错误:

// get the default api-client
ApiClient client = Config.defaultClient();

Configuration.setDefaultApiClient(client);

CoreV1Api api = new CoreV1Api();

List<V1EnvVar> envVariables = new ArrayList<V1EnvVar>();
for (Map.Entry<String, String> entry : attributes.entrySet()) {
    V1EnvVar env = new V1EnvVar();
    env.setName(entry.getKey());
    env.setValue(entry.getValue());
    envVariables.add(env);
}
// create pod of a Kubernetes cluster
V1Pod pod = new V1PodBuilder().withNewMetadata().withName(serviceName).endMetadata().withNewSpec()
        .addNewContainer().addAllToEnv(envVariables).withName("www").withImage(dockerImage).endContainer()
        .endSpec().build();

pod = api.createNamespacedPod("default", pod, null, null, null);

这里的任何输入都非常感谢。

1 个答案:

答案 0 :(得分:1)

答案是群集名称应为“小写字母数字字符”

我尝试使用以下代码调试此问题,

我的更新代码,

try {
    pod = api.createNamespacedPod("default", pod, null, null, null);
    LOGGER.debug(String.format("Cluster with name \"%s\" created successfully", pod.getMetadata().getName()));
} catch (ApiException ae) {
    LOGGER.error("API EXCEPTION: " + ae.getResponseBody());
}

错误:

API EXCEPTION: {
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "Pod \"Sample\" is invalid: metadata.name: Invalid value: \"Sample\": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')",
  "reason": "Invalid",
  "details": {
    "name": "Sample",
    "kind": "Pod",
    "causes": [
      {
        "reason": "FieldValueInvalid",
        "message": "Invalid value: \"Sample\": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')",
        "field": "metadata.name"
      }
    ]
  },
  "code": 422
}