-
Notifications
You must be signed in to change notification settings - Fork 197
/
writer.go
54 lines (48 loc) · 1.51 KB
/
writer.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
package kubehunter
import (
"context"
"errors"
"strings"
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1"
"github.com/aquasecurity/starboard/pkg/generated/clientset/versioned"
"github.com/aquasecurity/starboard/pkg/starboard"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Writer interface {
Write(ctx context.Context, report v1alpha1.KubeHunterOutput, cluster string) error
}
type writer struct {
clientset versioned.Interface
}
func NewWriter(clientset versioned.Interface) Writer {
return &writer{
clientset: clientset,
}
}
func (w *writer) Write(ctx context.Context, report v1alpha1.KubeHunterOutput, cluster string) error {
if strings.TrimSpace(cluster) == "" {
return errors.New("cluster name must not be blank")
}
_, err := w.clientset.AquasecurityV1alpha1().KubeHunterReports().Create(ctx, &v1alpha1.KubeHunterReport{
ObjectMeta: metav1.ObjectMeta{
Name: cluster,
Labels: map[string]string{
starboard.LabelResourceKind: "Cluster",
starboard.LabelResourceName: cluster,
},
},
Report: report,
}, metav1.CreateOptions{})
if err != nil && apierrors.IsAlreadyExists(err) {
found, err := w.clientset.AquasecurityV1alpha1().KubeHunterReports().Get(ctx, cluster, metav1.GetOptions{})
if err != nil {
return err
}
deepCopy := found.DeepCopy()
deepCopy.Report = report
_, err = w.clientset.AquasecurityV1alpha1().KubeHunterReports().Update(ctx, deepCopy, metav1.UpdateOptions{})
return err
}
return err
}