Skip to content

Commit

Permalink
Add Kourier LoadBalancerIP Config
Browse files Browse the repository at this point in the history
  • Loading branch information
theomessin committed Jun 10, 2023
1 parent ad01c40 commit 566614e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 31 deletions.
2 changes: 2 additions & 0 deletions config/crd/bases/operator.knative.dev_knativeservings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,8 @@ spec:
type: boolean
service-type:
type: string
service-loadBalancerIP:
type: string
bootstrap-configmap:
type: string
type: object
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/operator/base/ingressconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type KourierIngressConfiguration struct {
// ServiceType specifies the service type for kourier gateway.
ServiceType v1.ServiceType `json:"service-type,omitempty"`

// ServiceLoadBalancerIP specifies the service load balancer IP.
ServiceLoadBalancerIP string `json:"service-loadBalancerIP,omitempty"`

// BootstrapConfigmapName specifies the ConfigMap name which contains envoy bootstrap.
BootstrapConfigmapName string `json:"bootstrap-configmap,omitempty"`
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/knativeserving/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestTransformers(t *testing.T) {
},
},
},
expected: 3,
expected: 4,
}, {
name: "Available contour ingress",
instance: servingv1beta1.KnativeServing{
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestTransformers(t *testing.T) {
},
},
},
expected: 4,
expected: 5,
}}

for _, tt := range tests {
Expand Down
23 changes: 23 additions & 0 deletions pkg/reconciler/knativeserving/ingress/kourier.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func kourierTransformers(ctx context.Context, instance *v1beta1.KnativeServing)
return []mf.Transformer{
replaceGWNamespace(),
configureGWServiceType(instance),
configureGWServiceLoadBalancerIP(instance),
configureBootstrapConfigMap(instance),
}
}
Expand Down Expand Up @@ -105,6 +106,28 @@ func configureGWServiceType(instance *v1beta1.KnativeServing) mf.Transformer {
}
}

// configureGWServiceLoadBalancerIP configures Kourier GW's service loadBalancerIP.
func configureGWServiceLoadBalancerIP(instance *v1beta1.KnativeServing) mf.Transformer {
return func(u *unstructured.Unstructured) error {
if u.GetKind() == "Service" && u.GetName() == kourierGatewayServiceName {
if instance.Spec.Ingress.Kourier.ServiceLoadBalancerIP == "" {
// Do nothing if ServiceLoadBalancerIP is not configured.
return nil
}
svc := &v1.Service{}
if err := scheme.Scheme.Convert(u, svc, nil); err != nil {
return err
}

svc.Spec.LoadBalancerIP = instance.Spec.Ingress.Kourier.ServiceLoadBalancerIP
if err := scheme.Scheme.Convert(svc, u, nil); err != nil {
return err
}
}
return nil
}
}

