Skip to content

Commit

Permalink
refactor instanceidhandler with unique container type
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Jul 12, 2024
1 parent 2cb374b commit cdcc8cb
Show file tree
Hide file tree
Showing 33 changed files with 297 additions and 3,318 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ jobs:
security-events: write
uses: kubescape/workflows/.github/workflows/go-basic-tests.yaml@main
with:
GO_VERSION: '1.21'
GO_VERSION: '1.22'
secrets: inherit
5 changes: 2 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Release-Tag
on:
push:
branches: [ master, main ]
branches: [ master, main ]
paths-ignore:
- '**.md' ### Ignore running when .md files change
- '**.yaml' ### Ignore running when .yaml files change
Expand All @@ -17,7 +17,7 @@ jobs:
security-events: write
uses: kubescape/workflows/.github/workflows/go-basic-tests.yaml@main
with:
GO_VERSION: '1.21'
GO_VERSION: '1.22'
secrets: inherit
release:
needs: test
Expand All @@ -29,4 +29,3 @@ jobs:
- uses: rickstaa/action-create-tag@v1
with:
tag: "v0.0.${{ github.run_number }}"

4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/kubescape/k8s-interface

go 1.21

toolchain go1.21.6
go 1.22.5

require (
cloud.google.com/go/container v1.24.0
Expand Down
15 changes: 0 additions & 15 deletions instanceidhandler/interface.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
package instanceidhandler

import "github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers"

type IInstanceID interface {
// GetInstanceType returns the type of the instance ID
GetInstanceType() helpers.InstanceType

GetAPIVersion() string
GetNamespace() string
GetKind() string
GetName() string
GetContainerName() string
SetAPIVersion(string)
SetNamespace(string)
SetKind(string)
SetName(string)
SetContainerName(string)
GetStringFormatted() string
GetHashed() string
GetLabels() map[string]string
Expand Down
34 changes: 18 additions & 16 deletions instanceidhandler/v1/containerinstance/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,40 @@ package containerinstance
import (
"fmt"

"github.com/kubescape/k8s-interface/instanceidhandler"
"github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers"
core1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func validateInstanceID(instanceID instanceidhandler.IInstanceID) error {
if instanceID.GetAPIVersion() == "" {
func validateInstanceID(instanceID *InstanceID) error {
if instanceID.ApiVersion == "" {
return fmt.Errorf("invalid instanceID: apiVersion cannot be empty")
}
if instanceID.GetNamespace() == "" {
if instanceID.Namespace == "" {
return fmt.Errorf("invalid instanceID: namespace cannot be empty")
}
if instanceID.GetKind() == "" {
if instanceID.Kind == "" {
return fmt.Errorf("invalid instanceID: kind cannot be empty")
}
if instanceID.GetName() == "" {
if instanceID.Name == "" {
return fmt.Errorf("invalid instanceID: name cannot be empty")
}
if instanceID.GetContainerName() == "" {
if instanceID.ContainerName == "" {
return fmt.Errorf("invalid instanceID: containerName cannot be empty")
}
if instanceID.InstanceType == "" {
return fmt.Errorf("invalid instanceID: InstanceType cannot be empty")
}
return nil
}

func listInstanceIDs(ownerReferences []metav1.OwnerReference, containers []core1.Container, apiVersion, namespace, kind, name string) ([]InstanceID, error) {
func ListInstanceIDs(ownerReferences []metav1.OwnerReference, containers []core1.Container, instanceType, apiVersion, namespace, kind, name string) ([]InstanceID, error) {
instanceIDs := make([]InstanceID, 0)

if len(containers) == 0 {
return nil, fmt.Errorf("failed to validate instance ID: missing containers")
return instanceIDs, nil
}

instanceIDs := make([]InstanceID, 0)

parentApiVersion, parentKind, parentName := apiVersion, kind, name

if len(ownerReferences) != 0 && !helpers.IgnoreOwnerReference(ownerReferences[0].Kind) {
Expand All @@ -46,11 +47,12 @@ func listInstanceIDs(ownerReferences []metav1.OwnerReference, containers []core1

for i := range containers {
instanceID := InstanceID{
apiVersion: parentApiVersion,
namespace: namespace,
kind: parentKind,
name: parentName,
containerName: containers[i].Name,
ApiVersion: parentApiVersion,
Namespace: namespace,
Kind: parentKind,
Name: parentName,
ContainerName: containers[i].Name,
InstanceType: instanceType,
}

if err := validateInstanceID(&instanceID); err != nil {
Expand Down
164 changes: 97 additions & 67 deletions instanceidhandler/v1/containerinstance/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func Test_validateInstanceID(t *testing.T) {
name: "empty apiVersion",
args: args{
instanceID: &InstanceID{
apiVersion: "",
namespace: "test",
kind: "test",
name: "test",
containerName: "test",
ApiVersion: "",
Namespace: "test",
Kind: "test",
Name: "test",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: true,
Expand All @@ -42,11 +43,12 @@ func Test_validateInstanceID(t *testing.T) {
args: args{
instanceID: &InstanceID{

apiVersion: "test",
namespace: "",
kind: "test",
name: "test",
containerName: "test",
ApiVersion: "test",
Namespace: "",
Kind: "test",
Name: "test",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: true,
Expand All @@ -55,11 +57,12 @@ func Test_validateInstanceID(t *testing.T) {
name: "empty kind",
args: args{
instanceID: &InstanceID{
apiVersion: "test",
namespace: "test",
kind: "",
name: "test",
containerName: "test",
ApiVersion: "test",
Namespace: "test",
Kind: "",
Name: "test",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: true,
Expand All @@ -68,11 +71,12 @@ func Test_validateInstanceID(t *testing.T) {
name: "empty name",
args: args{
instanceID: &InstanceID{
apiVersion: "test",
namespace: "test",
kind: "test",
name: "",
containerName: "test",
ApiVersion: "test",
Namespace: "test",
Kind: "test",
Name: "",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: true,
Expand All @@ -81,11 +85,26 @@ func Test_validateInstanceID(t *testing.T) {
name: "empty containerName",
args: args{
instanceID: &InstanceID{
apiVersion: "test",
namespace: "test",
kind: "test",
name: "test",
containerName: "",
ApiVersion: "test",
Namespace: "test",
Kind: "test",
Name: "test",
ContainerName: "",
InstanceType: "container",
},
},
wantErr: true,
},
{
name: "empty InstanceType",
args: args{
instanceID: &InstanceID{
ApiVersion: "test",
Namespace: "test",
Kind: "test",
Name: "test",
ContainerName: "test",
InstanceType: "",
},
},
wantErr: true,
Expand All @@ -94,11 +113,12 @@ func Test_validateInstanceID(t *testing.T) {
name: "valid instanceID",
args: args{
instanceID: &InstanceID{
apiVersion: "test",
namespace: "test",
kind: "test",
name: "test",
containerName: "test",
ApiVersion: "test",
Namespace: "test",
Kind: "test",
Name: "test",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: false,
Expand Down Expand Up @@ -144,11 +164,12 @@ func Test_listInstanceIDs(t *testing.T) {
},
want: []*InstanceID{
{
apiVersion: "test",
namespace: "test",
kind: "Pod",
name: "test",
containerName: "test",
ApiVersion: "test",
Namespace: "test",
Kind: "Pod",
Name: "test",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: false,
Expand All @@ -164,17 +185,21 @@ func Test_listInstanceIDs(t *testing.T) {
name: "test",
},
want: []*InstanceID{},
wantErr: true,
wantErr: false,
},
{
name: "invalid instanceID",
args: args{
ownerReferences: []metav1.OwnerReference{},
containers: []core1.Container{},
apiVersion: "",
namespace: "test",
kind: "test",
name: "test",
containers: []core1.Container{
{
Name: "test",
},
},
apiVersion: "",
namespace: "test",
kind: "test",
name: "test",
},
want: []*InstanceID{},
wantErr: true,
Expand All @@ -195,11 +220,12 @@ func Test_listInstanceIDs(t *testing.T) {
},
want: []*InstanceID{
{
apiVersion: "test",
namespace: "test",
kind: "Pod",
name: "test",
containerName: "test",
ApiVersion: "test",
Namespace: "test",
Kind: "Pod",
Name: "test",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: false,
Expand All @@ -225,11 +251,12 @@ func Test_listInstanceIDs(t *testing.T) {
},
want: []*InstanceID{
{
apiVersion: "test",
namespace: "test",
kind: "Pod",
name: "podName",
containerName: "test",
ApiVersion: "test",
Namespace: "test",
Kind: "Pod",
Name: "podName",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: false,
Expand All @@ -256,11 +283,12 @@ func Test_listInstanceIDs(t *testing.T) {
},
want: []*InstanceID{
{
apiVersion: "apps/v1",
namespace: "test",
kind: "ReplicaSet",
name: "OwnerTest",
containerName: "test",
ApiVersion: "apps/v1",
Namespace: "test",
Kind: "ReplicaSet",
Name: "OwnerTest",
ContainerName: "test",
InstanceType: "container",
},
},
wantErr: false,
Expand Down Expand Up @@ -290,18 +318,20 @@ func Test_listInstanceIDs(t *testing.T) {
},
want: []*InstanceID{
{
apiVersion: "apps/v1",
namespace: "test",
kind: "ReplicaSet",
name: "OwnerTest",
containerName: "test-0",
ApiVersion: "apps/v1",
Namespace: "test",
Kind: "ReplicaSet",
Name: "OwnerTest",
ContainerName: "test-0",
InstanceType: "container",
},
{
apiVersion: "apps/v1",
namespace: "test",
kind: "ReplicaSet",
name: "OwnerTest",
containerName: "test-1",
ApiVersion: "apps/v1",
Namespace: "test",
Kind: "ReplicaSet",
Name: "OwnerTest",
ContainerName: "test-1",
InstanceType: "container",
},
},
wantErr: false,
Expand All @@ -310,7 +340,7 @@ func Test_listInstanceIDs(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := listInstanceIDs(tt.args.ownerReferences, tt.args.containers, tt.args.apiVersion, tt.args.namespace, tt.args.kind, tt.args.name)
got, err := ListInstanceIDs(tt.args.ownerReferences, tt.args.containers, "container", tt.args.apiVersion, tt.args.namespace, tt.args.kind, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("listInstanceIDs() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
Loading

0 comments on commit cdcc8cb

Please sign in to comment.