Skip to content

Commit 99f3b83

Browse files
authored
restructure configuration structs (#73)
1 parent 3e139fd commit 99f3b83

File tree

5 files changed

+50
-35
lines changed

5 files changed

+50
-35
lines changed

cmd/standalone/main.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ func main() {
6060
var secureMetrics bool
6161
var enableHTTP2 bool
6262

63-
awConfig := config.NewConfig(namespaceOrDie())
64-
awConfig.StandaloneMode = true
65-
awConfig.ManageJobsWithoutQueueName = false
63+
cfg := &config.OperatorConfig{
64+
AppWrapper: config.NewAppWrapperConfig(),
65+
CertManagement: config.NewCertManagementConfig(namespaceOrDie()),
66+
}
67+
cfg.AppWrapper.StandaloneMode = true
68+
cfg.AppWrapper.ManageJobsWithoutQueueName = false
6669

6770
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6871
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
@@ -81,9 +84,9 @@ func main() {
8184

8285
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
8386
setupLog.Info("Build info", "version", BuildVersion, "date", BuildDate)
84-
setupLog.Info("Configuration", "config", awConfig)
87+
setupLog.Info("Configuration", "config", cfg)
8588

86-
if err := config.ValidateConfig(awConfig); err != nil {
89+
if err := config.ValidateAppWrapperConfig(cfg.AppWrapper); err != nil {
8790
setupLog.Error(err, "invalid appwrapper config")
8891
os.Exit(1)
8992
}
@@ -142,16 +145,16 @@ func main() {
142145
if os.Getenv("ENABLE_WEBHOOKS") == "false" {
143146
close(certsReady)
144147
} else {
145-
if err := controller.SetupCertManagement(mgr, &awConfig.CertManagement, certsReady); err != nil {
148+
if err := controller.SetupCertManagement(mgr, cfg.CertManagement, certsReady); err != nil {
146149
setupLog.Error(err, "Unable to set up cert rotation")
147150
os.Exit(1)
148151
}
149152
}
150153

151154
// Ascynchronous because controllers need to wait for certificate to be ready for webhooks to work
152-
go controller.SetupControllers(ctx, mgr, awConfig, certsReady, setupLog)
155+
go controller.SetupControllers(ctx, mgr, cfg.AppWrapper, certsReady, setupLog)
153156

154-
if err := controller.SetupIndexers(ctx, mgr, awConfig); err != nil {
157+
if err := controller.SetupIndexers(ctx, mgr, cfg.AppWrapper); err != nil {
155158
setupLog.Error(err, "unable to setup indexers")
156159
os.Exit(1)
157160
}

cmd/unified/main.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ func main() {
6262
var secureMetrics bool
6363
var enableHTTP2 bool
6464

65-
awConfig := config.NewConfig(namespaceOrDie())
65+
cfg := &config.OperatorConfig{
66+
AppWrapper: config.NewAppWrapperConfig(),
67+
CertManagement: config.NewCertManagementConfig(namespaceOrDie()),
68+
}
6669

6770
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6871
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
@@ -73,7 +76,8 @@ func main() {
7376
"If set the metrics endpoint is served securely")
7477
flag.BoolVar(&enableHTTP2, "enable-http2", false,
7578
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
76-
flag.BoolVar(&awConfig.ManageJobsWithoutQueueName, "manage-no-queue", true, "Manage AppWrappers without queue names")
79+
flag.BoolVar(&cfg.AppWrapper.ManageJobsWithoutQueueName,
80+
"manage-no-queue", true, "Manage AppWrappers without queue names")
7781
opts := zap.Options{
7882
Development: true,
7983
}
@@ -82,9 +86,9 @@ func main() {
8286

8387
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
8488
setupLog.Info("Build info", "version", BuildVersion, "date", BuildDate)
85-
setupLog.Info("Configuration", "config", awConfig)
89+
setupLog.Info("Configuration", "config", cfg)
8690

87-
if err := config.ValidateConfig(awConfig); err != nil {
91+
if err := config.ValidateAppWrapperConfig(cfg.AppWrapper); err != nil {
8892
setupLog.Error(err, "invalid appwrapper config")
8993
os.Exit(1)
9094
}
@@ -143,16 +147,16 @@ func main() {
143147
if os.Getenv("ENABLE_WEBHOOKS") == "false" {
144148
close(certsReady)
145149
} else {
146-
if err := controller.SetupCertManagement(mgr, &awConfig.CertManagement, certsReady); err != nil {
150+
if err := controller.SetupCertManagement(mgr, cfg.CertManagement, certsReady); err != nil {
147151
setupLog.Error(err, "Unable to set up cert rotation")
148152
os.Exit(1)
149153
}
150154
}
151155

152156
// Ascynchronous because controllers need to wait for certificate to be ready for webhooks to work
153-
go controller.SetupControllers(ctx, mgr, awConfig, certsReady, setupLog)
157+
go controller.SetupControllers(ctx, mgr, cfg.AppWrapper, certsReady, setupLog)
154158

155-
if err := controller.SetupIndexers(ctx, mgr, awConfig); err != nil {
159+
if err := controller.SetupIndexers(ctx, mgr, cfg.AppWrapper); err != nil {
156160
setupLog.Error(err, "unable to setup indexers")
157161
os.Exit(1)
158162
}

internal/controller/appwrapper/appwrapper_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var _ = Describe("AppWrapper Controller", func() {
5656
Name: aw.Name,
5757
Namespace: aw.Namespace,
5858
}
59-
awConfig := config.NewConfig("")
59+
awConfig := config.NewAppWrapperConfig()
6060
awConfig.FaultTolerance.FailureGracePeriod = 0 * time.Second
6161
awConfig.FaultTolerance.ResetPause = 0 * time.Second
6262
awConfig.FaultTolerance.RetryLimit = 0

internal/webhook/suite_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ var _ = BeforeSuite(func() {
155155
})
156156
Expect(err).NotTo(HaveOccurred())
157157

158-
err = (&AppWrapperWebhook{Config: config.NewConfig("")}).SetupWebhookWithManager(mgr)
158+
err = (&AppWrapperWebhook{Config: config.NewAppWrapperConfig()}).SetupWebhookWithManager(mgr)
159159
Expect(err).NotTo(HaveOccurred())
160160

161161
//+kubebuilder:scaffold:webhook

pkg/config/config.go

+26-18
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ import (
2121
"time"
2222
)
2323

24+
type OperatorConfig struct {
25+
AppWrapper *AppWrapperConfig `json:"appWrapper,omitempty"`
26+
CertManagement *CertManagementConfig `json:"certManagement,omitempty"`
27+
}
28+
2429
type AppWrapperConfig struct {
25-
ManageJobsWithoutQueueName bool `json:"manageJobsWithoutQueueName,omitempty"`
26-
StandaloneMode bool `json:"standaloneMode,omitempty"`
27-
FaultTolerance FaultToleranceConfig `json:"faultTolerance,omitempty"`
28-
CertManagement CertManagementConfig `json:"certManagement,omitempty"`
30+
ManageJobsWithoutQueueName bool `json:"manageJobsWithoutQueueName,omitempty"`
31+
StandaloneMode bool `json:"standaloneMode,omitempty"`
32+
FaultTolerance *FaultToleranceConfig `json:"faultTolerance,omitempty"`
2933
}
3034

3135
type FaultToleranceConfig struct {
@@ -48,33 +52,23 @@ type CertManagementConfig struct {
4852
WebhookSecretName string `json:"webhookSecretName,omitempty"`
4953
}
5054

51-
// NewConfig constructs an AppWrapperConfig and fills in default values
52-
func NewConfig(namespace string) *AppWrapperConfig {
55+
// NewAppWrapperConfig constructs an AppWrapperConfig and fills in default values
56+
func NewAppWrapperConfig() *AppWrapperConfig {
5357
return &AppWrapperConfig{
5458
ManageJobsWithoutQueueName: true,
5559
StandaloneMode: false,
56-
FaultTolerance: FaultToleranceConfig{
60+
FaultTolerance: &FaultToleranceConfig{
5761
WarmupGracePeriod: 5 * time.Minute,
5862
FailureGracePeriod: 1 * time.Minute,
5963
ResetPause: 90 * time.Second,
6064
RetryLimit: 3,
6165
DeletionGracePeriod: 10 * time.Minute,
6266
GracePeriodCeiling: 24 * time.Hour,
6367
},
64-
CertManagement: CertManagementConfig{
65-
Namespace: namespace,
66-
CertificateDir: "/tmp/k8s-webhook-server/serving-certs",
67-
CertificateName: "appwrapper-ca",
68-
CertificateOrg: "appwrapper",
69-
MutatingWebhookConfigName: "appwrapper-mutating-webhook-configuration",
70-
ValidatingWebhookConfigName: "appwrapper-validating-webhook-configuration",
71-
WebhookServiceName: "appwrapper-webhook-service",
72-
WebhookSecretName: "appwrapper-webhook-server-cert",
73-
},
7468
}
7569
}
7670

77-
func ValidateConfig(config *AppWrapperConfig) error {
71+
func ValidateAppWrapperConfig(config *AppWrapperConfig) error {
7872
if config.FaultTolerance.DeletionGracePeriod > config.FaultTolerance.GracePeriodCeiling {
7973
return fmt.Errorf("DelectionGracePeriod %v exceeds GracePeriodCeiling %v",
8074
config.FaultTolerance.DeletionGracePeriod, config.FaultTolerance.GracePeriodCeiling)
@@ -94,3 +88,17 @@ func ValidateConfig(config *AppWrapperConfig) error {
9488

9589
return nil
9690
}
91+
92+
// NewCertManagermentConfig constructs a CertManagementConfig and fills in default values
93+
func NewCertManagementConfig(namespace string) *CertManagementConfig {
94+
return &CertManagementConfig{
95+
Namespace: namespace,
96+
CertificateDir: "/tmp/k8s-webhook-server/serving-certs",
97+
CertificateName: "appwrapper-ca",
98+
CertificateOrg: "appwrapper",
99+
MutatingWebhookConfigName: "appwrapper-mutating-webhook-configuration",
100+
ValidatingWebhookConfigName: "appwrapper-validating-webhook-configuration",
101+
WebhookServiceName: "appwrapper-webhook-service",
102+
WebhookSecretName: "appwrapper-webhook-server-cert",
103+
}
104+
}

0 commit comments

Comments
 (0)