-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Amir Malka <amirm@armosec.io>
- Loading branch information
Showing
17 changed files
with
1,530 additions
and
2 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
instanceidhandler/v1/ephemeralcontainerinstance/helpers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ephemeralcontainerinstance | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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 *InstanceID) error { | ||
if instanceID.GetAPIVersion() == "" { | ||
return fmt.Errorf("invalid instanceID: apiVersion cannot be empty") | ||
} | ||
if instanceID.GetNamespace() == "" { | ||
return fmt.Errorf("invalid instanceID: namespace cannot be empty") | ||
} | ||
if instanceID.GetKind() == "" { | ||
return fmt.Errorf("invalid instanceID: kind cannot be empty") | ||
} | ||
if instanceID.GetName() == "" { | ||
return fmt.Errorf("invalid instanceID: name cannot be empty") | ||
} | ||
if instanceID.GetContainerName() == "" { | ||
return fmt.Errorf("invalid instanceID: containerName cannot be empty") | ||
} | ||
return nil | ||
} | ||
|
||
func listInstanceIDs(ownerReferences []metav1.OwnerReference, ephemeralContainers []core1.EphemeralContainer, apiVersion, namespace, kind, name string) ([]InstanceID, error) { | ||
|
||
if len(ephemeralContainers) == 0 { | ||
return []InstanceID{}, nil // ephemeral containers are optional | ||
} | ||
|
||
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 | ||
} | ||
|
||
for i := range ephemeralContainers { | ||
instanceID := &InstanceID{ | ||
apiVersion: parentApiVersion, | ||
namespace: namespace, | ||
kind: parentKind, | ||
name: parentName, | ||
ephemeralContainerName: ephemeralContainers[i].Name, | ||
} | ||
|
||
if err := validateInstanceID(instanceID); err != nil { | ||
return nil, fmt.Errorf("failed to validate instance ID: %w", err) | ||
} | ||
|
||
instanceIDs = append(instanceIDs, *instanceID) | ||
} | ||
|
||
return instanceIDs, nil | ||
} |
Oops, something went wrong.