Skip to content

Commit

Permalink
Move types to castai pkg (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao authored Sep 28, 2022
1 parent 7c2d2df commit 9831ba2
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 103 deletions.
5 changes: 2 additions & 3 deletions castai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"k8s.io/apimachinery/pkg/util/wait"

"github.com/castai/sec-agent/config"
"github.com/castai/sec-agent/types"
)

const (
Expand All @@ -35,7 +34,7 @@ type Client interface {
SendLogs(ctx context.Context, req *LogEvent) error
SendCISReport(ctx context.Context, report *CustomReport) error
SendDeltaReport(ctx context.Context, report *Delta) error
SendLinterChecks(ctx context.Context, checks []types.LinterCheck) error
SendLinterChecks(ctx context.Context, checks []LinterCheck) error
}

func NewClient(
Expand Down Expand Up @@ -120,7 +119,7 @@ func (c *client) SendCISReport(ctx context.Context, report *CustomReport) error
return c.sendReport(ctx, report, "cis-report")
}

func (c *client) SendLinterChecks(ctx context.Context, checks []types.LinterCheck) error {
func (c *client) SendLinterChecks(ctx context.Context, checks []LinterCheck) error {
return c.sendReport(ctx, checks, "linter-checks")
}

Expand Down
28 changes: 28 additions & 0 deletions castai/delta_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package castai

import (
"time"
)

type EventType string

const (
EventAdd EventType = "add"
EventUpdate EventType = "update"
EventDelete EventType = "delete"
)

type Delta struct {
FullSnapshot bool `json:"full_snapshot,omitempty"`
Items []DeltaItem `json:"items"`
}

type DeltaItem struct {
Event EventType `json:"event"`
ObjectUID string `json:"object_uid"`
ObjectName string `json:"object_name,omitempty"`
ObjectNamespace string `json:"object_namespace,omitempty"`
ObjectKind string `json:"object_kind,omitempty"`
ObjectAPIVersion string `json:"object_api_version,omitempty"`
ObjectCreatedAt time.Time `json:"object_created_at,omitempty"`
}
37 changes: 37 additions & 0 deletions castai/kubebench_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package castai

import "github.com/google/uuid"

type State string

type Node struct {
NodeName string `json:"node_name"`
ResourceID uuid.UUID `json:"resource_id"`
}

type CustomReport struct {
OverallControls
Node
}

type OverallControls struct {
Controls []*Controls `json:"Controls"`
}

type Controls struct {
Groups []*Group `json:"tests"`
}

// Group is a collection of similar checks.
type Group struct {
Checks []*Check `json:"results"`
}

// Check contains information about a recommendation in the
// CIS Kubernetes document.
type Check struct {
ID string `yaml:"id" json:"test_number"`
Text string `json:"test_desc"`
TestInfo []string `json:"test_info"`
State `json:"status"`
}
2 changes: 1 addition & 1 deletion types/linter.go → castai/linter_types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package castai

import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion types/linter_test.go → castai/linter_types_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package castai

import (
"testing"
Expand Down
14 changes: 14 additions & 0 deletions castai/logevent_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package castai

import (
"time"

"github.com/sirupsen/logrus"
)

type LogEvent struct {
Level string `json:"level"`
Time time.Time `json:"time"`
Message string `json:"message"`
Fields logrus.Fields `json:"fields"`
}
3 changes: 1 addition & 2 deletions castai/mock/client.go

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

83 changes: 0 additions & 83 deletions castai/types.go

This file was deleted.

16 changes: 8 additions & 8 deletions imagescan/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"golang.org/x/sync/semaphore"
corev1 "k8s.io/api/core/v1"

"github.com/castai/sec-agent/castai"
"github.com/castai/sec-agent/controller"
"github.com/castai/sec-agent/types"
)

var supportedTypes = []reflect.Type{
Expand Down Expand Up @@ -103,7 +103,7 @@ func (s *Subscriber) modifyDelta(event controller.Event, o controller.Object) {
}

type imageInfo struct {
Resources []types.Resource
Resources []castai.Resource
NodeNames map[string]struct{}
}

Expand Down Expand Up @@ -172,15 +172,15 @@ func (s *Subscriber) scheduleScans(ctx context.Context) (rerr error) {
return nil
}

func appendImage(imgs map[string]*imageInfo, containers []corev1.Container, nodeName string, resource types.Resource) map[string]*imageInfo {
func appendImage(imgs map[string]*imageInfo, containers []corev1.Container, nodeName string, resource castai.Resource) map[string]*imageInfo {
for _, cont := range containers {
v, ok := imgs[cont.Image]
if ok {
v.NodeNames[nodeName] = struct{}{}
v.Resources = append(v.Resources, resource)
} else {
imgs[cont.Image] = &imageInfo{
Resources: []types.Resource{resource},
Resources: []castai.Resource{resource},
NodeNames: map[string]struct{}{
nodeName: {},
},
Expand All @@ -190,15 +190,15 @@ func appendImage(imgs map[string]*imageInfo, containers []corev1.Container, node
return imgs
}

func toResource(pod *corev1.Pod) types.Resource {
func toResource(pod *corev1.Pod) castai.Resource {
objMeta := pod.ObjectMeta
owner := getPodOwner(pod)
return types.Resource{
ObjectMeta: types.ObjectMeta{
return castai.Resource{
ObjectMeta: castai.ObjectMeta{
Namespace: owner.name,
Name: objMeta.Namespace,
},
ObjectType: types.ObjectType{
ObjectType: castai.ObjectType{
APIVersion: owner.APIVersion,
Kind: owner.kind,
},
Expand Down
2 changes: 1 addition & 1 deletion linters/kubebench/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (m *mockProvider) GetLogReader(_ context.Context, _ string) (io.ReadCloser,
}

func readReport() []byte {
file, _ := os.OpenFile("../../castai/kube-bench-gke.json", os.O_RDONLY, 0666)
file, _ := os.OpenFile("../../testdata/kube-bench-gke.json", os.O_RDONLY, 0666)
reportBytes, _ := io.ReadAll(file)

return reportBytes
Expand Down
2 changes: 1 addition & 1 deletion linters/kubelinter/kubelinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import (
_ "golang.stackrox.io/kube-linter/pkg/templates/writablehostmount"
"k8s.io/apimachinery/pkg/types"

casttypes "github.com/castai/sec-agent/types"
casttypes "github.com/castai/sec-agent/castai"
)

func New(checks []string) *Linter {
Expand Down
3 changes: 1 addition & 2 deletions linters/kubelinter/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/castai/sec-agent/castai"
"github.com/castai/sec-agent/controller"
casttypes "github.com/castai/sec-agent/types"
)

var supportedTypes = []reflect.Type{
Expand All @@ -34,7 +33,7 @@ var supportedTypes = []reflect.Type{
func NewSubscriber(log logrus.FieldLogger, client castai.Client) controller.ObjectSubscriber {
ctx, cancel := context.WithCancel(context.Background())

linter := New(lo.Keys(casttypes.LinterRuleMap))
linter := New(lo.Keys(castai.LinterRuleMap))

return &Subscriber{
ctx: ctx,
Expand Down
2 changes: 1 addition & 1 deletion linters/kubelinter/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

casttypes "github.com/castai/sec-agent/castai"
mock_castai "github.com/castai/sec-agent/castai/mock"
"github.com/castai/sec-agent/controller"
casttypes "github.com/castai/sec-agent/types"
)

func TestSubscriber(t *testing.T) {
Expand Down
File renamed without changes.

0 comments on commit 9831ba2

Please sign in to comment.