Skip to content

Commit 78678ff

Browse files
DCchoudhury15Vincent
andauthored
fix: respect namespace in structured local-gateways config (#2217)
* fix: respect namespace in structured local-gateways config Signed-off-by: DCchoudhury15 <divyanshuchoudhury3@gmail.com> * Update pkg/reconciler/knativeserving/ingress/ingress_service.go Co-authored-by: Vincent <shou73@bloomberg.net> * refactor: breakdown UpdateNamespace logic as requested --------- Signed-off-by: DCchoudhury15 <divyanshuchoudhury3@gmail.com> Co-authored-by: Vincent <shou73@bloomberg.net>
1 parent 70d1cc0 commit 78678ff

2 files changed

Lines changed: 117 additions & 20 deletions

File tree

pkg/reconciler/knativeserving/ingress/ingress_service.go

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,28 @@ import (
2323
corev1 "k8s.io/api/core/v1"
2424
"k8s.io/client-go/kubernetes/scheme"
2525
"knative.dev/operator/pkg/apis/operator/base"
26+
"sigs.k8s.io/yaml"
2627

2728
mf "github.com/manifestival/manifestival"
2829
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2930
"knative.dev/operator/pkg/apis/operator/v1beta1"
3031
)
3132

33+
// localGateway defines the structure for the entries in the 'local-gateways' array.
34+
type localGatewayConfig struct {
35+
Name string `json:"name"`
36+
Namespace string `json:"namespace"`
37+
Service string `json:"service"`
38+
}
39+
3240
// IngressServiceTransform pins the namespace to istio-system for the service named knative-local-gateway.
3341
// It also removes the OwnerReference to the operator, as they are in different namespaces, which is
3442
// invalid in Kubernetes 1.20+.
3543
func IngressServiceTransform(ks *v1beta1.KnativeServing) mf.Transformer {
3644
return func(u *unstructured.Unstructured) error {
3745
if u.GetAPIVersion() == "v1" && u.GetKind() == "Service" {
3846
if u.GetName() == "knative-local-gateway" {
47+
// Default to istio-system, then override if config exists
3948
u.SetNamespace("istio-system")
4049
u.SetOwnerReferences(nil)
4150
config := ks.GetSpec().GetConfig()
@@ -74,15 +83,58 @@ func updateIstioService(ks *v1beta1.KnativeServing, u *unstructured.Unstructured
7483
return nil
7584
}
7685

86+
var (
87+
knativeLocalGateway = "knative-local-gateway"
88+
localGateways = "local-gateways"
89+
)
90+
7791
// UpdateNamespace set correct namespace of istio to the service knative-local-gateway
78-
func UpdateNamespace(u *unstructured.Unstructured, data map[string]string, ns string) {
79-
key := fmt.Sprintf("%s.%s.%s", "local-gateway", ns, "knative-local-gateway")
80-
if val, ok := data[key]; ok {
81-
fields := strings.Split(val, ".")
82-
// The value is in the format of knative-local-gateway.{istio-namespace}.svc.cluster.local
83-
// The second item is the istio namespace
84-
if len(fields) >= 2 {
85-
u.SetNamespace(fields[1])
92+
func UpdateNamespace(u *unstructured.Unstructured, data map[string]string, namespace string) {
93+
if ns := resolveGatewayNamespace(data, namespace); ns != "" {
94+
u.SetNamespace(ns)
95+
}
96+
}
97+
98+
func resolveGatewayNamespace(data map[string]string, ns string) string {
99+
if ns := fromStructuredGateways(data); ns != "" {
100+
return ns
101+
}
102+
103+
return fromLegacyGateway(data, ns)
104+
}
105+
106+
func fromStructuredGateways(data map[string]string) string {
107+
raw, ok := data[localGateways]
108+
if !ok {
109+
return ""
110+
}
111+
112+
var gateways []localGatewayConfig
113+
if err := yaml.Unmarshal([]byte(raw), &gateways); err != nil {
114+
return ""
115+
}
116+
117+
for _, gw := range gateways {
118+
if gw.Name == knativeLocalGateway {
119+
return gw.Namespace
86120
}
87121
}
122+
123+
return ""
124+
}
125+
126+
func fromLegacyGateway(data map[string]string, ns string) string {
127+
key := fmt.Sprintf("local-gateway.%s.%s", ns, knativeLocalGateway)
128+
129+
raw, ok := data[key]
130+
if !ok {
131+
return ""
132+
}
133+
134+
fields := strings.Split(raw, ".")
135+
if len(fields) < 2 {
136+
return ""
137+
}
138+
139+
return fields[1]
88140
}

pkg/reconciler/knativeserving/ingress/ingress_service_test.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,49 @@ func TestIngressServiceTransformIstioSelector(t *testing.T) {
8888
},
8989
},
9090
expected: true,
91+
}, {
92+
name: "Structured local-gateways config",
93+
namespace: "test-namespace",
94+
serviceName: "knative-local-gateway",
95+
expectedNamespace: "custom-gateway-ns",
96+
instance: &servingv1beta1.KnativeServing{
97+
ObjectMeta: metav1.ObjectMeta{
98+
Name: "test-instance",
99+
Namespace: "test-namespace",
100+
},
101+
Spec: servingv1beta1.KnativeServingSpec{
102+
CommonSpec: base.CommonSpec{
103+
Config: map[string]map[string]string{
104+
"istio": {
105+
"local-gateways": "- name: knative-local-gateway\n namespace: custom-gateway-ns\n service: knative-local-gateway.custom-gateway-ns.svc.cluster.local",
106+
},
107+
},
108+
},
109+
},
110+
},
111+
expected: true,
112+
}, {
113+
name: "Structured config precedence",
114+
namespace: "test-namespace",
115+
serviceName: "knative-local-gateway",
116+
expectedNamespace: "modern-ns",
117+
instance: &servingv1beta1.KnativeServing{
118+
ObjectMeta: metav1.ObjectMeta{
119+
Name: "test-instance",
120+
Namespace: "test-namespace",
121+
},
122+
Spec: servingv1beta1.KnativeServingSpec{
123+
CommonSpec: base.CommonSpec{
124+
Config: map[string]map[string]string{
125+
"istio": {
126+
"local-gateways": "- name: knative-local-gateway\n namespace: modern-ns",
127+
"local-gateway.test-namespace.knative-local-gateway": "knative-local-gateway.legacy-ns.svc.cluster.local",
128+
},
129+
},
130+
},
131+
},
132+
},
133+
expected: true,
91134
}, {
92135
name: "IstioNotUnderDefaultNS with both istio and config-istio",
93136
namespace: "test-namespace",
@@ -137,14 +180,12 @@ func TestIngressServiceTransformIstioSelector(t *testing.T) {
137180

138181
func TestIngressServiceTransform(t *testing.T) {
139182
tests := []struct {
140-
name string
141-
namespace string
142-
serviceName string
143-
expectedNamespace string
144-
knativeIngressGateway *base.IstioGatewayOverride
145-
clusterLocalGateway *base.IstioGatewayOverride
146-
instance *servingv1beta1.KnativeServing
147-
expected map[string]string
183+
name string
184+
namespace string
185+
serviceName string
186+
expectedNamespace string
187+
instance *servingv1beta1.KnativeServing
188+
expected map[string]string
148189
}{{
149190
name: "Istio ingress selector configuration for local gateway service",
150191
namespace: "test-namespace",
@@ -158,8 +199,10 @@ func TestIngressServiceTransform(t *testing.T) {
158199
Spec: servingv1beta1.KnativeServingSpec{
159200
Ingress: &servingv1beta1.IngressConfigs{
160201
Istio: base.IstioIngressConfiguration{
161-
Enabled: true,
162-
KnativeLocalGateway: gatewayOverride(map[string]string{"istio": "cluster-local-1"}, nil),
202+
Enabled: true,
203+
KnativeLocalGateway: &base.IstioGatewayOverride{
204+
Selector: map[string]string{"istio": "cluster-local-1"},
205+
},
163206
},
164207
},
165208
},
@@ -180,8 +223,10 @@ func TestIngressServiceTransform(t *testing.T) {
180223
Spec: servingv1beta1.KnativeServingSpec{
181224
Ingress: &servingv1beta1.IngressConfigs{
182225
Istio: base.IstioIngressConfiguration{
183-
Enabled: true,
184-
KnativeIngressGateway: gatewayOverride(map[string]string{"istio": "ingress-gateway-1"}, nil),
226+
Enabled: true,
227+
KnativeIngressGateway: &base.IstioGatewayOverride{
228+
Selector: map[string]string{"istio": "ingress-gateway-1"},
229+
},
185230
},
186231
},
187232
},

0 commit comments

Comments
 (0)