guettli
guettli

Reputation: 27816

Unstructured: spec.replicas is float?

I am using the unstructured package to read a Deployment:

    i, _, err := unstructured.NestedInt64(obj.Object, "spec", "replicas")

fails:

.spec.replicas accessor error: 1 is of the type float64, expected int64

According to the resource definition spec.replicas is of type integer.

I am using manifestreader from cli-utils to create the unstructred objects from yaml files on disk.

I would like to avoid converting the float to int myself.

I guess I did something wrong when reading the files:

    restMapper := crClient.RESTMapper()
    reader := manifestreader.PathManifestReader{
        Path: path,
        ReaderOptions: manifestreader.ReaderOptions{
            Mapper:   restMapper,
            Validate: true,
        },
    }
    objs, err := reader.Read()

How can I make NestedInt64 work?

Upvotes: 0

Views: 30

Answers (1)

guettli
guettli

Reputation: 27816

PathManifestReader is using the stdlib's encoding/json for unmarshaling instead of apimachinery json Unmarshal

The json package of apimachinery encodes to int64 when a number has no fractional part. Stdlib always encodes to float64.

In my case, I can't influence the Unmarshal, so I need to use unstructured.NestFieldNoCopy and do a manual type switch.

Thank you for Ben Luddy and Stefan Schimanski which answered my question at #api-machinery

Upvotes: 0

Related Questions