// configureBootstrapConfigMap sets Kourier GW's bootstrap configmap name.
func configureBootstrapConfigMap(instance *v1beta1.KnativeServing) mf.Transformer {
return func(u *unstructured.Unstructured) error {
Expand Down
87 changes: 58 additions & 29 deletions pkg/reconciler/knativeserving/ingress/kourier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

const servingNamespace = "knative-serving"

func servingInstance(ns string, serviceType v1.ServiceType, bootstrapConfigmapName string) *servingv1beta1.KnativeServing {
func servingInstance(ns string, serviceType v1.ServiceType, bootstrapConfigmapName string, serviceLoadBalancerIP string) *servingv1beta1.KnativeServing {
return &servingv1beta1.KnativeServing{
ObjectMeta: metav1.ObjectMeta{
Name: "test-instance",
Expand All @@ -45,6 +45,7 @@ func servingInstance(ns string, serviceType v1.ServiceType, bootstrapConfigmapNa
Kourier: base.KourierIngressConfiguration{
Enabled: true,
ServiceType: serviceType,
ServiceLoadBalancerIP: serviceLoadBalancerIP,
BootstrapConfigmapName: bootstrapConfigmapName,
},
},
Expand All @@ -54,38 +55,50 @@ func servingInstance(ns string, serviceType v1.ServiceType, bootstrapConfigmapNa

func TestTransformKourierManifest(t *testing.T) {
tests := []struct {
name string
instance *servingv1beta1.KnativeServing
expNamespace string
expServiceType string
expConfigMapName string
expError error
name string
instance *servingv1beta1.KnativeServing
expNamespace string
expServiceType string
expServiceLoadBalancerIP string
expConfigMapName string
expError error
}{{
name: "Replaces Kourier Gateway Namespace, ServiceType and bootstrap cm",
instance: servingInstance(servingNamespace, "ClusterIP", "my-bootstrap"),
expNamespace: servingNamespace,
expConfigMapName: "my-bootstrap",
expServiceType: "ClusterIP",
name: "Replaces Kourier Gateway Namespace, ServiceType and bootstrap cm",
instance: servingInstance(servingNamespace, "ClusterIP", "my-bootstrap", ""),
expNamespace: servingNamespace,
expConfigMapName: "my-bootstrap",
expServiceType: "ClusterIP",
expServiceLoadBalancerIP: "",
}, {
name: "Use Kourier default service type",
instance: servingInstance(servingNamespace, "" /* empty service type */, ""),
expNamespace: servingNamespace,
expConfigMapName: kourierDefaultVolumeName,
expServiceType: "LoadBalancer", // kourier GW default service type
name: "Use Kourier default service type",
instance: servingInstance(servingNamespace, "" /* empty service type */, "", ""),
expNamespace: servingNamespace,
expConfigMapName: kourierDefaultVolumeName,
expServiceType: "LoadBalancer", // kourier GW default service type
expServiceLoadBalancerIP: "",
}, {
name: "Use unsupported service type",
instance: servingInstance(servingNamespace, "ExternalName", ""),
expNamespace: servingNamespace,
expServiceType: "ExternalName",
expConfigMapName: kourierDefaultVolumeName,
expError: fmt.Errorf("unsupported service type \"ExternalName\""),
name: "Sets Kourier Gateway ServiceLoadBalancerIP",
instance: servingInstance(servingNamespace, "" /* empty service type */, "", "1.2.3.4"),
expNamespace: servingNamespace,
expConfigMapName: kourierDefaultVolumeName,
expServiceType: "LoadBalancer",
expServiceLoadBalancerIP: "1.2.3.4",
}, {
name: "Use unknown service type",
instance: servingInstance(servingNamespace, "Foo", ""),
expNamespace: servingNamespace,
expServiceType: "Foo",
expConfigMapName: kourierDefaultVolumeName,
expError: fmt.Errorf("unknown service type \"Foo\""),
name: "Use unsupported service type",
instance: servingInstance(servingNamespace, "ExternalName", "", ""),
expNamespace: servingNamespace,
expServiceType: "ExternalName",
expConfigMapName: kourierDefaultVolumeName,
expError: fmt.Errorf("unsupported service type \"ExternalName\""),
expServiceLoadBalancerIP: "",
}, {
name: "Use unknown service type",
instance: servingInstance(servingNamespace, "Foo", "", ""),
expNamespace: servingNamespace,
expServiceType: "Foo",
expConfigMapName: kourierDefaultVolumeName,
expError: fmt.Errorf("unknown service type \"Foo\""),
expServiceLoadBalancerIP: "",
}}

for _, tt := range tests {
Expand Down Expand Up @@ -113,9 +126,15 @@ func TestTransformKourierManifest(t *testing.T) {
t.Fatalf("Failed to transform manifest: %v", err)
}

manifest, err = manifest.Transform(configureGWServiceLoadBalancerIP(tt.instance))
if err != nil {
t.Fatalf("Failed to transform manifest: %v", err)
}

for _, u := range manifest.Resources() {
verifyControllerNamespace(t, &u, tt.expNamespace)
verifyGatewayServiceType(t, &u, tt.expServiceType)
verifyGatewayServiceLoadBalancerIP(t, &u, tt.expServiceLoadBalancerIP)
verifyBootstrapVolumeName(t, &u, tt.expConfigMapName)
}
})
Expand Down Expand Up @@ -158,6 +177,16 @@ func verifyGatewayServiceType(t *testing.T, u *unstructured.Unstructured, expSer
}
}

func verifyGatewayServiceLoadBalancerIP(t *testing.T, u *unstructured.Unstructured, expServiceLoadBalancerIP string) {
if u.GetKind() == "Service" && u.GetName() == kourierGatewayServiceName {
svc := &v1.Service{}
err := scheme.Scheme.Convert(u, svc, nil)
util.AssertEqual(t, err, nil)
svcLoadBalancerIP := svc.Spec.LoadBalancerIP
util.AssertDeepEqual(t, string(svcLoadBalancerIP), expServiceLoadBalancerIP)
}
}

// removeProviderLabels removes labels. This util is used for tests without provider label.
func removeLabels() mf.Transformer {
return func(u *unstructured.Unstructured) error {
Expand Down

0 comments on commit 566614e

Please sign in to comment.