Move from go-yaml/yaml to ghodss/yaml

This commit is contained in:
Sam Ghods 2014-11-30 20:12:37 -08:00
parent 603ec33821
commit 9a9a1e0939
27 changed files with 61 additions and 122 deletions

View File

@ -23,8 +23,8 @@ import (
"os" "os"
"strings" "strings"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"gopkg.in/v1/yaml"
) )
const usage = "usage: enscope specFilename configFilename" const usage = "usage: enscope specFilename configFilename"

View File

@ -37,7 +37,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
dockerclient "github.com/fsouza/go-dockerclient" dockerclient "github.com/fsouza/go-dockerclient"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
const usage = "usage: podex [-json|-yaml] [-id PODNAME] IMAGES" const usage = "usage: podex [-json|-yaml] [-id PODNAME] IMAGES"

View File

@ -35,8 +35,8 @@ import (
// TODO: handle multiple versions correctly // TODO: handle multiple versions correctly
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"gopkg.in/v1/yaml"
) )
// TODO: Also handle lists of simple services, and multiple input files // TODO: Also handle lists of simple services, and multiple input files

View File

@ -45,8 +45,8 @@ import (
// it should be possible to specify the version for the whole map. // it should be possible to specify the version for the whole map.
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"gopkg.in/v1/yaml"
) )
const usage = "usage: srvexpand filename" const usage = "usage: srvexpand filename"

View File

@ -23,6 +23,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"testing" "testing"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
@ -138,6 +139,14 @@ var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
c.RandString(): c.RandString(), c.RandString(): c.RandString(),
} }
}, },
func(t *time.Time, c fuzz.Continue) {
// This is necessary because the standard fuzzed time.Time object is
// completely nil, but when JSON unmarshals dates it fills in the
// unexported loc field with the time.UTC object, resulting in
// reflect.DeepEqual returning false in the round trip tests. We solve it
// by using a date that will be identical to the one JSON unmarshals.
*t = time.Date(2000, 1, 1, 1, 1, 1, 0, time.UTC)
},
) )
func runTest(t *testing.T, codec runtime.Codec, source runtime.Object) { func runTest(t *testing.T, codec runtime.Codec, source runtime.Object) {
@ -162,7 +171,7 @@ func runTest(t *testing.T, codec runtime.Codec, source runtime.Object) {
return return
} }
if !reflect.DeepEqual(source, obj2) { if !reflect.DeepEqual(source, obj2) {
t.Errorf("1: %v: diff: %v\nCodec: %v\nData: %s\nSource: %#v", name, util.ObjectDiff(source, obj2), codec, string(data), source) t.Errorf("1: %v: diff: %v\nCodec: %v\nData: %s\nSource: %#v", name, util.ObjectGoPrintDiff(source, obj2), codec, string(data), source)
return return
} }

View File

@ -20,7 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
// Decode converts a YAML or JSON string back into a pointer to an api object. // Decode converts a YAML or JSON string back into a pointer to an api object.

View File

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
// MetaFactory is used to store and retrieve the version and kind // MetaFactory is used to store and retrieve the version and kind

View File

@ -26,8 +26,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/ghodss/yaml"
"github.com/google/gofuzz" "github.com/google/gofuzz"
"gopkg.in/v1/yaml"
) )
var fuzzIters = flag.Int("fuzz_iters", 50, "How many fuzzing iterations to do.") var fuzzIters = flag.Int("fuzz_iters", 50, "How many fuzzing iterations to do.")

View File

@ -33,8 +33,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/wait" "github.com/GoogleCloudPlatform/kubernetes/pkg/util/wait"
"github.com/GoogleCloudPlatform/kubernetes/pkg/version" "github.com/GoogleCloudPlatform/kubernetes/pkg/version"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"gopkg.in/v1/yaml"
) )
func GetServerVersion(client *client.Client) (*version.Info, error) { func GetServerVersion(client *client.Client) (*version.Info, error) {

View File

@ -24,7 +24,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
func TestParseBadStorage(t *testing.T) { func TestParseBadStorage(t *testing.T) {

View File

@ -29,8 +29,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"gopkg.in/v1/yaml"
) )
// ResourcePrinter is an interface that knows how to print API resources. // ResourcePrinter is an interface that knows how to print API resources.

View File

@ -26,7 +26,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
func TestYAMLPrinterPrint(t *testing.T) { func TestYAMLPrinterPrint(t *testing.T) {

View File

@ -24,9 +24,9 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/config" "github.com/GoogleCloudPlatform/kubernetes/pkg/config"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/v1/yaml"
) )
// DataToObjects converts the raw JSON data into API objects // DataToObjects converts the raw JSON data into API objects

View File

@ -29,8 +29,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"gopkg.in/v1/yaml"
) )
// GetPrinter takes a format type, an optional format argument, a version and a convertor // GetPrinter takes a format type, an optional format argument, a version and a convertor

