Skip to content

Commit

Permalink
Add service topology key logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dtomcej committed Apr 6, 2020
1 parent 570613f commit bb67e22
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/controller/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strconv"

"github.com/containous/maesh/pkg/k8s"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -75,6 +76,13 @@ func (s *ShadowServiceManager) Create(userSvc *corev1.Service) error {
},
}

major, minor := parseKubernetesServerVersion(s.kubeClient)

// If the kuberentes server version is 1.17+, then use the topology key.
if major == 1 && minor >= 17 {
svc.Spec.TopologyKeys = []string{"kubernetes.io/hostname", "*"}
}

if _, err = s.kubeClient.CoreV1().Services(s.namespace).Create(context.TODO(), svc, metav1.CreateOptions{}); err != nil {
return fmt.Errorf("unable to create kubernetes service: %w", err)
}
Expand Down Expand Up @@ -248,3 +256,22 @@ func (s *ShadowServiceManager) getTCPPort(svcName, svcNamespace string, svcPort

return port, nil
}

func parseKubernetesServerVersion(kubeClient kubernetes.Interface) (major, minor int) {
kubeVersion, err := kubeClient.Discovery().ServerVersion()
if err != nil {
return 0, 0
}

major, err = strconv.Atoi(kubeVersion.Major)
if err != nil {
return 0, 0
}

minor, err = strconv.Atoi(kubeVersion.Minor)
if err != nil {
return 0, 0
}

return major, minor
}

0 comments on commit bb67e22

Please sign in to comment.