/
start.go
115 lines (99 loc) · 4.17 KB
/
start.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
/*
Copyright 2021 Wim Henderickx.
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 rbaccmd
import (
"time"
"github.com/netw-device-driver/ndd-core/internal/controllers/rbac"
"github.com/netw-device-driver/ndd-runtime/pkg/logging"
"github.com/pkg/errors"
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
// Available RBAC management policies.
const (
ManagementPolicyAll = string(rbac.ManagementPolicyAll)
ManagementPolicyBasic = string(rbac.ManagementPolicyBasic)
)
var (
metricsAddr string
probeAddr string
enableLeaderElection bool
concurrency int
sync string
managementPolicy string
providerClusterRole string
)
// startCmd represents the start command for the network device driver
var startCmd = &cobra.Command{
Use: "start",
Short: "start the network device driver rbac",
Long: "start the network device driver rbac",
Aliases: []string{"start"},
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
zlog := zap.New(zap.UseDevMode(debug), zap.JSONEncoder())
if debug {
// Only use a logr.Logger when debug is on
ctrl.SetLogger(zlog)
}
zlog.Info("create ndd rbac manager")
syncPeriod, err := time.ParseDuration(sync)
if err != nil {
return errors.Wrap(err, "Cannot parse sync duration")
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "c66ce353.rbac.ndd.yndd.io",
SyncPeriod: &syncPeriod,
})
if err != nil {
return errors.Wrap(err, "Cannot create manager")
}
if err := rbac.Setup(mgr, logging.NewLogrLogger(zlog.WithName("nddrbac")), rbac.ManagementPolicy(managementPolicy), providerClusterRole); err != nil {
return errors.Wrap(err, "Cannot add rbac controllers to manager")
}
// +kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("health", healthz.Ping); err != nil {
return errors.Wrap(err, "unable to set up health check")
}
if err := mgr.AddReadyzCheck("check", healthz.Ping); err != nil {
return errors.Wrap(err, "unable to set up ready check")
}
zlog.Info("starting nddrbac manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
return errors.Wrap(err, "problem running manager")
}
return nil
},
}
func init() {
rootCmd.AddCommand(startCmd)
startCmd.Flags().StringVarP(&metricsAddr, "metrics-bind-address", "m", ":8080", "The address the metric endpoint binds to.")
startCmd.Flags().StringVarP(&probeAddr, "health-probe-bind-address", "p", ":8081", "The address the probe endpoint binds to.")
startCmd.Flags().BoolVarP(&enableLeaderElection, "leader-elect", "l", false, "Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
startCmd.Flags().IntVarP(&concurrency, "concurrency", "", 1, "Number of items to process simultaneously")
startCmd.Flags().StringVarP(&sync, "sync", "s", "1h", "Controller manager sync period duration such as 300ms, 1.5h or 2h45m")
startCmd.Flags().StringVarP(&managementPolicy, "management-policy", "", ManagementPolicyBasic, "RBAC management policy.")
startCmd.Flags().StringVarP(&providerClusterRole, "provider-clusterrole", "c", "", "A ClusterRole enumerating the permissions provider packages may request.")
}
func nddConcurrency(c int) controller.Options {
return controller.Options{MaxConcurrentReconciles: c}
}