-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from kubescape/silence
add presave processor to deduplicate profile info
- Loading branch information
Showing
8 changed files
with
620 additions
and
48 deletions.
There are no files selected for viewing
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
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
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
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,64 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
sets "github.com/deckarep/golang-set/v2" | ||
"github.com/kubescape/storage/pkg/apis/softwarecomposition" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
type Processor interface { | ||
PreSave(object runtime.Object) error | ||
} | ||
|
||
type DefaultProcessor struct { | ||
} | ||
|
||
var _ Processor = (*DefaultProcessor)(nil) | ||
|
||
func (d DefaultProcessor) PreSave(_ runtime.Object) error { | ||
return nil | ||
} | ||
|
||
type ApplicationProfileProcessor struct { | ||
} | ||
|
||
var _ Processor = (*ApplicationProfileProcessor)(nil) | ||
|
||
func (a ApplicationProfileProcessor) PreSave(object runtime.Object) error { | ||
profile, ok := object.(*softwarecomposition.ApplicationProfile) | ||
if !ok { | ||
return fmt.Errorf("given object is not an ApplicationProfile") | ||
} | ||
for i, container := range profile.Spec.Containers { | ||
profile.Spec.Containers[i] = deflate(container) | ||
} | ||
return nil | ||
} | ||
|
||
func deflate(container softwarecomposition.ApplicationProfileContainer) softwarecomposition.ApplicationProfileContainer { | ||
return softwarecomposition.ApplicationProfileContainer{ | ||
Name: container.Name, | ||
Capabilities: sets.NewThreadUnsafeSet(container.Capabilities...).ToSlice(), | ||
Execs: deflateStringer(container.Execs), | ||
Opens: deflateStringer(container.Opens), | ||
Syscalls: sets.NewThreadUnsafeSet(container.Syscalls...).ToSlice(), | ||
} | ||
} | ||
|
||
type Stringer interface { | ||
String() string | ||
} | ||
|
||
func deflateStringer[T Stringer](in []T) []T { | ||
var out []T | ||
set := sets.NewThreadUnsafeSet[string]() | ||
for _, item := range in { | ||
if set.Contains(item.String()) { | ||
continue | ||
} | ||
set.Add(item.String()) | ||
out = append(out, item) | ||
} | ||
return out | ||
} |
Oops, something went wrong.