diff --git a/pkg/mapper/fakes.go b/pkg/mapper/fakes.go index b973499802..bca3970711 100644 --- a/pkg/mapper/fakes.go +++ b/pkg/mapper/fakes.go @@ -35,3 +35,7 @@ func NewFakeClusterServiceMapper(returnError bool) ClusterServiceMapper { func (f *fakeClusterServiceMapper) Services(ing *v1beta1.Ingress) (map[v1beta1.IngressBackend]v1.Service, error) { return nil, fmt.Errorf("fake error") } + +func (f *fakeClusterServiceMapper) SetExpectedServices(expectedSvcs []string) { + return +} diff --git a/pkg/mapper/interfaces.go b/pkg/mapper/interfaces.go index d05f3bf0e8..73207c4251 100644 --- a/pkg/mapper/interfaces.go +++ b/pkg/mapper/interfaces.go @@ -23,4 +23,5 @@ import ( // Services it defines for a specific cluster, type ClusterServiceMapper interface { Services(ing *v1beta1.Ingress) (map[v1beta1.IngressBackend]v1.Service, error) + SetExpectedServices(expectedSvcs []string) } diff --git a/pkg/mapper/mapper.go b/pkg/mapper/mapper.go index 85fd591aa8..2ebe338906 100644 --- a/pkg/mapper/mapper.go +++ b/pkg/mapper/mapper.go @@ -20,6 +20,7 @@ import ( multierror "github.com/hashicorp/go-multierror" "k8s.io/api/core/v1" "k8s.io/api/extensions/v1beta1" + "k8s.io/ingress-gce/pkg/utils" ) // Implements ClusterServiceMapper @@ -38,14 +39,10 @@ var _ ClusterServiceMapper = &clusterServiceMapper{} func NewClusterServiceMapper( svcGetter func(svcName string, namespace string) (*v1.Service, error), expectedSvcs []string) ClusterServiceMapper { - es := make(map[string]bool) - if expectedSvcs == nil { - return &clusterServiceMapper{svcGetter, es} + return &clusterServiceMapper{ + svcGetter, + utils.StringsToKeyMap(expectedSvcs), } - for _, expectedSvc := range expectedSvcs { - es[expectedSvc] = true - } - return &clusterServiceMapper{svcGetter, es} } // Services returns a mapping for a cluster of IngressBackend -> Service given an Ingress. @@ -91,6 +88,13 @@ Loop: return backendToService, result.ErrorOrNil() } +// SetExpectedServices makes the mapper aware of services that are expected +// to exist in the cluster. Multiple calls to this function will overwrite +// any previous state. +func (c *clusterServiceMapper) SetExpectedServices(expectedSvcs []string) { + c.expectedSvcs = utils.StringsToKeyMap(expectedSvcs) +} + // expectedToExist returns true if the provided service name is expected to exist. func (c *clusterServiceMapper) expectedToExist(svcName string) bool { if _, ok := c.expectedSvcs[svcName]; !ok && len(c.expectedSvcs) > 0 { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index be7f1c005e..4a6b2d71a0 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -180,3 +180,11 @@ func BackendServiceComparablePath(url string) string { } return fmt.Sprintf("global/%s", path_parts[1]) } + +func StringsToKeyMap(strings []string) map[string]bool { + m := make(map[string]bool) + for _, s := range strings { + m[s] = true + } + return m +}