Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Fix issue with infinite loop for editing services
Browse files Browse the repository at this point in the history
Signed-off-by: tylerslaton <[email protected]>
  • Loading branch information
tylerslaton committed Mar 20, 2023
1 parent 6c3f542 commit d0a1015
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
5 changes: 4 additions & 1 deletion pkg/controller/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func TestHandler_AddAnnotations(t *testing.T) {

existingAnnotations := input.GetAnnotations()

expected := existingAnnotations
expected["foo"] = "bar"

h := Handler{
client: fake.NewSimpleClientset(input),
annotations: map[string]string{"foo": "bar"},
Expand All @@ -48,7 +51,7 @@ func TestHandler_AddAnnotations(t *testing.T) {
}

if tt.shouldChange {
assert.Equal(t, "bar", input.GetAnnotations()["foo"])
assert.Equal(t, expected, input.GetAnnotations())
} else {
assert.Equal(t, existingAnnotations, input.GetAnnotations())
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func (h Handler) AddAnnotations(req router.Request, resp router.Response) error
}

logrus.Infof("Updating service %v with injected annotations", service.Name)
service.Annotations = h.annotations
for key, value := range h.annotations {
service.Annotations[key] = value
}
if err := req.Client.Update(req.Ctx, service); err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/controller/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ var (
"acorn.io/managed": "true",
})

appNameLabel = "acorn.io/app-name"
appNamespaceLabel = "acorn.io/app-namespace"
appNameLabel = "acorn.io/app-name"
appNamespaceLabel = "acorn.io/app-namespace"
servicePublishLabel = "acorn.io/service-publish"
)

func RegisterRoutes(router *router.Router, client kubernetes.Interface, annotations map[string]string) error {
Expand All @@ -23,7 +24,7 @@ func RegisterRoutes(router *router.Router, client kubernetes.Interface, annotati
annotations: annotations,
}

managedSelector, err := getAcornManagedSelector()
managedSelector, err := getAcornPublishedServiceSelector()
if err != nil {
return err
}
Expand All @@ -33,7 +34,7 @@ func RegisterRoutes(router *router.Router, client kubernetes.Interface, annotati
return nil
}

func getAcornManagedSelector() (labels.Selector, error) {
func getAcornPublishedServiceSelector() (labels.Selector, error) {
r1, err := labels.NewRequirement(appNameLabel, selection.Exists, nil)
if err != nil {
return nil, err
Expand All @@ -42,6 +43,10 @@ func getAcornManagedSelector() (labels.Selector, error) {
if err != nil {
return nil, err
}
acornManagedSelector.Add(*r1, *r2)
r3, err := labels.NewRequirement(servicePublishLabel, selection.Equals, []string{"true"})
if err != nil {
return nil, err
}
acornManagedSelector.Add(*r1, *r2, *r3)
return acornManagedSelector, nil
}
5 changes: 3 additions & 2 deletions pkg/controller/testdata/service-lb/input.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
apiVersion: v1
kind: Service
metadata:
annotations:
should-not-delete: "true"
labels:
acorn.io/app-name: test-app
acorn.io/app-namespace: test-app-namespace
acorn.io/container-name: foo
acorn.io/managed: "true"
acorn.io/service-publish: "true"
name: foo
spec:
type: LoadBalancer
5 changes: 3 additions & 2 deletions pkg/controller/testdata/service-non-lb/input.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
apiVersion: v1
kind: Service
metadata:
annotations:
should-not-delete: "true"
labels:
acorn.io/app-name: test-app
acorn.io/app-namespace: test-app-namespace
acorn.io/container-name: foo
acorn.io/managed: "true"
acorn.io/service-publish: "true"
name: foo
spec:
type: NodePort

0 comments on commit d0a1015

Please sign in to comment.