From 907a9fbdd894906b9331ee38a7ad6632fe5c356b Mon Sep 17 00:00:00 2001 From: Shubheksha Jalan Date: Tue, 25 Sep 2018 01:02:22 +0530 Subject: [PATCH 1/3] fix error during restore when spec.ports are not found Signed-off-by: Shubheksha Jalan --- pkg/restore/service_action.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/restore/service_action.go b/pkg/restore/service_action.go index 7c00fdbdd1..6f171ddc11 100644 --- a/pkg/restore/service_action.go +++ b/pkg/restore/service_action.go @@ -57,6 +57,11 @@ func (a *serviceAction) Execute(obj runtime.Unstructured, restore *api.Restore) delete(spec, "clusterIP") } + // Since spec.ports is an optional key, we can ignore 'not found' errors. Also assuming it was a string already. + if val, _ := collections.GetString(spec, "spec.ports"); val != "None" { + delete(spec, "spec.ports") + } + preservedPorts, err := getPreservedPorts(obj) if err != nil { return nil, nil, err From d95b18bad8ca1c97286a723e55cf3e43e27d02f4 Mon Sep 17 00:00:00 2001 From: Shubheksha Jalan Date: Tue, 25 Sep 2018 01:35:05 +0530 Subject: [PATCH 2/3] ignore spec.ports not being there for services of type ExternalName Signed-off-by: Shubheksha Jalan --- pkg/restore/service_action.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/restore/service_action.go b/pkg/restore/service_action.go index 6f171ddc11..e7b7c55f04 100644 --- a/pkg/restore/service_action.go +++ b/pkg/restore/service_action.go @@ -57,18 +57,14 @@ func (a *serviceAction) Execute(obj runtime.Unstructured, restore *api.Restore) delete(spec, "clusterIP") } - // Since spec.ports is an optional key, we can ignore 'not found' errors. Also assuming it was a string already. - if val, _ := collections.GetString(spec, "spec.ports"); val != "None" { - delete(spec, "spec.ports") - } - preservedPorts, err := getPreservedPorts(obj) if err != nil { return nil, nil, err } ports, err := collections.GetSlice(obj.UnstructuredContent(), "spec.ports") - if err != nil { + serviceType, _ := collections.GetString(spec, "type") + if err != nil && serviceType != "ExternalName" { return nil, nil, err } From e3222a9e3f6b90710c7fbfd7a4f4eb31bd844311 Mon Sep 17 00:00:00 2001 From: Shubheksha Jalan Date: Tue, 25 Sep 2018 18:08:57 +0530 Subject: [PATCH 3/3] move code dealing with node ports into a separate function Signed-off-by: Shubheksha Jalan --- pkg/restore/service_action.go | 47 ++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/pkg/restore/service_action.go b/pkg/restore/service_action.go index e7b7c55f04..b71f3471c3 100644 --- a/pkg/restore/service_action.go +++ b/pkg/restore/service_action.go @@ -57,29 +57,10 @@ func (a *serviceAction) Execute(obj runtime.Unstructured, restore *api.Restore) delete(spec, "clusterIP") } - preservedPorts, err := getPreservedPorts(obj) + err = deleteNodePorts(obj, &spec) if err != nil { return nil, nil, err } - - ports, err := collections.GetSlice(obj.UnstructuredContent(), "spec.ports") - serviceType, _ := collections.GetString(spec, "type") - if err != nil && serviceType != "ExternalName" { - return nil, nil, err - } - - for _, port := range ports { - p := port.(map[string]interface{}) - var name string - if nameVal, ok := p["name"]; ok { - name = nameVal.(string) - } - if preservedPorts[name] { - continue - } - delete(p, "nodePort") - } - return obj, nil, nil } @@ -102,3 +83,29 @@ func getPreservedPorts(obj runtime.Unstructured) (map[string]bool, error) { } return preservedPorts, nil } + +func deleteNodePorts(obj runtime.Unstructured, spec *map[string]interface{}) error { + preservedPorts, err := getPreservedPorts(obj) + if err != nil { + return err + } + + ports, err := collections.GetSlice(obj.UnstructuredContent(), "spec.ports") + serviceType, _ := collections.GetString(*spec, "type") + if err != nil && serviceType != "ExternalName" { + return err + } + + for _, port := range ports { + p := port.(map[string]interface{}) + var name string + if nameVal, ok := p["name"]; ok { + name = nameVal.(string) + } + if preservedPorts[name] { + continue + } + delete(p, "nodePort") + } + return nil +}