/
config_updater.go
93 lines (81 loc) · 2.38 KB
/
config_updater.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
package static
import (
"encoding/json"
"fmt"
"github.com/go-logr/logr"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/tools/record"
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
)
// updateControlPlane updates the control plane configuration with the given user spec.
// If any fields are not set within the user spec, the default configuration values are used.
func updateControlPlane(
cfg *ngfAPI.NginxGateway,
logger logr.Logger,
eventRecorder record.EventRecorder,
configNSName types.NamespacedName,
logLevelSetter logLevelSetter,
) error {
// build up default configuration
controlConfig := ngfAPI.NginxGatewaySpec{
Logging: &ngfAPI.Logging{
Level: helpers.GetPointer(ngfAPI.ControllerLogLevelInfo),
},
}
// by marshaling the user config and then unmarshaling on top of the default config,
// we ensure that any unset user values are set with the default values
if cfg != nil {
cfgBytes, err := json.Marshal(cfg.Spec)
if err != nil {
return fmt.Errorf("error marshaling control config: %w", err)
}
if err := json.Unmarshal(cfgBytes, &controlConfig); err != nil {
return fmt.Errorf("error unmarshaling control config: %w", err)
}
} else {
msg := "NginxGateway configuration was deleted; using defaults"
logger.Info(msg)
eventRecorder.Event(
&ngfAPI.NginxGateway{
ObjectMeta: metav1.ObjectMeta{
Namespace: configNSName.Namespace,
Name: configNSName.Name,
},
},
apiv1.EventTypeWarning,
"ResourceDeleted",
msg,
)
}
level := *controlConfig.Logging.Level
if err := validateLogLevel(level); err != nil {
return err
}
if err := logLevelSetter.SetLevel(string(level)); err != nil {
return field.Invalid(
field.NewPath("logging.level"),
level,
err.Error(),
)
}
return nil
}
func validateLogLevel(level ngfAPI.ControllerLogLevel) error {
switch level {
case ngfAPI.ControllerLogLevelInfo, ngfAPI.ControllerLogLevelDebug, ngfAPI.ControllerLogLevelError:
default:
return field.NotSupported(
field.NewPath("logging.level"),
level,
[]string{
string(ngfAPI.ControllerLogLevelInfo),
string(ngfAPI.ControllerLogLevelDebug),
string(ngfAPI.ControllerLogLevelError),
})
}
return nil
}