-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcmd.go
188 lines (172 loc) · 5.57 KB
/
cmd.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright © 2015 Victor Antonovich <[email protected]>
//
// 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 (
"bytes"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/pflag"
)
const (
FlagVersion = "version"
FlagRunOnce = "once"
FlagDryRun = "dry-run"
FlagMaster = "master"
FlagConfig = "config"
FlagPollTime = "poll-time"
FlagPollPeriod = "poll-period"
FlagTemplate = "template"
FlagHelpMd = "help-md"
FlagGuessKubeApiSettings = "guess-kube-api-settings"
FlagKubeConfig = "kube-config"
FlagLeftDelim = "left-delimiter"
FlagRightDelim = "right-delimiter"
FlagCommandTimeout = "command-timeout"
)
func newCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "kube-template",
Long: "Watches Kubernetes for updates, writing output of a series of templates to files.",
Run: runCmd,
}
initCmd(cmd)
return cmd
}
func initCmd(cmd *cobra.Command) {
// Command-related flags set
f := cmd.Flags()
f.Bool(FlagVersion, false, "display the version number and build timestamp")
f.Bool(FlagDryRun, false, "don't write template output, dump result to stdout")
f.Bool(FlagRunOnce, false, "run template processing once and exit")
f.Bool(FlagGuessKubeApiSettings, false, "guess Kubernetes API settings from POD environment")
f.String(FlagMaster, "", fmt.Sprintf("Kubernetes API server address (default is %s)", DEFAULT_MASTER_HOST))
f.DurationP(FlagPollPeriod, "p", 15*time.Second, "Kubernetes API server poll period (0 disables server polling)")
f.Duration(FlagPollTime, 15*time.Second, "")
_ = f.MarkDeprecated(FlagPollTime, "use --"+FlagPollPeriod+" instead")
f.StringP(FlagKubeConfig, "k", "", "Kubernetes config file to use")
f.StringP(FlagLeftDelim, "l", "{{", "templating left delimiter")
f.StringP(FlagRightDelim, "r", "}}", "templating right delimiter")
f.StringVarP(&cfgFile, FlagConfig, "c", "", fmt.Sprintf("config file (default is ./%s.(yaml|json))", CfgFile))
f.StringSliceP(FlagTemplate, "t", nil, `adds a new template to watch on disk in the format
'templatePath:outputPath[:command]'. This option is additive
and may be specified multiple times for multiple templates`)
f.Duration(FlagCommandTimeout, 15*time.Second, "Default command execution timeout (0 to execute commands without timeout checking)")
f.Bool(FlagHelpMd, false, "get help in Markdown format")
// Merge flags
pflag.CommandLine.SetNormalizeFunc(func(_ *pflag.FlagSet, name string) pflag.NormalizedName {
if strings.Contains(name, "_") {
return pflag.NormalizedName(strings.Replace(name, "_", "-", -1))
}
return pflag.NormalizedName(name)
})
pflag.CommandLine.AddFlagSet(f)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// Init logging
initLogs()
defer flushLogs()
}
func runCmd(cmd *cobra.Command, _ []string) {
if f, _ := cmd.Flags().GetBool(FlagVersion); f {
fmt.Printf("Build version: %s\n", BuildVersion)
fmt.Printf("Build timestamp: %s\n", BuildTimestamp)
return
}
if f, _ := cmd.Flags().GetBool(FlagHelpMd); f {
out := new(bytes.Buffer)
if err := doc.GenMarkdown(cmd, out); err != nil {
fmt.Printf("can't generate help in markdown format: %v", err)
return
}
fmt.Println(out)
return
}
if c := cmd.Flags().Changed(FlagPollTime); c {
if d, err := cmd.Flags().GetDuration(FlagPollTime); err == nil {
_ = cmd.Flags().Set(FlagPollPeriod, d.String())
}
glog.Warningf("'%s' flag is deprecated, use '%s' instead", CfgPollTime, CfgPollPeriod)
}
getConfig := func() (*Config, error) {
config, err := newConfig(cmd)
if err != nil {
return nil, err
}
if len(config.TemplateDescriptors) == 0 {
return nil, errors.New("no templates to process")
}
return config, nil
}
config, err := getConfig()
if err != nil {
glog.Fatalf("config error: %v, exiting...", err)
}
app, err := newApp(config)
if err != nil {
glog.Fatalf("config couldn't be used: %v", err)
}
if config.RunOnce {
app.RunOnce()
return
}
// Start templates processing
go app.Start()
// Listen for signals
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
// Event loop
EventLoop:
for {
sig := <-signalCh
switch sig {
case syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
glog.V(2).Infof("received %v signal...", sig)
// Stop templates processing and exit
app.Stop()
<-app.doneCh
break EventLoop
case syscall.SIGHUP:
glog.V(2).Infof("received %v signal, reloading config", sig)
config, err := getConfig()
if err != nil {
glog.Errorf("config reloading error: %v", err)
continue
}
newApp, err := newApp(config)
if err != nil {
glog.Errorf("reloaded config couldn't be used: %v", err)
continue
}
// Stop templates processing using current config
app.Stop()
<-app.doneCh
// Start templates processing using new config
app = newApp
go app.Start()
}
}
}