Skip to content

Commit

Permalink
fix: when service type = clusterIP set the address to service.cluster…
Browse files Browse the repository at this point in the history
…IP (#2582)

when service type = clusterIP set the address to service.clusterIP (#2166)

Signed-off-by: ShyunnY <1147212064@qq.com>
  • Loading branch information
ShyunnY authored Feb 12, 2024
1 parent 5acc233 commit bb59484
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
13 changes: 12 additions & 1 deletion internal/infrastructure/kubernetes/proxy/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,18 @@ func (r *ResourceRender) Service() (*corev1.Service, error) {
serviceSpec := resource.ExpectedServiceSpec(envoyServiceConfig)
serviceSpec.Ports = ports
serviceSpec.Selector = resource.GetSelector(labels).MatchLabels
serviceSpec.ExternalIPs = r.infra.Addresses

if (*envoyServiceConfig.Type) == egv1a1.ServiceTypeClusterIP {
if len(r.infra.Addresses) > 0 {
// Since K8s Service requires specify no more than one IP for each IP family
// So we only use the first address
// if address is not set, the automatically assigned clusterIP is used
serviceSpec.ClusterIP = r.infra.Addresses[0]
serviceSpec.ClusterIPs = r.infra.Addresses[0:1]
}
} else {
serviceSpec.ExternalIPs = r.infra.Addresses
}

svc := &corev1.Service{
TypeMeta: metav1.TypeMeta{
Expand Down
16 changes: 16 additions & 0 deletions internal/infrastructure/kubernetes/proxy/resource_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func newTestInfraWithAnnotations(annotations map[string]string) *ir.Infra {
return newTestInfraWithAnnotationsAndLabels(annotations, nil)
}

func newTestInfraWithAddresses(addresses []string) *ir.Infra {
infra := newTestInfraWithAnnotationsAndLabels(nil, nil)
infra.Proxy.Addresses = addresses

return infra
}

func newTestInfraWithAnnotationsAndLabels(annotations, labels map[string]string) *ir.Infra {
i := ir.NewInfra()

Expand Down Expand Up @@ -548,6 +555,15 @@ func TestService(t *testing.T) {
},
},
},
{
caseName: "clusterIP-custom-addresses",
infra: newTestInfraWithAddresses([]string{
"10.102.168.100",
}),
service: &egv1a1.KubernetesServiceSpec{
Type: &svcType,
},
},
}
for _, tc := range cases {
t.Run(tc.caseName, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/name: envoy
app.kubernetes.io/component: proxy
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
clusterIP: 10.102.168.100
clusterIPs:
- 10.102.168.100
ports:
- name: envoy-EnvoyHTTPPort-d76a15e2
port: 0
protocol: TCP
targetPort: 8080
- name: envoy-EnvoyHTTPSPort-6658f727
port: 0
protocol: TCP
targetPort: 8443
selector:
app.kubernetes.io/name: envoy
app.kubernetes.io/component: proxy
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
sessionAffinity: None
type: ClusterIP
4 changes: 3 additions & 1 deletion internal/status/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ func UpdateGatewayStatusProgrammedCondition(gw *gwapiv1.Gateway, svc *corev1.Ser
// If the addresses is explicitly set in the Gateway spec by the user, use it
// to populate the Status
if len(gw.Spec.Addresses) > 0 {
// Make sure the addresses have been populated into ExternalIPs
// Make sure the addresses have been populated into ExternalIPs/ClusterIPs
// and use that value
if len(svc.Spec.ExternalIPs) > 0 {
addresses = append(addresses, svc.Spec.ExternalIPs...)
} else if len(svc.Spec.ClusterIPs) > 0 {
addresses = append(addresses, svc.Spec.ClusterIPs...)
}
} else {
if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
Expand Down

0 comments on commit bb59484

Please sign in to comment.