import ( "fmt" "github.com/google/go-cmp/cmp" conversion "github.com/googlecloudplatform/kubernets/pkg/conversion" schema "github.com/googlecloudplatform/kubernets/pkg/apis/meta/v1" ) func convertObject(oldObj, newObj interface{}) error { oldAccessor, err := conversion.NewAccessor(oldObj) if err != nil { return err } newAccessor, err := conversion.NewAccessor(newObj) if err != nil { return err } err = newAccessor.ConvertFrom(oldAccessor) if err != nil { return err } if !cmp.Equal(oldObj, newObj) { return fmt.Errorf("converted obj not equal to old obj") } return nil } func main() { apiObj := &schema.APIObject{} v1beta1Obj := &v1beta1.CustomResourceDefinition{} if err := convertObject(apiObj, v1beta1Obj); err != nil { panic(err.Error()) } }
import ( "fmt" "github.com/googlecloudplatform/kubernetes/pkg/conversion" ) func main() { yaml := "apiVersion: v1\nkind: Pod\nmetadata:\n name: my-pod" json, err := conversion.ConvertString(yaml, conversion.EncodingYAML, conversion.EncodingJSON) if err != nil { panic(err.Error()) } fmt.Println(json) }
import ( "fmt" "github.com/googlecloudplatform/kubernetes/pkg/conversion" ) func main() { json := "{\"apiVersion\": \"v1\",\"kind\": \"Pod\",\"metadata\": {\"name\": \"my-pod\"}}" yaml, err := conversion.ConvertString(json, conversion.EncodingJSON, conversion.EncodingYAML) if err != nil { panic(err.Error()) } fmt.Println(yaml) }