Skip to content

Commit

Permalink
Merge pull request #102 from kubescape/slug
Browse files Browse the repository at this point in the history
refactor slug for cronjobs
  • Loading branch information
matthyx authored Jul 19, 2024
2 parents 2cb374b + 0761af7 commit 7552a40
Show file tree
Hide file tree
Showing 36 changed files with 930 additions and 3,081 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
12 changes: 1 addition & 11 deletions instanceidhandler/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@ 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
// GetSlug returns a string with a human-friendly and Kubernetes-compatible name
GetSlug() (string, error)
GetSlug(noContainer bool) (string, error)
}
43 changes: 23 additions & 20 deletions instanceidhandler/v1/containerinstance/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,57 @@ 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(ownerReference *metav1.OwnerReference, containers []core1.Container, instanceType, apiVersion, namespace, kind, name, alternateName 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) {
parentApiVersion = ownerReferences[0].APIVersion
parentKind = ownerReferences[0].Kind
parentName = ownerReferences[0].Name
if ownerReference != nil && !helpers.IgnoreOwnerReference(ownerReference.Kind) {
parentApiVersion = ownerReference.APIVersion
parentKind = ownerReference.Kind
parentName = ownerReference.Name
}

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,
AlternateName: alternateName,
ContainerName: containers[i].Name,
InstanceType: instanceType,
}

if err := validateInstanceID(&instanceID); err != nil {
Expand Down
Loading

0 comments on commit 7552a40

Please sign in to comment.