Skip to content

Commit 02134a2

Browse files
authored
Merge pull request #3599 from SimonTheLeg/fix-tests
[release-0.28] Fix object validation
2 parents 9e1acea + 2b01646 commit 02134a2

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

pkg/virtual/framework/forwardingregistry/store.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,39 @@ func DefaultDynamicDelegatedStoreFuncs(
9595

9696
return delegate.Get(ctx, name, *options, subResources...)
9797
}
98-
s.CreaterFunc = func(ctx context.Context, obj runtime.Object, _ rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
98+
s.CreaterFunc = func(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
9999
unstructuredObj, ok := obj.(*unstructured.Unstructured)
100100
if !ok {
101101
return nil, fmt.Errorf("not an Unstructured: %T", obj)
102102
}
103103

104+
if err := createValidation(ctx, obj); err != nil {
105+
return nil, err
106+
}
107+
104108
delegate, err := client(ctx)
105109
if err != nil {
106110
return nil, err
107111
}
108112

109113
return delegate.Create(ctx, unstructuredObj, *options, subResources...)
110114
}
111-
s.GracefulDeleterFunc = func(ctx context.Context, name string, _ rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
115+
s.GracefulDeleterFunc = func(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
112116
delegate, err := client(ctx)
113117
if err != nil {
114118
return nil, false, err
115119
}
116120

121+
preDeleteObj, err := delegate.Get(ctx, name, metav1.GetOptions{})
122+
if err != nil {
123+
return nil, false, err
124+
}
125+
126+
err = deleteValidation(ctx, preDeleteObj)
127+
if err != nil {
128+
return nil, false, err
129+
}
130+
117131
deleter, err := dynamicextension.NewDeleterWithResults(delegate)
118132
if err != nil {
119133
return nil, false, err
@@ -171,7 +185,7 @@ func DefaultDynamicDelegatedStoreFuncs(
171185

172186
return delegate.List(ctx, v1ListOptions)
173187
}
174-
s.UpdaterFunc = func(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, _ rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
188+
s.UpdaterFunc = func(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
175189
delegate, err := client(ctx)
176190
if err != nil {
177191
return nil, false, err
@@ -228,6 +242,11 @@ func DefaultDynamicDelegatedStoreFuncs(
228242

229243
return delegate.Create(ctx, unstructuredObj, updateToCreateOptions(options), subResources...)
230244
}
245+
246+
if err := updateValidation(ctx, obj, oldObj); err != nil {
247+
return nil, err
248+
}
249+
231250
return delegate.Update(ctx, unstructuredObj, *options, subResources...)
232251
}
233252

test/e2e/virtual/initializingworkspaces/virtualworkspace_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package initializingworkspaces
1919
import (
2020
"context"
2121
"encoding/json"
22+
"fmt"
2223
"math/rand"
2324
"sort"
2425
"strings"
@@ -27,6 +28,7 @@ import (
2728

2829
jsonpatch "github.com/evanphx/json-patch"
2930
"github.com/google/go-cmp/cmp"
31+
"github.com/stretchr/testify/assert"
3032
"github.com/stretchr/testify/require"
3133

3234
corev1 "k8s.io/api/core/v1"
@@ -356,10 +358,9 @@ func TestInitializingWorkspacesVirtualWorkspaceAccess(t *testing.T) {
356358

357359
t.Log("Ensure that LIST calls through the virtual workspace eventually show the correct values")
358360
for _, wsName := range wsNames {
359-
require.Eventually(t, func() bool {
361+
require.EventuallyWithT(t, func(c *assert.CollectT) {
360362
_, err := sourceKcpClusterClient.CoreV1alpha1().Cluster(wsPath.Join(wsName)).LogicalClusters().Get(ctx, corev1alpha1.LogicalClusterName, metav1.GetOptions{})
361-
require.True(t, err == nil || errors.IsForbidden(err), "got %#v error getting logicalcluster %q, expected unauthorized or success", err, wsPath.Join(wsName))
362-
return err == nil
363+
require.NoError(c, err, "got %#v error getting logicalcluster %q, expected success", err, wsPath.Join(wsName))
363364
}, wait.ForeverTestTimeout, 100*time.Millisecond)
364365
}
365366

@@ -522,12 +523,25 @@ func TestInitializingWorkspacesVirtualWorkspaceAccess(t *testing.T) {
522523

523524
t.Logf("Attempt to do something more than just removing our initializer %q, get denied", initializer)
524525
patchBytes := patchBytesFor(logicalCluster, func(workspace *corev1alpha1.LogicalCluster) {
525-
workspace.Status.Initializers = []corev1alpha1.LogicalClusterInitializer{"wrong"}
526+
workspace.Status.Initializers = []corev1alpha1.LogicalClusterInitializer{"wrong:wrong"}
526527
})
527528
_, err = clusterClient.Cluster(wsClusterName.Path()).Patch(ctx, corev1alpha1.LogicalClusterName, types.MergePatchType, patchBytes, metav1.PatchOptions{}, "status")
528529
if !errors.IsInvalid(err) {
529530
t.Fatalf("got %#v error from patch, expected invalid", err)
530531
}
532+
// Since Invalid is a generic error, which is not exclusive to an
533+
// initializer failing our custom updateValidation, we need to check for it
534+
// as well.
535+
// Unfortunately, it is not possible to make use of
536+
// field.Error.Origin to do so, as we convert our field.ErrorList into an
537+
// errors.StatusError, thus loosing this information. As a result, our only
538+
// option is to reconstruct the expected error.
539+
expErrMsg := fmt.Sprintf("only removing the %q initializer is supported", initialization.InitializerForType(workspacetypes[initializer]))
540+
// for now using contains seems to strike the best balance between
541+
// identifying the error, while not making the test too brittle as
542+
// kubernetes statusError creation use a lot of squashing an string
543+
// manipulation to create the final exact message.
544+
require.Contains(t, err.Error(), expErrMsg)
531545

532546
t.Logf("Remove just our initializer %q", initializer)
533547
patchBytes = patchBytesFor(logicalCluster, func(workspace *corev1alpha1.LogicalCluster) {

0 commit comments

Comments
 (0)