Skip to content

Commit

Permalink
feat: enable templating in static GW addrs
Browse files Browse the repository at this point in the history
  • Loading branch information
shaneutt committed Sep 18, 2023
1 parent 5bed656 commit 0452c86
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
15 changes: 6 additions & 9 deletions conformance/tests/gateway-static-addresses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ metadata:
spec:
gatewayClassName: "{GATEWAY_CLASS_NAME}"
addresses:
# This address is valid for its type, but the underlying implementation
# can not actually support it, so it should result in Programmed False.
# Until removed or fixed.
- type: IPAddress
value: "1.1.1.1" # TODO: template
# This is a valid IP address that the underlying implementation will
# accept and can be bound to the Gateway.
- type: IPAddress
value: "172.18.0.242" # TODO: template
# This indicates an address that is known to not be usable by the
# implementation and will be substituted with user provided types and values.
- value: "PLACEHOLDER_UNUSABLE_ADDRS"
# This indicates an address that is known to be usable by the implementation
# and will be substituted with user provided types and values.
- value: "PLACEHOLDER_USABLE_ADDRS"
listeners:
- name: http
port: 8080
Expand Down
36 changes: 35 additions & 1 deletion conformance/utils/kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/gateway-api/apis/v1beta1"
"sigs.k8s.io/gateway-api/conformance/utils/config"
)

Expand All @@ -51,12 +52,45 @@ type Applier struct {

// FS is the filesystem to use when reading manifests.
FS embed.FS

// UsableNetworkAddresses is an optional pool of usable addresses for
// Gateways for tests which need to test manual address assignments.
UsableNetworkAddresses []v1beta1.GatewayAddress

// UnusableNetworkAddresses is an optional pool of unusable addresses for
// Gateways for tests which need to test failures with manual Gateway
// address assignment.
UnusableNetworkAddresses []v1beta1.GatewayAddress
}

// prepareGateway adjusts the gatewayClassName.
func (a Applier) prepareGateway(t *testing.T, uObj *unstructured.Unstructured) {
ns := uObj.GetNamespace()
name := uObj.GetName()

err := unstructured.SetNestedField(uObj.Object, a.GatewayClass, "spec", "gatewayClassName")
require.NoErrorf(t, err, "error setting `spec.gatewayClassName` on %s Gateway resource", uObj.GetName())
require.NoErrorf(t, err, "error setting `spec.gatewayClassName` on Gateway %s/%s", ns, name)

rawAddrs, hasStaticAddrs, err := unstructured.NestedFieldCopy(uObj.Object, "spec", "addresses")
require.NoError(t, err, "error retrieving spec.addresses to verify if any static addresses were present on Gateway resource %s/%s", ns, name)

if hasStaticAddrs {
addrs, ok := rawAddrs.([]v1beta1.GatewayAddress)
require.True(t, ok)
require.NotEmpty(t, addrs, "a test called for static Gateway network addresses, but none were provided")

var overlayAddrs []v1beta1.GatewayAddress
for _, addr := range addrs {
if addr.Value == "PLACEHOLDER_USABLE_ADDRS" {
overlayAddrs = append(overlayAddrs, a.UsableNetworkAddresses...)
} else if addr.Value == "PLACEHOLDER_UNUSABLE_ADDRS" {
overlayAddrs = append(overlayAddrs, a.UnusableNetworkAddresses...)
}
}

err = unstructured.SetNestedField(uObj.Object, overlayAddrs, "spec", "addresses")
require.NoError(t, err, "could not overlay static addresses on Gateway %s/%s", ns, name)
}
}

// prepareGatewayClass adjust the spec.controllerName on the resource
Expand Down
16 changes: 14 additions & 2 deletions conformance/utils/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/gateway-api/apis/v1beta1"
"sigs.k8s.io/gateway-api/conformance"
"sigs.k8s.io/gateway-api/conformance/utils/config"
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
Expand Down Expand Up @@ -78,6 +79,15 @@ type Options struct {
SkipTests []string

FS *embed.FS

// UsableNetworkAddresses is an optional pool of usable addresses for
// Gateways for tests which need to test manual address assignments.
UsableNetworkAddresses []v1beta1.GatewayAddress

// UnusableNetworkAddresses is an optional pool of unusable addresses for
// Gateways for tests which need to test failures with manual Gateway
// address assignment.
UnusableNetworkAddresses []v1beta1.GatewayAddress
}

// New returns a new ConformanceTestSuite.
Expand Down Expand Up @@ -119,8 +129,10 @@ func New(s Options) *ConformanceTestSuite {
BaseManifests: s.BaseManifests,
MeshManifests: s.MeshManifests,
Applier: kubernetes.Applier{
NamespaceLabels: s.NamespaceLabels,
NamespaceAnnotations: s.NamespaceAnnotations,
NamespaceLabels: s.NamespaceLabels,
NamespaceAnnotations: s.NamespaceAnnotations,
UsableNetworkAddresses: s.UnusableNetworkAddresses,
UnusableNetworkAddresses: s.UnusableNetworkAddresses,
},
SupportedFeatures: s.SupportedFeatures,
TimeoutConfig: s.TimeoutConfig,
Expand Down

0 comments on commit 0452c86

Please sign in to comment.