Skip to content

Commit

Permalink
internal/contour: allow servicename helper to work on any ObjectMeta
Browse files Browse the repository at this point in the history
Updates projectcontour#186

Signed-off-by: Dave Cheney <[email protected]>
  • Loading branch information
davecheney committed Jan 28, 2018
1 parent e3826fb commit 96eb042
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/contour/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/heptio/contour/internal/log"
"k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
)

Expand All @@ -41,7 +42,6 @@ type metadata struct {
// Translator receives notifications from the Kubernetes API and translates those
// objects into additions and removals entries of Envoy gRPC objects from a cache.
type Translator struct {

// The logger for this Translator. There is no valid default, this value
// must be supplied by the caller.
log.Logger
Expand Down Expand Up @@ -116,7 +116,7 @@ func (t *Translator) addService(svc *v1.Service) {
case "TCP":
config := &v2.Cluster_EdsClusterConfig{
EdsConfig: apiconfigsource("xds_cluster"), // hard coded by initconfig
ServiceName: svc.ObjectMeta.Namespace + "/" + svc.ObjectMeta.Name + "/" + p.TargetPort.String(),
ServiceName: servicename(svc.ObjectMeta, p.TargetPort.String()),
}
if p.Name != "" {
// service port is named, so we must generate both a cluster for the port name
Expand Down Expand Up @@ -178,7 +178,8 @@ func (t *Translator) addEndpoints(e *v1.Endpoints) {

for _, p := range s.Ports {
cla := v2.ClusterLoadAssignment{
ClusterName: hashname(60, e.ObjectMeta.Namespace, e.ObjectMeta.Name, strconv.Itoa(int(p.Port))),
// ClusterName must match Cluster.ServiceName
ClusterName: servicename(e.ObjectMeta, strconv.Itoa(int(p.Port))),
Endpoints: []*v2.LocalityLbEndpoints{{
Locality: &v2.Locality{
Region: "ap-southeast-2",
Expand Down Expand Up @@ -350,6 +351,10 @@ func (t *Translator) writeCerts(s *v1.Secret) {
}
}

func servicename(meta metav1.ObjectMeta, port string) string {
return meta.Namespace + "/" + meta.Name + "/" + port
}

// TODO(dfc) need tests
func appendIfMissing(haystack []*v1beta1.Ingress, needle *v1beta1.Ingress) []*v1beta1.Ingress {
for i := range haystack {
Expand Down

0 comments on commit 96eb042

Please sign in to comment.