-
Notifications
You must be signed in to change notification settings - Fork 64
/
filter.go
46 lines (38 loc) · 1.62 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package platform
import (
"context"
"fmt"
zip "api.zip"
machinev1alpha1 "kraftkit.sh/api/machine/v1alpha1"
)
// storePlatformFilter is a mechanism to narrow the results returned from the
// store which is shared between all platforms. In this filter, we prefix all
// requests to the Zip API client with a check for the machine's specification
// of the platform based on the provided argument.
func storePlatformFilter(platform Platform) zip.OnBefore {
return func(_ context.Context, req zip.ReferenceObject) (any, error) {
// If this object is listable, attempt to retrieve from a list from
// the store instead.
if list, ok := req.(*zip.ObjectList[machinev1alpha1.MachineSpec, machinev1alpha1.MachineStatus]); ok {
cached := list.Items
list.Items = []zip.Object[machinev1alpha1.MachineSpec, machinev1alpha1.MachineStatus]{}
for _, machine := range cached {
if machine.Spec.Platform != platform.String() {
continue
}
list.Items = append(list.Items, machine)
}
return list, nil
}
// Cast the referenceable object, which we know is a spec-and-status object.
obj := req.(*zip.Object[machinev1alpha1.MachineSpec, machinev1alpha1.MachineStatus])
if obj.Spec.Platform != platform.String() {
return nil, fmt.Errorf("wanted machine platform \"%s\" but got \"%s\" instead for instance \"%s\"", obj.Spec.Platform, platform.String(), obj.Name)
}
return obj, nil
}
}