-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (134 loc) · 5.14 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
/*
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"
"os"
"riser-controller/pkg/ping"
riserruntime "riser-controller/pkg/runtime"
"riser-controller/pkg/sealedsecret"
"time"
"github.com/riser-platform/riser-server/pkg/sdk"
corev1 "k8s.io/api/core/v1"
"riser-controller/controllers"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
knserving "knative.dev/serving/pkg/apis/serving/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
// All env vars are prefixed with RISER_
const envPrefix = "RISER"
// DotEnv file typically used For local development
const dotEnvFile = ".env"
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
err := appsv1.AddToScheme(scheme)
exitIfError(err, "appsv1")
err = corev1.AddToScheme(scheme)
exitIfError(err, "corev1")
err = knserving.AddToScheme(scheme)
exitIfError(err, "knserving")
// +kubebuilder:scaffold:scheme
}
func main() {
// TODO: Even those these are standard switches for a kubebuilder controller, should consider moving to env vars (RuntimeConfiguration)
var metricsAddr string
var enableLeaderElection bool
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
err := loadDotEnv()
exitIfError(err, "Error loading .env file")
var rc riserruntime.Config
err = envconfig.Process(envPrefix, &rc)
exitIfError(err, "Error loading environment variables")
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
})
exitIfError(err, "unable to start manager")
riserClient, err := sdk.NewClient(rc.ServerURL, rc.ServerApikey)
exitIfError(err, "Unable to initialize riser client")
serverPingDuration := time.Second * time.Duration(rc.ServerPingSeconds)
err = ping.StartNewPinger(riserClient, ctrl.Log.WithName("pinger"), rc.Environment, serverPingDuration)
// TODO: We need a better solution for controller->server bootstrapping. We probably need a lease model as well as a per cluster key.
// We exit early to take advantage of k8s backoff retry. This mitigates race conditions during initial setup of the stack.
if err != nil {
exitIfError(err, "Unable to reach server")
}
sealedSecretRefreshDuration, err := time.ParseDuration(rc.SealedsecretCertRefreshDuration)
exitIfError(err, "Unable to parse sealed secret cert refresh duration")
if rc.SealedSecretEnabled {
err = sealedsecret.StartCertRefresher(
ctrl.GetConfigOrDie(),
riserClient,
rc.Environment,
rc.SealedsecretControllerName,
rc.SealedsecretNamespace,
sealedSecretRefreshDuration,
ctrl.Log.WithName("sealedsecret").WithName("refresher"),
)
exitIfError(err, "Unable to start sealed secret cert refresher")
}
err = (&controllers.KNativeConfigurationReconciler{
KNativeReconciler: controllers.KNativeReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("KNativeConfiguration"),
Config: rc,
RiserClient: riserClient,
},
}).SetupWithManager(mgr)
exitIfError(err, "unable to create controller", "controller", "KNativeConfiguration")
err = (&controllers.KNativeRouteReconciler{
KNativeReconciler: controllers.KNativeReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("KNativeRouteReconciler"),
Config: rc,
RiserClient: riserClient,
},
}).SetupWithManager(mgr)
exitIfError(err, "unable to create controller", "controller", "KNativeRouteReconciler")
err = (&controllers.KNativeDomainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("KNativeDomain"),
Config: rc,
RiserClient: riserClient,
}).SetupWithManager(mgr)
exitIfError(err, "unable to create controller", "controller", "KNativeDomain")
setupLog.Info("starting manager")
err = mgr.Start(ctrl.SetupSignalHandler())
exitIfError(err, "problem starting manager")
}
func loadDotEnv() error {
_, err := os.Stat(dotEnvFile)
if !os.IsNotExist(err) {
return godotenv.Load(dotEnvFile)
}
return nil
}
func exitIfError(err error, message string, keysAndValues ...interface{}) {
if err != nil {
setupLog.Error(err, message, keysAndValues...)
os.Exit(1)
}
}