Skip to content

Commit

Permalink
Do not emit an Event on THC annotation typos
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianSawicki committed May 12, 2023
1 parent 9c2d0d8 commit 0d3a993
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
13 changes: 4 additions & 9 deletions pkg/controller/translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,9 @@ func (t *Translator) maybeEnableBackendConfig(sp *utils.ServicePort, svc *api_v1
// setEnableTHC sets the THCEnabled for the service port as true or false depending on whether
// Transparent Health Checks should be enabled.
func (t *Translator) setEnableTHC(sp *utils.ServicePort, svc *api_v1.Service) (flagWarning bool) {
var THCEnabled bool
defer func() { //TODO(DamianSawicki): remove defer and move the following to the end of the function.
klog.Infof("Is THC enabled for the sevice %v with service port (%v, %v)? %v", svc.Name, sp.Port, sp.PortName, THCEnabled)
sp.THCConfiguration.THCEnabled = THCEnabled
}()

THCEnabled, err := annotations.FromService(svc).ShouldEnableTHC()
if err != nil {
message := fmt.Sprintf("Parsing THC annotation failed: %+v.", err)
t.recorderGetter.Recorder(sp.ID.Service.Namespace).Event(svc, api_v1.EventTypeWarning, "THCAnnotationParsingFailed", message)
klog.Warning(message)
klog.Warningf("Parsing THC annotation failed: %+v.", err)
}

if THCEnabled && !t.enableTHC {
Expand All @@ -242,6 +234,9 @@ func (t *Translator) setEnableTHC(sp *utils.ServicePort, svc *api_v1.Service) (f
sp.THCConfiguration.THCEvents.THCConfigured = true
}

klog.Infof("Is THC enabled for the sevice %v with service port (%v, %v)? %v", svc.Name, sp.Port, sp.PortName, THCEnabled)
sp.THCConfiguration.THCEnabled = THCEnabled

return
}

Expand Down
22 changes: 1 addition & 21 deletions pkg/controller/translator/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io/ioutil"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1322,33 +1321,14 @@ func TestSetEnableTHC(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
translator := fakeTranslator()
bufferSize := 1
fakeSingletonRecorderGetter := healthchecks.NewFakeSingletonRecorderGetter(bufferSize)
translator.recorderGetter = fakeSingletonRecorderGetter
translator.recorderGetter = healthchecks.NewFakeRecorderGetter(0)
translator.enableTHC = tc.enableTHC
sp := *tc.sp

translator.setEnableTHC(&sp, tc.svc)
if !reflect.DeepEqual(&sp, tc.want) {
t.Errorf("setEnableTHC, %s\ngot %s,\nwant %s", tc.name, pretty.Sprint(&sp), pretty.Sprint(tc.want))
}
fakeRecorder := fakeSingletonRecorderGetter.FakeRecorder()
if tc.wantEvent {
select {
case output := <-fakeRecorder.Events:
if !strings.HasPrefix(output, tc.eventPrefix) {
t.Fatalf("Incorrect event emitted: %s.", output)
}
case <-time.After(10 * time.Second):
t.Fatalf("Timeout when expecting Event.")
}
} else {
select {
case output := <-fakeRecorder.Events:
t.Fatalf("Unexpected event emitted: %s.", output)
case <-time.After(100 * time.Millisecond):
}
}
})
}
}

0 comments on commit 0d3a993

Please sign in to comment.