Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reject updates on complete app profile, remove omitempty #110

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/apis/softwarecomposition/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ type ApplicationProfileSpec struct {

type ApplicationProfileContainer struct {
Name string `json:"name,omitempty"`
Capabilities []string `json:"capabilities,omitempty"`
Capabilities []string `json:"capabilities"`
// +patchMergeKey=path
// +patchStrategy=merge
Execs []ExecCalls `json:"execs,omitempty" patchStrategy:"merge" patchMergeKey:"path"`
Execs []ExecCalls `json:"execs" patchStrategy:"merge" patchMergeKey:"path"`
// +patchMergeKey=path
// +patchStrategy=merge
Opens []OpenCalls `json:"opens,omitempty" patchStrategy:"merge" patchMergeKey:"path"`
Syscalls []string `json:"syscalls,omitempty"`
Opens []OpenCalls `json:"opens" patchStrategy:"merge" patchMergeKey:"path"`
Syscalls []string `json:"syscalls"`
}

type ExecCalls struct {
Expand Down
1 change: 1 addition & 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.

15 changes: 15 additions & 0 deletions pkg/registry/softwarecomposition/applicationprofile/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

logHelpers "github.com/kubescape/go-logger/helpers"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -12,6 +13,7 @@ import (
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/names"

"github.com/kubescape/go-logger"
"github.com/kubescape/k8s-interface/instanceidhandler/v1/helpers"
"github.com/kubescape/storage/pkg/apis/softwarecomposition"
"github.com/kubescape/storage/pkg/utils"
Expand Down Expand Up @@ -62,9 +64,22 @@ func (applicationProfileStrategy) PrepareForUpdate(ctx context.Context, obj, old
newAP := obj.(*softwarecomposition.ApplicationProfile)
oldAP := old.(*softwarecomposition.ApplicationProfile)

// if we have an application profile that is marked as complete and completed, we do not allow any updates
if oldAP.Annotations[helpers.CompletionMetadataKey] == helpers.Complete && oldAP.Annotations[helpers.StatusMetadataKey] == helpers.Completed {
logger.L().Debug("application profile is marked as complete and completed, rejecting update",
logHelpers.String("name", oldAP.Name),
logHelpers.String("namespace", oldAP.Namespace))
*newAP = *oldAP // reset the new object to the old object
return
}

// completion status cannot be transitioned from 'complete' -> 'partial'
// in such case, we reject status updates
if oldAP.Annotations[helpers.CompletionMetadataKey] == helpers.Complete && newAP.Annotations[helpers.CompletionMetadataKey] == helpers.Partial {
logger.L().Debug("application profile completion status cannot be transitioned from 'complete' to 'partial', rejecting status updates",
logHelpers.String("name", oldAP.Name),
logHelpers.String("namespace", oldAP.Namespace))

newAP.Annotations[helpers.CompletionMetadataKey] = helpers.Complete

if v, ok := oldAP.Annotations[helpers.StatusMetadataKey]; ok {
Expand Down
178 changes: 177 additions & 1 deletion pkg/registry/softwarecomposition/applicationprofile/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestPrepareForUpdate(t *testing.T) {
func TestPrepareForUpdateAnnotations(t *testing.T) {
tests := []struct {
name string
oldAnnotations map[string]string
Expand Down Expand Up @@ -74,6 +74,21 @@ func TestPrepareForUpdate(t *testing.T) {
helpers.CompletionMetadataKey: "complete",
},
},
{
name: "transition from a final AP - all changes are rejected",
oldAnnotations: map[string]string{
helpers.CompletionMetadataKey: "complete",
helpers.StatusMetadataKey: "completed",
},
newAnnotations: map[string]string{
helpers.CompletionMetadataKey: "partial",
helpers.StatusMetadataKey: "initializing",
},
expected: map[string]string{
helpers.CompletionMetadataKey: "complete",
helpers.StatusMetadataKey: "completed",
},
},
}

for _, tt := range tests {
Expand All @@ -90,3 +105,164 @@ func TestPrepareForUpdate(t *testing.T) {
})
}
}

func TestPrepareForUpdateFullObj(t *testing.T) {
tests := []struct {
name string
old *softwarecomposition.ApplicationProfile
new *softwarecomposition.ApplicationProfile
expected *softwarecomposition.ApplicationProfile
}{
{
name: "transition from initializing to ready - changes are accepted",
old: &softwarecomposition.ApplicationProfile{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
helpers.CompletionMetadataKey: "complete",
helpers.StatusMetadataKey: "initializing",
},
},
Spec: softwarecomposition.ApplicationProfileSpec{
Containers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "container1",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
},
},
},
new: &softwarecomposition.ApplicationProfile{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
helpers.CompletionMetadataKey: "complete",
helpers.StatusMetadataKey: "ready",
},
},
Spec: softwarecomposition.ApplicationProfileSpec{
Containers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "container1",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
{
Name: "container2",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
},
},
},
expected: &softwarecomposition.ApplicationProfile{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
helpers.CompletionMetadataKey: "complete",
helpers.StatusMetadataKey: "ready",
},
},
Spec: softwarecomposition.ApplicationProfileSpec{
Containers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "container1",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
{
Name: "container2",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
},
},
},
},
{
name: "transition from a final AP - all changes are rejected",
old: &softwarecomposition.ApplicationProfile{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
helpers.CompletionMetadataKey: "complete",
helpers.StatusMetadataKey: "completed",
},
},
Spec: softwarecomposition.ApplicationProfileSpec{
Containers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "container1",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
},
},
},
new: &softwarecomposition.ApplicationProfile{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
helpers.CompletionMetadataKey: "partial",
helpers.StatusMetadataKey: "initializing",
},
},
Spec: softwarecomposition.ApplicationProfileSpec{
Containers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "container1",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
{
Name: "container2",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
},
},
},
expected: &softwarecomposition.ApplicationProfile{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
helpers.CompletionMetadataKey: "complete",
helpers.StatusMetadataKey: "completed",
},
},
Spec: softwarecomposition.ApplicationProfileSpec{
Containers: []softwarecomposition.ApplicationProfileContainer{
{
Name: "container1",
Capabilities: []string{},
Execs: []softwarecomposition.ExecCalls{
{Path: "/usr/bin/ls", Args: []string{"-l", "/tmp"}},
},
},
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := applicationProfileStrategy{}
s.PrepareForUpdate(context.Background(), tt.new, tt.old)
if !reflect.DeepEqual(tt.new, tt.expected) {
t.Errorf("PrepareForUpdate() = %v, want %v", tt.new, tt.expected)
}
})
}
}
Loading