From 0447dd2f9ebed4f3fa8696afe8847c4e8053da8f Mon Sep 17 00:00:00 2001 From: Benjamin Elder Date: Mon, 14 Oct 2019 17:43:38 -0700 Subject: [PATCH] switch to yaml.v3 --- go.mod | 1 + go.sum | 2 + pkg/apis/config/v1alpha3/types.go | 54 ++++++++++--------- .../config/v1alpha3/zz_generated.deepcopy.go | 25 +++++---- pkg/internal/apis/config/convert.go | 7 --- pkg/internal/apis/config/encoding/load.go | 22 ++++++-- pkg/internal/apis/config/types.go | 7 --- .../apis/config/zz_generated.deepcopy.go | 10 ---- 8 files changed, 64 insertions(+), 64 deletions(-) diff --git a/go.mod b/go.mod index e96b9f10..6f5eb216 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/pkg/errors v0.8.1 github.com/spf13/cobra v0.0.3 golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 + gopkg.in/yaml.v3 v3.0.0-20191010095647-fc94e3f71652 k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b // indirect k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d sigs.k8s.io/kustomize/v3 v3.2.0 diff --git a/go.sum b/go.sum index 8869a0d1..0b555f33 100644 --- a/go.sum +++ b/go.sum @@ -142,6 +142,8 @@ gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20191010095647-fc94e3f71652 h1:VKvJ/mQ4BgCjZUDggYFxTe0qv9jPMHsZPD4Xt91Y5H4= +gopkg.in/yaml.v3 v3.0.0-20191010095647-fc94e3f71652/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= k8s.io/api v0.0.0-20190313235455-40a48860b5ab/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA= k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b h1:aBGgKJUM9Hk/3AE8WaZIApnTxG35kbuQba2w+SXqezo= k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA= diff --git a/pkg/apis/config/v1alpha3/types.go b/pkg/apis/config/v1alpha3/types.go index 1eea3644..384650b0 100644 --- a/pkg/apis/config/v1alpha3/types.go +++ b/pkg/apis/config/v1alpha3/types.go @@ -17,38 +17,40 @@ limitations under the License. package v1alpha3 import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/kind/pkg/container/cri" ) -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object - // Cluster contains kind cluster configuration type Cluster struct { - // TypeMeta representing the type of the object and its API schema version. - metav1.TypeMeta `json:",inline"` + TypeMeta `yaml:",inline" json:",inline"` // Nodes contains the list of nodes defined in the `kind` Cluster // If unset this will default to a single control-plane node // Note that if more than one control plane is specified, an external // control plane load balancer will be provisioned implicitly - Nodes []Node `json:"nodes,omitempty"` + Nodes []Node `yaml:"nodes,omitempty" json:"nodes,omitempty"` /* Advanced fields */ // Networking contains cluster wide network settings - Networking Networking `json:"networking,omitempty"` + Networking Networking `yaml:"networking,omitempty" json:"networking,omitempty"` // KubeadmConfigPatches are applied to the generated kubeadm config as // strategic merge patches to `kustomize build` internally // https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md // This should be an inline yaml blob-string - KubeadmConfigPatches []string `json:"kubeadmConfigPatches,omitempty"` + KubeadmConfigPatches []string `yaml:"kubeadmConfigPatches,omitempty" json:"kubeadmConfigPatches,omitempty"` // KubeadmConfigPatchesJSON6902 are applied to the generated kubeadm config // as patchesJson6902 to `kustomize build` - KubeadmConfigPatchesJSON6902 []PatchJSON6902 `json:"kubeadmConfigPatchesJson6902,omitempty"` + KubeadmConfigPatchesJSON6902 []PatchJSON6902 `yaml:"kubeadmConfigPatchesJson6902,omitempty" json:"kubeadmConfigPatchesJson6902,omitempty"` +} + +// TypeMeta partially copies apimachinery/pkg/apis/meta/v1.TypeMeta +// No need for a direct dependence; the fields are stable. +type TypeMeta struct { + Kind string `json:"kind,omitempty" yaml:"kind,omitempty"` + APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"` } // Node contains settings for a node in the `kind` Cluster. @@ -59,22 +61,22 @@ type Node struct { // created by kind // // Defaults to "control-plane" - Role NodeRole `json:"role,omitempty"` + Role NodeRole `yaml:"role,omitempty" json:"role,omitempty"` // Image is the node image to use when creating this node // If unset a default image will be used, see defaults.Image - Image string `json:"image,omitempty"` + Image string `yaml:"image,omitempty" json:"image,omitempty"` /* Advanced fields */ // TODO: cri-like types should be inline instead // ExtraMounts describes additional mount points for the node container // These may be used to bind a hostPath - ExtraMounts []cri.Mount `json:"extraMounts,omitempty"` + ExtraMounts []cri.Mount `yaml:"extraMounts,omitempty" json:"extraMounts,omitempty"` // ExtraPortMappings describes additional port mappings for the node container // binded to a host Port - ExtraPortMappings []cri.PortMapping `json:"extraPortMappings,omitempty"` + ExtraPortMappings []cri.PortMapping `yaml:"extraPortMappings,omitempty" json:"extraPortMappings,omitempty"` } // NodeRole defines possible role for nodes in a Kubernetes cluster managed by `kind` @@ -93,24 +95,24 @@ const ( // Networking contains cluster wide network settings type Networking struct { // IPFamily is the network cluster model, currently it can be ipv4 or ipv6 - IPFamily ClusterIPFamily `json:"ipFamily,omitempty"` + IPFamily ClusterIPFamily `yaml:"ipFamily,omitempty" json:"ipFamily,omitempty"` // APIServerPort is the listen port on the host for the Kubernetes API Server // Defaults to a random port on the host - APIServerPort int32 `json:"apiServerPort,omitempty"` + APIServerPort int32 `yaml:"apiServerPort,omitempty" json:"apiServerPort,omitempty"` // APIServerAddress is the listen address on the host for the Kubernetes // API Server. This should be an IP address. // // Defaults to 127.0.0.1 - APIServerAddress string `json:"apiServerAddress,omitempty"` + APIServerAddress string `yaml:"apiServerAddress,omitempty" json:"apiServerAddress,omitempty"` // PodSubnet is the CIDR used for pod IPs // kind will select a default if unspecified - PodSubnet string `json:"podSubnet,omitempty"` + PodSubnet string `yaml:"podSubnet,omitempty" json:"podSubnet,omitempty"` // ServiceSubnet is the CIDR used for services VIPs // kind will select a default if unspecified for IPv6 - ServiceSubnet string `json:"serviceSubnet,omitempty"` + ServiceSubnet string `yaml:"serviceSubnet,omitempty" json:"serviceSubnet,omitempty"` // If DisableDefaultCNI is true, kind will not install the default CNI setup. // Instead the user should install their own CNI after creating the cluster. - DisableDefaultCNI bool `json:"disableDefaultCNI,omitempty"` + DisableDefaultCNI bool `yaml:"disableDefaultCNI,omitempty" json:"disableDefaultCNI,omitempty"` } // ClusterIPFamily defines cluster network IP family @@ -127,14 +129,14 @@ const ( // https://tools.ietf.org/html/rfc6902 type PatchJSON6902 struct { // these fields specify the patch target resource - Group string `json:"group"` - Version string `json:"version"` - Kind string `json:"kind"` + Group string `yaml:"group" json:"group"` + Version string `yaml:"version" json:"version"` + Kind string `yaml:"kind" json:"kind"` // Name and Namespace are optional // NOTE: technically name is required now, but we default it elsewhere // Third party users of this type / library would need to set it. - Name string `json:"name,omitempty"` - Namespace string `json:"namespace,omitempty"` + Name string `yaml:"name,omitempty" json:"name,omitempty"` + Namespace string `yaml:"name,omitempty" json:"namespace,omitempty"` // Patch should contain the contents of the json patch as a string - Patch string `json:"patch"` + Patch string `yaml:"patch" json:"patch"` } diff --git a/pkg/apis/config/v1alpha3/zz_generated.deepcopy.go b/pkg/apis/config/v1alpha3/zz_generated.deepcopy.go index 194aa310..2b1cd0fc 100644 --- a/pkg/apis/config/v1alpha3/zz_generated.deepcopy.go +++ b/pkg/apis/config/v1alpha3/zz_generated.deepcopy.go @@ -21,7 +21,6 @@ limitations under the License. package v1alpha3 import ( - runtime "k8s.io/apimachinery/pkg/runtime" cri "sigs.k8s.io/kind/pkg/container/cri" ) @@ -60,14 +59,6 @@ func (in *Cluster) DeepCopy() *Cluster { return out } -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *Cluster) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Networking) DeepCopyInto(out *Networking) { *out = *in @@ -125,3 +116,19 @@ func (in *PatchJSON6902) DeepCopy() *PatchJSON6902 { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TypeMeta) DeepCopyInto(out *TypeMeta) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TypeMeta. +func (in *TypeMeta) DeepCopy() *TypeMeta { + if in == nil { + return nil + } + out := new(TypeMeta) + in.DeepCopyInto(out) + return out +} diff --git a/pkg/internal/apis/config/convert.go b/pkg/internal/apis/config/convert.go index 38d22e47..660a8681 100644 --- a/pkg/internal/apis/config/convert.go +++ b/pkg/internal/apis/config/convert.go @@ -17,18 +17,11 @@ limitations under the License. package config import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - v1alpha3 "sigs.k8s.io/kind/pkg/apis/config/v1alpha3" ) func Convertv1alpha3(in *v1alpha3.Cluster) *Cluster { out := &Cluster{ - TypeMeta: metav1.TypeMeta{ - Kind: "Cluster", - APIVersion: runtime.APIVersionInternal, - }, Nodes: make([]Node, len(in.Nodes)), KubeadmConfigPatches: in.KubeadmConfigPatches, KubeadmConfigPatchesJSON6902: make([]PatchJSON6902, len(in.KubeadmConfigPatchesJSON6902)), diff --git a/pkg/internal/apis/config/encoding/load.go b/pkg/internal/apis/config/encoding/load.go index 7eafe6d7..0dabef9f 100644 --- a/pkg/internal/apis/config/encoding/load.go +++ b/pkg/internal/apis/config/encoding/load.go @@ -17,12 +17,11 @@ limitations under the License. package encoding import ( + "bytes" "io/ioutil" "os" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "sigs.k8s.io/yaml" + yaml "gopkg.in/yaml.v3" "sigs.k8s.io/kind/pkg/apis/config/v1alpha3" "sigs.k8s.io/kind/pkg/errors" @@ -50,7 +49,7 @@ func Load(path string) (*config.Cluster, error) { } // get kind & apiVersion - tm := metav1.TypeMeta{} + tm := typeMeta{} if err := yaml.Unmarshal(raw, &tm); err != nil { return nil, errors.Wrap(err, "could not determine kind / apiVersion for config") } @@ -63,7 +62,8 @@ func Load(path string) (*config.Cluster, error) { } // load version cfg := &v1alpha3.Cluster{} - if err := yaml.UnmarshalStrict(raw, cfg); err != nil { + //if err := yaml.UnmarshalStrict(raw, cfg); err != nil { + if err := yamlUnmarshalStrict(raw, cfg); err != nil { return nil, errors.Wrap(err, "unable to decode config") } // apply defaults for version and convert @@ -74,6 +74,18 @@ func Load(path string) (*config.Cluster, error) { return nil, errors.Errorf("unknown apiVersion: %s", tm.APIVersion) } +// basically metav1.TypeMeta, but with yaml tags +type typeMeta struct { + Kind string `yaml:"kind,omitempty"` + APIVersion string `yaml:"apiVersion,omitempty"` +} + +func yamlUnmarshalStrict(raw []byte, v interface{}) error { + d := yaml.NewDecoder(bytes.NewReader(raw)) + d.KnownFields(true) + return d.Decode(v) +} + func readAll(path string) ([]byte, error) { // read in stdin if - if path == "-" { diff --git a/pkg/internal/apis/config/types.go b/pkg/internal/apis/config/types.go index 77bf2873..1c19df8a 100644 --- a/pkg/internal/apis/config/types.go +++ b/pkg/internal/apis/config/types.go @@ -17,18 +17,11 @@ limitations under the License. package config import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/kind/pkg/container/cri" ) -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object - // Cluster contains kind cluster configuration type Cluster struct { - // TypeMeta representing the type of the object and its API schema version. - metav1.TypeMeta - // Nodes contains the list of nodes defined in the `kind` Cluster // If unset this will default to a single control-plane node // Note that if more than one control plane is specified, an external diff --git a/pkg/internal/apis/config/zz_generated.deepcopy.go b/pkg/internal/apis/config/zz_generated.deepcopy.go index 6ddd65e3..4cc2c30b 100644 --- a/pkg/internal/apis/config/zz_generated.deepcopy.go +++ b/pkg/internal/apis/config/zz_generated.deepcopy.go @@ -21,14 +21,12 @@ limitations under the License. package config import ( - runtime "k8s.io/apimachinery/pkg/runtime" cri "sigs.k8s.io/kind/pkg/container/cri" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Cluster) DeepCopyInto(out *Cluster) { *out = *in - out.TypeMeta = in.TypeMeta if in.Nodes != nil { in, out := &in.Nodes, &out.Nodes *out = make([]Node, len(*in)) @@ -60,14 +58,6 @@ func (in *Cluster) DeepCopy() *Cluster { return out } -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *Cluster) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Networking) DeepCopyInto(out *Networking) { *out = *in