Skip to content

Commit

Permalink
[refactor] Passing down logger for FailureMode Setting
Browse files Browse the repository at this point in the history
Signed-off-by: dd di cesare <[email protected]>
  • Loading branch information
didierofrivia committed Nov 20, 2024
1 parent eb66d70 commit b70d785
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 42 deletions.
2 changes: 1 addition & 1 deletion controllers/envoy_gateway_extension_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (r *EnvoyGatewayExtensionReconciler) buildWasmConfigs(ctx context.Context,
wasmConfigs := lo.MapValues(wasmActionSets.Sorted(), func(configs kuadrantgatewayapi.SortableHTTPRouteMatchConfigs, _ string) wasm.Config {
return wasm.BuildConfigForActionSet(lo.Map(configs, func(c kuadrantgatewayapi.HTTPRouteMatchConfig, _ int) wasm.ActionSet {
return c.Config.(wasm.ActionSet)
}))
}), &logger)
})

return wasmConfigs, nil
Expand Down
2 changes: 1 addition & 1 deletion controllers/istio_extension_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (r *IstioExtensionReconciler) buildWasmConfigs(ctx context.Context, state *
wasmConfigs := lo.MapValues(wasmActionSets.Sorted(), func(configs kuadrantgatewayapi.SortableHTTPRouteMatchConfigs, _ string) wasm.Config {
return wasm.BuildConfigForActionSet(lo.Map(configs, func(c kuadrantgatewayapi.HTTPRouteMatchConfig, _ int) wasm.ActionSet {
return c.Config.(wasm.ActionSet)
}))
}), &logger)
})

return wasmConfigs, nil
Expand Down
20 changes: 11 additions & 9 deletions pkg/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"strings"

"github.com/go-logr/logr"

"github.com/kuadrant/policy-machinery/machinery"
"github.com/samber/lo"
"google.golang.org/protobuf/types/known/structpb"
Expand All @@ -31,19 +33,19 @@ func AuthServiceTimeout() string {
return env.GetString("AUTH_SERVICE_TIMEOUT", "200ms")
}

func AuthServiceFailureMode() FailureModeType {
return parseFailureModeValue("AUTH_SERVICE_FAILURE_MODE", FailureModeDeny)
func AuthServiceFailureMode(logger *logr.Logger) FailureModeType {
return parseFailureModeValue("AUTH_SERVICE_FAILURE_MODE", FailureModeDeny, logger)
}

func RatelimitServiceTimeout() string {
return env.GetString("RATELIMIT_SERVICE_TIMEOUT", "100ms")
}

func RatelimitServiceFailureMode() FailureModeType {
return parseFailureModeValue("RATELIMIT_SERVICE_FAILURE_MODE", FailureModeAllow)
func RatelimitServiceFailureMode(logger *logr.Logger) FailureModeType {
return parseFailureModeValue("RATELIMIT_SERVICE_FAILURE_MODE", FailureModeAllow, logger)
}

func parseFailureModeValue(envVarName string, defaultValue FailureModeType) FailureModeType {
func parseFailureModeValue(envVarName string, defaultValue FailureModeType, logger *logr.Logger) FailureModeType {
value := os.Getenv(envVarName)
if value == "" {
return defaultValue
Expand All @@ -53,7 +55,7 @@ func parseFailureModeValue(envVarName string, defaultValue FailureModeType) Fail
case string(FailureModeAllow), string(FailureModeDeny):
return FailureModeType(value)
default:
fmt.Printf("Warning: Invalid value '%s' for %s. Using default value '%s'.\n", value, envVarName, defaultValue)
logger.Info("Warning: Invalid FailureMode value '%s' for %s. Using default value '%s'.\n", value, envVarName, defaultValue)
return defaultValue
}
}
Expand All @@ -62,19 +64,19 @@ func ExtensionName(gatewayName string) string {
return fmt.Sprintf("kuadrant-%s", gatewayName)
}

func BuildConfigForActionSet(actionSets []ActionSet) Config {
func BuildConfigForActionSet(actionSets []ActionSet, logger *logr.Logger) Config {
return Config{
Services: map[string]Service{
AuthServiceName: {
Type: AuthServiceType,
Endpoint: kuadrant.KuadrantAuthClusterName,
FailureMode: AuthServiceFailureMode(),
FailureMode: AuthServiceFailureMode(logger),
Timeout: ptr.To(AuthServiceTimeout()),
},
RateLimitServiceName: {
Type: RateLimitServiceType,
Endpoint: kuadrant.KuadrantRateLimitClusterName,
FailureMode: RatelimitServiceFailureMode(),
FailureMode: RatelimitServiceFailureMode(logger),
Timeout: ptr.To(RatelimitServiceTimeout()),
},
},
Expand Down
13 changes: 9 additions & 4 deletions tests/envoygateway/extension_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"strings"
"time"

"github.com/go-logr/logr"

egv1alpha1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/kuadrant/policy-machinery/controller"
"github.com/kuadrant/policy-machinery/machinery"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -37,6 +40,7 @@ var _ = Describe("wasm controller", func() {
gwHost = fmt.Sprintf("*.toystore-%s.com", rand.String(4))
gatewayClass *gatewayapiv1.GatewayClass
gateway *gatewayapiv1.Gateway
logger logr.Logger
)

BeforeEach(func(ctx SpecContext) {
Expand All @@ -49,6 +53,7 @@ var _ = Describe("wasm controller", func() {
Gateway
err = testClient().Create(ctx, gateway)
Expect(err).ToNot(HaveOccurred())
logger = controller.LoggerFromContext(ctx).WithName("EnvoyExtensionReconcilerTest")

Eventually(tests.GatewayIsReady(ctx, testClient(), gateway)).WithContext(ctx).Should(BeTrue())
})
Expand Down Expand Up @@ -171,13 +176,13 @@ var _ = Describe("wasm controller", func() {
wasm.AuthServiceName: {
Type: wasm.AuthServiceType,
Endpoint: kuadrant.KuadrantAuthClusterName,
FailureMode: wasm.AuthServiceFailureMode(),
FailureMode: wasm.AuthServiceFailureMode(&logger),
Timeout: ptr.To(wasm.AuthServiceTimeout()),
},
wasm.RateLimitServiceName: {
Type: wasm.RateLimitServiceType,
Endpoint: kuadrant.KuadrantRateLimitClusterName,
FailureMode: wasm.RatelimitServiceFailureMode(),
FailureMode: wasm.RatelimitServiceFailureMode(&logger),
Timeout: ptr.To(wasm.RatelimitServiceTimeout()),
},
},
Expand Down Expand Up @@ -337,13 +342,13 @@ var _ = Describe("wasm controller", func() {
wasm.AuthServiceName: {
Type: wasm.AuthServiceType,
Endpoint: kuadrant.KuadrantAuthClusterName,
FailureMode: wasm.AuthServiceFailureMode(),
FailureMode: wasm.AuthServiceFailureMode(&logger),
Timeout: ptr.To(wasm.AuthServiceTimeout()),
},
wasm.RateLimitServiceName: {
Type: wasm.RateLimitServiceType,
Endpoint: kuadrant.KuadrantRateLimitClusterName,
FailureMode: wasm.RatelimitServiceFailureMode(),
FailureMode: wasm.RatelimitServiceFailureMode(&logger),
Timeout: ptr.To(wasm.RatelimitServiceTimeout()),
},
},
Expand Down
Loading

0 comments on commit b70d785

Please sign in to comment.