-
Notifications
You must be signed in to change notification settings - Fork 134
/
main.go
93 lines (79 loc) · 2.53 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
// Copyright 2016 Google Inc. All Rights Reserved.
// 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 (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path"
"sync"
"syscall"
"github.com/boltdb/bolt"
)
var (
dataDir = "/var/lib/cert-manager"
discoveryURL = "https://acme-staging.api.letsencrypt.org/directory"
syncInterval = 120
)
func main() {
flag.StringVar(&dataDir, "data-dir", dataDir, "Data directory path.")
flag.StringVar(&discoveryURL, "acme-url", discoveryURL, "AMCE endpoint URL.")
flag.IntVar(&syncInterval, "sync-interval", syncInterval, "Sync interval in seconds.")
flag.Parse()
log.Println("Starting Kubernetes Certificate Controller...")
go func() {
log.Println(http.ListenAndServe("127.0.0.1:6060", nil))
}()
db, err := bolt.Open(path.Join(dataDir, "data.db"), 0600, nil)
if err != nil {
log.Fatal(err)
}
err = db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucketIfNotExists([]byte("Accounts"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
if err != nil {
log.Fatal(err)
}
log.Println("Kubernetes Certificate Controller started successfully.")
// Process all Certificates definitions during the startup process.
err = syncCertificates(db)
if err != nil {
log.Println(err)
}
doneChan := make(chan struct{})
var wg sync.WaitGroup
// Watch for events that add, modify, or delete Certificate definitions and
// process them asynchronously.
log.Println("Watching for certificate events.")
wg.Add(1)
watchCertificateEvents(db, doneChan, &wg)
// Start the certificate reconciler that will ensure all Certificate
// definitions are backed by a LetsEncrypt certificate and a Kubernetes
// TLS secret.
log.Println("Starting reconciliation loop.")
wg.Add(1)
reconcileCertificates(syncInterval, db, doneChan, &wg)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
log.Printf("Shutdown signal received, exiting...")
close(doneChan)
wg.Wait()
os.Exit(0)
}