-
Notifications
You must be signed in to change notification settings - Fork 193
/
main.go
154 lines (120 loc) · 5.12 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
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/submariner-io/admiral/pkg/http"
"github.com/submariner-io/admiral/pkg/log"
"github.com/submariner-io/admiral/pkg/log/kzerolog"
"github.com/submariner-io/admiral/pkg/names"
"github.com/submariner-io/admiral/pkg/syncer/broker"
"github.com/submariner-io/admiral/pkg/util"
admversion "github.com/submariner-io/admiral/pkg/version"
"github.com/submariner-io/admiral/pkg/watcher"
subv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
"github.com/submariner-io/submariner/pkg/cableengine"
submarinerClientset "github.com/submariner-io/submariner/pkg/client/clientset/versioned"
"github.com/submariner-io/submariner/pkg/gateway"
"github.com/submariner-io/submariner/pkg/natdiscovery"
"github.com/submariner-io/submariner/pkg/types"
"github.com/submariner-io/submariner/pkg/versions"
"golang.org/x/net/http/httpproxy"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
var (
localMasterURL string
localKubeconfig string
showVersion = false
logger = log.Logger{Logger: logf.Log.WithName("main")}
)
func init() {
flag.StringVar(&localKubeconfig, "kubeconfig", "", "Path to kubeconfig of local cluster. Only required if out-of-cluster.")
flag.StringVar(&localMasterURL, "master", "",
"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
flag.BoolVar(&showVersion, "version", showVersion, "Show version")
}
type leaderConfig struct {
LeaseDuration int64
RenewDeadline int64
RetryPeriod int64
}
const leadershipConfigEnvPrefix = "leadership"
func main() {
kzerolog.AddFlags(nil)
flag.Parse()
admversion.Print(names.GatewayComponent, versions.Submariner())
if showVersion {
return
}
kzerolog.InitK8sLogging()
versions.Log(&logger)
gwLeadershipConfig := leaderConfig{}
logger.FatalfOnError(envconfig.Process(leadershipConfigEnvPrefix, &gwLeadershipConfig), "Error processing %s env vars",
leadershipConfigEnvPrefix)
submSpec := types.SubmarinerSpecification{}
logger.FatalOnError(envconfig.Process("submariner", &submSpec), "Error processing env vars")
logger.Infof("Parsed env variables: %#v", submSpec)
proxyEnv := httpproxy.FromEnvironment()
logger.Infof("Proxy env variables: HTTP_PROXY: %v, HTTPS_PROXY: %v, NO_PROXY: %v",
proxyEnv.HTTPProxy, proxyEnv.HTTPSProxy, proxyEnv.NoProxy)
defer http.StartServer(http.Metrics|http.Profile, submSpec.MetricsPort)()
var err error
util.AddCertificateErrorHandler(submSpec.HaltOnCertError)
restConfig, err := clientcmd.BuildConfigFromFlags(localMasterURL, localKubeconfig)
logger.FatalOnError(err, "Error building kubeconfig")
submarinerClient, err := submarinerClientset.NewForConfig(restConfig)
logger.FatalOnError(err, "Error creating submariner clientset")
k8sClient, err := kubernetes.NewForConfig(restConfig)
logger.FatalOnError(err, "Error creating Kubernetes clientset")
leClient, err := kubernetes.NewForConfig(rest.AddUserAgent(restConfig, "leader-election"))
logger.FatalOnError(err, "Error creating leader election kubernetes clientset")
dynClient, err := dynamic.NewForConfig(restConfig)
logger.FatalOnError(err, "Error creating dynamic client")
restMapper, err := util.BuildRestMapper(restConfig)
logger.FatalOnError(err, "Error building the REST mapper")
logger.FatalOnError(subv1.AddToScheme(scheme.Scheme), "Error adding submariner types to the scheme")
ctx := signals.SetupSignalHandler()
gw, err := gateway.New(ctx, &gateway.Config{
LeaderElectionConfig: gateway.LeaderElectionConfig{
LeaseDuration: time.Duration(gwLeadershipConfig.LeaseDuration) * time.Second,
RenewDeadline: time.Duration(gwLeadershipConfig.RenewDeadline) * time.Second,
RetryPeriod: time.Duration(gwLeadershipConfig.RetryPeriod) * time.Second,
},
Spec: submSpec,
SyncerConfig: broker.SyncerConfig{
LocalRestConfig: restConfig,
LocalClient: dynClient,
RestMapper: restMapper,
},
WatcherConfig: watcher.Config{
RestConfig: restConfig,
},
SubmarinerClient: submarinerClient,
KubeClient: k8sClient,
LeaderElectionClient: leClient,
NewCableEngine: cableengine.NewEngine,
NewNATDiscovery: natdiscovery.New,
})
logger.FatalOnError(err, "Error creating gateway instance")
err = gw.Run(ctx)
logger.FatalOnError(err, "Error running the gateway")
}