Skip to content

Commit

Permalink
feat: add option to publish entries without namespace
Browse files Browse the repository at this point in the history
Add an option to publish service entries without a namespace by
specifying `-without-namespace=true`.

This commit also changes the behavior to not append namespace for
hostnames specified on ingress resources. The hostnames specified on
the resource will be advertised as-is.

Resolves #1

Signed-off-by: Renan Rodrigues <renanqts@gmail.com>

Co-authored-by: Blake Covarrubias <blake@covarrubi.as>
  • Loading branch information
renanqts and blake committed Jul 18, 2021
1 parent 9bb025d commit d757c6d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LoadBalancer, will be advertised on the local network.

By default External-mDNS will advertise hostnames for exposed resources in all
namespaces. Use the `-namespace` flag to restrict advertisement to a single
namespace.
namespace, or `-without-namespace=true` for all namespaces.

DNS records are advertised with the format `<hostname/service_name>.<namespace>.local`.
In addition, hostnames for resources in the `-default-namespace` will also be
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dp
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903 h1:LbsanbbD6LieFkXbj9YNNBupiGHJgFeLpO0j0Fza1h8=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef h1:veQD95Isof8w9/WXiA+pa3tz3fJXkt5B7QaRBrM62gk=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -179,7 +178,6 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.5.0 h1:KxkO13IPW4Lslp2bz+KHP2E3gtFlrIGNThxkZQ3g+4c=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.1 h1:QzqyMA1tlu6CgqCDUtU9V+ZKhLFT2dkJuANu5QaxI3I=
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
Expand Down
22 changes: 21 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ func lookupEnvOrInt(key string, defaultVal int) int {
return defaultVal
}

func lookupEnvOrBool(key string, defaultVal bool) bool {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("lookupEnvOrBool[%s]: %v", key, err)
}
return v
}
return defaultVal
}

func constructRecords(r resource.Resource) []string {
var records []string

Expand All @@ -90,10 +101,17 @@ func constructRecords(r resource.Resource) []string {
// Construct reverse IP
reverseIP := net.IPv4(ip[15], ip[14], ip[13], ip[12])

// Publish A records resources as <name>.<namespace>.local
// Ensure corresponding PTR records map to this hostname
records = append(records, fmt.Sprintf("%s.%s.local. %d IN A %s", r.Name, r.Namespace, recordTTL, ip))
records = append(records, fmt.Sprintf("%s.in-addr.arpa. %d IN PTR %s.%s.local.", reverseIP, recordTTL, r.Name, r.Namespace))

if r.Namespace == defaultNamespace {
// Publish services without the name in the namespace if any of the following
// criteria is satisfied:
// 1. The Service exists in the default namespace
// 2. The -without-namespace flag is equal to true
// 3. The record to be published is from an Ingress with a defined hostname
if r.Namespace == defaultNamespace || withoutNamespace || r.SourceType == "ingress" {
records = append(records, fmt.Sprintf("%s.local. %d IN A %s", r.Name, recordTTL, ip))
}

Expand All @@ -116,6 +134,7 @@ var (
master = ""
namespace = ""
defaultNamespace = "default"
withoutNamespace = false
test = flag.Bool("test", false, "testing mode, no connection to k8s")
sourceFlag k8sSource
kubeconfig string
Expand All @@ -131,6 +150,7 @@ func main() {

// External-mDNS options
flag.StringVar(&defaultNamespace, "default-namespace", lookupEnvOrString("EXTERNAL_MDNS_DEFAULT_NAMESPACE", defaultNamespace), "Namespace in which services should also be published with a shorter entry")
flag.BoolVar(&withoutNamespace, "without-namespace", lookupEnvOrBool("EXTERNAL_MDNS_WITHOUT_NAMESPACE", withoutNamespace), "Published with a shorter entry without namespace (default: false)")
flag.StringVar(&namespace, "namespace", lookupEnvOrString("EXTERNAL_MDNS_NAMESPACE", namespace), "Limit sources of endpoints to a specific namespace (default: all namespaces)")
flag.Var(&sourceFlag, "source", "The resource types that are queried for endpoints; specify multiple times for multiple sources (required, options: service, ingress)")
flag.IntVar(&recordTTL, "record-ttl", lookupEnvOrInt("EXTERNAL_MDNS_RECORD_TTL", recordTTL), "DNS record time-to-live")
Expand Down
9 changes: 5 additions & 4 deletions resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const (

// Resource represents a resource to advertise over mDNS
type Resource struct {
Action string
IP string
Name string
Namespace string
SourceType string
Action string
IP string
Name string
Namespace string
}
18 changes: 9 additions & 9 deletions source/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ import (

// IngressSource handles adding, updating, or removing mDNS record advertisements
type IngressSource struct {
namespace string
publishInternal bool
notifyChan chan<- resource.Resource
sharedInformer cache.SharedIndexInformer
namespace string
notifyChan chan<- resource.Resource
sharedInformer cache.SharedIndexInformer
}

// Run starts shared informers and waits for the shared informer cache to
// synchronize.
func (i *IngressSource) Run(stopCh chan struct{}) error {
i.sharedInformer.Run(stopCh)
if !cache.WaitForCacheSync(stopCh, i.sharedInformer.HasSynced) {
runtime.HandleError(fmt.Errorf("Timed out waiting for caches to sync"))
runtime.HandleError(fmt.Errorf("timed out waiting for caches to sync"))
}
return nil
}
Expand Down Expand Up @@ -134,10 +133,11 @@ func (i *IngressSource) buildRecords(obj interface{}, action string) ([]resource
hostname = parsedHost.Domain
}
advertiseObj := resource.Resource{
Action: action,
Name: hostname,
Namespace: ingress.Namespace,
IP: ipField,
SourceType: "ingress",
Action: action,
Name: hostname,
Namespace: ingress.Namespace,
IP: ipField,
}

records = append(records, advertiseObj)
Expand Down
5 changes: 3 additions & 2 deletions source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ServiceSource struct {
func (s *ServiceSource) Run(stopCh chan struct{}) error {
s.sharedInformer.Run(stopCh)
if !cache.WaitForCacheSync(stopCh, s.sharedInformer.HasSynced) {
runtime.HandleError(fmt.Errorf("Timed out waiting for caches to sync"))
runtime.HandleError(fmt.Errorf("timed out waiting for caches to sync"))
}
return nil
}
Expand Down Expand Up @@ -82,7 +82,8 @@ func (s *ServiceSource) onUpdate(oldObj interface{}, newObj interface{}) {
func (s *ServiceSource) buildRecord(obj interface{}, action string) (resource.Resource, error) {

var advertiseObj = resource.Resource{
Action: action,
SourceType: "service",
Action: action,
}

service, ok := obj.(*corev1.Service)
Expand Down

0 comments on commit d757c6d

Please sign in to comment.