kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.79k stars 2.91k forks source link

apps.V1.Deployment.Patch(): W0313 warnings.go: fractional byte value "4402341478400m" is invalid, must be an integer #1235

Closed kivagant-ba closed 1 year ago

kivagant-ba commented 1 year ago

We have an old code that throws warnings when patching deployments. The code is controversial, but the warning comes from an implementation of appsclientv1.DeploymentInterface. The Deployment has several containers, one of them is returned with the resource specificaiton:

    resources:
      limits:
        memory: 4402341478400m
      requests:
        memory: 4402341478400m

And the code prints the warning:

W0313 15:01:55.695574   83180 warnings.go:70] spec.template.spec.containers[2].resources.limits[memory]: fractional byte value "4402341478400m" is invalid, must be an integer

The code is complicated, but a simplified piece looks like that:


changeAction := func(k8sDeployments appsclientv1.DeploymentInterface, k8sDeployment appsv1.Deployment) error {
        // https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md
        // https://stackoverflow.com/a/57559438
        // A no-op patch that adds an annotiation will restart the deployment
        patchTemplate := `{ "spec": { "template": { "metadata": { "annotations": { "restartedAt": "%s" } } } } }`
        patch := fmt.Sprintf(patchTemplate, now.Format(time.RFC3339))
        patchBytes := []byte(patch)
        _, err := deployments.k8sDeployments.Patch(todoctx.TODO(), k8sDeployment.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
        return err
    }

// ...
k8sDeployments := client.AppsV1().Deployments(namespace)

// and k8sDeployment comes from a loop over:
// deployments.k8sDeployments.List(todoctx.TODO(), deployments.k8sListOptions)

err := changeAction(k8sDeployments, k8sDeployment)
liggitt commented 1 year ago

fractional bytes specified for memory values are invalid, so those requests/limits are invalid

even though your controller is not modifying those fields, the warning is trying to tell anyone who will listen that the deployment resource requests of the object being updated are malformed and should be fixed.

kivagant-ba commented 1 year ago

Interesting... Okay. The thing is that we never set the resource requests and limits in this format, something happened within K8s that converted it to 4402341478400m. But good to know, thank you anyway.