From 330a0502d1591d3e9a578bb4626045178450efa0 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Tue, 31 Oct 2017 17:20:08 -0700 Subject: [PATCH] Don't delete 'headless' services Deleting the clusterIP field when the service should be headless will cause it to be assigned a new IP on restore; instead it should retain the headless state after restoration. Fixes #168 Signed-off-by: Nolan Brubaker (cherry picked from commit d87e8ee16e5d1d47f8ebc05e5264955b43934325) --- pkg/restore/restorers/service_restorer.go | 5 ++++- pkg/restore/restorers/service_restorer_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/restore/restorers/service_restorer.go b/pkg/restore/restorers/service_restorer.go index 8f2d28e5..bc4f7797 100644 --- a/pkg/restore/restorers/service_restorer.go +++ b/pkg/restore/restorers/service_restorer.go @@ -45,7 +45,10 @@ func (sr *serviceRestorer) Prepare(obj runtime.Unstructured, restore *api.Restor return nil, nil, err } - delete(spec, "clusterIP") + // Since clusterIP is an optional key, we can ignore 'not found' errors. Also assuming it was a string already. + if val, _ := collections.GetString(spec, "clusterIP"); val != "None" { + delete(spec, "clusterIP") + } ports, err := collections.GetSlice(obj.UnstructuredContent(), "spec.ports") if err != nil { diff --git a/pkg/restore/restorers/service_restorer_test.go b/pkg/restore/restorers/service_restorer_test.go index d7c22ba5..096e44f6 100644 --- a/pkg/restore/restorers/service_restorer_test.go +++ b/pkg/restore/restorers/service_restorer_test.go @@ -42,6 +42,12 @@ func TestServiceRestorerPrepare(t *testing.T) { expectedErr: false, expectedRes: NewTestUnstructured().WithName("svc-1").WithSpec("foo").WithSpecField("ports", []interface{}{}).Unstructured, }, + { + name: "headless clusterIP should not be deleted from spec", + obj: NewTestUnstructured().WithName("svc-1").WithSpecField("clusterIP", "None").WithSpecField("ports", []interface{}{}).Unstructured, + expectedErr: false, + expectedRes: NewTestUnstructured().WithName("svc-1").WithSpecField("clusterIP", "None").WithSpecField("ports", []interface{}{}).Unstructured, + }, { name: "nodePort (only) should be deleted from all spec.ports", obj: NewTestUnstructured().WithName("svc-1").