Skip to content

Commit 26cc1f4

Browse files
committed
Use []byte for event body
Using []byte rather than string is cleaner: * That's what we get from yaml.Marshal, and what Write() to file will do * Easier to make things io.Reader / io.Writer interface compliant
1 parent af7823b commit 26cc1f4

File tree

6 files changed

+9
-18
lines changed

6 files changed

+9
-18
lines changed

pkg/controller/controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (c *Controller) processItem(key string) error {
186186

187187
if !exists {
188188
// deleted object
189-
c.enqueue(&event.Notification{Action: event.Delete, Key: key, Kind: c.name, Object: ""})
189+
c.enqueue(&event.Notification{Action: event.Delete, Key: key, Kind: c.name, Object: nil})
190190
return nil
191191
}
192192

@@ -207,7 +207,7 @@ func (c *Controller) processItem(key string) error {
207207
return fmt.Errorf("failed to marshal %s: %v", key, err)
208208
}
209209

210-
c.enqueue(&event.Notification{Action: event.Upsert, Key: key, Kind: c.name, Object: string(yml)})
210+
c.enqueue(&event.Notification{Action: event.Upsert, Key: key, Kind: c.name, Object: yml})
211211
return nil
212212
}
213213

pkg/controller/controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestController(t *testing.T) {
110110
gotFoo2 := false
111111
for _, ev := range evt.evts {
112112
// ensure cleanup filters works as expected
113-
if strings.Contains(ev.Object, "shouldnotbethere") {
113+
if strings.Contains(string(ev.Object), "shouldnotbethere") {
114114
t.Error("object cleanup filters didn't work")
115115
}
116116

pkg/event/event.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Notification struct {
1717
Action Action
1818
Key string
1919
Kind string
20-
Object string
20+
Object []byte
2121
}
2222

2323
// Notifier mediates notifications between controllers and recorder

pkg/event/event_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var (
1010
Action: Upsert,
1111
Key: "foo",
1212
Kind: "bar",
13-
Object: "spam egg",
13+
Object: []byte("spam egg"),
1414
}
1515
)
1616

pkg/recorder/recorder.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (w *Listener) relativePath(file string) string {
126126
return strings.Replace(file, root+"/", "", 1)
127127
}
128128

129-
func (w *Listener) save(file string, data string) error {
129+
func (w *Listener) save(file string, data []byte) error {
130130
w.config.Logger.Debugf("Saving %s to disk", file)
131131

132132
if w.config.DryRun {
@@ -144,21 +144,12 @@ func (w *Listener) save(file string, data string) error {
144144
w.actives[w.relativePath(file)] = true
145145
w.activesLock.Unlock()
146146

147-
f, err := appFs.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
148-
if err != nil {
149-
return fmt.Errorf("failed to create %s on disk: %v", file, err)
150-
}
147+
err = afero.WriteFile(appFs, file, data, 0600)
151148

152-
_, err = f.WriteString(data)
153149
if err != nil {
154150
return fmt.Errorf("failed to write to %s on disk: %v", file, err)
155151
}
156152

157-
err = f.Close()
158-
if err != nil {
159-
return fmt.Errorf("failed to close %s file: %v", file, err)
160-
}
161-
162153
return nil
163154
}
164155

pkg/recorder/recorder_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newNotif(action event.Action, key string) *event.Notification {
1616
Action: action,
1717
Key: key,
1818
Kind: "foo",
19-
Object: "bar",
19+
Object: []byte("bar"),
2020
}
2121
}
2222

@@ -116,7 +116,7 @@ func TestFailingFSRecorder(t *testing.T) {
116116
// switching to failing (read-only) filesystem
117117
appFs = afero.NewReadOnlyFs(appFs)
118118

119-
err := rec.save("foo", "bar")
119+
err := rec.save("foo", []byte("bar"))
120120
if err == nil {
121121
t.Error("save should return an error in case of failure")
122122
}

0 commit comments

Comments
 (0)