Skip to content

Commit ddbed57

Browse files
authored
Set the Service as ServiceExport's OwnerReference (aws#115)
1 parent 40b6a98 commit ddbed57

File tree

6 files changed

+44
-31
lines changed

6 files changed

+44
-31
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ build: manifests generate generate-mocks fmt vet lint ## Build manager binary.
103103
go build -ldflags="-s -w -X ${PKG}.GitVersion=${GIT_TAG} -X ${PKG}.GitCommit=${GIT_COMMIT}" -o bin/manager main.go
104104

105105
run: manifests generate generate-mocks fmt vet ## Run a controller from your host.
106-
go run -ldflags="-s -w -X ${PKG}.GitVersion=${GIT_TAG} -X ${PKG}.GitCommit=${GIT_COMMIT}" ./main.go --zap-devel=true
106+
go run -ldflags="-s -w -X ${PKG}.GitVersion=${GIT_TAG} -X ${PKG}.GitCommit=${GIT_COMMIT}" ./main.go --zap-devel=true $(ARGS)
107107

108108
docker-build: test ## Build docker image with the manager.
109109
docker build --no-cache -t ${IMG} .

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/onsi/ginkgo v1.14.1
1212
github.com/onsi/gomega v1.10.2
1313
github.com/stretchr/testify v1.6.1
14-
golang.org/x/tools v0.1.7 // indirect
1514
k8s.io/api v0.20.2
1615
k8s.io/apimachinery v0.20.2
1716
k8s.io/client-go v0.20.2

go.sum

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
394394
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
395395
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
396396
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
397-
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
398397
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
399398
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
400399
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
@@ -489,9 +488,8 @@ golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLL
489488
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
490489
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
491490
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
491+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
492492
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
493-
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=
494-
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
495493
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
496494
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
497495
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -543,19 +541,16 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
543541
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
544542
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
545543
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
546-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
544+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
547545
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
548-
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
549-
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
550546
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
551547
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
552548
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
553549
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
554550
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
555551
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
552+
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
556553
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
557-
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
558-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
559554
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
560555
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
561556
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -603,9 +598,8 @@ golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapK
603598
golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
604599
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
605600
golang.org/x/tools v0.0.0-20200616133436-c1934b75d054/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
601+
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
606602
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
607-
golang.org/x/tools v0.1.7 h1:6j8CgantCy3yc8JGBqkDLMKWqZ0RDU2g1HVgacojGWQ=
608-
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
609603
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
610604
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
611605
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/common/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewLoggerWithLogr(l logr.Logger) Logger {
2828
}
2929

3030
func (l logger) Info(msg string, keysAndValues ...interface{}) {
31-
l.log.Info(msg, keysAndValues...)
31+
l.log.V(0).Info(msg, keysAndValues...)
3232
}
3333

3434
func (l logger) Debug(msg string, keysAndValues ...interface{}) {

pkg/controllers/cloudmap_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func (r *CloudMapReconciler) reconcileNamespace(ctx context.Context, namespaceNa
8989

9090
desiredServices, err := r.Cloudmap.ListServices(ctx, namespaceName)
9191
if err != nil {
92+
r.Log.Error(err, "failed to fetch the list Services")
9293
return err
9394
}
9495

@@ -128,7 +129,7 @@ func (r *CloudMapReconciler) reconcileNamespace(ctx context.Context, namespaceNa
128129
}
129130

130131
func (r *CloudMapReconciler) reconcileService(ctx context.Context, svc *model.Service) error {
131-
r.Log.Info("syncing service", "namespace", svc.Namespace, "service", svc.Name)
132+
r.Log.Debug("syncing service", "namespace", svc.Namespace, "service", svc.Name)
132133

133134
svcImport, err := r.getServiceImport(ctx, svc.Namespace, svc.Name)
134135
if err != nil {

pkg/controllers/serviceexport_controller.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
ctrl "sigs.k8s.io/controller-runtime"
4141
"sigs.k8s.io/controller-runtime/pkg/client"
4242

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"
4444
)
4545

4646
const (
@@ -66,13 +66,20 @@ func (r *ServiceExportReconciler) Reconcile(ctx context.Context, req ctrl.Reques
6666
ctx, cancel := context.WithCancel(ctx)
6767
defer cancel()
6868

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)
7072

7173
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
7683
}
7784

7885
// 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
8289
namespacedName := types.NamespacedName{Namespace: serviceExport.Namespace, Name: serviceExport.Name}
8390
if err := r.Client.Get(ctx, namespacedName, &service); err != nil {
8491
if errors.IsNotFound(err) {
85-
r.Log.Error(err, "no Service found for ServiceExport",
92+
r.Log.Info("no Service found, deleting the ServiceExport",
8693
"Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
8794
// Mark ServiceExport to be deleted, if the corresponding Service is not found
8895
isServiceExportMarkedForDelete = true
8996
} else {
90-
r.Log.Error(err, "error fetching service",
97+
r.Log.Error(err, "error fetching Service",
9198
"Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
92-
return ctrl.Result{}, err
99+
return ctrl.Result{}, nil
93100
}
94101
}
95102

@@ -112,18 +119,30 @@ func (r *ServiceExportReconciler) handleUpdate(ctx context.Context, serviceExpor
112119
}
113120
}
114121

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+
115134
r.Log.Info("updating Cloud Map service", "namespace", service.Namespace, "name", service.Name)
116135
cmService, err := r.createOrGetCloudMapService(ctx, service)
117136
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",
119138
"namespace", service.Namespace, "name", service.Name)
120139
return ctrl.Result{}, err
121140
}
122141

123142
endpoints, err := r.extractEndpoints(ctx, service)
124143
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)
127146
return ctrl.Result{}, err
128147
}
129148

@@ -140,15 +159,15 @@ func (r *ServiceExportReconciler) handleUpdate(ctx context.Context, serviceExpor
140159
upserts = append(upserts, changes.Update...)
141160

142161
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",
144163
"namespace", service.Namespace, "name", service.Name)
145164
return ctrl.Result{}, err
146165
}
147166
}
148167

149168
if changes.HasDeletes() {
150169
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",
152171
"namespace", cmService.Namespace, "name", cmService.Name)
153172
return ctrl.Result{}, err
154173
}
@@ -170,7 +189,7 @@ func (r *ServiceExportReconciler) createOrGetCloudMapService(ctx context.Context
170189
if cmService == nil {
171190
err = r.CloudMap.CreateService(ctx, service.Namespace, service.Name)
172191
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",
174193
"namespace", service.Namespace, "name", service.Name)
175194
return nil, err
176195
}
@@ -188,13 +207,13 @@ func (r *ServiceExportReconciler) handleDelete(ctx context.Context, serviceExpor
188207

189208
cmService, err := r.CloudMap.GetService(ctx, serviceExport.Namespace, serviceExport.Name)
190209
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",
192211
"namespace", serviceExport.Namespace, "name", serviceExport.Name)
193212
return ctrl.Result{}, err
194213
}
195214
if cmService != nil {
196215
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",
198217
"namespace", cmService.Namespace, "name", cmService.Name)
199218
return ctrl.Result{}, err
200219
}

0 commit comments

Comments
 (0)