Skip to content

Commit

Permalink
switch to yaml.v3
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTheElder committed Oct 15, 2019
1 parent d0f4a5a commit 0447dd2
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 64 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
54 changes: 28 additions & 26 deletions pkg/apis/config/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`
Expand All @@ -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
Expand All @@ -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"`
}
25 changes: 16 additions & 9 deletions pkg/apis/config/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions pkg/internal/apis/config/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
22 changes: 17 additions & 5 deletions pkg/internal/apis/config/encoding/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand All @@ -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 == "-" {
Expand Down
7 changes: 0 additions & 7 deletions pkg/internal/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions pkg/internal/apis/config/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0447dd2

Please sign in to comment.