View File

@ -29,7 +29,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
type testStruct struct { type testStruct struct {

View File

@ -287,7 +287,7 @@ func (s *podStorage) Sync() {
func (s *podStorage) MergedState() interface{} { func (s *podStorage) MergedState() interface{} {
s.podLock.RLock() s.podLock.RLock()
defer s.podLock.RUnlock() defer s.podLock.RUnlock()
var pods []api.BoundPod pods := make([]api.BoundPod, 0)
for _, sourcePods := range s.pods { for _, sourcePods := range s.pods {
for _, podRef := range sourcePods { for _, podRef := range sourcePods {
pod, err := api.Scheme.Copy(podRef) pod, err := api.Scheme.Copy(podRef)

View File

@ -59,10 +59,13 @@ func CreateValidPod(name, namespace, source string) api.BoundPod {
} }
func CreatePodUpdate(op kubelet.PodOperation, pods ...api.BoundPod) kubelet.PodUpdate { func CreatePodUpdate(op kubelet.PodOperation, pods ...api.BoundPod) kubelet.PodUpdate {
if len(pods) == 0 { // We deliberately return an empty slice instead of a nil pointer here
return kubelet.PodUpdate{Op: op} // because reflect.DeepEqual differentiates between the two and we need to
} // pick one for consistency.
newPods := make([]api.BoundPod, len(pods)) newPods := make([]api.BoundPod, len(pods))
if len(pods) == 0 {
return kubelet.PodUpdate{newPods, op}
}
for i := range pods { for i := range pods {
newPods[i] = pods[i] newPods[i] = pods[i]
} }

View File

@ -33,8 +33,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"gopkg.in/v1/yaml"
) )
type sourceFile struct { type sourceFile struct {
@ -97,12 +97,13 @@ func extractFromDir(name string) ([]api.BoundPod, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("glob failed: %v", err) return nil, fmt.Errorf("glob failed: %v", err)
} }
pods := make([]api.BoundPod, 0)
if len(dirents) == 0 { if len(dirents) == 0 {
return nil, nil return pods, nil
} }
sort.Strings(dirents) sort.Strings(dirents)
pods := []api.BoundPod{}
for _, path := range dirents { for _, path := range dirents {
statInfo, err := os.Stat(path) statInfo, err := os.Stat(path)
if err != nil { if err != nil {

View File

@ -29,7 +29,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
func ExampleManifestAndPod(id string) (api.ContainerManifest, api.BoundPod) { func ExampleManifestAndPod(id string) (api.ContainerManifest, api.BoundPod) {

View File

@ -29,8 +29,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"gopkg.in/v1/yaml"
) )
type sourceURL struct { type sourceURL struct {

View File

@ -35,9 +35,9 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/healthz" "github.com/GoogleCloudPlatform/kubernetes/pkg/healthz"
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog" "github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/google/cadvisor/info" "github.com/google/cadvisor/info"
"gopkg.in/v1/yaml"
) )
// Server is a http.Handler which exposes kubelet functionality over HTTP. // Server is a http.Handler which exposes kubelet functionality over HTTP.

View File

@ -43,6 +43,10 @@ const (
// For setting the state of the system to a given state for this source configuration, set // For setting the state of the system to a given state for this source configuration, set
// Pods as desired and Op to SET, which will reset the system state to that specified in this // Pods as desired and Op to SET, which will reset the system state to that specified in this
// operation for this source channel. To remove all pods, set Pods to empty object and Op to SET. // operation for this source channel. To remove all pods, set Pods to empty object and Op to SET.
//
// Additionally, Pods should never be nil - it should always point to an empty slice. While
// functionally similar, this helps our unit tests properly check that the correct PodUpdates
// are generated.
type PodUpdate struct { type PodUpdate struct {
Pods []api.BoundPod Pods []api.BoundPod
Op PodOperation Op PodOperation

View File

@ -1,86 +0,0 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package runtime
import (
"gopkg.in/v1/yaml"
)
// Encode()/Decode() are the canonical way of converting an API object to/from
// wire format. This file provides utility functions which permit doing so
// recursively, such that API objects of types known only at run time can be
// embedded within other API types.
// UnmarshalJSON implements the json.Unmarshaler interface.
func CodecUnmarshalJSON(codec Codec, b []byte) (Object, error) {
// Handle JSON's "null": Decode() doesn't expect it.
if len(b) == 4 && string(b) == "null" {
return nil, nil
}
obj, err := codec.Decode(b)
if err != nil {
return nil, err
}
return obj, nil
}
// MarshalJSON implements the json.Marshaler interface.
func CodecMarshalJSON(codec Codec, obj Object) ([]byte, error) {
if obj == nil {
// Encode unset/nil objects as JSON's "null".
return []byte("null"), nil
}
return codec.Encode(obj)
}
// SetYAML implements the yaml.Setter interface.
func CodecSetYAML(codec Codec, tag string, value interface{}) (Object, bool) {
if value == nil {
return nil, true
}
// Why does the yaml package send value as a map[interface{}]interface{}?
// It's especially frustrating because encoding/json does the right thing
// by giving a []byte. So here we do the embarrasing thing of re-encode and
// de-encode the right way.
// TODO: Write a version of Decode that uses reflect to turn this value
// into an API object.
b, err := yaml.Marshal(value)
if err != nil {
panic("yaml can't reverse its own object")
}
obj, err := codec.Decode(b)
if err != nil {
return nil, false
}
return obj, true
}
// GetYAML implements the yaml.Getter interface.
func CodecGetYAML(codec Codec, obj Object) (tag string, value interface{}) {
if obj == nil {
value = "null"
return
}
// Encode returns JSON, which is conveniently a subset of YAML.
v, err := codec.Encode(obj)
if err != nil {
panic("impossible to encode API object!")
}
return tag, v
}

View File

@ -19,7 +19,7 @@ package runtime
import ( import (
"errors" "errors"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
func (re *RawExtension) UnmarshalJSON(in []byte) error { func (re *RawExtension) UnmarshalJSON(in []byte) error {

View File

@ -19,6 +19,8 @@ package util
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/davecgh/go-spew/spew"
) )
// StringDiff diffs a and b and returns a human readable diff. // StringDiff diffs a and b and returns a human readable diff.
@ -56,13 +58,19 @@ func ObjectDiff(a, b interface{}) string {
return StringDiff(string(ab), string(bb)) return StringDiff(string(ab), string(bb))
} }
// ObjectGoPrintDiff is like ObjectDiff, but uses go's %#v formatter to print the // ObjectGoPrintDiff is like ObjectDiff, but uses go-spew to print the objects,
// objects, in case json isn't showing you the difference. (reflect.DeepEqual makes // which shows absolutely everything by recursing into every single pointer
// a distinction between nil and empty slices, for example, even though nothing else // (go's %#v formatters OTOH stop at a certain point). This is needed when you
// really does.) // can't figure out why reflect.DeepEqual is returning false and nothing is
// showing you differences. This will.
func ObjectGoPrintDiff(a, b interface{}) string { func ObjectGoPrintDiff(a, b interface{}) string {
s := spew.ConfigState{
Indent: " ",
// Extra deep spew.
DisableMethods: true,
}
return StringDiff( return StringDiff(
fmt.Sprintf("%#v", a), s.Sdump(a),
fmt.Sprintf("%#v", b), s.Sdump(b),
) )
} }

View File

@ -22,7 +22,7 @@ import (
"testing" "testing"
"time" "time"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
type TimeHolder struct { type TimeHolder struct {
@ -34,7 +34,7 @@ func TestTimeMarshalYAML(t *testing.T) {
input Time input Time
result string result string
}{ }{
{Time{}, "t: \"null\"\n"}, {Time{}, "t: null\n"},
{Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "t: 1998-05-05T05:05:05Z\n"}, {Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "t: 1998-05-05T05:05:05Z\n"},
{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: 1998-05-05T05:05:05Z\n"}, {Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: 1998-05-05T05:05:05Z\n"},
} }
@ -56,7 +56,7 @@ func TestTimeUnmarshalYAML(t *testing.T) {
input string input string
result Time result Time
}{ }{
{"t: \"null\"\n", Time{}}, {"t: null\n", Time{}},
{"t: 1998-05-05T05:05:05Z\n", Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC)}, {"t: 1998-05-05T05:05:05Z\n", Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC)},
} }

View File

@ -21,7 +21,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"gopkg.in/v1/yaml" "github.com/ghodss/yaml"
) )
func TestHandleCrash(t *testing.T) { func TestHandleCrash(t *testing.T) {