openshift / origin

Conformance test suite for OpenShift
http://www.openshift.org
Apache License 2.0
8.48k stars 4.7k forks source link

Go client missing json struct tags #11845

Closed maleck13 closed 7 years ago

maleck13 commented 7 years ago

When using the github.com/openshift/origin/pkg/client programatically, I noticed that json responses were not be decoded into the struct as expected. Digging into this further, I believe it is because the client uses the type definition in https://github.com/openshift/origin/blob/master/pkg/project/api/types.go#L35 Which seems to be missing the struct tags. Locally I modified this struct definition and added: json:"metadata" and then it started to work as expected, as in the values were present in the struct fields.

Version

v1.3.0

Steps To Reproduce

This is how I am using the client

var oc *oclient.Client
    factory := kubectlutil.NewFactory(nil)
    factory.BindFlags(flags)
    factory.BindExternalFlags(flags)
    flags.Parse(os.Args)
    flag.CommandLine.Parse([]string{})
    kubeClient, err := factory.Client()
    if err != nil {
        log.Panic(err)
    }
    restClientConfig, err := factory.ClientConfig()
    if err != nil {
        log.Panic(err)
    }
    ocfg := *restClientConfig
    ocfg.APIPath = ""
    oc, err = oclient.New(&ocfg)
    if err != nil {
        log.Panic(err)
    }
    oc.Projects().Get("someproject")
       if err != nil {
        log.Panic(err)
    }
    fmt.Println(p.Name)
Current Result

The name returned is empty as are most other fields

Expected Result

The name of the project

liggitt commented 7 years ago

Internal types should never be used for serialization

Are you importing the openshift client in combination with kubernetes code? Do you have the generated JSON codecs from kubernetes present? Those can cause problems because of the anonymous inclusion of ObjectMeta

maleck13 commented 7 years ago

@liggitt Thanks for the quick reply. I am importing kubernetes code. Is there a way to use the client without importing code from kubernetes, they seem quite tightly coupled? I am experimenting at the moment, so not sure I am doing things correctly. Here are what my imports look like for setting up the client:

import (
    oclient "github.com/openshift/origin/pkg/client"
    kubectlutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)

I am not sure how to check if I have the JSON codecs present.

liggitt commented 7 years ago

if you have the types.generated.go files with CodecEncodeSelf/CodecDecodeSelf implementations for kubernetes types, that can cause problems with serializing/deserializing openshift objects that embed those types

maleck13 commented 7 years ago

@liggitt I don't have those present. But I believe I figured out what I was doing wrong: I needed to register the Scheme(s) and then use the scheme convertor as shown below when retrieving buildConfigs. Is this how you would expect it to work? Or is there a better way? I have cut down the example for brevity.

//in the imports
imports(
        bc "github.com/openshift/origin/pkg/build/api"
    bcv1 "github.com/openshift/origin/pkg/build/api/v1"
)

bc.AddToScheme(api.Scheme)
bcv1.AddToScheme(api.Scheme)
out, err := api.Scheme.ConvertToVersion(bl, oc.APIVersion())
    if err != nil {
        log.Panic(err)
    }
    if v1bl, ok := out.(*bcv1.BuildConfigList); ok {
        fmt.Println("bc", v1bl.Items[0].Name)
    }
maleck13 commented 7 years ago

@liggitt I am happy for this to be closed now as I can progress, but interested in any opinions. Also is there any initiative to reduce the number of dependencies and simplify the usage of the client as a lib for external projects

mfojtik commented 7 years ago

@maleck13 https://trello.com/c/PTDrY0GF/794-13-provide-go-client-similar-to-kubernetes