Skip to content

Commit

Permalink
add container name, remove wrong annotations/labels
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Nov 14, 2023
1 parent 5df5b57 commit d9c6b09
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
22 changes: 9 additions & 13 deletions pkg/applicationprofilemanager/v1/applicationprofile_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,10 @@ func (am *ApplicationProfileManager) saveProfile(ctx context.Context, watchedCon
ObjectMeta: metav1.ObjectMeta{
Name: slug,
Annotations: map[string]string{
instanceidhandler.WlidMetadataKey: watchedContainer.Wlid,
instanceidhandler.InstanceIDMetadataKey: watchedContainer.InstanceID.GetStringFormatted(),
instanceidhandler.ContainerNameMetadataKey: watchedContainer.InstanceID.GetContainerName(),
instanceidhandler.ImageIDMetadataKey: watchedContainer.ImageID,
instanceidhandler.StatusMetadataKey: "",
instanceidhandler.WlidMetadataKey: watchedContainer.Wlid,
instanceidhandler.StatusMetadataKey: "",
},
Labels: utils.GetLabels(watchedContainer),
Labels: utils.GetLabels(watchedContainer, true),
},
}
// add syscalls
Expand Down Expand Up @@ -229,16 +226,15 @@ func (am *ApplicationProfileManager) saveProfile(ctx context.Context, watchedCon
ObjectMeta: metav1.ObjectMeta{
Name: slug,
Annotations: map[string]string{
instanceidhandler.WlidMetadataKey: watchedContainer.Wlid,
instanceidhandler.InstanceIDMetadataKey: watchedContainer.InstanceID.GetStringFormatted(),
instanceidhandler.ContainerNameMetadataKey: watchedContainer.InstanceID.GetContainerName(),
instanceidhandler.ImageIDMetadataKey: watchedContainer.ImageID,
instanceidhandler.StatusMetadataKey: "",
instanceidhandler.WlidMetadataKey: watchedContainer.Wlid,
instanceidhandler.StatusMetadataKey: "",
},
Labels: utils.GetLabels(watchedContainer),
Labels: utils.GetLabels(watchedContainer, true),
},
}
newProfileContainer := v1beta1.ApplicationProfileContainer{}
newProfileContainer := v1beta1.ApplicationProfileContainer{
Name: watchedContainer.InstanceID.GetContainerName(),
}
// add capabilities
newProfileContainer.Capabilities = capabilities.ToSlice()
sort.Strings(newProfileContainer.Capabilities)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sbomhandler/v1/sbomhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (sc *SBOMHandler) FilterSBOM(watchedContainer *utils.WatchedContainerData,
instanceidhandler.ImageIDMetadataKey: watchedContainer.ImageID,
instanceidhandler.StatusMetadataKey: "",
},
Labels: utils.GetLabels(watchedContainer),
Labels: utils.GetLabels(watchedContainer, false),
},
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ func Atoi(s string) int {
return i
}

func GetLabels(watchedContainer *WatchedContainerData) map[string]string {
func GetLabels(watchedContainer *WatchedContainerData, stripContainer bool) map[string]string {
labels := watchedContainer.InstanceID.GetLabels()
for i := range labels {
if labels[i] == "" {
delete(labels, i)
} else if stripContainer && i == instanceidhandler2.ContainerNameMetadataKey {
delete(labels, i)
} else {
if i == instanceidhandler2.KindMetadataKey {
labels[i] = wlid.GetKindFromWlid(watchedContainer.Wlid)
Expand Down
19 changes: 18 additions & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func TestAtoi(t *testing.T) {
func Test_GetLabels(t *testing.T) {
type args struct {
watchedContainer *WatchedContainerData
stripContainer bool
}
instanceID, _ := instanceidhandler.GenerateInstanceIDFromString("apiVersion-v1/namespace-aaa/kind-deployment/name-redis/containerName-redis")
tests := []struct {
Expand All @@ -196,10 +197,26 @@ func Test_GetLabels(t *testing.T) {
"kubescape.io/workload-namespace": "aaa",
},
},
{
name: "TestGetLabels",
args: args{
watchedContainer: &WatchedContainerData{
InstanceID: instanceID,
Wlid: "wlid://cluster-name/namespace-aaa/deployment-redis",
},
stripContainer: true,
},
want: map[string]string{
"kubescape.io/workload-api-version": "v1",
"kubescape.io/workload-kind": "Deployment",
"kubescape.io/workload-name": "redis",
"kubescape.io/workload-namespace": "aaa",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetLabels(tt.args.watchedContainer)
got := GetLabels(tt.args.watchedContainer, tt.args.stripContainer)
assert.Equal(t, tt.want, got)
})
}
Expand Down

0 comments on commit d9c6b09

Please sign in to comment.