-
Notifications
You must be signed in to change notification settings - Fork 28
/
options.go
96 lines (80 loc) · 2.86 KB
/
options.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package app
import (
"context"
goflag "flag"
"github.com/go-logr/logr"
flag "github.com/spf13/pflag"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/gardener/test-infra/pkg/logger"
"github.com/gardener/test-infra/pkg/testmachinery"
"github.com/gardener/test-infra/pkg/testmachinery/controller/admission/webhooks"
"github.com/gardener/test-infra/pkg/testmachinery/controller/configwatcher"
)
type options struct {
log logr.Logger
configwatcher *configwatcher.ConfigWatcher
configPath string
}
func NewOptions() *options {
return &options{}
}
func (o *options) AddFlags(fs *flag.FlagSet) {
fs.StringVar(&o.configPath, "config", "", "Specify the path to the configuration file")
logger.InitFlags(fs)
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
}
// Run parses all options and flags and initializes the basic functions
func (o *options) Complete() error {
log, err := logger.New(nil)
if err != nil {
return err
}
o.log = log.WithName("setup")
logger.SetLogger(log)
ctrl.SetLogger(log)
o.configwatcher, err = configwatcher.New(o.log, o.configPath)
if err != nil {
return err
}
return testmachinery.Setup(o.configwatcher.GetConfiguration())
}
func (o *options) ApplyWebhooks(ctx context.Context, mgr manager.Manager) {
config := o.configwatcher.GetConfiguration()
if !config.TestMachinery.Local {
webhooks.StartHealthCheck(ctx, mgr.GetAPIReader(), config.Controller.DependencyHealthCheck.Namespace, config.Controller.DependencyHealthCheck.DeploymentName, config.Controller.DependencyHealthCheck.Interval)
o.log.Info("Setup webhooks")
hookServer := mgr.GetWebhookServer()
decoder := admission.NewDecoder(testmachinery.TestMachineryScheme)
hookServer.Register("/webhooks/validate-testrun", &webhook.Admission{Handler: webhooks.NewValidatorWithDecoder(logger.Log.WithName("validator"), decoder)})
}
}
func (o *options) GetManagerOptions() manager.Options {
c := o.configwatcher.GetConfiguration()
webhookOpts := webhook.Options{
CertDir: c.Controller.WebhookConfig.CertDir,
}
if !c.TestMachinery.Local {
webhookOpts.Port = c.Controller.WebhookConfig.Port
}
mgrOpts := manager.Options{
LeaderElection: c.Controller.EnableLeaderElection,
WebhookServer: webhook.NewServer(webhookOpts),
Metrics: server.Options{
BindAddress: "0",
}, // disable the metrics serving by default
}
if len(c.Controller.HealthAddr) != 0 {
mgrOpts.HealthProbeBindAddress = c.Controller.HealthAddr
}
if len(c.Controller.MetricsAddr) != 0 {
mgrOpts.Metrics.BindAddress = c.Controller.MetricsAddr
}
return mgrOpts
}