Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small addition to ClusterServiceMapper interface #235

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}