Skip to content

Commit adda62e

Browse files
authored
Add middleware information in status (#59)
1 parent 27b729b commit adda62e

5 files changed

Lines changed: 130 additions & 10 deletions

File tree

api/v1alpha1/function_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type FunctionStatus struct {
7878

7979
Git FunctionStatusGit `json:"git,omitempty"`
8080
Deployment FunctionStatusDeployment `json:"deployment,omitempty"`
81+
Middleware FunctionStatusMiddleware `json:"middleware,omitempty"`
8182
}
8283

8384
type FunctionStatusGit struct {
@@ -93,6 +94,19 @@ type FunctionStatusDeployment struct {
9394
Runtime string `json:"runtime,omitempty"`
9495
}
9596

97+
type FunctionStatusMiddleware struct {
98+
Current string `json:"current,omitempty"`
99+
Available *string `json:"available,omitempty"`
100+
AutoUpdate FunctionStatusMiddlewareAutoUpdate `json:"autoUpdate"`
101+
PendingRebuild bool `json:"pendingRebuild"` // no omitempty to have it always shown
102+
LastRebuild metav1.Time `json:"lastRebuild,omitempty"`
103+
}
104+
105+
type FunctionStatusMiddlewareAutoUpdate struct {
106+
Enabled bool `json:"enabled"` // no omitempty to have it always shown
107+
Source string `json:"source,omitempty"`
108+
}
109+
96110
// +kubebuilder:object:root=true
97111

98112
// FunctionList contains a list of Function.

api/v1alpha1/zz_generated.deepcopy.go

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

config/crd/bases/functions.dev_functions.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,30 @@ spec:
187187
resolvedBranch:
188188
type: string
189189
type: object
190+
middleware:
191+
properties:
192+
autoUpdate:
193+
properties:
194+
enabled:
195+
type: boolean
196+
source:
197+
type: string
198+
required:
199+
- enabled
200+
type: object
201+
available:
202+
type: string
203+
current:
204+
type: string
205+
lastRebuild:
206+
format: date-time
207+
type: string
208+
pendingRebuild:
209+
type: boolean
210+
required:
211+
- autoUpdate
212+
- pendingRebuild
213+
type: object
190214
name:
191215
type: string
192216
required:

internal/controller/function_controller.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,34 @@ func (r *FunctionReconciler) ensureDeployment(ctx context.Context, function *v1a
214214
func (r *FunctionReconciler) handleMiddlewareUpdate(ctx context.Context, function *v1alpha1.Function, repo *git.Repository, metadata *funcfn.Function) error {
215215
logger := log.FromContext(ctx)
216216

217+
functionDescribe, err := r.FuncCliManager.Describe(ctx, metadata.Name, function.Namespace)
218+
if err != nil {
219+
return fmt.Errorf("failed to describe function to get image details: %w", err)
220+
}
221+
function.Status.Deployment.Image = functionDescribe.Image
222+
223+
isMiddlewareUpdateEnabled, source, err := r.isMiddlewareUpdateEnabled(ctx, function)
224+
if err != nil {
225+
function.MarkMiddlewareNotUpToDate("MiddlewareCheckFailed", "Failed to check if middleware should be updated: %s", err)
226+
return fmt.Errorf("failed to check if middleware should be updated: %w", err)
227+
}
228+
function.Status.Middleware.AutoUpdate.Enabled = isMiddlewareUpdateEnabled
229+
function.Status.Middleware.AutoUpdate.Source = source
230+
function.Status.Middleware.Current = functionDescribe.Middleware.Version
231+
function.Status.Middleware.PendingRebuild = false
232+
217233
isOnLatestMiddleware, err := r.isMiddlewareLatest(ctx, metadata, function.Namespace)
218234
if err != nil {
219235
function.MarkMiddlewareNotUpToDate("MiddlewareCheckFailed", "Failed to check middleware version: %s", err.Error())
220236
return fmt.Errorf("failed to check if function is using latest middleware: %w", err)
221237
}
222238

223239
if !isOnLatestMiddleware {
224-
isMiddlewareUpdateEnabled, source, err := r.isMiddlewareUpdateEnabled(ctx, function)
240+
latestMiddleware, err := r.FuncCliManager.GetLatestMiddlewareVersion(ctx, metadata.Runtime, metadata.Invoke)
225241
if err != nil {
226-
function.MarkMiddlewareNotUpToDate("MiddlewareCheckFailed", "Failed to check if middleware should be updated: %s", err)
227-
return fmt.Errorf("failed to check if middleware should be updated: %w", err)
242+
return fmt.Errorf("failed to get latest available middleware version: %w", err)
228243
}
244+
function.Status.Middleware.Available = ptr.To(latestMiddleware)
229245

230246
if !isMiddlewareUpdateEnabled {
231247
logger.Info("Skipping middleware update, as middleware update is disabled")
@@ -235,12 +251,7 @@ func (r *FunctionReconciler) handleMiddlewareUpdate(ctx context.Context, functio
235251
logger.Info("Function is not on latest middleware and middleware update is enabled. Will redeploy")
236252
function.MarkMiddlewareNotUpToDate("MiddlewareOutdated", "Middleware is outdated, redeploying")
237253

238-
// update function image in status before long redeploy operation
239-
functionDescribe, err := r.FuncCliManager.Describe(ctx, metadata.Name, function.Namespace)
240-
if err != nil {
241-
return fmt.Errorf("failed to describe function to get image details: %w", err)
242-
}
243-
function.Status.Deployment.Image = functionDescribe.Image
254+
function.Status.Middleware.PendingRebuild = true
244255

245256
// Flush status before long deploy operation
246257
if err := FlushStatus(ctx, function); err != nil {
@@ -252,20 +263,26 @@ func (r *FunctionReconciler) handleMiddlewareUpdate(ctx context.Context, functio
252263
return fmt.Errorf("failed to redeploy function: %w", err)
253264
}
254265

266+
function.Status.Middleware.PendingRebuild = false
267+
function.Status.Middleware.LastRebuild = metav1.Now()
268+
255269
// After successful deployment, middleware is now up-to-date
256270
function.MarkMiddlewareUpToDate()
271+
function.Status.Middleware.Available = nil // if function is on latest, we don't need to show this field
257272
}
258273
} else {
259274
logger.Info("Function is deployed with latest middleware. No need to redeploy")
260275
function.MarkMiddlewareUpToDate()
276+
function.Status.Middleware.Available = nil // if function is on latest, we don't need to show this field
261277
}
262278

263279
// Update deployment status
264-
functionDescribe, err := r.FuncCliManager.Describe(ctx, metadata.Name, function.Namespace)
280+
functionDescribe, err = r.FuncCliManager.Describe(ctx, metadata.Name, function.Namespace)
265281
if err != nil {
266282
return fmt.Errorf("failed to describe function to get image details: %w", err)
267283
}
268284
function.Status.Deployment.Image = functionDescribe.Image
285+
function.Status.Middleware.Current = functionDescribe.Middleware.Version
269286

270287
function.MarkDeployReady()
271288
return nil

internal/controller/function_controller_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,33 @@ var _ = Describe("Function Controller", func() {
275275
"autoUpdateMiddleware": "true",
276276
},
277277
}),
278+
279+
Entry("Should populate the middleware information in the status", reconcileTestCase{
280+
spec: functionsdevv1alpha1.FunctionSpec{
281+
Repository: functionsdevv1alpha1.FunctionSpecRepository{
282+
URL: "https://github.com/foo/bar",
283+
},
284+
AutoUpdateMiddleware: ptr.To(true),
285+
},
286+
configureMocks: func(funcMock *funccli.MockManager, gitMock *git.MockManager) {
287+
funcMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
288+
Middleware: functions.Middleware{
289+
Version: "v2.0.0",
290+
},
291+
Image: "my-image:v1.2.3",
292+
}, nil)
293+
funcMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v2.0.0", nil)
294+
funcMock.EXPECT().GetMiddlewareVersion(mock.Anything, functionName, resourceNamespace).Return("v2.0.0", nil)
295+
296+
gitMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "", "main", mock.Anything).Return(createTmpGitRepo(functions.Function{Name: "func-go"}), nil)
297+
},
298+
statusChecks: func(status *functionsdevv1alpha1.FunctionStatus) {
299+
Expect(status.Middleware.Current).Should(Equal("v2.0.0"))
300+
Expect(status.Middleware.AutoUpdate.Enabled).Should(BeTrue())
301+
Expect(status.Middleware.AutoUpdate.Source).Should(Equal("function"))
302+
Expect(status.Middleware.Available).Should(BeNil())
303+
},
304+
}),
278305
)
279306
})
280307
})

0 commit comments

Comments
 (0)