Skip to content

Commit

Permalink
support ephemeral containers (#106)
Browse files Browse the repository at this point in the history
Signed-off-by: Amir Malka <[email protected]>
  • Loading branch information
amirmalka authored Apr 5, 2024
1 parent c8186a0 commit d0ee88d
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 7 deletions.
5 changes: 3 additions & 2 deletions pkg/apis/softwarecomposition/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ type ApplicationProfile struct {
}

type ApplicationProfileSpec struct {
Containers []ApplicationProfileContainer
InitContainers []ApplicationProfileContainer
Containers []ApplicationProfileContainer
InitContainers []ApplicationProfileContainer
EphemeralContainers []ApplicationProfileContainer
}

type ApplicationProfileContainer struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/softwarecomposition/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ type ApplicationProfileSpec struct {
// +patchMergeKey=name
// +patchStrategy=merge
InitContainers []ApplicationProfileContainer `json:"initContainers,omitempty" patchStrategy:"merge" patchMergeKey:"name"`
// +patchMergeKey=name
// +patchStrategy=merge
EphemeralContainers []ApplicationProfileContainer `json:"ephemeralContainers,omitempty" patchStrategy:"merge" patchMergeKey:"name"`
}

type ApplicationProfileContainer struct {
Expand Down

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

7 changes: 7 additions & 0 deletions pkg/apis/softwarecomposition/v1beta1/zz_generated.deepcopy.go

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

7 changes: 7 additions & 0 deletions pkg/apis/softwarecomposition/zz_generated.deepcopy.go

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

29 changes: 29 additions & 0 deletions pkg/cleanup/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ func (h *KubernetesAPI) fetchWlidsFromRunningWorkloads(resourceMaps *ResourceMap
resourceMaps.RunningWlidsToContainerNames.Get(wlid).Add(nameStr)
}

ephemeralC, ok := workloadinterface.InspectMap(workload.Object, append(workloadinterface.PodSpec(workload.GetKind()), "ephemeralContainers")...)
if !ok {
continue
}
ephemralContainers := ephemeralC.([]interface{})
for _, container := range ephemralContainers {
name, ok := workloadinterface.InspectMap(container, "name")
if !ok {
logger.L().Debug("container has no name", helpers.String("resource", resource))
continue
}
nameStr := name.(string)
resourceMaps.RunningWlidsToContainerNames.Get(wlid).Add(nameStr)
}

}
}
return nil
Expand Down Expand Up @@ -192,6 +207,20 @@ func (h *KubernetesAPI) fetchInstanceIdsAndImageIdsAndReplicasFromRunningPods(re
imageIdStr := containerImageId.(string)
resourceMaps.RunningContainerImageIds.Add(imageIdStr)
}

ephemeralC, ok := workloadinterface.InspectMap(p.Object, "status", "ephemeralContainerStatuses")
if !ok {
continue
}
ephemeralContainers := ephemeralC.([]interface{})
for _, cs := range ephemeralContainers {
containerImageId, ok := workloadinterface.InspectMap(cs, "imageID")
if !ok {
continue
}
imageIdStr := containerImageId.(string)
resourceMaps.RunningContainerImageIds.Add(imageIdStr)
}
}
return nil
}
19 changes: 19 additions & 0 deletions pkg/generated/openapi/zz_generated.openapi.go

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

6 changes: 4 additions & 2 deletions pkg/registry/file/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package file

import (
"fmt"
"strconv"

sets "github.com/deckarep/golang-set/v2"
"github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers"
"github.com/kubescape/storage/pkg/apis/softwarecomposition"
"k8s.io/apimachinery/pkg/runtime"
"strconv"
)

type Processor interface {
Expand Down Expand Up @@ -46,7 +47,8 @@ func (a ApplicationProfileProcessor) PreSave(object runtime.Object) error {
return containers
}

// Use the function for both InitContainers and Containers
// Use the function for InitContainers, EphemeralContainers and Containers
profile.Spec.EphemeralContainers = processContainers(profile.Spec.EphemeralContainers)
profile.Spec.InitContainers = processContainers(profile.Spec.InitContainers)
profile.Spec.Containers = processContainers(profile.Spec.Containers)

Expand Down
25 changes: 22 additions & 3 deletions pkg/registry/file/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package file

import (
"fmt"
"testing"

"github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers"
"github.com/kubescape/storage/pkg/apis/softwarecomposition"
"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"testing"
)

func TestApplicationProfileProcessor_PreSave(t *testing.T) {
Expand All @@ -18,12 +19,20 @@ func TestApplicationProfileProcessor_PreSave(t *testing.T) {
wantErr assert.ErrorAssertionFunc
}{
{
name: "ApplicationProfile with initContainers",
name: "ApplicationProfile with initContainers and ephemeralContainers",
object: &softwarecomposition.ApplicationProfile{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{},
},
Spec: softwarecomposition.ApplicationProfileSpec{
EphemeralContainers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "ephemeralContainer",
Execs: []softwarecomposition.ExecCalls{
{Path: "/bin/bash", Args: []string{"-c", "echo abc"}},
},
},
},
InitContainers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "initContainer",
Expand Down Expand Up @@ -56,10 +65,20 @@ func TestApplicationProfileProcessor_PreSave(t *testing.T) {
want: &softwarecomposition.ApplicationProfile{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{
helpers.ResourceSizeMetadataKey: "5",
helpers.ResourceSizeMetadataKey: "6",
},
},
Spec: softwarecomposition.ApplicationProfileSpec{
EphemeralContainers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "ephemeralContainer",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/bin/bash", Args: []string{"-c", "echo abc"}},
},
Syscalls: []string{},
},
},
InitContainers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "initContainer",
Expand Down

0 comments on commit d0ee88d

Please sign in to comment.