Skip to content

Commit

Permalink
Create a cap for the max number of deploy log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
dtomcej authored and traefiker committed Nov 12, 2019
1 parent 0673eda commit f108f49
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
10 changes: 9 additions & 1 deletion internal/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,17 @@ func (a *API) getReadiness(w http.ResponseWriter, r *http.Request) {

// getDeployLog returns the current deploylog.
func (a *API) getDeployLog(w http.ResponseWriter, r *http.Request) {
entries := a.deployLog.GetLog()

data, err := json.Marshal(entries)
if err != nil {
writeErrorResponse(w, fmt.Sprintf("unable to marshal deploy log entries: %v", err), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")

if _, err := w.Write(a.deployLog.GetLog()); err != nil {
if _, err := w.Write(data); err != nil {
log.Error(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestGetCurrentConfiguration(t *testing.T) {

func TestGetDeployLog(t *testing.T) {
config := safe.Safe{}
log := NewDeployLog()
log := NewDeployLog(1000)
api := NewAPI(9000, &config, log, nil, "foo")

currentTime := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *Controller) Init() error {

c.tcpStateTable = &k8s.State{Table: make(map[int]*k8s.ServiceWithPort)}

c.deployLog = NewDeployLog()
c.deployLog = NewDeployLog(1000)
c.api = NewAPI(c.apiPort, &c.lastConfiguration, c.deployLog, c.clients, c.meshNamespace)

if c.smiEnabled {
Expand Down
31 changes: 17 additions & 14 deletions internal/controller/log.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package controller

import (
"encoding/json"
"time"

log "github.com/sirupsen/logrus"
)

type logEntry struct {
// Entry holds the details of a deployment.
type Entry struct {
TimeStamp time.Time
PodName string
PodIP string
Expand All @@ -17,12 +17,15 @@ type logEntry struct {

// DeployLog holds a slice of log entries.
type DeployLog struct {
Entries []logEntry
entries []Entry
maxEntries int
}

// NewDeployLog returns an initialized DeployLog.
func NewDeployLog() *DeployLog {
d := &DeployLog{}
func NewDeployLog(maxEntries int) *DeployLog {
d := &DeployLog{
maxEntries: maxEntries,
}

if err := d.Init(); err != nil {
log.Error("Could not initialize DeployLog")
Expand All @@ -40,23 +43,23 @@ func (d *DeployLog) Init() error {

// LogDeploy adds a record to the entries list.
func (d *DeployLog) LogDeploy(timeStamp time.Time, podName string, podIP string, deploySuccessful bool, reason string) {
newEntry := logEntry{
newEntry := Entry{
TimeStamp: timeStamp,
PodName: podName,
PodIP: podIP,
DeploySuccessful: deploySuccessful,
Reason: reason,
}

d.Entries = append(d.Entries, newEntry)
for len(d.entries) >= d.maxEntries {
// Pull elements off the front of the slice to make sure that the newly appended record is one under the max record value.
d.entries = d.entries[1:]
}

d.entries = append(d.entries, newEntry)
}

// GetLog returns a json representation of the entries list.
func (d *DeployLog) GetLog() []byte {
data, err := json.Marshal(d.Entries)
if err != nil {
log.Error("Could not marshal deploylog entries")
}

return data
func (d *DeployLog) GetLog() []Entry {
return d.entries
}
26 changes: 22 additions & 4 deletions internal/controller/log_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"encoding/json"
"fmt"
"testing"
"time"
Expand All @@ -9,13 +10,13 @@ import (
)

func TestLogDeploy(t *testing.T) {
log := NewDeployLog()
log := NewDeployLog(1000)
log.LogDeploy(time.Now(), "foo", "bar", true, "blabla")
assert.Equal(t, 1, len(log.Entries))
assert.Equal(t, 1, len(log.entries))
}

func TestGetLog(t *testing.T) {
log := NewDeployLog()
log := NewDeployLog(1000)
currentTime := time.Now()
log.LogDeploy(currentTime, "foo", "bar", true, "blabla")

Expand All @@ -24,7 +25,24 @@ func TestGetLog(t *testing.T) {

currentTimeString := string(data)

actual := string(log.GetLog())
data, err = json.Marshal(log.GetLog())
assert.NoError(t, err)

actual := string(data)
expected := fmt.Sprintf("[{\"TimeStamp\":%s,\"PodName\":\"foo\",\"PodIP\":\"bar\",\"DeploySuccessful\":true,\"Reason\":\"blabla\"}]", currentTimeString)
assert.Equal(t, expected, actual)
}

func TestLogRotationAndGetLogLength(t *testing.T) {
log := NewDeployLog(10)

for i := 0; i < 10; i++ {
log.LogDeploy(time.Now(), "foo", "bar", true, "blabla")
}

assert.Equal(t, 10, len(log.entries))

log.LogDeploy(time.Now(), "foo", "bar", true, "blabla")

assert.Equal(t, 10, len(log.entries))
}

0 comments on commit f108f49

Please sign in to comment.