kubernetes-client / java

Official Java client library for kubernetes
http://kubernetes.io/
Apache License 2.0
3.54k stars 1.88k forks source link

Create Pod BadRequest Error #699

Closed liboyue23 closed 4 years ago

liboyue23 commented 5 years ago

I use the sdk to create pod, here is the dependency:

        <dependency>
            <groupId>io.kubernetes</groupId>
            <artifactId>client-java</artifactId>
            <version>5.0.0</version>
        </dependency>

and here is the error

ResponseBody : {
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "Pod in version \"v1\" cannot be handled as a Pod: v1.Pod.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.Resources: v1.ResourceRequirements.Limits: unmarshalerDecoder: quantities must match the regular expression '^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$', error found in #10 byte of ...|INARY_SI\"},\"cpu\":{\"n|..., bigger context ...|memory\":{\"number\":8589934592,\"format\":\"BINARY_SI\"},\"cpu\":{\"number\":4,\"format\":\"DECIMAL_SI\"}},\"reques|...",
  "reason": "BadRequest",
  "code": 400
}

the print json is:

{
  "apiVersion": "v1",
  "kind": "Pod",
  "metadata": {
    "labels": {
      "app": "drds-server-b28382e3-15aa-4f4f-94a8-1855dd1495ad"
    },
    "name": "drds-server-b28382e3-15aa-4f4f-94a8-1855dd1495ad",
    "namespace": "drds"
  },
  "spec": {
    "affinity": {
      "podAntiAffinity": {
        "requiredDuringSchedulingIgnoredDuringExecution": [
          {
            "labelSelector": {
              "matchExpressions": [
                {
                  "key": "app",
                  "operator": "In",
                  "values": [
                    "b28382e3-15aa-4f4f-94a8-1855dd1495ad"
                  ]
                }
              ]
            },
            "topologyKey": "kubernetes.io/hostname"
          }
        ]
      }
    },
    "containers": [
      {
        "env": [
          {
            "name": "room_id",
            "value": "cn-qd-hyq-d01"
          },
          {
            "name": "deploy_mode",
            "value": "private"
          },
          {
            "name": "advancedSecurity",
            "value": "true"
          },
          {
            "name": "dncs_server_list",
            "value": ""
          },
          {
            "name": "manager_reg_domain",
            "value": "172.16.190.244,172.16.190.245"
          },
          {
            "name": "manager_domain_ip",
            "value": "drds-manager-inner.drds.cn-qd-hyq-d01.paas.com"
          },
          {
            "name": "manager_grab_port",
            "value": "8080"
          },
          {
            "name": "region_id",
            "value": "cn-qd-hyq-d01"
          },
          {
            "name": "enable_vipclient",
            "value": ""
          },
          {
            "name": "default_namespace",
            "value": "drds"
          },
          {
            "name": "NetworkMode",
            "value": ""
          },
          {
            "name": "cert_keypath",
            "value": "/apsara/cloud/app/aliware-ark/ArkAgent#/ark-agent/current/conf.global/self/user/https_certificates/conf/privatecloud.pem"
          },
          {
            "name": "manager_grab_period",
            "value": "5000"
          },
          {
            "name": "sslEnable",
            "value": "false"
          },
          {
            "name": "constraint_node_type",
            "value": ""
          },
          {
            "name": "manager_reg_port",
            "value": "3888"
          },
          {
            "name": "manager_domain_port",
            "value": "80"
          },
          {
            "name": "diamond_addr_server",
            "value": "10.14.26.35"
          },
          {
            "name": "rootca_keypath",
            "value": ""
          },
          {
            "name": "trusted_ips",
            "value": "127.0.0.1,172.16.190.244,172.16.190.245"
          },
          {
            "name": "typeId",
            "value": "4c8g"
          },
          {
            "name": "azone_id",
            "value": "cn-qd-hyq-amtest8001-a"
          },
          {
            "name": "switchCloud",
            "value": "k8s"
          },
          {
            "name": "priate_keypath",
            "value": "/apsara/cloud/app/aliware-ark/ArkAgent#/ark-agent/current/conf.global/self/user/https_certificates/conf/privatecloud_key.pem"
          }
        ],
        "image": "drds/drds-server@sha256:d9752d2cb36141f0430cd5b8827c198d7fc7b5bf02dfa09d5580c8d5ddfe3f17",
        "imagePullPolicy": "Always",
        "livenessProbe": {
          "exec": {
            "command": [
              "/bin/sh",
              "-c",
              "python /checkHealth.py"
            ]
          },
          "initialDelaySeconds": 600,
          "timeoutSeconds": 300
        },
        "name": "drds-server",
        "readinessProbe": {
          "exec": {
            "command": [
              "/bin/sh",
              "-c",
              "python /checkHealth.py"
            ]
          },
          "initialDelaySeconds": 10,
          "periodSeconds": 5
        },
        "resources": {
          "limits": {
            "memory": {
              "number": 8589934592,
              "format": "BINARY_SI"
            },
            "cpu": {
              "number": 4,
              "format": "DECIMAL_SI"
            }
          },
          "requests": {
            "memory": {
              "number": 8589934592,
              "format": "BINARY_SI"
            },
            "cpu": {
              "number": 4,
              "format": "DECIMAL_SI"
            }
          }
        }
      }
    ],
    "restartPolicy": "Always"
  }
}

What's wrong with my code?

brendandburns commented 5 years ago

Can you move forward to at least version 6.0.1 in your pom.xml? That's the latest released library.

It looks to me like the Quantity serializer isn't properly registered in GSON for serialization.

Please move to the 6.0.1 and if that doesn't fix things, please include the code for how you are creating your Client.

