Commit 934a62b8 authored by Antoine Pelisse's avatar Antoine Pelisse

diff: Fix overlapping filenames

The filename can overlap when multiple resources have the same name (but obviously are of a different type). Include the name of the type in the file name to prevent the overlap.
parent aa277804
apiVersion: v1
kind: ConfigMap
metadata:
name: test
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
replicas: 3
selector:
matchLabels:
name: test
template:
metadata:
labels:
name: test
spec:
containers:
- name: nginx
image: nginx
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: nginx
image: nginx
apiVersion: v1
kind: Secret
metadata:
name: test
...@@ -289,7 +289,17 @@ func (obj InfoObject) Merged() (runtime.Object, error) { ...@@ -289,7 +289,17 @@ func (obj InfoObject) Merged() (runtime.Object, error) {
} }
func (obj InfoObject) Name() string { func (obj InfoObject) Name() string {
return obj.Info.Name group := ""
if obj.Info.Mapping.GroupVersionKind.Group != "" {
group = fmt.Sprintf("%v.", obj.Info.Mapping.GroupVersionKind.Group)
}
return group + fmt.Sprintf(
"%v.%v:%v.%v",
obj.Info.Mapping.GroupVersionKind.Version,
obj.Info.Mapping.GroupVersionKind.Kind,
obj.Info.Namespace,
obj.Info.Name,
)
} }
// Differ creates two DiffVersion and diffs them. // Differ creates two DiffVersion and diffs them.
......
...@@ -40,3 +40,20 @@ run_kubectl_diff_tests() { ...@@ -40,3 +40,20 @@ run_kubectl_diff_tests() {
set +o nounset set +o nounset
set +o errexit set +o errexit
} }
run_kubectl_diff_same_names() {
set -o nounset
set -o errexit
create_and_use_new_namespace
kube::log::status "Test kubectl diff with multiple resources with the same name"
output_message=$(KUBECTL_EXTERNAL_DIFF=find kubectl diff -Rf hack/testdata/diff/)
kube::test::if_has_string "${output_message}" 'v1.Pod:.*.test'
kube::test::if_has_string "${output_message}" 'apps.v1.Deployment:.*.test'
kube::test::if_has_string "${output_message}" 'v1.ConfigMap:.*.test'
kube::test::if_has_string "${output_message}" 'v1.Secret:.*.test'
set +o nounset
set +o errexit
}
...@@ -473,6 +473,7 @@ runTests() { ...@@ -473,6 +473,7 @@ runTests() {
# Kubectl diff # # Kubectl diff #
################ ################
record_command run_kubectl_diff_tests record_command run_kubectl_diff_tests
record_command run_kubectl_diff_same_names
############### ###############
# Kubectl get # # Kubectl get #
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment