Skip to content

Commit

Permalink
Merge pull request #180 from AndiDog/detect-duplicates
Browse files Browse the repository at this point in the history
Detect duplicate resources (same [API version, kind, namespace, name])
  • Loading branch information
garethr authored Apr 30, 2020
2 parents b121327 + 2878db0 commit 9b9f26f
Show file tree
Hide file tree
Showing 14 changed files with 429 additions and 13 deletions.
39 changes: 39 additions & 0 deletions fixtures/duplicates-non-namespaced.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Two objects with same name in same namespace, resource of non-namespaced kind

apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0003
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: slow
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: /tmp
server: 172.17.0.2
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0003
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: slow
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: /tmp
server: 172.17.0.2
41 changes: 41 additions & 0 deletions fixtures/duplicates-skipped-kinds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Two objects with same name in same namespace, but of a kind configured to be skipped

apiVersion: v1
kind: SkipThisKind
metadata:
name: "identical"
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: SkipThisKind
metadata:
name: "identical"
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
43 changes: 43 additions & 0 deletions fixtures/duplicates-with-namespace-default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Two objects with same name in same namespace (one of them not given, i.e. will use default namespace as passed to kubeval)

apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
namespace: the-default-namespace
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
# namespace not given
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
43 changes: 43 additions & 0 deletions fixtures/duplicates-with-namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Two objects with same name in same namespace

apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
namespace: x
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
namespace: x
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
41 changes: 41 additions & 0 deletions fixtures/duplicates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Two objects with same name in same namespace

apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
49 changes: 49 additions & 0 deletions fixtures/same-kind-different-api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Two objects with same name in same namespace, and having the same kind, but
# of different API (apps/v1 vs. apps/v1beta1). This is important when CRDs
# introduce overlapping `metadata:name` values, e.g. `Deployment` in
# `my-awesome-cd-tool.io/v1` (contrived scenario).


apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
43 changes: 43 additions & 0 deletions fixtures/same-object-different-namespace-default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Two objects with same name in different namespace, one of them being the configured default namespace

apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
namespace: a
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
namespace: the-default-namespace
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
43 changes: 43 additions & 0 deletions fixtures/same-object-different-namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Two objects with same name in different namespace

apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
namespace: a
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
namespace: b
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 // indirect
github.com/pelletier/go-toml v0.0.0-20180724185102-c2dbbc24a979 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/spf13/afero v1.1.1 // indirect
github.com/spf13/cast v1.2.0 // indirect
github.com/spf13/cobra v0.0.0-20180820174524-ff0d02e85550
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 h1:KXZJFdun
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v0.0.0-20180724185102-c2dbbc24a979 h1:Uh8pTMDzw+nuDTW7lyxcpmYqQJFE8SnO93F3lyY4XzY=
github.com/pelletier/go-toml v0.0.0-20180724185102-c2dbbc24a979/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/afero v1.1.1 h1:Lt3ihYMlE+lreX1GS4Qw4ZsNpYQLxIXKBTEOXm3nt6I=
Expand Down
8 changes: 8 additions & 0 deletions kubeval/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const OpenShiftSchemaLocation = "https://raw.githubusercontent.com/garethr/opens

// A Config object contains various configuration data for kubeval
type Config struct {
// DefaultNamespace is the namespace to assume in resources
// if no namespace is set in `metadata:namespace` (as used with
// `kubectl apply --namespace ...` or `helm install --namespace ...`,
// for example)
DefaultNamespace string

// KubernetesVersion represents the version of Kubernetes
// for which we should load the schema
KubernetesVersion string
Expand Down Expand Up @@ -69,13 +75,15 @@ type Config struct {
// NewDefaultConfig creates a Config with default values
func NewDefaultConfig() *Config {
return &Config{
DefaultNamespace: "default",
FileName: "stdin",
KubernetesVersion: "master",
}
}

// AddKubevalFlags adds the default flags for kubeval to cmd
func AddKubevalFlags(cmd *cobra.Command, config *Config) *cobra.Command {
cmd.Flags().StringVarP(&config.DefaultNamespace, "default-namespace", "n", "default", "Namespace to assume in resources if no namespace is set in metadata:namespace")
cmd.Flags().BoolVar(&config.ExitOnError, "exit-on-error", false, "Immediately stop execution when the first error is encountered")
cmd.Flags().BoolVar(&config.IgnoreMissingSchemas, "ignore-missing-schemas", false, "Skip validation for resource definitions without a schema")
cmd.Flags().BoolVar(&config.OpenShift, "openshift", false, "Use OpenShift schemas instead of upstream Kubernetes")
Expand Down
Loading

0 comments on commit 9b9f26f

Please sign in to comment.