Skip to content

Commit

Permalink
conformance: enable HTTPRouteRedirectPortAndScheme (#1601)
Browse files Browse the repository at this point in the history
* add logic for final redirect port derived

Signed-off-by: Shawnh2 <[email protected]>

* fix unit test

Signed-off-by: Shawnh2 <[email protected]>

* resolve conflicts

Signed-off-by: sh2 <[email protected]>

* change hash sum and unify ths format of nsName

Signed-off-by: sh2 <[email protected]>

* update unit test

Signed-off-by: sh2 <[email protected]>

* fix test

Signed-off-by: sh2 <[email protected]>

* optimize scheme check and fix slash pos

Signed-off-by: sh2 <[email protected]>

---------

Signed-off-by: Shawnh2 <[email protected]>
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 authored Aug 11, 2023
1 parent c0a619c commit f466430
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 46 deletions.
18 changes: 18 additions & 0 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,24 @@ func (t *Translator) processHTTPRouteParentRefListener(route RouteContext, route
}

for _, routeRoute := range routeRoutes {
// If the redirect port is not set, the final redirect port must be derived.
if routeRoute.Redirect != nil && routeRoute.Redirect.Port == nil {
redirectPort := uint32(listener.Port)
// If redirect scheme is not-empty, the redirect post must be the
// well-known port associated with the redirect scheme.
if scheme := routeRoute.Redirect.Scheme; scheme != nil {
switch strings.ToLower(*scheme) {
case "http":
redirectPort = 80
case "https":
redirectPort = 443
}
}
// If the redirect scheme does not have a well-known port, or
// if the redirect scheme is empty, the redirect port must be the Gateway Listener port.
routeRoute.Redirect.Port = &redirectPort
}

hostRoute := &ir.HTTPRoute{
Name: fmt.Sprintf("%s-%s", routeRoute.Name, host),
PathMatch: routeRoute.PathMatch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@ xdsIR:
path:
fullReplace: /redirected
prefixMatchReplace: null
port: null
port: 443
scheme: https
statusCode: 301
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ xdsIR:
redirect:
hostname: redirected.com
path: null
port: null
port: 443
scheme: https
statusCode: 301
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
data:
xds-certificate.json: '{"resources":[{"@type":"type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.Secret","name":"xds_certificate","tls_certificate":{"certificate_chain":{"filename":"/certs/tls.crt"},"private_key":{"filename":"/certs/tls.key"}}}]}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 1
Expand Down Expand Up @@ -84,7 +84,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
volumes:
- name: certs
Expand All @@ -98,7 +98,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 1
Expand Down Expand Up @@ -85,7 +85,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
volumes:
- name: certs
Expand All @@ -99,7 +99,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 2
Expand Down Expand Up @@ -194,7 +194,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
securityContext:
runAsUser: 1000
Expand All @@ -210,7 +210,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 2
Expand Down Expand Up @@ -192,7 +192,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
securityContext:
runAsUser: 1000
Expand All @@ -208,7 +208,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 1
Expand Down Expand Up @@ -185,7 +185,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
volumes:
- name: certs
Expand All @@ -199,7 +199,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 1
Expand Down Expand Up @@ -214,7 +214,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
volumes:
- name: certs
Expand All @@ -228,7 +228,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 2
Expand Down Expand Up @@ -196,7 +196,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
securityContext:
runAsUser: 1000
Expand All @@ -212,7 +212,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 2
Expand Down Expand Up @@ -196,7 +196,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
securityContext:
runAsUser: 1000
Expand All @@ -212,7 +212,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
replicas: 1
Expand Down Expand Up @@ -85,7 +85,7 @@ spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
serviceAccountName: envoy-default-64656661
serviceAccountName: envoy-default-37a8eec1
terminationGracePeriodSeconds: 300
volumes:
- name: certs
Expand All @@ -99,7 +99,7 @@ spec:
path: xds-trusted-ca.json
- key: xds-certificate.json
path: xds-certificate.json
name: envoy-default-64656661
name: envoy-default-37a8eec1
optional: false
name: sds
revisionHistoryLimit: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
ports:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-64656661
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
externalTrafficPolicy: Local
Expand Down
4 changes: 2 additions & 2 deletions internal/infrastructure/kubernetes/proxy_configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestCreateOrUpdateProxyConfigMap(t *testing.T) {
expect: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: cfg.Namespace,
Name: "envoy-test-74657374",
Name: "envoy-test-9f86d081",
Labels: map[string]string{
"app.kubernetes.io/name": "envoy",
"app.kubernetes.io/component": "proxy",
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestCreateOrUpdateProxyConfigMap(t *testing.T) {
expect: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: cfg.Namespace,
Name: "envoy-test-74657374",
Name: "envoy-test-9f86d081",
Labels: map[string]string{
"app.kubernetes.io/name": "envoy",
"app.kubernetes.io/component": "proxy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestCreateOrUpdateProxyServiceAccount(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "envoy-test-74657374",
Name: "envoy-test-9f86d081",
Labels: map[string]string{
"app.kubernetes.io/name": "envoy",
"app.kubernetes.io/component": "proxy",
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestCreateOrUpdateProxyServiceAccount(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "envoy-test-74657374",
Name: "envoy-test-9f86d081",
Labels: map[string]string{
"app.kubernetes.io/name": "envoy",
"app.kubernetes.io/component": "proxy",
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestCreateOrUpdateProxyServiceAccount(t *testing.T) {
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "envoy-very-long-name-that-will-be-hashed-and-cut-off-b-76657279",
Name: "envoy-very-long-name-that-will-be-hashed-and-cut-off-b-5bacc75e",
Labels: map[string]string{
"app.kubernetes.io/name": "envoy",
"app.kubernetes.io/component": "proxy",
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/kubernetes/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ func refsSecret(ref *gwapiv1b1.SecretObjectReference) bool {
}

func infraServiceName(gateway *gwapiv1b1.Gateway) string {
infraName := utils.GetHashedName(fmt.Sprintf("%s-%s", gateway.Namespace, gateway.Name))
infraName := utils.GetHashedName(fmt.Sprintf("%s/%s", gateway.Namespace, gateway.Name))
return fmt.Sprintf("%s-%s", config.EnvoyPrefix, infraName)
}

func infraDeploymentName(gateway *gwapiv1b1.Gateway) string {
infraName := utils.GetHashedName(fmt.Sprintf("%s-%s", gateway.Namespace, gateway.Name))
infraName := utils.GetHashedName(fmt.Sprintf("%s/%s", gateway.Namespace, gateway.Name))
return fmt.Sprintf("%s-%s", config.EnvoyPrefix, infraName)
}

Expand Down
7 changes: 4 additions & 3 deletions internal/provider/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ func NamespacedName(obj client.Object) types.NamespacedName {
}
}

// GetHashedName returns a partially hashed name for the string including up to 48 characters of the original name before the hash
// GetHashedName returns a partially hashed name for the string including up to 48 characters of the original name before the hash.
// Input `nsName` should be formatted as `{Namespace}/{ResourceName}`.
func GetHashedName(nsName string) string {

h := sha256.New() // Using sha256 instead of sha1 due to Blocklisted import crypto/sha1: weak cryptographic primitive (gosec)
hSum := h.Sum([]byte(nsName))
hashedName := strings.ToLower(fmt.Sprintf("%x", hSum))
h.Write([]byte(nsName))
hashedName := strings.ToLower(fmt.Sprintf("%x", h.Sum(nil)))

// replace `/` with `-` to create a valid K8s resource name
resourceName := strings.ReplaceAll(nsName, "/", "-")
Expand Down
Loading

0 comments on commit f466430

Please sign in to comment.