@@ -40,7 +40,7 @@ import (
40
40
ctrl "sigs.k8s.io/controller-runtime"
41
41
"sigs.k8s.io/controller-runtime/pkg/client"
42
42
43
- v1alpha1 "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/api/v1alpha1"
43
+ "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/api/v1alpha1"
44
44
)
45
45
46
46
const (
@@ -66,13 +66,20 @@ func (r *ServiceExportReconciler) Reconcile(ctx context.Context, req ctrl.Reques
66
66
ctx , cancel := context .WithCancel (ctx )
67
67
defer cancel ()
68
68
69
- r .Log .Debug ("reconciling ServiceExport" , "Namespace" , req .Namespace , "Name" , req .NamespacedName )
69
+ namespace := req .Namespace
70
+ name := req .NamespacedName
71
+ r .Log .Debug ("reconciling ServiceExport" , "Namespace" , namespace , "Name" , name )
70
72
71
73
serviceExport := v1alpha1.ServiceExport {}
72
- if err := r .Client .Get (ctx , req .NamespacedName , & serviceExport ); err != nil {
73
- r .Log .Error (err , "error fetching ServiceExport" ,
74
- "Namespace" , serviceExport .Namespace , "Name" , serviceExport .Name )
75
- return ctrl.Result {}, client .IgnoreNotFound (err )
74
+ if err := r .Client .Get (ctx , name , & serviceExport ); err != nil {
75
+ if errors .IsNotFound (err ) {
76
+ r .Log .Debug ("no ServiceExport found" ,
77
+ "Namespace" , namespace , "Name" , name )
78
+ } else {
79
+ r .Log .Error (err , "error fetching ServiceExport" ,
80
+ "Namespace" , namespace , "Name" , name )
81
+ }
82
+ return ctrl.Result {}, nil
76
83
}
77
84
78
85
// Mark ServiceExport to be deleted, which is indicated by the deletion timestamp being set.
@@ -82,14 +89,14 @@ func (r *ServiceExportReconciler) Reconcile(ctx context.Context, req ctrl.Reques
82
89
namespacedName := types.NamespacedName {Namespace : serviceExport .Namespace , Name : serviceExport .Name }
83
90
if err := r .Client .Get (ctx , namespacedName , & service ); err != nil {
84
91
if errors .IsNotFound (err ) {
85
- r .Log .Error ( err , "no Service found for ServiceExport" ,
92
+ r .Log .Info ( "no Service found, deleting the ServiceExport" ,
86
93
"Namespace" , serviceExport .Namespace , "Name" , serviceExport .Name )
87
94
// Mark ServiceExport to be deleted, if the corresponding Service is not found
88
95
isServiceExportMarkedForDelete = true
89
96
} else {
90
- r .Log .Error (err , "error fetching service " ,
97
+ r .Log .Error (err , "error fetching Service " ,
91
98
"Namespace" , serviceExport .Namespace , "Name" , serviceExport .Name )
92
- return ctrl.Result {}, err
99
+ return ctrl.Result {}, nil
93
100
}
94
101
}
95
102
@@ -112,18 +119,30 @@ func (r *ServiceExportReconciler) handleUpdate(ctx context.Context, serviceExpor
112
119
}
113
120
}
114
121
122
+ if len (serviceExport .GetOwnerReferences ()) == 0 {
123
+ err := controllerutil .SetControllerReference (service , serviceExport , r .Scheme )
124
+ if err == nil {
125
+ err = r .Client .Update (ctx , serviceExport )
126
+ }
127
+ if err != nil {
128
+ r .Log .Error (err , "error setting Service as an owner of the ServiceExport" ,
129
+ "namespace" , service .Namespace , "name" , service .Name )
130
+ return ctrl.Result {}, err
131
+ }
132
+ }
133
+
115
134
r .Log .Info ("updating Cloud Map service" , "namespace" , service .Namespace , "name" , service .Name )
116
135
cmService , err := r .createOrGetCloudMapService (ctx , service )
117
136
if err != nil {
118
- r .Log .Error (err , "error fetching service from Cloud Map" ,
137
+ r .Log .Error (err , "error fetching Service from Cloud Map" ,
119
138
"namespace" , service .Namespace , "name" , service .Name )
120
139
return ctrl.Result {}, err
121
140
}
122
141
123
142
endpoints , err := r .extractEndpoints (ctx , service )
124
143
if err != nil {
125
- r .Log .Error (err , "error extracting endpoints " ,
126
- "Namespace " , serviceExport .Namespace , "Name " , serviceExport .Name )
144
+ r .Log .Error (err , "error extracting Endpoints " ,
145
+ "namespace " , serviceExport .Namespace , "name " , serviceExport .Name )
127
146
return ctrl.Result {}, err
128
147
}
129
148
@@ -140,15 +159,15 @@ func (r *ServiceExportReconciler) handleUpdate(ctx context.Context, serviceExpor
140
159
upserts = append (upserts , changes .Update ... )
141
160
142
161
if err := r .CloudMap .RegisterEndpoints (ctx , service .Namespace , service .Name , upserts ); err != nil {
143
- r .Log .Error (err , "error registering endpoints to Cloud Map" ,
162
+ r .Log .Error (err , "error registering Endpoints to Cloud Map" ,
144
163
"namespace" , service .Namespace , "name" , service .Name )
145
164
return ctrl.Result {}, err
146
165
}
147
166
}
148
167
149
168
if changes .HasDeletes () {
150
169
if err := r .CloudMap .DeleteEndpoints (ctx , service .Namespace , service .Name , changes .Delete ); err != nil {
151
- r .Log .Error (err , "error deleting endpoints from Cloud Map" ,
170
+ r .Log .Error (err , "error deleting Endpoints from Cloud Map" ,
152
171
"namespace" , cmService .Namespace , "name" , cmService .Name )
153
172
return ctrl.Result {}, err
154
173
}
@@ -170,7 +189,7 @@ func (r *ServiceExportReconciler) createOrGetCloudMapService(ctx context.Context
170
189
if cmService == nil {
171
190
err = r .CloudMap .CreateService (ctx , service .Namespace , service .Name )
172
191
if err != nil {
173
- r .Log .Error (err , "error creating a new service in Cloud Map" ,
192
+ r .Log .Error (err , "error creating a new Service in Cloud Map" ,
174
193
"namespace" , service .Namespace , "name" , service .Name )
175
194
return nil , err
176
195
}
@@ -188,13 +207,13 @@ func (r *ServiceExportReconciler) handleDelete(ctx context.Context, serviceExpor
188
207
189
208
cmService , err := r .CloudMap .GetService (ctx , serviceExport .Namespace , serviceExport .Name )
190
209
if err != nil {
191
- r .Log .Error (err , "error fetching service from Cloud Map" ,
210
+ r .Log .Error (err , "error fetching Service from Cloud Map" ,
192
211
"namespace" , serviceExport .Namespace , "name" , serviceExport .Name )
193
212
return ctrl.Result {}, err
194
213
}
195
214
if cmService != nil {
196
215
if err := r .CloudMap .DeleteEndpoints (ctx , cmService .Namespace , cmService .Name , cmService .Endpoints ); err != nil {
197
- r .Log .Error (err , "error deleting endpoints from Cloud Map" ,
216
+ r .Log .Error (err , "error deleting Endpoints from Cloud Map" ,
198
217
"namespace" , cmService .Namespace , "name" , cmService .Name )
199
218
return ctrl.Result {}, err
200
219
}
0 commit comments