Skip to content

Commit

Permalink
Enable strict parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Apr 5, 2019
1 parent 9a210da commit 5ec469c
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/05-advanced-nodegroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ nodeGroups:
labels:
nodegroup-type: backend-cluster-addons
privateNetworking: true
preBootsrapCommand:
preBootstrapCommand:
# allow docker registries to be deployed as cluster service
- 'echo {\"insecure-registries\": [\"172.20.0.0/16\",\"10.100.0.0/16\"]} > /etc/docker/daemon.json'
- "systemctl restart docker"
Expand Down
10 changes: 10 additions & 0 deletions pkg/eks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/yaml"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -158,6 +159,15 @@ func LoadConfigFromFile(configFile string, cfg *api.ClusterConfig) error {
return errors.Wrapf(err, "reading config file %q", configFile)
}

// strict mode is not available in runtime.Decode, so we use the parser
// directly; we don't store the resulting object, this is just the means
// of detecting any unknown keys
// NOTE: we must use sigs.k8s.io/yaml, as it behaves differently from
// github.com/ghodss/yaml, which didn't handle nested structs well
if err := yaml.UnmarshalStrict(data, &api.ClusterConfig{}); err != nil {
return errors.Wrapf(err, "loading config file %q", configFile)
}

obj, err := runtime.Decode(scheme.Codecs.UniversalDeserializer(), data)
if err != nil {
return errors.Wrapf(err, "loading config file %q", configFile)
Expand Down
74 changes: 74 additions & 0 deletions pkg/eks/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package eks_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4"

. "github.com/weaveworks/eksctl/pkg/eks"
)

var _ = Describe("eksctl API", func() {

Context("loading config files", func() {
var (
cfg *api.ClusterConfig
)
BeforeEach(func() {
err := api.Register()
Expect(err).ToNot(HaveOccurred())
cfg = &api.ClusterConfig{}
})

It("should load a valid YAML config without error", func() {
err := LoadConfigFromFile("../../examples/01-simple-cluster.yaml", cfg)
Expect(err).ToNot(HaveOccurred())
Expect(cfg.Metadata.Name).To(Equal("cluster-1"))
Expect(cfg.NodeGroups).To(HaveLen(1))
})

It("should load a valid JSON config without error", func() {
err := LoadConfigFromFile("testdata/example.json", cfg)
Expect(err).ToNot(HaveOccurred())
Expect(cfg.Metadata.Name).To(Equal("cluster-1"))
Expect(cfg.NodeGroups).To(HaveLen(1))
})

It("should error when version is a float, not a string", func() {
err := LoadConfigFromFile("testdata/bad-type-1.yaml", cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-type-1.yaml": v1alpha4.ClusterConfig.Metadata: v1alpha4.ClusterMeta.Version: ReadString: expects " or n, but found 1`))
})

It("should reject unknown field in a YAML config", func() {
err := LoadConfigFromFile("testdata/bad-field-1.yaml", cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-1.yaml": error unmarshaling JSON: while decoding JSON: json: unknown field "zone"`))
})

It("should reject unknown field in a YAML config", func() {
err := LoadConfigFromFile("testdata/bad-field-2.yaml", cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-2.yaml": error unmarshaling JSON: while decoding JSON: json: unknown field "bar"`))
})

It("should reject unknown field in a JSON config", func() {
err := LoadConfigFromFile("testdata/bad-field-1.json", cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-1.json": error unmarshaling JSON: while decoding JSON: json: unknown field "nodes"`))
})

It("should reject old API version", func() {
err := LoadConfigFromFile("testdata/old-version.json", cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/old-version.json": no kind "ClusterConfig" is registered for version "eksctl.io/v1alpha3" in scheme "k8s.io/client-go/kubernetes/scheme/register.go:60"`))
})

It("should error when cannot read a file", func() {
err := LoadConfigFromFile("../../examples/nothing.xml", cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(`reading config file "../../examples/nothing.xml": open ../../examples/nothing.xml: no such file or directory`))
})
})
})
2 changes: 1 addition & 1 deletion pkg/eks/eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
)

var _ = Describe("Eks", func() {
var _ = Describe("EKS API wrapper", func() {
var (
c *ClusterProvider
p *mockprovider.MockProvider
Expand Down
11 changes: 11 additions & 0 deletions pkg/eks/testdata/bad-field-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"apiVersion": "eksctl.io/v1alpha3",
"kind": "ClusterConfig",
"metadata": {
"name": "cluster-1",
"region": "eu-north-1"
},
"nodeGroups": [
{ "name": "ng-1", "instanceType": "m5.large", "nodes": 10 }
]
}
7 changes: 7 additions & 0 deletions pkg/eks/testdata/bad-field-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
apiVersion: eksctl.io/v1alpha4
kind: ClusterConfig

metadata:
name: cluster-1
zone: eu-north-1a
17 changes: 17 additions & 0 deletions pkg/eks/testdata/bad-field-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
apiVersion: eksctl.io/v1alpha4
kind: ClusterConfig

metadata:
name: cluster-1
region: eu-north-1

nodeGroups:
- name: ng-1
instanceType: m5.large
desiredCapacity: 10
ssh: true
iam:
withAddonPolicies:
foo: 1
bar: 2
8 changes: 8 additions & 0 deletions pkg/eks/testdata/bad-type-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: eksctl.io/v1alpha4
kind: ClusterConfig

metadata:
name: cluster-1
region: eu-north-1
version: 1.10
11 changes: 11 additions & 0 deletions pkg/eks/testdata/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"apiVersion": "eksctl.io/v1alpha4",
"kind": "ClusterConfig",
"metadata": {
"name": "cluster-1",
"region": "eu-north-1"
},
"nodeGroups": [
{ "name": "ng-1", "instanceType": "m5.large", "desiredCapacity": 10 }
]
}
11 changes: 11 additions & 0 deletions pkg/eks/testdata/old-version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"apiVersion": "eksctl.io/v1alpha3",
"kind": "ClusterConfig",
"metadata": {
"name": "cluster-1",
"region": "eu-north-1"
},
"nodeGroups": [
{ "name": "ng-1", "instanceType": "m5.large", "desiredCapacity": 10 }
]
}

0 comments on commit 5ec469c

Please sign in to comment.