Skip to content

Commit 19f8761

Browse files
committed
ServiceImport inherits local VIP from dummy Service resource
1 parent b5fa92a commit 19f8761

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

pkg/controllers/cloudmap_controller.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
"k8s.io/apimachinery/pkg/runtime/schema"
1616
"k8s.io/apimachinery/pkg/types"
17+
"reflect"
1718
"sigs.k8s.io/controller-runtime/pkg/client"
1819
"strings"
1920
"time"
@@ -139,6 +140,11 @@ func (r *CloudMapReconciler) reconcileService(ctx context.Context, svc *model.Se
139140
return nil
140141
}
141142

143+
// update ServiceImport to match IP and port of previously created service
144+
if err = r.updateServiceImport(ctx, svcImport, existingService); err != nil {
145+
return err
146+
}
147+
142148
err = r.updateEndpointSlices(ctx, svc, existingService)
143149
if err != nil {
144150
return err
@@ -162,6 +168,7 @@ func (r *CloudMapReconciler) createServiceImport(ctx context.Context, namespace
162168
Annotations: map[string]string{DerivedServiceAnnotation: DerivedName(namespace, name)},
163169
},
164170
Spec: v1alpha1.ServiceImportSpec{
171+
IPs: []string{},
165172
Type: v1alpha1.ClusterSetIP,
166173
Ports: []v1alpha1.ServicePort{},
167174
},
@@ -330,3 +337,41 @@ func extractPorts(svc *model.Service) []int32 {
330337

331338
return ports
332339
}
340+
341+
func (r *CloudMapReconciler) updateServiceImport(ctx context.Context, svcImport *v1alpha1.ServiceImport, svc *v1.Service) error {
342+
if len(svcImport.Spec.IPs) != 1 || svcImport.Spec.IPs[0] != svc.Spec.ClusterIP || !portsEqual(svcImport, svc) {
343+
svcImport.Spec.IPs = []string{svc.Spec.ClusterIP}
344+
345+
svcImport.Spec.Ports = make([]v1alpha1.ServicePort, 0)
346+
for _, p := range svc.Spec.Ports {
347+
svcImport.Spec.Ports = append(svcImport.Spec.Ports, servicePortToServiceImport(p))
348+
}
349+
if err := r.Update(ctx, svcImport); err != nil {
350+
return err
351+
}
352+
r.Logger.Info("updated ServiceImport",
353+
"namespace", svcImport.Namespace, "name", svcImport.Name,
354+
"IP", svcImport.Spec.IPs, "ports", svcImport.Spec.Ports)
355+
}
356+
357+
return nil
358+
}
359+
360+
func portsEqual(svcImport *v1alpha1.ServiceImport, svc *v1.Service) bool {
361+
impPorts := svcImport.Spec.Ports
362+
svcPorts := make([]v1alpha1.ServicePort, 0)
363+
for _, p := range svc.Spec.Ports {
364+
svcPorts = append(svcPorts, servicePortToServiceImport(p))
365+
}
366+
367+
return reflect.DeepEqual(impPorts, svcPorts)
368+
}
369+
370+
func servicePortToServiceImport(port v1.ServicePort) v1alpha1.ServicePort {
371+
return v1alpha1.ServicePort{
372+
Name: port.Name,
373+
Protocol: port.Protocol,
374+
AppProtocol: port.AppProtocol,
375+
Port: port.Port,
376+
}
377+
}

0 commit comments

Comments
 (0)