-
Notifications
You must be signed in to change notification settings - Fork 197
/
io.go
70 lines (57 loc) · 1.46 KB
/
io.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package kubebench
import (
"context"
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1"
"github.com/aquasecurity/starboard/pkg/kube"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type Writer interface {
Write(ctx context.Context, report v1alpha1.CISKubeBenchReport) error
}
type Reader interface {
FindByOwner(ctx context.Context, node kube.Object) (*v1alpha1.CISKubeBenchReport, error)
}
type ReadWriter interface {
Writer
Reader
}
type rw struct {
client client.Client
}
func NewReadWriter(client client.Client) ReadWriter {
return &rw{
client: client,
}
}
func (w *rw) Write(ctx context.Context, report v1alpha1.CISKubeBenchReport) error {
// TODO Try CreateOrUpdate method
var existing v1alpha1.CISKubeBenchReport
err := w.client.Get(ctx, types.NamespacedName{
Name: report.Name,
}, &existing)
if err == nil {
copied := existing.DeepCopy()
copied.Labels = report.Labels
copied.Report = report.Report
return w.client.Update(ctx, copied)
}
if errors.IsNotFound(err) {
return w.client.Create(ctx, &report)
}
return err
}
func (w *rw) FindByOwner(ctx context.Context, node kube.Object) (*v1alpha1.CISKubeBenchReport, error) {
report := &v1alpha1.CISKubeBenchReport{}
err := w.client.Get(ctx, types.NamespacedName{
Name: node.Name,
}, report)
if err != nil {
if errors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return report, nil
}