-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathmain.go
355 lines (325 loc) · 14.1 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
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"
"path/filepath"
"time"
"github.com/kfsoftware/hlf-operator/controllers/chaincode/approve"
"github.com/kfsoftware/hlf-operator/controllers/chaincode/commit"
"github.com/kfsoftware/hlf-operator/controllers/chaincode/deploy"
"github.com/kfsoftware/hlf-operator/controllers/chaincode/install"
"github.com/kfsoftware/hlf-operator/controllers/console"
"github.com/kfsoftware/hlf-operator/controllers/followerchannel"
"github.com/kfsoftware/hlf-operator/controllers/hlfmetrics"
"github.com/kfsoftware/hlf-operator/controllers/identity"
"github.com/kfsoftware/hlf-operator/controllers/mainchannel"
"github.com/kfsoftware/hlf-operator/controllers/networkconfig"
"github.com/kfsoftware/hlf-operator/controllers/operatorapi"
"github.com/kfsoftware/hlf-operator/controllers/operatorui"
"github.com/kfsoftware/hlf-operator/controllers/ordnode"
"github.com/kfsoftware/hlf-operator/controllers/utils"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"github.com/kfsoftware/hlf-operator/controllers/ca"
"github.com/kfsoftware/hlf-operator/controllers/ordservice"
"github.com/kfsoftware/hlf-operator/controllers/peer"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
hlfv1alpha1 "github.com/kfsoftware/hlf-operator/pkg/apis/hlf.kungfusoftware.es/v1alpha1"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(hlfv1alpha1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var autoRenewCertificatesPeerEnabled bool
var autoRenewCertificatesOrdererEnabled bool
var autoRenewCertificatesIdentityEnabled bool
var autoRenewOrdererCertificatesDelta time.Duration
var autoRenewPeerCertificatesDelta time.Duration
var autoRenewIdentityCertificatesDelta time.Duration
var helmChartWait bool
var helmChartTimeout time.Duration
var maxHistory int
var maxReconciles int
flag.StringVar(&metricsAddr, "metrics-addr", ":8090", "The address the metric endpoint binds to.")
flag.DurationVar(&autoRenewOrdererCertificatesDelta, "auto-renew-orderer-certificates-delta", 15*24*time.Hour, "The delta to renew orderer certificates before expiration. Default is 15 days.")
flag.DurationVar(&autoRenewPeerCertificatesDelta, "auto-renew-peer-certificates-delta", 15*24*time.Hour, "The delta to renew peer certificates before expiration. Default is 15 days.")
flag.DurationVar(&autoRenewIdentityCertificatesDelta, "auto-renew-identity-certificates-delta", 15*24*time.Hour, "The delta to renew FabricIdentity certificates before expiration. Default is 15 days.")
flag.BoolVar(&autoRenewCertificatesPeerEnabled, "auto-renew-peer-certificates", false, "Enable auto renew certificates for orderer and peer nodes. Default is false.")
flag.BoolVar(&autoRenewCertificatesOrdererEnabled, "auto-renew-orderer-certificates", false, "Enable auto renew certificates for orderer and peer nodes. Default is false.")
flag.BoolVar(&autoRenewCertificatesIdentityEnabled, "auto-renew-identity-certificates", true, "Enable auto renew certificates for FabricIdentity. Default is true.")
flag.IntVar(&maxReconciles, "max-reconciles", 10, "Max reconciles for a resource. Default is 10.")
flag.BoolVar(&helmChartWait, "helm-chart-wait", false, "Wait for helm chart to be deployed. Default is false.")
flag.IntVar(&maxHistory, "helm-max-history", 10, "Max history for helm chart. Default is 10.")
flag.DurationVar(&helmChartTimeout, "helm-chart-timeout", 5*time.Minute, "Timeout for helm chart to be deployed. Default is 5 minutes.")
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()
log.SetFormatter(&log.JSONFormatter{})
log.Infof("Auto renew peer certificates enabled: %t", autoRenewCertificatesPeerEnabled)
log.Infof("Auto renew orderer certificates enabled: %t", autoRenewCertificatesOrdererEnabled)
log.Infof("Auto renew identity certificates enabled: %t", autoRenewCertificatesIdentityEnabled)
log.Infof("Auto renew peer certificates delta: %s", autoRenewPeerCertificatesDelta)
log.Infof("Auto renew orderer certificates delta: %s", autoRenewOrdererCertificatesDelta)
log.Infof("Auto renew identity certificates delta: %s", autoRenewIdentityCertificatesDelta)
// Pass a Config struct
// to initialize a Client struct
// which implements Client interface
ctrl.SetLogger(zap.New(
zap.UseDevMode(true),
zap.JSONEncoder(),
))
kubeContext, exists := os.LookupEnv("KUBECONTEXT")
var restConfig *rest.Config
var err error
if exists {
restConfig, err = config.GetConfigWithContext(kubeContext)
if err != nil {
log.Fatalf("Failed to get context %s", kubeContext)
}
} else {
restConfig = ctrl.GetConfigOrDie()
}
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(hlfmetrics.CertificateExpiryTimeSeconds)
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme,
LeaderElection: enableLeaderElection,
LeaderElectionID: "a1f969eb.kungfusoftware.es",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
peerChartPath, err := filepath.Abs("./charts/hlf-peer")
if err != nil {
setupLog.Error(err, "unable to find the peer chart")
os.Exit(1)
}
if err = (&peer.FabricPeerReconciler{
Client: mgr.GetClient(),
ChartPath: peerChartPath,
Log: ctrl.Log.WithName("controllers").WithName("FabricPeer"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
AutoRenewCertificates: autoRenewCertificatesPeerEnabled,
AutoRenewCertificatesDelta: autoRenewPeerCertificatesDelta,
Wait: helmChartWait,
Timeout: helmChartTimeout,
MaxHistory: maxHistory,
}).SetupWithManager(mgr, maxReconciles); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricPeer")
os.Exit(1)
}
caChartPath, err := filepath.Abs("./charts/hlf-ca")
if err != nil {
setupLog.Error(err, "unable to find the ca chart")
os.Exit(1)
}
clientSet, err := utils.GetClientKubeWithConf(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "unable to create client set", "controller", "FabricPeer")
os.Exit(1)
}
if err = (&ca.FabricCAReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricCA"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
ClientSet: clientSet,
ChartPath: caChartPath,
Wait: helmChartWait,
Timeout: helmChartTimeout,
MaxHistory: maxHistory,
}).SetupWithManager(mgr, maxReconciles); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricCA")
os.Exit(1)
}
ordServiceChartPath, err := filepath.Abs("./charts/hlf-ord")
if err != nil {
setupLog.Error(err, "unable to find the orderer chart")
os.Exit(1)
}
if err = (&ordservice.FabricOrderingServiceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricOrderingService"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
ChartPath: ordServiceChartPath,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricOrderingService")
os.Exit(1)
}
ordNodeChartPath, err := filepath.Abs("./charts/hlf-ordnode")
if err != nil {
setupLog.Error(err, "unable to find the orderer node chart")
os.Exit(1)
}
if err = (&ordnode.FabricOrdererNodeReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricOrdererNode"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
ChartPath: ordNodeChartPath,
AutoRenewCertificates: autoRenewCertificatesOrdererEnabled,
AutoRenewCertificatesDelta: autoRenewOrdererCertificatesDelta,
Wait: helmChartWait,
Timeout: helmChartTimeout,
MaxHistory: maxHistory,
}).SetupWithManager(mgr, maxReconciles); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricOrdererNode")
os.Exit(1)
}
fabricConsoleChartPath, err := filepath.Abs("./charts/fabric-operations-console")
if err != nil {
setupLog.Error(err, "unable to find the fabric-operations-console chart")
os.Exit(1)
}
if err = (&console.FabricOperationsConsoleReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricOperationsConsole"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
ChartPath: fabricConsoleChartPath,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricOperationsConsole")
os.Exit(1)
}
fabricOperatorAPIChartPath, err := filepath.Abs("./charts/hlf-operator-api")
if err != nil {
setupLog.Error(err, "unable to find the fabric-operations-api chart")
os.Exit(1)
}
if err = (&operatorapi.FabricOperatorAPIReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricOperatorAPI"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
ChartPath: fabricOperatorAPIChartPath,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricOperatorAPI")
os.Exit(1)
}
fabricOperatorUIChartPath, err := filepath.Abs("./charts/hlf-operator-ui")
if err != nil {
setupLog.Error(err, "unable to find the fabric-operations-ui chart")
os.Exit(1)
}
if err = (&operatorui.FabricOperatorUIReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricOperatorUI"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
ChartPath: fabricOperatorUIChartPath,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricOperatorUI")
os.Exit(1)
}
if err = (&networkconfig.FabricNetworkConfigReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricNetworkConfig"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricNetworkConfig")
os.Exit(1)
}
if err = (&mainchannel.FabricMainChannelReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricMainChannel"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricMainChannel")
os.Exit(1)
}
if err = (&identity.FabricIdentityReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricIdentity"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
AutoRenewCertificates: autoRenewCertificatesIdentityEnabled,
AutoRenewCertificatesDelta: autoRenewIdentityCertificatesDelta,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricIdentity")
os.Exit(1)
}
if err = (&followerchannel.FabricFollowerChannelReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricFollowerChannel"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricFollowerChannel")
os.Exit(1)
}
if err = (&deploy.FabricChaincodeDeployReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricChaincode"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricNetworkConfig")
os.Exit(1)
}
if err = (&install.FabricChaincodeInstallReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricChaincodeInstall"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricChaincodeInstall")
os.Exit(1)
}
if err = (&approve.FabricChaincodeApproveReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricChaincodeApprove"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricChaincodeApprove")
os.Exit(1)
}
if err = (&commit.FabricChaincodeCommitReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FabricChaincodeCommit"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FabricChaincodeCommit")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}