Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/v1alpha1/function_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,17 @@ func (f *Function) MarkServiceNotReady(reason, messageFormat string, messageA ..
ObservedGeneration: f.Generation,
})
}

// History helpers

const MaxHistoryEntries = 20

func (f *Function) RecordHistoryEvent(message string) {
f.Status.History = append([]FunctionStatusHistoryEntry{{
Time: metav1.Now(),
Message: message,
}}, f.Status.History...)
if len(f.Status.History) > MaxHistoryEntries {
f.Status.History = f.Status.History[:MaxHistoryEntries]
}
}
95 changes: 95 additions & 0 deletions api/v1alpha1/function_lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,107 @@
package v1alpha1

import (
"fmt"
"testing"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestRecordHistoryEvent(t *testing.T) {
tests := []struct {
name string
existingHistory []FunctionStatusHistoryEntry
newMessage string
expectedLen int
expectedFirst string
expectedLast string
}{
{
name: "adds event to empty history",
existingHistory: nil,
newMessage: "Function deployed",
expectedLen: 1,
expectedFirst: "Function deployed",
expectedLast: "Function deployed",
},
{
name: "prepends event to existing history",
existingHistory: []FunctionStatusHistoryEntry{
{Time: metav1.Now(), Message: "Older event"},
},
newMessage: "Newer event",
expectedLen: 2,
expectedFirst: "Newer event",
expectedLast: "Older event",
},
{
name: "trims oldest entries when exceeding max",
existingHistory: func() []FunctionStatusHistoryEntry {
entries := make([]FunctionStatusHistoryEntry, MaxHistoryEntries)
for i := range entries {
entries[i] = FunctionStatusHistoryEntry{
Time: metav1.Now(),
Message: fmt.Sprintf("Event %d", i),
}
}
return entries
}(),
newMessage: "Overflow event",
expectedLen: MaxHistoryEntries,
expectedFirst: "Overflow event",
expectedLast: fmt.Sprintf("Event %d", MaxHistoryEntries-2),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &Function{}
f.Status.History = tt.existingHistory

f.RecordHistoryEvent(tt.newMessage)

if len(f.Status.History) != tt.expectedLen {
t.Errorf("expected %d entries, got %d", tt.expectedLen, len(f.Status.History))
}
if f.Status.History[0].Message != tt.expectedFirst {
t.Errorf("expected first message %q, got %q", tt.expectedFirst, f.Status.History[0].Message)
}
if f.Status.History[len(f.Status.History)-1].Message != tt.expectedLast {
t.Errorf("expected last message %q, got %q", tt.expectedLast, f.Status.History[len(f.Status.History)-1].Message)
}
})
}
}

func TestRecordHistoryEventFIFOOrder(t *testing.T) {
f := &Function{}

for i := 0; i < MaxHistoryEntries+5; i++ {
f.RecordHistoryEvent(fmt.Sprintf("Event %d", i))
}

if len(f.Status.History) != MaxHistoryEntries {
t.Fatalf("expected %d entries, got %d", MaxHistoryEntries, len(f.Status.History))
}

for i, entry := range f.Status.History {
expected := fmt.Sprintf("Event %d", MaxHistoryEntries+4-i)
if entry.Message != expected {
t.Errorf("entry %d: expected message %q, got %q", i, expected, entry.Message)
}
}
}

func TestRecordHistoryEventSetsTime(t *testing.T) {
f := &Function{}
f.RecordHistoryEvent("test event")

if f.Status.History[0].Time.IsZero() {
t.Error("expected non-zero time")
}
}

