external-dns/main.go
Yerken 25eef9159c [PR-156 follow-up] Generate endpoints hostnames if go-template is specified (#160)
* add --fqdn-template

* add missing ,

* gofmt

* no endpoint creation on empty fqdntemplate

* improve test coverage

* gofmt simple on service_test.go and ingress_test.go

* import package order changed

* gofmt

* refactor to generate template in the source init

* refactor for err handling

* fix service tests

* fix wrong check, check for priorities, mate > template

* fix tests, check for controller annotation in the right place

* add to changelog

* add flag description, improve testing, reorganize imports

* review changes: log the error, use text/template, change func interface
2017-04-18 18:13:08 +02:00

199 lines
4.4 KiB
Go

/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/kubernetes-incubator/external-dns/controller"
"github.com/kubernetes-incubator/external-dns/pkg/apis/externaldns"
"github.com/kubernetes-incubator/external-dns/pkg/apis/externaldns/validation"
"github.com/kubernetes-incubator/external-dns/plan"
"github.com/kubernetes-incubator/external-dns/provider"
"github.com/kubernetes-incubator/external-dns/registry"
"github.com/kubernetes-incubator/external-dns/source"
"github.com/spf13/pflag"
)
var (
version = "unknown"
)
func main() {
cfg := externaldns.NewConfig()
if err := cfg.ParseFlags(os.Args); err != nil {
if err == pflag.ErrHelp {
os.Exit(0)
}
log.Fatalf("flag parsing error: %v", err)
}
if cfg.Version {
fmt.Println(version)
os.Exit(0)
}
log.Infof("config: %+v", cfg)
if err := validation.ValidateConfig(cfg); err != nil {
log.Fatalf("config validation failed: %v", err)
}
if cfg.LogFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
}
if cfg.DryRun {
log.Info("running in dry-run mode. No changes to DNS records will be made.")
}
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
stopChan := make(chan struct{}, 1)
go serveMetrics(cfg.MetricsAddress)
go handleSigterm(stopChan)
client, err := newClient(cfg)
if err != nil {
log.Fatal(err)
}
serviceSource, err := source.NewServiceSource(client, cfg.Namespace, cfg.FqdnTemplate, cfg.Compatibility)
if err != nil {
log.Fatal(err)
}
source.Register("service", serviceSource)
ingressSource, err := source.NewIngressSource(client, cfg.Namespace, cfg.FqdnTemplate)
if err != nil {
log.Fatal(err)
}
source.Register("ingress", ingressSource)
sources, err := source.LookupMultiple(cfg.Sources)
if err != nil {
log.Fatal(err)
}
multiSource := source.NewMultiSource(sources)
var p provider.Provider
switch cfg.Provider {
case "google":
p, err = provider.NewGoogleProvider(cfg.GoogleProject, cfg.DryRun)
case "aws":
p, err = provider.NewAWSProvider(cfg.Domain, cfg.DryRun)
default:
log.Fatalf("unknown dns provider: %s", cfg.Provider)
}
if err != nil {
log.Fatal(err)
}
var r registry.Registry
switch cfg.Registry {
case "noop":
r, err = registry.NewNoopRegistry(p)
case "txt":
r, err = registry.NewTXTRegistry(p, cfg.TXTPrefix, cfg.RecordOwnerID)
default:
log.Fatalf("unknown registry: %s", cfg.Registry)
}
if err != nil {
log.Fatal(err)
}
policy, exists := plan.Policies[cfg.Policy]
if !exists {
log.Fatalf("unknown policy: %s", cfg.Policy)
}
ctrl := controller.Controller{
Zone: cfg.Zone,
Source: multiSource,
Registry: r,
Policy: policy,
Interval: cfg.Interval,
}
if cfg.Once {
err := ctrl.RunOnce()
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
ctrl.Run(stopChan)
for {
log.Infoln("pod waiting to be deleted")
time.Sleep(time.Second * 30)
}
}
func handleSigterm(stopChan chan struct{}) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM)
<-signals
log.Infoln("received SIGTERM. Terminating...")
close(stopChan)
}
func newClient(cfg *externaldns.Config) (*kubernetes.Clientset, error) {
if !cfg.InCluster && cfg.KubeConfig == "" {
cfg.KubeConfig = clientcmd.RecommendedHomeFile
}
config, err := clientcmd.BuildConfigFromFlags("", cfg.KubeConfig)
if err != nil {
return nil, err
}
log.Infof("targeting cluster at %s", config.Host)
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return client, nil
}
func serveMetrics(address string) {
http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(address, nil))
}