forked from fluxcd/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
250 lines (205 loc) · 9.01 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/go-kit/kit/log"
"github.com/spf13/pflag"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/weaveworks/flux/checkpoint"
"github.com/weaveworks/flux/git"
clientset "github.com/weaveworks/flux/integrations/client/clientset/versioned"
ifinformers "github.com/weaveworks/flux/integrations/client/informers/externalversions"
fluxhelm "github.com/weaveworks/flux/integrations/helm"
helmop "github.com/weaveworks/flux/integrations/helm"
"github.com/weaveworks/flux/integrations/helm/chartsync"
"github.com/weaveworks/flux/integrations/helm/operator"
"github.com/weaveworks/flux/integrations/helm/release"
"github.com/weaveworks/flux/integrations/helm/status"
)
var (
fs *pflag.FlagSet
err error
logger log.Logger
kubectl string
versionFlag *bool
kubeconfig *string
master *string
tillerIP *string
tillerPort *string
tillerNamespace *string
tillerTLSVerify *bool
tillerTLSEnable *bool
tillerTLSKey *string
tillerTLSCert *string
tillerTLSCACert *string
chartsSyncInterval *time.Duration
chartsSyncTimeout *time.Duration
logReleaseDiffs *bool
gitURL *string
gitBranch *string
gitChartsPath *string
gitPollInterval *time.Duration
queueWorkerCount *int
name *string
listenAddr *string
gcInterval *time.Duration
)
const (
product = "weave-flux-helm"
defaultGitChartsPath = "charts"
ErrOperatorFailure = "Operator failure: %q"
)
var version = "unversioned"
func init() {
// Flags processing
fs = pflag.NewFlagSet("default", pflag.ExitOnError)
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "DESCRIPTION\n")
fmt.Fprintf(os.Stderr, " helm-operator releases Helm charts from git.\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "FLAGS\n")
fs.PrintDefaults()
}
versionFlag = fs.Bool("version", false, "Print version and exit")
kubeconfig = fs.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
master = fs.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
tillerIP = fs.String("tiller-ip", "", "Tiller IP address. Only required if out-of-cluster.")
tillerPort = fs.String("tiller-port", "", "Tiller port.")
tillerNamespace = fs.String("tiller-namespace", "kube-system", "Tiller namespace. If not provided, the default is kube-system.")
tillerTLSVerify = fs.Bool("tiller-tls-verify", false, "Verify TLS certificate from Tiller. Will enable TLS communication when provided.")
tillerTLSEnable = fs.Bool("tiller-tls-enable", false, "Enable TLS communication with Tiller. If provided, requires TLSKey and TLSCert to be provided as well.")
tillerTLSKey = fs.String("tiller-tls-key-path", "/etc/fluxd/helm/tls.key", "Path to private key file used to communicate with the Tiller server.")
tillerTLSCert = fs.String("tiller-tls-cert-path", "/etc/fluxd/helm/tls.crt", "Path to certificate file used to communicate with the Tiller server.")
tillerTLSCACert = fs.String("tiller-tls-ca-cert-path", "", "Path to CA certificate file used to validate the Tiller server. Required if tiller-tls-verify is enabled.")
chartsSyncInterval = fs.Duration("charts-sync-interval", 3*time.Minute, "Interval at which to check for changed charts")
chartsSyncTimeout = fs.Duration("charts-sync-timeout", 1*time.Minute, "Timeout when checking for changed charts")
logReleaseDiffs = fs.Bool("log-release-diffs", false, "Log the diff when a chart release diverges; potentially insecure")
gitURL = fs.String("git-url", "", "URL of git repo with Helm Charts; e.g., git@github.com:weaveworks/flux-example")
gitBranch = fs.String("git-branch", "master", "branch of git repo")
gitChartsPath = fs.String("git-charts-path", defaultGitChartsPath, "path within git repo to locate Helm Charts (relative path)")
gitPollInterval = fs.Duration("git-poll-interval", 5*time.Minute, "period on which to poll for changes to the git repo")
queueWorkerCount = fs.Int("queue-worker-count", 2, "Number of workers to process queue with Chart release jobs. Two by default")
}
func main() {
// Stop glog complaining
flag.CommandLine.Parse([]string{"-logtostderr"})
// Now do our own
fs.Parse(os.Args)
if *versionFlag {
println(version)
os.Exit(0)
}
// LOGGING ------------------------------------------------------------------------------
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
// SHUTDOWN ----------------------------------------------------------------------------
errc := make(chan error)
// Shutdown trigger for goroutines
shutdown := make(chan struct{})
shutdownWg := &sync.WaitGroup{}
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
defer func() {
logger.Log("exiting...", <-errc)
close(shutdown)
shutdownWg.Wait()
}()
mainLogger := log.With(logger, "component", "helm-operator")
// CLUSTER ACCESS -----------------------------------------------------------------------
cfg, err := clientcmd.BuildConfigFromFlags(*master, *kubeconfig)
if err != nil {
mainLogger.Log("error", fmt.Sprintf("Error building kubeconfig: %v", err))
os.Exit(1)
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
mainLogger.Log("error", fmt.Sprintf("Error building kubernetes clientset: %v", err))
os.Exit(1)
}
// CUSTOM RESOURCES CLIENT --------------------------------------------------------------
ifClient, err := clientset.NewForConfig(cfg)
if err != nil {
mainLogger.Log("error", fmt.Sprintf("Error building integrations clientset: %v", err))
//errc <- fmt.Errorf("Error building integrations clientset: %v", err)
os.Exit(1)
}
// HELM ---------------------------------------------------------------------------------
helmClient := fluxhelm.ClientSetup(log.With(logger, "component", "helm"), kubeClient, fluxhelm.TillerOptions{
IP: *tillerIP,
Port: *tillerPort,
Namespace: *tillerNamespace,
TLSVerify: *tillerTLSVerify,
TLSEnable: *tillerTLSEnable,
TLSKey: *tillerTLSKey,
TLSCert: *tillerTLSCert,
TLSCACert: *tillerTLSCACert,
})
// The status updater, to keep track the release status for each
// FluxHelmRelease. It runs as a separate loop for now.
statusUpdater := status.New(ifClient, kubeClient, helmClient)
go statusUpdater.Loop(shutdown, log.With(logger, "component", "annotator"))
gitRemote := git.Remote{URL: *gitURL}
repo := git.NewRepo(gitRemote, git.PollInterval(*gitPollInterval), git.ReadOnly)
// Chart releases sync due to Custom Resources changes -------------------------------
{
mainLogger.Log("info", "Attempting to clone repo ...", "url", gitRemote.URL)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
err := repo.Ready(ctx)
cancel()
if err != nil {
mainLogger.Log("error", err)
os.Exit(2)
}
mainLogger.Log("info", "Repo cloned", "url", gitRemote.URL)
// Start the repo fetching from upstream
shutdownWg.Add(1)
go func() {
errc <- repo.Start(shutdown, shutdownWg)
}()
}
releaseConfig := release.Config{
ChartsPath: *gitChartsPath,
}
repoConfig := helmop.RepoConfig{
Repo: repo,
Branch: *gitBranch,
ChartsPath: *gitChartsPath,
}
// release instance is needed during the sync of Charts changes and during the sync of FluxHelmRelease changes
rel := release.New(log.With(logger, "component", "release"), helmClient, releaseConfig)
// CHARTS CHANGES SYNC ------------------------------------------------------------------
chartSync := chartsync.New(log.With(logger, "component", "chartsync"),
chartsync.Polling{Interval: *chartsSyncInterval, Timeout: *chartsSyncTimeout},
chartsync.Clients{KubeClient: *kubeClient, IfClient: *ifClient},
rel, repoConfig, *logReleaseDiffs)
chartSync.Run(shutdown, errc, shutdownWg)
// OPERATOR - CUSTOM RESOURCE CHANGE SYNC -----------------------------------------------
// CUSTOM RESOURCES CACHING SETUP -------------------------------------------------------
// SharedInformerFactory sets up informer, that maps resource type to a cache shared informer.
// operator attaches event handler to the informer and syncs the informer cache
ifInformerFactory := ifinformers.NewSharedInformerFactory(ifClient, 30*time.Second)
// Reference to shared index informers for the FluxHelmRelease
fhrInformer := ifInformerFactory.Helm().V1alpha2().FluxHelmReleases()
opr := operator.New(log.With(logger, "component", "operator"), *logReleaseDiffs, kubeClient, fhrInformer, chartSync, repoConfig)
// Starts handling k8s events related to the given resource kind
go ifInformerFactory.Start(shutdown)
checkpoint.CheckForUpdates(product, version, nil, log.With(logger, "component", "checkpoint"))
if err = opr.Run(*queueWorkerCount, shutdown, shutdownWg); err != nil {
msg := fmt.Sprintf("Failure to run controller: %s", err.Error())
logger.Log("error", msg)
errc <- fmt.Errorf(ErrOperatorFailure, err)
}
}