forked from mikkeloscar/pdb-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (74 loc) · 2.26 KB
/
main.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
package main
import (
"os"
"os/signal"
"syscall"
"time"
"gopkg.in/alecthomas/kingpin.v2"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
log "github.com/sirupsen/logrus"
)
const (
defaultInterval = "1m"
defaultPDBNameSuffix = "pdb-controller"
defaultNonReadyTTL = "0s"
)
type config struct {
Interval time.Duration
Debug bool
PDBNameSuffix string
NonReadyTTL time.Duration
Kubeconfig string
}
func init() {
log.SetFormatter(&log.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
func main() {
config := config{}
kingpin.Flag("interval", "Interval between creating PDBs.").Default(defaultInterval).DurationVar(&config.Interval)
kingpin.Flag("kubeconfig", "Path to kubeconfig file; if not specified, use in-cluster config").ExistingFileVar(&config.Kubeconfig)
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&config.Debug)
kingpin.Flag("pdb-name-suffix", "Specify default PDB name suffix.").Default(defaultPDBNameSuffix).StringVar(&config.PDBNameSuffix)
kingpin.Flag("non-ready-ttl", "Set the ttl for when to remove the managed PDB if the deployment/statefulset is unhealthy (default: disabled).").Default(defaultNonReadyTTL).DurationVar(&config.NonReadyTTL)
kingpin.Parse()
if config.Debug {
log.SetLevel(log.DebugLevel)
}
var err error
var kubernetesClientConfig *rest.Config
if config.Kubeconfig != "" {
log.Infof("Using current context from kubeconfig file: %v", config.Kubeconfig)
kubernetesClientConfig, err = clientcmd.BuildConfigFromFlags("", config.Kubeconfig)
if err != nil {
log.Fatal(err)
}
} else {
kubernetesClientConfig, err = rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
}
client, err := kubernetes.NewForConfig(kubernetesClientConfig)
if err != nil {
log.Fatal(err)
}
controller, err := NewPDBController(config.Interval, client, config.PDBNameSuffix, config.NonReadyTTL)
if err != nil {
log.Fatal(err)
}
stopChan := make(chan struct{}, 1)
go handleSigterm(stopChan)
controller.Run(stopChan)
}
func handleSigterm(stopChan chan struct{}) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM)
<-signals
log.Info("Received Term signal. Terminating...")
close(stopChan)
}