Skip to content

Commit

Permalink
Small addition to ClusterServiceMapper interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rramkumar1 committed Apr 19, 2018
1 parent 11ff79f commit 811a0e3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions pkg/mapper/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/mapper/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
18 changes: 11 additions & 7 deletions pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 811a0e3

Please sign in to comment.