Skip to content

Commit b0852ea

Browse files
author
Vadim Rutkovsky
committed
Reapply "OCPBUGS-38120: project: run validation function when deleting an object"
This reverts commit 28133f9.
1 parent eefb161 commit b0852ea

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

pkg/project/apiserver/registry/project/proxy/proxy.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,26 @@ func (s *REST) Delete(ctx context.Context, name string, objectFunc rest.Validate
214214
if options != nil {
215215
opts = *options
216216
}
217+
if objectFunc != nil {
218+
obj, err := s.Get(ctx, name, &metav1.GetOptions{})
219+
if err != nil {
220+
return nil, false, err
221+
}
222+
projectObj, ok := obj.(*projectapi.Project)
223+
if !ok || projectObj == nil {
224+
return nil, false, fmt.Errorf("not a project: %#v", obj)
225+
}
226+
227+
// Make sure the object hasn't changed between Get and Delete - pass UID and RV to delete options
228+
if opts.Preconditions == nil {
229+
opts.Preconditions = &metav1.Preconditions{}
230+
}
231+
opts.Preconditions.UID = &projectObj.UID
232+
opts.Preconditions.ResourceVersion = &projectObj.ResourceVersion
233+
234+
if err := objectFunc(ctx, obj); err != nil {
235+
return nil, false, err
236+
}
237+
}
217238
return &metav1.Status{Status: metav1.StatusSuccess}, false, s.client.Delete(ctx, name, opts)
218239
}

pkg/project/apiserver/registry/project/proxy/proxy_test.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package proxy
22

33
import (
4+
"context"
45
"strings"
56
"testing"
67

78
corev1 "k8s.io/api/core/v1"
89
"k8s.io/apimachinery/pkg/api/errors"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/labels"
12+
"k8s.io/apimachinery/pkg/runtime"
1113
"k8s.io/apiserver/pkg/authentication/user"
1214
apirequest "k8s.io/apiserver/pkg/endpoints/request"
1315
"k8s.io/apiserver/pkg/registry/rest"
@@ -101,6 +103,27 @@ func TestCreateProjectOK(t *testing.T) {
101103
}
102104
}
103105

106+
func TestCreateProjectValidation(t *testing.T) {
107+
mockClient := &fake.Clientset{}
108+
storage := NewREST(mockClient.CoreV1().Namespaces(), &mockLister{}, nil, nil)
109+
110+
validationCalled := false
111+
validationFunc := func(ctx context.Context, obj runtime.Object) error {
112+
validationCalled = true
113+
return nil
114+
}
115+
116+
_, err := storage.Create(apirequest.NewContext(), &projectapi.Project{
117+
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
118+
}, validationFunc, &metav1.CreateOptions{})
119+
if err != nil {
120+
t.Errorf("Unexpected non-nil error: %#v", err)
121+
}
122+
if !validationCalled {
123+
t.Errorf("Expected validation function to be called")
124+
}
125+
}
126+
104127
func TestGetProjectOK(t *testing.T) {
105128
mockClient := fake.NewSimpleClientset(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
106129
storage := NewREST(mockClient.CoreV1().Namespaces(), &mockLister{}, nil, nil)
@@ -136,9 +159,26 @@ func TestDeleteProject(t *testing.T) {
136159
t.Errorf("Expected status=success, got: %#v", status)
137160
}
138161
if len(mockClient.Actions()) != 1 {
139-
t.Errorf("Expected client action for delete")
162+
t.Errorf("Expected client action for delete, got %v", mockClient.Actions())
140163
}
141164
if !mockClient.Actions()[0].Matches("delete", "namespaces") {
142-
t.Errorf("Expected call to delete-namespace")
165+
t.Errorf("Expected call to delete-namespace, got %#v", mockClient.Actions()[0])
166+
}
167+
}
168+
169+
func TestDeleteProjectValidation(t *testing.T) {
170+
mockClient := &fake.Clientset{}
171+
storage := REST{
172+
client: mockClient.CoreV1().Namespaces(),
173+
}
174+
validationCalled := false
175+
validationFunc := func(ctx context.Context, obj runtime.Object) error {
176+
validationCalled = true
177+
return nil
178+
}
179+
180+
storage.Delete(apirequest.NewContext(), "foo", validationFunc, &metav1.DeleteOptions{})
181+
if !validationCalled {
182+
t.Errorf("Expected validation function to be called")
143183
}
144184
}

0 commit comments

Comments
 (0)