Add possibility to use multiple names for one service (#31)
Co-authored-by: Geoff Bourne <itzgeoff@gmail.com>
This commit is contained in:
committed by
GitHub
parent
6bf14043bb
commit
1b1f8e5f22
+68
-44
@@ -11,6 +11,7 @@ import (
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -66,47 +67,9 @@ func (w *k8sWatcherImpl) startWithLoadedConfig(config *rest.Config) error {
|
||||
&v1.Service{},
|
||||
0,
|
||||
cache.ResourceEventHandlerFuncs{
|
||||
AddFunc: func(obj interface{}) {
|
||||
routableService := extractRoutableService(obj)
|
||||
if routableService != nil {
|
||||
logrus.WithField("routableService", routableService).Debug("ADD")
|
||||
|
||||
if routableService.externalServiceName != "" {
|
||||
Routes.CreateMapping(routableService.externalServiceName, routableService.containerEndpoint)
|
||||
} else {
|
||||
Routes.SetDefaultRoute(routableService.containerEndpoint)
|
||||
}
|
||||
}
|
||||
},
|
||||
DeleteFunc: func(obj interface{}) {
|
||||
routableService := extractRoutableService(obj)
|
||||
if routableService != nil {
|
||||
logrus.WithField("routableService", routableService).Debug("DELETE")
|
||||
|
||||
if routableService.externalServiceName != "" {
|
||||
Routes.DeleteMapping(routableService.externalServiceName)
|
||||
} else {
|
||||
Routes.SetDefaultRoute("")
|
||||
}
|
||||
}
|
||||
},
|
||||
UpdateFunc: func(oldObj, newObj interface{}) {
|
||||
oldRoutableService := extractRoutableService(oldObj)
|
||||
newRoutableService := extractRoutableService(newObj)
|
||||
if oldRoutableService != nil && newRoutableService != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"old": oldRoutableService,
|
||||
"new": newRoutableService,
|
||||
}).Debug("UPDATE")
|
||||
|
||||
if oldRoutableService.externalServiceName != "" && newRoutableService.externalServiceName != "" {
|
||||
Routes.DeleteMapping(oldRoutableService.externalServiceName)
|
||||
Routes.CreateMapping(newRoutableService.externalServiceName, newRoutableService.containerEndpoint)
|
||||
} else {
|
||||
Routes.SetDefaultRoute(newRoutableService.containerEndpoint)
|
||||
}
|
||||
}
|
||||
},
|
||||
AddFunc: w.handleAdd,
|
||||
DeleteFunc: w.handleDelete,
|
||||
UpdateFunc: w.handleUpdate,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -117,6 +80,61 @@ func (w *k8sWatcherImpl) startWithLoadedConfig(config *rest.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// oldObj and newObj are expected to be *v1.Service
|
||||
func (w *k8sWatcherImpl) handleUpdate(oldObj interface{}, newObj interface{}) {
|
||||
for _, oldRoutableService := range extractRoutableServices(oldObj) {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"old": oldRoutableService,
|
||||
}).Debug("UPDATE")
|
||||
if oldRoutableService.externalServiceName != "" {
|
||||
Routes.DeleteMapping(oldRoutableService.externalServiceName)
|
||||
}
|
||||
}
|
||||
|
||||
for _, newRoutableService := range extractRoutableServices(newObj) {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"new": newRoutableService,
|
||||
}).Debug("UPDATE")
|
||||
if newRoutableService.externalServiceName != "" {
|
||||
Routes.CreateMapping(newRoutableService.externalServiceName, newRoutableService.containerEndpoint)
|
||||
} else {
|
||||
Routes.SetDefaultRoute(newRoutableService.containerEndpoint)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// obj is expected to be a *v1.Service
|
||||
func (w *k8sWatcherImpl) handleDelete(obj interface{}) {
|
||||
routableServices := extractRoutableServices(obj)
|
||||
for _, routableService := range routableServices {
|
||||
if routableService != nil {
|
||||
logrus.WithField("routableService", routableService).Debug("DELETE")
|
||||
|
||||
if routableService.externalServiceName != "" {
|
||||
Routes.DeleteMapping(routableService.externalServiceName)
|
||||
} else {
|
||||
Routes.SetDefaultRoute("")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// obj is expected to be a *v1.Service
|
||||
func (w *k8sWatcherImpl) handleAdd(obj interface{}) {
|
||||
routableServices := extractRoutableServices(obj)
|
||||
for _, routableService := range routableServices {
|
||||
if routableService != nil {
|
||||
logrus.WithField("routableService", routableService).Debug("ADD")
|
||||
|
||||
if routableService.externalServiceName != "" {
|
||||
Routes.CreateMapping(routableService.externalServiceName, routableService.containerEndpoint)
|
||||
} else {
|
||||
Routes.SetDefaultRoute(routableService.containerEndpoint)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *k8sWatcherImpl) Stop() {
|
||||
if w.stop != nil {
|
||||
w.stop <- struct{}{}
|
||||
@@ -128,16 +146,22 @@ type routableService struct {
|
||||
containerEndpoint string
|
||||
}
|
||||
|
||||
func extractRoutableService(obj interface{}) *routableService {
|
||||
// obj is expected to be a *v1.Service
|
||||
func extractRoutableServices(obj interface{}) []*routableService {
|
||||
service, ok := obj.(*v1.Service)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
routableServices := make([]*routableService, 0)
|
||||
if externalServiceName, exists := service.Annotations[AnnotationExternalServerName]; exists {
|
||||
return buildDetails(service, externalServiceName)
|
||||
serviceNames := strings.Split(externalServiceName, ",")
|
||||
for _, serviceName := range serviceNames {
|
||||
routableServices = append(routableServices, buildDetails(service, serviceName))
|
||||
}
|
||||
return routableServices
|
||||
} else if _, exists := service.Annotations[AnnotationDefaultServer]; exists {
|
||||
return buildDetails(service, "")
|
||||
return []*routableService{buildDetails(service, "")}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user