-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
94 lines (79 loc) · 3.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
// Copyright 2019 Yusuke Kuoka. 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 (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/mumoshu/crossover/pkg/controller"
)
func main() {
var tokenfile string
manager := &controller.Manager{}
defaultNs := os.Getenv("NS")
if defaultNs == "" {
defaultNs = os.Getenv("POD_NAMESPACE")
}
flag.StringVar(&manager.Namespace, "namespace", defaultNs, "the namespace to process.")
flag.StringVar(&tokenfile, "token-file", "/var/run/secrets/kubernetes.io/serviceaccount/token", "path to serviceaccount token file")
flag.StringVar(&manager.Server, "apiserver", "https://kubernetes", "K8s api endpoint")
flag.StringVar(&manager.OutputDir, "output-dir", "", "Directory to putput xDS configs so that Envoy can read")
flag.Var(&manager.ConfigMaps, "configmap", "the configmap to process.")
flag.BoolVar(&manager.Noop, "dry-run", false, "print processed configmaps and secrets and do not submit them to the cluster.")
flag.BoolVar(&manager.Onetime, "onetime", false, "run one time and exit.")
flag.BoolVar(&manager.Insecure, "insecure", false, "disable tls server verification")
flag.BoolVar(&manager.Watch, "watch", false, "use watch api to detect changes near realtime")
flag.BoolVar(&manager.SMIEnabled, "smi", false, "Enable SMI integration")
flag.Var(&manager.TrafficSplits, "trafficsplit", "the trafficsplit to be watched and merged into the configmap")
flag.StringVar(&manager.SMITrafficSplitVersion, "trafficsplit-api-version", "v1alpha2", "API version of SMI TrafficSplits e.g. v1alpha1")
flag.DurationVar(&manager.SyncInterval, "sync-interval", (60 * time.Second), "the time duration between template processing.")
flag.Parse()
if len(manager.TrafficSplits) > 0 {
manager.SMIEnabled = true
}
tokenBytes, err := ioutil.ReadFile(tokenfile)
if err != nil {
fmt.Fprintf(os.Stderr, "reading token: %v\n", err)
}
manager.Token = strings.TrimSpace(string(tokenBytes))
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
wg.Add(1)
go func() {
if err := manager.Run(ctx); err != nil {
log.Printf("Error: %v", err)
os.Exit(1)
}
wg.Done()
cancel()
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
select {
case <-signalChan:
log.Printf("Shutdown signal received. Exiting...")
cancel()
wg.Wait()
case <-ctx.Done():
log.Printf("Done writing Envoy configs. Existing...")
}
os.Exit(0)
}