func TestCalculateReadyCondition(t *testing.T) {
tests := []struct {
name string
Expand Down
12 changes: 9 additions & 3 deletions api/v1alpha1/function_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ type FunctionStatus struct {

Conditions []metav1.Condition `json:"conditions,omitempty"`

Git FunctionStatusGit `json:"git,omitempty"`
Deployment FunctionStatusDeployment `json:"deployment,omitempty"`
Middleware FunctionStatusMiddleware `json:"middleware,omitempty"`
Git FunctionStatusGit `json:"git,omitempty"`
Deployment FunctionStatusDeployment `json:"deployment,omitempty"`
Middleware FunctionStatusMiddleware `json:"middleware,omitempty"`
History []FunctionStatusHistoryEntry `json:"history,omitempty"`
}

type FunctionStatusHistoryEntry struct {
Time metav1.Time `json:"time"`
Message string `json:"message"`
}

type FunctionStatusGit struct {
Expand Down
23 changes: 23 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions config/crd/bases/functions.dev_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ spec:
resolvedBranch:
type: string
type: object
history:
items:
properties:
message:
type: string
time:
format: date-time
type: string
required:
- message
- time
type: object
type: array
middleware:
properties:
autoUpdate:
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/function_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ func (r *FunctionReconciler) handleMiddlewareUpdate(ctx context.Context, functio
function.Status.Middleware.PendingRebuild = false
function.Status.Middleware.LastRebuild = metav1.Now()

function.RecordHistoryEvent(fmt.Sprintf("Middleware updated from %q to %q", functionDescribe.Middleware.Version, latestMiddleware))

// After successful deployment, middleware is now up-to-date
function.MarkMiddlewareUpToDate()
function.Status.Middleware.Available = nil // if function is on latest, we don't need to show this field
Expand Down
43 changes: 43 additions & 0 deletions internal/controller/function_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,49 @@ var _ = Describe("Function Controller", func() {
Expect(readyCond.Status).To(Equal(metav1.ConditionFalse))
},
}),
Entry("should record history event when middleware is redeployed", reconcileTestCase{
spec: defaultSpec,
configureMocks: func(funcMock *funccli.MockManager, gitMock *git.MockManager) {
funcMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
Middleware: functions.Middleware{
Version: "v1.0.0",
},
}, nil)
funcMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v2.0.0", nil)
funcMock.EXPECT().GetMiddlewareVersion(mock.Anything, functionName, resourceNamespace).Return("v1.0.0", nil)
funcMock.EXPECT().Deploy(mock.Anything, mock.Anything, resourceNamespace, funccli.DeployOptions{}).Return(nil)

gitMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "", "my-branch", mock.Anything).Return(createTmpGitRepo(functions.Function{Name: "func-go"}), nil)
},
statusChecks: func(status *functionsdevv1alpha1.FunctionStatus) {
messages := make([]string, len(status.History))
for i, entry := range status.History {
messages[i] = entry.Message
}
Expect(messages).To(ContainElement(`Middleware updated from "v1.0.0" to "v2.0.0"`))
},
}),
Entry("should not record middleware history event when middleware is already up to date", reconcileTestCase{
spec: defaultSpec,
configureMocks: func(funcMock *funccli.MockManager, gitMock *git.MockManager) {
funcMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
Middleware: functions.Middleware{
Version: "v1.0.0",
},
}, nil)
funcMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v1.0.0", nil)
funcMock.EXPECT().GetMiddlewareVersion(mock.Anything, functionName, resourceNamespace).Return("v1.0.0", nil)

gitMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "", "my-branch", mock.Anything).Return(createTmpGitRepo(functions.Function{Name: "func-go"}), nil)
},
statusChecks: func(status *functionsdevv1alpha1.FunctionStatus) {
messages := make([]string, len(status.History))
for i, entry := range status.History {
messages[i] = entry.Message
}
Expect(messages).ToNot(ContainElement(ContainSubstring("Middleware updated")))
},
}),
Entry("should set ServiceReady condition to false with unknown reason when ready status is empty", reconcileTestCase{
spec: defaultSpec,
configureMocks: func(funcMock *funccli.MockManager, gitMock *git.MockManager) {
Expand Down
Loading