Skip to content

Commit

Permalink
use patch instead of update + remove logs + validate empty labels
Browse files Browse the repository at this point in the history
Signed-off-by: rcohen <rcohen@armosec.io>
  • Loading branch information
rcohen committed Mar 27, 2023
1 parent 5077882 commit b1e056e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 2 additions & 3 deletions pkg/conthandler/container_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,12 @@ func (ch *ContainerHandler) handleContainerRunningEvent(contEvent v1.ContainerEv
}

func (ch *ContainerHandler) handleContainerTerminatedEvent(contEvent v1.ContainerEventData) error {
watchedContainer, exist := ch.watchedContainers.Load(contEvent.GetContainerID())
if exist {
watchedContainer, _ := ch.watchedContainers.Load(contEvent.GetContainerID())
if watchedContainer != nil {
data, ok := watchedContainer.(watchedContainerData)
if !ok {
return fmt.Errorf("failed to stop container ID %s", contEvent.GetContainerID())
}
logger.L().Info("container has terminated - stop monitor it", []helpers.IDetails{helpers.String("ContainerID", contEvent.GetContainerID()), helpers.String("Container name", data.event.GetContainerName()), helpers.String("k8s workload", data.event.GetK8SWorkloadID())}...)
data.syncChannel[StepEventAggregator] <- containerHasTerminatedError
}
return nil
Expand Down
8 changes: 7 additions & 1 deletion pkg/sbom/v1/sbom_spdx_storage_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ func (sbom *SBOMData) StoreFilteredSBOMName(name string) {
}

func (sbom *SBOMData) StoreMetadata(instanceID instanceidhandler.IInstanceID) {
sbom.filteredSpdxData.ObjectMeta.SetLabels(instanceID.GetLabels())
labels := instanceID.GetLabels()
for i := range labels {
if labels[i] == "" {
delete(labels, i)
}
}
sbom.filteredSpdxData.ObjectMeta.SetLabels(labels)
}

func (sc *SBOMData) AddResourceVersionIfNeeded(resourceVersion string) {
Expand Down
11 changes: 10 additions & 1 deletion pkg/storageclient/storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package storageclient

import (
gcontext "context"
"encoding/json"
"fmt"
"os"
"sync"
"time"

apimachineryerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -136,17 +138,23 @@ func (sc *StorageK8SAggregatedAPIClient) GetData(key string) (any, error) {
}
return SBOM, nil
}

func (sc *StorageK8SAggregatedAPIClient) PutData(key string, data any) error {
SBOM, ok := data.(*spdxv1beta1.SBOMSPDXv2p3Filtered)
if !ok {
return fmt.Errorf("failed to update SBOM: SBOM is not in the right form")
}
_, err := sc.clientset.SpdxV1beta1().SBOMSPDXv2p3Filtereds(KubescapeNamespace).Update(gcontext.TODO(), SBOM, metav1.UpdateOptions{})
bytes, err := json.Marshal(SBOM)
if err != nil {
return err
}
_, err = sc.clientset.SpdxV1beta1().SBOMSPDXv2p3Filtereds(KubescapeNamespace).Patch(gcontext.TODO(), key, types.StrategicMergePatchType, bytes, metav1.PatchOptions{})
if err != nil {
return err
}
return nil
}

func (sc *StorageK8SAggregatedAPIClient) PostData(key string, data any) error {
SBOM, ok := data.(*spdxv1beta1.SBOMSPDXv2p3Filtered)
if !ok {
Expand All @@ -159,6 +167,7 @@ func (sc *StorageK8SAggregatedAPIClient) PostData(key string, data any) error {
SBOM.ObjectMeta = retSBOM.ObjectMeta
return nil
}

func (sc *StorageK8SAggregatedAPIClient) GetResourceVersion(key string) string {
SBOM, err := sc.clientset.SpdxV1beta1().SBOMSPDXv2p3Filtereds(KubescapeNamespace).Get(gcontext.TODO(), key, metav1.GetOptions{})
if err != nil {
Expand Down

0 comments on commit b1e056e

Please sign in to comment.