forked from fabric8io/configmapcontroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configmapcontroller.go
111 lines (90 loc) · 2.68 KB
/
configmapcontroller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"flag"
"fmt"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/fabric8io/configmapcontroller/client"
"github.com/fabric8io/configmapcontroller/controller"
"github.com/fabric8io/configmapcontroller/util"
"github.com/fabric8io/configmapcontroller/version"
"github.com/golang/glog"
oclient "github.com/openshift/origin/pkg/client"
"github.com/spf13/pflag"
"k8s.io/kubernetes/pkg/api"
kubectlutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
const (
healthPort = 10254
)
var (
flags = pflag.NewFlagSet("", pflag.ExitOnError)
resyncPeriod = flags.Duration("sync-period", 30*time.Second,
`Relist and confirm services this often.`)
healthzPort = flags.Int("healthz-port", healthPort, "port for healthz endpoint.")
profiling = flags.Bool("profiling", true, `Enable profiling via web interface host:port/debug/pprof/`)
)
func main() {
factory := kubectlutil.NewFactory(nil)
factory.BindFlags(flags)
factory.BindExternalFlags(flags)
flags.Parse(os.Args)
flag.CommandLine.Parse([]string{})
glog.Infof("Using build: %v", version.Version)
kubeClient, err := factory.Client()
if err != nil {
glog.Fatalf("failed to create client: %s", err)
}
restClientConfig, err := factory.ClientConfig()
if err != nil {
glog.Fatalf("failed to create REST client config: %s", err)
}
var oc *oclient.Client
typeOfMaster, err := util.TypeOfMaster(kubeClient)
if err != nil {
glog.Fatalf("failed to create REST client config: %s", err)
}
if typeOfMaster == util.OpenShift {
oc, _, err = client.NewOpenShiftClient(restClientConfig)
if err != nil {
glog.Fatalf("failed to create REST client config: %s", err)
}
}
watchNamespaces := api.NamespaceAll
currentNamespace := os.Getenv("KUBERNETES_NAMESPACE")
if len(currentNamespace) > 0 {
watchNamespaces = currentNamespace
}
glog.Infof("Watching services in namespaces: `%s`", watchNamespaces)
c, err := controller.NewController(kubeClient, oc, restClientConfig, factory.JSONEncoder(), *resyncPeriod, watchNamespaces)
if err != nil {
glog.Fatalf("%s", err)
}
go registerHandlers()
go handleSigterm(c)
c.Run()
}
func registerHandlers() {
mux := http.NewServeMux()
if *profiling {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
}
server := &http.Server{
Addr: fmt.Sprintf(":%v", *healthzPort),
Handler: mux,
}
glog.Fatal(server.ListenAndServe())
}
func handleSigterm(c *controller.Controller) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
sig := <-signalChan
glog.Infof("Received %s, shutting down", sig)
c.Stop()
}