Skip to content

Commit 88c9dd7

Browse files
committed
Update AllAlpha integration tests to recognize CBOR.
Setting AllAlpha=true in integration tests changes the dynamic client request encoding and the custom resource storage encoding to CBOR. The etcd storage path is updated to accept either JSON or CBOR as storage encoding. The client feature gate controlling the dynamic client request encoding is temporarily disabled until the serving codecs for builtin APIs are wired to the CBOR apiserver feature gate.
1 parent eafe7ed commit 88c9dd7

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

test/integration/controlplane/transformation/kms_transformation_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import (
5353
mock "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/testing/v1beta1"
5454
utilfeature "k8s.io/apiserver/pkg/util/feature"
5555
"k8s.io/client-go/dynamic"
56+
clientfeatures "k8s.io/client-go/features"
57+
clientfeaturestesting "k8s.io/client-go/features/testing"
5658
"k8s.io/client-go/rest"
5759
featuregatetesting "k8s.io/component-base/featuregate/testing"
5860
kmsapi "k8s.io/kms/apis/v1beta1"
@@ -629,6 +631,9 @@ resources:
629631
// Need to enable this explicitly as the feature is deprecated
630632
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv1, true)
631633

634+
// TODO: Remove this override when the codecs used for serving all built-in APIs are wired to the apiserver feature gate.
635+
clientfeaturestesting.SetFeatureDuringTest(t, clientfeatures.ClientsPreferCBOR, false)
636+
632637
test, err := newTransformTest(t, encryptionConfig, false, "", nil)
633638
if err != nil {
634639
t.Fatalf("failed to start KUBE API Server with encryptionConfig")

test/integration/etcd/etcd_storage_path_test.go

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package etcd
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"fmt"
2322
"path/filepath"
2423
"reflect"
@@ -33,10 +32,17 @@ import (
3332
"k8s.io/apimachinery/pkg/api/meta"
3433
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3534
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
35+
"k8s.io/apimachinery/pkg/runtime"
3636
"k8s.io/apimachinery/pkg/runtime/schema"
37+
"k8s.io/apimachinery/pkg/runtime/serializer/cbor"
38+
"k8s.io/apimachinery/pkg/runtime/serializer/json"
39+
"k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
40+
utiljson "k8s.io/apimachinery/pkg/util/json"
3741
"k8s.io/apimachinery/pkg/util/sets"
3842
"k8s.io/apiserver/pkg/util/feature"
3943
"k8s.io/client-go/dynamic"
44+
clientfeatures "k8s.io/client-go/features"
45+
clientfeaturestesting "k8s.io/client-go/features/testing"
4046
featuregatetesting "k8s.io/component-base/featuregate/testing"
4147
)
4248

@@ -77,6 +83,9 @@ func TestEtcdStoragePath(t *testing.T) {
7783
featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, "AllAlpha", true)
7884
featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, "AllBeta", true)
7985

