Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{{- if .Values.topologyInjector.enabled }}

{{ $watchedNamespaces := list }}
{{ $gatewayNamespaceMode := false}}
{{- if .Values.config.envoyGateway.provider.kubernetes }}
{{- $kube := .Values.config.envoyGateway.provider.kubernetes }}
{{- $gatewayNamespaceMode = and ($kube.deploy) (eq $kube.deploy.type "GatewayNamespace") }}
{{- if $kube.watch }}
{{- if $kube.watch.namespaces }}
{{- if gt (len $kube.watch.namespaces) 0 }}
{{- $watchedNamespaces = $kube.watch.namespaces }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
Expand Down Expand Up @@ -28,10 +43,21 @@ webhooks:
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods/binding"]
{{- if not $gatewayNamespaceMode }}
namespaceSelector:
matchExpressions:
- key: kubernetes.io/metadata.name
operator: In
values:
- {{ .Release.Namespace }}
{{- else if gt (len $watchedNamespaces) 0 }}
namespaceSelector:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is an issue with this approach, with multiple envoy-gateway running in 1 cluster, there's no way to decide which special web hook server to call

matchExpressions:
- key: kubernetes.io/metadata.name
operator: In
values:
{{- range $watchedNamespaces }}
- {{ . | quote }}
{{- end }}
{{- end }}
{{- end }}
1 change: 1 addition & 0 deletions internal/provider/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
mgr.GetWebhookServer().Register("/inject-pod-topology", &webhook.Admission{
Handler: &ProxyTopologyInjector{
Client: mgr.GetClient(),
Logger: svrCfg.Logger.WithName("proxy-topology-injector"),

Check warning on line 130 in internal/provider/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/kubernetes.go#L130

Added line #L130 was not covered by tests
Decoder: admission.NewDecoder(mgr.GetScheme()),
},
})
Expand Down
23 changes: 17 additions & 6 deletions internal/provider/kubernetes/topology_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/envoyproxy/gateway/internal/gatewayapi"
"github.com/envoyproxy/gateway/internal/logging"
"github.com/envoyproxy/gateway/internal/metrics"
)

type ProxyTopologyInjector struct {
client.Client
Decoder admission.Decoder

Logger logging.Logger
}

func (m *ProxyTopologyInjector) Handle(ctx context.Context, req admission.Request) admission.Response {
logger := m.Logger
logger.V(1).Info("receive injector request", "request", req)

binding := &corev1.Binding{}
if err := m.Decoder.Decode(req, binding); err != nil {
klog.Error(err, "decoding binding failed", "request.ObjectKind", req.Object.Object.GetObjectKind())
logger.Error(err, "decoding binding failed", "request.ObjectKind", req.Object.Object.GetObjectKind())

Check warning on line 36 in internal/provider/kubernetes/topology_injector.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/topology_injector.go#L36

Added line #L36 was not covered by tests
topologyInjectorEventsTotal.WithFailure(metrics.ReasonError).Increment()
return admission.Allowed("internal error, skipped")
}
Expand All @@ -45,43 +50,49 @@

pod := &corev1.Pod{}
if err := m.Get(ctx, podName, pod); err != nil {
klog.Error(err, "get pod failed", "pod", podName.String())
logger.Error(err, "get pod failed", "pod", podName.String())

Check warning on line 53 in internal/provider/kubernetes/topology_injector.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/topology_injector.go#L53

Added line #L53 was not covered by tests
topologyInjectorEventsTotal.WithFailure(metrics.ReasonError).Increment()
return admission.Allowed("internal error, skipped")
}

// Skip non-proxy pods
if !hasEnvoyProxyLabels(pod.Labels) {
klog.V(1).Info("skipping pod due to missing labels", "pod", podName)
logger.V(1).Info("skipping pod due to missing labels", "pod", podName)

Check warning on line 60 in internal/provider/kubernetes/topology_injector.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/topology_injector.go#L60

Added line #L60 was not covered by tests
topologyInjectorEventsTotal.WithStatus(statusNoAction).Increment()
return admission.Allowed("skipped")
}

logger = logger.WithValues("pod", podName)

nodeName := types.NamespacedName{
Name: binding.Target.Name,
}
node := &corev1.Node{}
if err := m.Get(ctx, nodeName, node); err != nil {
klog.Error(err, "get node failed", "node", node.Name)
logger.Error(err, "get node failed", "node", node.Name)

Check warning on line 72 in internal/provider/kubernetes/topology_injector.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/topology_injector.go#L72

Added line #L72 was not covered by tests

topologyInjectorEventsTotal.WithFailure(metrics.ReasonError).Increment()
return admission.Allowed("internal error, skipped")
}
logger = logger.WithValues("node", node)

if zone, ok := node.Labels[corev1.LabelTopologyZone]; ok {
if binding.Annotations == nil {
binding.Annotations = map[string]string{}
}
binding.Annotations[corev1.LabelTopologyZone] = zone
} else {
logger.V(1).Info("Skipping injection due to missing topology label on node")

Check warning on line 85 in internal/provider/kubernetes/topology_injector.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/topology_injector.go#L85

Added line #L85 was not covered by tests
return admission.Allowed("Skipping injection due to missing topology label on node")
}

marshaledBinding, err := json.Marshal(binding)
if err != nil {
klog.Errorf("failed to marshal Pod Binding: %v", err)
logger.Error(err, "failed to marshal Pod Binding")

Check warning on line 91 in internal/provider/kubernetes/topology_injector.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/topology_injector.go#L91

Added line #L91 was not covered by tests
return admission.Allowed(fmt.Sprintf("failed to marshal binding, skipped: %v", err))
}

topologyInjectorEventsTotal.WithSuccess().Increment()
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledBinding)
}

Expand Down
1 change: 0 additions & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func TestE2E(t *testing.T) {
skipTests = append(skipTests,
tests.HTTPWasmTest.ShortName,
tests.OCIWasmTest.ShortName,
tests.ZoneAwareRoutingTest.ShortName,

// Skip RateLimit tests because they are not supported in GatewayNamespaceMode
tests.RateLimitCIDRMatchTest.ShortName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,4 +1010,5 @@ webhooks:
- key: kubernetes.io/metadata.name
operator: In
values:
- envoy-gateway-system
- "default"
- "app-ns"
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,3 @@ webhooks:
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods/binding"]
namespaceSelector:
matchExpressions:
- key: kubernetes.io/metadata.name
operator: In
values:
- envoy-gateway-system
2 changes: 1 addition & 1 deletion tools/make/helm.mk
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ helm-generate.%:
envsubst < charts/${CHART_NAME}/values.tmpl.yaml > ./charts/${CHART_NAME}/values.yaml; \
fi
helm dependency update charts/${CHART_NAME}
helm lint charts/${CHART_NAME}
helm lint charts/${CHART_NAME} || exit 1

# The jb does not support self-assigned jsonnetfile, so entering working dir before executing jb.
@if [ ${CHART_NAME} == "gateway-addons-helm" ]; then \
Expand Down