Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting FailureMode from ENV #1030

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 29 additions & 3 deletions pkg/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
"encoding/json"
"errors"
"fmt"
"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 @@ -30,27 +33,50 @@
return env.GetString("AUTH_SERVICE_TIMEOUT", "200ms")
}

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(logger *logr.Logger) FailureModeType {
return parseFailureModeValue("RATELIMIT_SERVICE_FAILURE_MODE", FailureModeAllow, logger)
}

func parseFailureModeValue(envVarName string, defaultValue FailureModeType, logger *logr.Logger) FailureModeType {
value := os.Getenv(envVarName)
if value == "" {
return defaultValue
}

switch value {
case string(FailureModeAllow), string(FailureModeDeny):
return FailureModeType(value)
default:
logger.Info("Warning: Invalid FailureMode value '%s' for %s. Using default value '%s'.\n", value, envVarName, defaultValue)
return defaultValue

Check warning on line 59 in pkg/wasm/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/wasm/utils.go#L54-L59

Added lines #L54 - L59 were not covered by tests
}
}

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: FailureModeDeny,
FailureMode: AuthServiceFailureMode(logger),
Timeout: ptr.To(AuthServiceTimeout()),
},
RateLimitServiceName: {
Type: RateLimitServiceType,
Endpoint: kuadrant.KuadrantRateLimitClusterName,
FailureMode: FailureModeAllow,
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.FailureModeDeny,
FailureMode: wasm.AuthServiceFailureMode(&logger),
Timeout: ptr.To(wasm.AuthServiceTimeout()),
},
wasm.RateLimitServiceName: {
Type: wasm.RateLimitServiceType,
Endpoint: kuadrant.KuadrantRateLimitClusterName,
FailureMode: wasm.FailureModeAllow,
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.FailureModeDeny,
FailureMode: wasm.AuthServiceFailureMode(&logger),
Timeout: ptr.To(wasm.AuthServiceTimeout()),
},
wasm.RateLimitServiceName: {
Type: wasm.RateLimitServiceType,
Endpoint: kuadrant.KuadrantRateLimitClusterName,
FailureMode: wasm.FailureModeAllow,
FailureMode: wasm.RatelimitServiceFailureMode(&logger),
Timeout: ptr.To(wasm.RatelimitServiceTimeout()),
},
},
Expand Down
Loading
Loading