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

feat: add option to bypass mutation using a label on the namespace #39

Merged
merged 1 commit into from
Nov 26, 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
18 changes: 17 additions & 1 deletion internal/webhook/route_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ type RouteMutator struct {
Client client.Client
}

const clusterIngressName = "cluster"
const (
clusterIngressName = "cluster"
bypassLabel = "haproxy.router.dana.io/bypass-env-mutation"
)

// +kubebuilder:rbac:groups="route.openshift.io",resources=routes,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups="config.openshift.io",resources=ingresses,verbs=get;list;watch
Expand Down Expand Up @@ -68,6 +71,10 @@ func (r *RouteMutator) Handle(ctx context.Context, req admission.Request) admiss
// handleInner implements the main mutating logic. It modifies the host of an OpenShift Route
// based on environment data and cluster ingress information.
func (r *RouteMutator) handleInner(logger logr.Logger, route *routev1.Route, clusterIngress string, environments []string, labels map[string]string) {
if checkBypass(labels) {
logger.Info("Bypassing mutation")
return
}
for _, env := range environments {
if labels[environment.Key] == env {
routeHost := route.Spec.Host
Expand Down Expand Up @@ -97,3 +104,12 @@ func (r *RouteMutator) getClusterIngressDomain(ctx context.Context) (string, err
}
return ingress.Spec.Domain, nil
}

// checkBypass checks if the namespace has the bypass mutation label.
func checkBypass(labels map[string]string) bool {
if val, ok := labels[bypassLabel]; ok && val == "true" {
return true
}

return false
}
2 changes: 2 additions & 0 deletions internal/webhook/route_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func TestRouteMutator(t *testing.T) {
{name: "routeWithCustomNameDefaultDomain", namespace: testNamespace, hostname: "test2", customDomain: "", defaultDomain: true, nsLabels: map[string]string{environment.Key: env1}, mutated: true},
{name: "routeWithNoCustomNameNoDomain", namespace: testNamespace, hostname: "", customDomain: "", defaultDomain: false, nsLabels: map[string]string{environment.Key: env1}, mutated: true},
{name: "routeWithoutLabels", namespace: testNamespace, hostname: "test5", customDomain: "", defaultDomain: true, nsLabels: map[string]string{}, mutated: false},
{name: "routeWithBypassLabel", namespace: testNamespace, hostname: "test6", customDomain: "", defaultDomain: true, nsLabels: map[string]string{bypassLabel: "true", environment.Key: env1}, mutated: false},
{name: "routeWithInvalidBypassLabel", namespace: testNamespace, hostname: "test7", customDomain: "", defaultDomain: true, nsLabels: map[string]string{bypassLabel: "false", environment.Key: env1}, mutated: true},
}

client := testclient.NewClientBuilder().WithScheme(scheme.Scheme).Build()
Expand Down