86+
// TODO: Remove this override when the codecs used for serving all built-in APIs are wired to the apiserver feature gate.
87+
clientfeaturestesting.SetFeatureDuringTest(t, clientfeatures.ClientsPreferCBOR, false)
88+
8089
apiServer := StartRealAPIServerOrDie(t)
8190
defer apiServer.Cleanup()
8291
defer dumpEtcdKVOnFailure(t, apiServer.KV)
@@ -124,7 +133,8 @@ func TestEtcdStoragePath(t *testing.T) {
124133
err error
125134
)
126135
if shouldCreate {
127-
if input, err = jsonToMetaObject([]byte(testData.Stub)); err != nil || input.isEmpty() {
136+
input = new(metaObject)
137+
if err = utiljson.Unmarshal([]byte(testData.Stub), input); err != nil || input.isEmpty() {
128138
t.Fatalf("invalid test data for %s: %v", gvResource, err)
129139
}
130140
// unset type meta fields - we only set these in the CRD test data and it makes
@@ -152,7 +162,20 @@ func TestEtcdStoragePath(t *testing.T) {
152162
}
153163
}
154164

155-
output, err := getFromEtcd(apiServer.KV, testData.ExpectedEtcdPath)
165+
// Build a decoder that can decode JSON and CBOR from storage.
166+
scheme := runtime.NewScheme()
167+
if testData.ExpectedGVK != nil {
168+
scheme.AddKnownTypeWithName(*testData.ExpectedGVK, &metaObject{})
169+
} else {
170+
scheme.AddKnownTypeWithName(gvk, &metaObject{})
171+
172+
}
173+
decoder := recognizer.NewDecoder(
174+
cbor.NewSerializer(scheme, scheme),
175+
json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, json.SerializerOptions{}),
176+
)
177+
178+
output, err := getFromEtcd(decoder, apiServer.KV, testData.ExpectedEtcdPath)
156179
if err != nil {
157180
t.Fatalf("failed to get from etcd for %s: %#v", gvResource, err)
158181
}
@@ -208,7 +231,7 @@ func TestEtcdStoragePath(t *testing.T) {
208231
)
209232
}
210233

211-
actualGVK := output.getGVK()
234+
actualGVK := output.GroupVersionKind()
212235
if actualGVK != expectedGVK {
213236
t.Errorf("GVK for %s does not match, expected %s got %s", kind, expectedGVK, actualGVK)
214237
}
@@ -289,9 +312,7 @@ func getEtcdBucket(path string) string {
289312

290313
// stable fields to compare as a sanity check
291314
type metaObject struct {
292-
// all of type meta
293-
Kind string `json:"kind,omitempty"`
294-
APIVersion string `json:"apiVersion,omitempty"`
315+
metav1.TypeMeta `json:",inline"`
295316

296317
// parts of object meta
297318
Metadata struct {
@@ -300,8 +321,10 @@ type metaObject struct {
300321
} `json:"metadata,omitempty"`
301322
}
302323

303-
func (obj *metaObject) getGVK() schema.GroupVersionKind {
304-
return schema.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
324+
var _ runtime.Object = &metaObject{}
325+
326+
func (*metaObject) DeepCopyObject() runtime.Object {
327+
panic("unimplemented")
305328
}
306329

307330
func (obj *metaObject) isEmpty() bool {
@@ -315,14 +338,6 @@ type cleanupData struct {
315338
resource schema.GroupVersionResource
316339
}
317340

318-
func jsonToMetaObject(stub []byte) (*metaObject, error) {
319-
obj := &metaObject{}
320-
if err := json.Unmarshal(stub, obj); err != nil {
321-
return nil, err
322-
}
323-
return obj, nil
324-
}
325-
326341
func keyStringer(i interface{}) string {
327342
base := "\n\t"
328343
switch key := i.(type) {
@@ -386,15 +401,19 @@ func (c *allClient) createPrerequisites(mapper meta.RESTMapper, ns string, prere
386401
return nil
387402
}
388403

389-
func getFromEtcd(keys clientv3.KV, path string) (*metaObject, error) {
404+
func getFromEtcd(decoder runtime.Decoder, keys clientv3.KV, path string) (*metaObject, error) {
390405
response, err := keys.Get(context.Background(), path)
391406
if err != nil {
392407
return nil, err
393408
}
394409
if response.More || response.Count != 1 || len(response.Kvs) != 1 {
395410
return nil, fmt.Errorf("Invalid etcd response (not found == %v): %#v", response.Count == 0, response)
396411
}
397-
return jsonToMetaObject(response.Kvs[0].Value)
412+
var obj metaObject
413+
if err := runtime.DecodeInto(decoder, response.Kvs[0].Value, &obj); err != nil {
414+
return nil, err
415+
}
416+
return &obj, nil
398417
}
399418

400419
func diffMaps(a, b interface{}) ([]string, []string) {

0 commit comments

Comments
 (0)