@@ -14,6 +14,7 @@ import (
14
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
15
"k8s.io/apimachinery/pkg/runtime/schema"
16
16
"k8s.io/apimachinery/pkg/types"
17
+ "reflect"
17
18
"sigs.k8s.io/controller-runtime/pkg/client"
18
19
"strings"
19
20
"time"
@@ -139,6 +140,11 @@ func (r *CloudMapReconciler) reconcileService(ctx context.Context, svc *model.Se
139
140
return nil
140
141
}
141
142
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
+
142
148
err = r .updateEndpointSlices (ctx , svc , existingService )
143
149
if err != nil {
144
150
return err
@@ -162,6 +168,7 @@ func (r *CloudMapReconciler) createServiceImport(ctx context.Context, namespace
162
168
Annotations : map [string ]string {DerivedServiceAnnotation : DerivedName (namespace , name )},
163
169
},
164
170
Spec : v1alpha1.ServiceImportSpec {
171
+ IPs : []string {},
165
172
Type : v1alpha1 .ClusterSetIP ,
166
173
Ports : []v1alpha1.ServicePort {},
167
174
},
@@ -330,3 +337,41 @@ func extractPorts(svc *model.Service) []int32 {
330
337
331
338
return ports
332
339
}
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