Skip to content

Commit 241aa98

Browse files
authored
Correct update handling (#83)
* update crd also if it has changes * du no update in logger mode * migrate test to ginkgo * make apigroup nullable * migrate to ginkgo * add tests
1 parent f108613 commit 241aa98

23 files changed

+1016
-607
lines changed

Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ endif
1414
all: manager
1515

1616
# Run tests
17-
test: generate tidy fmt vet manifests
17+
test: generate mocks tidy fmt vet manifests
1818
go test ./... -coverprofile cover.out
1919

2020
# Build manager binary
@@ -48,6 +48,7 @@ fmt:
4848
go fmt ./...
4949
gofmt -s -w .
5050

51+
5152
# Run go vet against code
5253
vet:
5354
go vet ./...
@@ -75,6 +76,11 @@ release: goreleaser
7576
test-release: goreleaser
7677
goreleaser --skip-publish --snapshot --rm-dist
7778

79+
# generate mocks
80+
mocks: mockgen
81+
mockgen -destination pkg/mocks/client/mock.go sigs.k8s.io/controller-runtime/pkg/client Client
82+
mockgen -destination pkg/mocks/logr/mock.go github.com/go-logr/logr Logger
83+
7884

7985
# find or download controller-gen
8086
# download controller-gen if necessary
@@ -124,4 +130,9 @@ bundle-build:
124130
goreleaser:
125131
ifeq (, $(shell which goreleaser))
126132
$(shell go get github.com/goreleaser/goreleaser)
133+
endif
134+
135+
mockgen:
136+
ifeq (, $(shell which mockgen))
137+
$(shell go get github.com/golang/mock/[email protected])
127138
endif

api/v1/eventlogger_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ type Kind struct {
6565
Name string `json:"name"`
6666

6767
// +optional
68-
ApiGroup string `json:"apiGroup"`
68+
// +nullable
69+
ApiGroup *string `json:"apiGroup"`
6970

7071
// EventTypes the event types to log. If empty events are logged as defined in spec.
7172
// +kubebuilder:validation:MinItems=0

api/v1/eventlogger_validation.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"crypto/sha256"
66
"encoding/json"
77
"fmt"
8+
"reflect"
9+
"regexp"
10+
"strings"
11+
812
"github.com/bakito/k8s-event-logger-operator/version"
913
english "github.com/go-playground/locales/en"
1014
ut "github.com/go-playground/universal-translator"
1115
"github.com/go-playground/validator/v10"
1216
"github.com/go-playground/validator/v10/translations/en"
13-
"reflect"
14-
"regexp"
15-
"strings"
1617
)
1718

1819
var (
Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
1-
package v1
1+
package v1_test
22

33
import (
4-
is "gotest.tools/assert/cmp"
5-
"testing"
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
66

7-
. "gotest.tools/assert"
7+
v1 "github.com/bakito/k8s-event-logger-operator/api/v1"
88
)
99

10-
func Test_Validate_Success(t *testing.T) {
11-
s := &EventLoggerSpec{
12-
Annotations: map[string]string{"valid/valid": "valid", "valid": "valid"},
13-
Labels: map[string]string{"valid": "valid"},
14-
}
15-
16-
Assert(t, is.Nil(s.Validate()))
17-
}
18-
19-
func Test_Validate_Invalid_LabelKey(t *testing.T) {
20-
s := &EventLoggerSpec{
21-
Labels: map[string]string{"in valid": "valid"},
22-
}
23-
24-
Assert(t, s.Validate() != nil)
25-
}
26-
27-
func Test_Validate_Invalid_LabelValue(t *testing.T) {
28-
s := &EventLoggerSpec{
29-
Labels: map[string]string{"valid": "in valid"},
30-
}
31-
32-
Assert(t, s.Validate() != nil)
33-
}
34-
35-
func Test_Validate_Invalid_AnnotationKey(t *testing.T) {
36-
s := &EventLoggerSpec{
37-
Annotations: map[string]string{"in valid": "valid"},
38-
}
39-
Assert(t, s.Validate() != nil)
40-
s = &EventLoggerSpec{
41-
Annotations: map[string]string{"in/valid/": "valid"},
42-
}
43-
Assert(t, s.Validate() != nil)
44-
}
10+
var _ = Describe("V1", func() {
11+
Context("Validate", func() {
12+
It("should succeed", func() {
13+
s := &v1.EventLoggerSpec{
14+
Annotations: map[string]string{"valid/valid": "valid", "valid": "valid"},
15+
Labels: map[string]string{"valid": "valid"},
16+
}
17+
Ω(s.Validate()).ShouldNot(HaveOccurred())
18+
})
19+
It("should have invalid label key", func() {
20+
s := &v1.EventLoggerSpec{
21+
Labels: map[string]string{"in valid": "valid"},
22+
}
23+
24+
Ω(s.Validate()).Should(HaveOccurred())
25+
})
26+
It("should have invalid label value", func() {
27+
s := &v1.EventLoggerSpec{
28+
Labels: map[string]string{"valid": "in valid"},
29+
}
30+
31+
Ω(s.Validate()).Should(HaveOccurred())
32+
})
33+
It("should have invalid annotation key", func() {
34+
s := &v1.EventLoggerSpec{
35+
Annotations: map[string]string{"in valid": "valid"},
36+
}
37+
38+
Ω(s.Validate()).Should(HaveOccurred())
39+
40+
s = &v1.EventLoggerSpec{
41+
Annotations: map[string]string{"in/valid/": "valid"},
42+
}
43+
44+
Ω(s.Validate()).Should(HaveOccurred())
45+
})
46+
})
47+
})

api/v1/v1_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package v1_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestV1(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "V1 Suite")
13+
}

api/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/eventlogger.bakito.ch_eventloggers.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ spec:
5252
description: Kind defines a kind to log events for
5353
properties:
5454
apiGroup:
55+
nullable: true
5556
type: string
5657
eventTypes:
5758
description: EventTypes the event types to log. If empty events

controllers/logging/event_controller.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package logging
1919
import (
2020
"context"
2121
"reflect"
22-
"sigs.k8s.io/controller-runtime/pkg/handler"
23-
"sigs.k8s.io/controller-runtime/pkg/source"
2422

2523
eventloggerv1 "github.com/bakito/k8s-event-logger-operator/api/v1"
2624
"github.com/fatih/structs"
@@ -32,9 +30,11 @@ import (
3230
ctrl "sigs.k8s.io/controller-runtime"
3331
"sigs.k8s.io/controller-runtime/pkg/client"
3432
"sigs.k8s.io/controller-runtime/pkg/event"
33+
"sigs.k8s.io/controller-runtime/pkg/handler"
3534
"sigs.k8s.io/controller-runtime/pkg/manager"
3635
"sigs.k8s.io/controller-runtime/pkg/predicate"
3736
"sigs.k8s.io/controller-runtime/pkg/reconcile"
37+
"sigs.k8s.io/controller-runtime/pkg/source"
3838
)
3939

4040
var (
@@ -47,6 +47,8 @@ type Reconciler struct {
4747
Log logr.Logger
4848
Scheme *runtime.Scheme
4949
Config *Config
50+
// LoggerMode if enable, the controller does only logging and no update on the custom resource
51+
LoggerMode bool
5052
}
5153

5254
// +kubebuilder:rbac:groups=eventlogger.bakito.ch,resources=eventloggers,verbs=get;list;watch;create;update;patch;delete
@@ -102,6 +104,10 @@ func (r *Reconciler) updateCR(ctx context.Context, cr *eventloggerv1.EventLogger
102104
if err != nil {
103105
logger.Error(err, "")
104106
}
107+
if r.LoggerMode {
108+
// return only, no update
109+
return reconcile.Result{}, err
110+
}
105111
cr.Apply(err)
106112
err = r.Update(ctx, cr)
107113
return reconcile.Result{}, err
@@ -123,7 +129,7 @@ func (p loggingPredicate) Create(e event.CreateEvent) bool {
123129

124130
// Update implements Predicate
125131
func (p loggingPredicate) Update(e event.UpdateEvent) bool {
126-
if _, ok := e.ObjectOld.(*eventloggerv1.EventLogger); ok {
132+
if _, ok := e.ObjectNew.(*eventloggerv1.EventLogger); ok {
127133
return p.Config.matches(e.MetaNew)
128134
}
129135
return p.logEvent(e.ObjectNew)

0 commit comments

Comments
 (0)