Skip to content

Commit

Permalink
error messages if config is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
j1m-ryan committed Nov 18, 2024
1 parent dd7897a commit 3447c97
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ func processConfigMaps(kubeClient *kubernetes.Clientset, cfgParams *configs.Conf
if err != nil {
nl.Fatalf(l, "Error when getting %v: %v", *nginxConfigMaps, err)
}
cfgParams = configs.ParseConfigMap(cfgParams.Context, cfm, *nginxPlus, *appProtect, *appProtectDos, *enableTLSPassthrough, eventLog)
cfgParams, _ = configs.ParseConfigMap(cfgParams.Context, cfm, *nginxPlus, *appProtect, *appProtectDos, *enableTLSPassthrough, eventLog)
if cfgParams.MainServerSSLDHParamFileContent != nil {
fileName, err := nginxManager.CreateDHParam(*cfgParams.MainServerSSLDHParamFileContent)
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions internal/configs/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// ParseConfigMap parses ConfigMap into ConfigParams.
//
//nolint:gocyclo
func ParseConfigMap(ctx context.Context, cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasAppProtectDos bool, hasTLSPassthrough bool, eventLog record.EventRecorder) *ConfigParams {
func ParseConfigMap(ctx context.Context, cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasAppProtectDos bool, hasTLSPassthrough bool, eventLog record.EventRecorder) (*ConfigParams, bool) {
l := nl.LoggerFromContext(ctx)
cfgParams := NewDefaultConfigParams(ctx, nginxPlus)
configOk := true
Expand Down Expand Up @@ -652,13 +652,7 @@ func ParseConfigMap(ctx context.Context, cfgm *v1.ConfigMap, nginxPlus bool, has
}
}

if configOk {
eventLog.Event(cfgm, v1.EventTypeNormal, "Updated", fmt.Sprintf("ConfigMap %s/%s updated without error", cfgm.GetNamespace(), cfgm.GetName()))
} else {
eventLog.Event(cfgm, v1.EventTypeWarning, "UpdatedWithError", fmt.Sprintf("ConfigMap %s/%s updated with errors. Ignoring invalid values", cfgm.GetNamespace(), cfgm.GetName()))
}

return cfgParams
return cfgParams, configOk
}

// GenerateNginxMainConfig generates MainConfig.
Expand Down
12 changes: 6 additions & 6 deletions internal/configs/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestParseConfigMapWithAppProtectCompressedRequestsAction(t *testing.T) {
"app-protect-compressed-requests-action": test.action,
},
}
result := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
result, _ := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
if result.MainAppProtectCompressedRequestsAction != test.expect {
t.Errorf("ParseConfigMap() returned %q but expected %q for the case %s", result.MainAppProtectCompressedRequestsAction, test.expect, test.msg)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestParseConfigMapWithAppProtectReconnectPeriod(t *testing.T) {
"app-protect-reconnect-period-seconds": test.period,
},
}
result := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
result, _ := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
if result.MainAppProtectReconnectPeriod != test.expect {
t.Errorf("ParseConfigMap() returned %q but expected %q for the case %s", result.MainAppProtectReconnectPeriod, test.expect, test.msg)
}
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestParseConfigMapWithTLSPassthroughProxyProtocol(t *testing.T) {
"real-ip-header": test.realIPheader,
},
}
result := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
result, _ := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
if result.RealIPHeader != test.want {
t.Errorf("want %q, got %q", test.want, result.RealIPHeader)
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestParseConfigMapWithoutTLSPassthroughProxyProtocol(t *testing.T) {
"real-ip-header": test.realIPheader,
},
}
result := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
result, _ := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
if result.RealIPHeader != test.want {
t.Errorf("want %q, got %q", test.want, result.RealIPHeader)
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestParseConfigMapAccessLog(t *testing.T) {
"access-log-off": test.accessLogOff,
},
}
result := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
result, _ := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
if result.MainAccessLog != test.want {
t.Errorf("want %q, got %q", test.want, result.MainAccessLog)
}
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestParseConfigMapAccessLogDefault(t *testing.T) {
"access-log-off": "False",
},
}
result := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
result, _ := ParseConfigMap(context.Background(), cm, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
if result.MainAccessLog != test.want {
t.Errorf("want %q, got %q", test.want, result.MainAccessLog)
}
Expand Down
11 changes: 8 additions & 3 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"github.com/nginxinc/kubernetes-ingress/internal/metrics/collectors"

api_v1 "k8s.io/api/core/v1"

Check failure on line 56 in internal/k8s/controller.go

View workflow job for this annotation

GitHub Actions / Unit Tests

package "k8s.io/api/core/v1" is being imported more than once (ST1019)
v1 "k8s.io/api/core/v1"

Check failure on line 57 in internal/k8s/controller.go

View workflow job for this annotation

GitHub Actions / Unit Tests

other import of "k8s.io/api/core/v1"
discovery_v1 "k8s.io/api/discovery/v1"
networking "k8s.io/api/networking/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -841,9 +842,10 @@ func (lbc *LoadBalancerController) createExtendedResources(resources []Resource)
func (lbc *LoadBalancerController) updateAllConfigs() {
ctx := nl.ContextWithLogger(context.Background(), lbc.Logger)
cfgParams := configs.NewDefaultConfigParams(ctx, lbc.isNginxPlus)
var isNGINXConfigValid bool

if lbc.configMap != nil {
cfgParams = configs.ParseConfigMap(ctx, lbc.configMap, lbc.isNginxPlus, lbc.appProtectEnabled, lbc.appProtectDosEnabled, lbc.configuration.isTLSPassthroughEnabled, lbc.recorder)
cfgParams, isNGINXConfigValid = configs.ParseConfigMap(ctx, lbc.configMap, lbc.isNginxPlus, lbc.appProtectEnabled, lbc.appProtectDosEnabled, lbc.configuration.isTLSPassthroughEnabled, lbc.recorder)
}

resources := lbc.configuration.GetResources()
Expand All @@ -869,8 +871,11 @@ func (lbc *LoadBalancerController) updateAllConfigs() {
}

if lbc.configMap != nil {
key := getResourceKey(&lbc.configMap.ObjectMeta)
lbc.recorder.Eventf(lbc.configMap, eventType, eventTitle, "Configuration from %v was updated %s", key, eventWarningMessage)
if isNGINXConfigValid {
lbc.recorder.Event(lbc.configMap, v1.EventTypeNormal, "Updated", fmt.Sprintf("ConfigMap %s/%s updated without error", lbc.configMap.GetNamespace(), lbc.configMap.GetName()))
} else {
lbc.recorder.Event(lbc.configMap, v1.EventTypeWarning, "UpdatedWithError", fmt.Sprintf("ConfigMap %s/%s updated with errors. Ignoring invalid values", lbc.configMap.GetNamespace(), lbc.configMap.GetName()))
}
}

gc := lbc.configuration.GetGlobalConfiguration()
Expand Down

0 comments on commit 3447c97

Please sign in to comment.