Thanks!

yue9944882 commented 5 years ago
"resources": {
          "limits": {
            "memory": {
              "number": 8589934592,
              "format": "BINARY_SI"
            },
            "cpu": {
              "number": 4,
              "format": "DECIMAL_SI"
            }
          },
          "requests": {
            "memory": {
              "number": 8589934592,
              "format": "BINARY_SI"
            },
            "cpu": {
              "number": 4,
              "format": "DECIMAL_SI"
            }
          }
        }

the quantities are not correctly serialized.. and it's suggesting that you're not using the provided GSON codec from ApiClient..

liboyue23 commented 5 years ago
"resources": {
          "limits": {
            "memory": {
              "number": 8589934592,
              "format": "BINARY_SI"
            },
            "cpu": {
              "number": 4,
              "format": "DECIMAL_SI"
            }
          },
          "requests": {
            "memory": {
              "number": 8589934592,
              "format": "BINARY_SI"
            },
            "cpu": {
              "number": 4,
              "format": "DECIMAL_SI"
            }
          }
        }

the quantities are not correctly serialized.. and it's suggesting that you're not using the provided GSON codec from ApiClient..

I use the same code, to run in minikube v1.1.1, it works and the print json is:

        "resources": {
          "limits": {
            "memory": "2Gi",
            "cpu": "1"
          },
          "requests": {
            "memory": "2Gi",
            "cpu": "1"
          }
        }

here is my code:

   public String createInst(){
      V1Pod body = new V1Pod();
       body.setApiVersion("v1");
                body.setKind("Pod");
                V1ObjectMeta meta = new V1ObjectMeta();
                meta.setName(dockerName);
                Map<String, String> labelMap = Maps.newHashMap();
                labelMap.put(ANTI_AFFINITY_KEY, dockerName);
                meta.setLabels(labelMap);
                String namespace = getDefaultNamespace();
                logger.info("namespace is : " + namespace);
                meta.setNamespace(namespace);
                body.setMetadata(meta);
                V1PodSpec spec = new V1PodSpec();
                V1Container container = new V1Container();
                container.setName("drds-server");
                container.setImage(rq.getImageId());
                container.setImagePullPolicy("Always");
                container.setEnv(buildEnv(rq.getInstanceType(), String.valueOf(rq.isSslEnable())));
                container.setResources(buildResource(rq.getInstanceType()));
                container.setLivenessProbe(buildLivenessProbe());
                container.setReadinessProbe(buildReadinessProbe());

                List<V1Container> list = Lists.newArrayList();
                list.add(container);
                spec.setContainers(list);
                spec.setRestartPolicy("Always");
                spec.setAffinity(buildAffinity(rq.getAffinityId()));
                body.setSpec(spec);
                System.out.println(body.toString());
                V1Pod result = api.createNamespacedPod(getDefaultNamespace(), body, "false", null, null);
   }

    private V1ResourceRequirements buildResource(String spec) {
        Map<String, String> parseSpec = parseSpec(spec);
        String mem = parseSpec.get("mem");
        String cpu = parseSpec.get("cpu");
        V1ResourceRequirements resourceRequirements = new V1ResourceRequirements();
        Long m = Integer.valueOf(mem) * 1024L;
        Quantity memoryQuantity = new QuantityFormatter().parse(String.valueOf(m) + "Mi");
        Quantity cpuQuantity = new QuantityFormatter().parse(cpu);
        resourceRequirements.putLimitsItem("memory", memoryQuantity);
        resourceRequirements.putLimitsItem("cpu", cpuQuantity);
        resourceRequirements.putRequestsItem("memory", memoryQuantity);
        resourceRequirements.putRequestsItem("cpu", cpuQuantity);
        return resourceRequirements;
    }

    private Map<String, String> parseSpec(String spec) {
        Map<String, String> map = Maps.newHashMap();

        char[] charArray = spec.toCharArray();
        StringBuilder cpuSb = new StringBuilder();
        StringBuilder memSb = new StringBuilder();
        boolean isCpu = true;
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] != 'c' && isCpu) {
                cpuSb.append(charArray[i]);
            } else if (charArray[i] == 'c') {
                isCpu = false;
            } else if (charArray[i] != 'g' && !isCpu) {
                memSb.append(charArray[i]);
            } else if (charArray[i] == 'g') {
                break;
            }
        }

        String cpu = cpuSb.toString();
        String mem = memSb.toString();
        map.put("cpu", cpu);
        map.put("mem", mem);
        return map;
    }

but use in kubernetes 1.12.6 version, the print json is:

"resources": {
          "limits": {
            "memory": {
              "number": 8589934592,
              "format": "BINARY_SI"
            },
            "cpu": {
              "number": 4,
              "format": "DECIMAL_SI"
            }
          },
          "requests": {
            "memory": {
              "number": 8589934592,
              "format": "BINARY_SI"
            },
            "cpu": {
              "number": 4,
              "format": "DECIMAL_SI"
            }
          }
        }

why ?

fejta-bot commented 4 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

fejta-bot commented 4 years ago

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle rotten

fejta-bot commented 4 years ago

Rotten issues close after 30d of inactivity. Reopen the issue with /reopen. Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /close

k8s-ci-robot commented 4 years ago

@fejta-bot: Closing this issue.

In response to [this](https://github.com/kubernetes-client/java/issues/699#issuecomment-586153022): >Rotten issues close after 30d of inactivity. >Reopen the issue with `/reopen`. >Mark the issue as fresh with `/remove-lifecycle rotten`. > >Send feedback to sig-testing, kubernetes/test-infra and/or [fejta](https://github.com/fejta). >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.