| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package storage |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"mime" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/runtime" |
| 24 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 25 |
"k8s.io/apimachinery/pkg/runtime/serializer/recognizer" |
| 26 |
"k8s.io/apiserver/pkg/storage/storagebackend" |
| 27 |
) |
| 28 |
|
| 29 |
// StorageCodecConfig are the arguments passed to newStorageCodecFn |
| 30 |
type StorageCodecConfig struct { |
| 31 |
StorageMediaType string |
| 32 |
StorageSerializer runtime.StorageSerializer |
| 33 |
StorageVersion schema.GroupVersion |
| 34 |
MemoryVersion schema.GroupVersion |
| 35 |
Config storagebackend.Config |
| 36 |
|
| 37 |
EncoderDecoratorFn func(runtime.Encoder) runtime.Encoder |
| 38 |
DecoderDecoratorFn func([]runtime.Decoder) []runtime.Decoder |
| 39 |
} |
| 40 |
|
| 41 |
// NewStorageCodec assembles a storage codec for the provided storage media type, the provided serializer, and the requested |
| 42 |
// storage and memory versions. |
| 43 |
func NewStorageCodec(opts StorageCodecConfig) (runtime.Codec, runtime.GroupVersioner, error) { |
| 44 |
mediaType, _, err := mime.ParseMediaType(opts.StorageMediaType) |
| 45 |
if err != nil { |
| 46 |
return nil, nil, fmt.Errorf("%q is not a valid mime-type", opts.StorageMediaType) |
| 47 |
} |
| 48 |
|
| 49 |
supportedMediaTypes := opts.StorageSerializer.SupportedMediaTypes() |
| 50 |
serializer, ok := runtime.SerializerInfoForMediaType(supportedMediaTypes, mediaType) |
| 51 |
if !ok { |
| 52 |
supportedMediaTypeList := make([]string, len(supportedMediaTypes)) |
| 53 |
for i, mediaType := range supportedMediaTypes { |
| 54 |
supportedMediaTypeList[i] = mediaType.MediaType |
| 55 |
} |
| 56 |
return nil, nil, fmt.Errorf("unable to find serializer for %q, supported media types: %v", mediaType, supportedMediaTypeList) |
| 57 |
} |
| 58 |
|
| 59 |
s := serializer.Serializer |
| 60 |
|
| 61 |
// Give callers the opportunity to wrap encoders and decoders. For decoders, each returned decoder will |
| 62 |
// be passed to the recognizer so that multiple decoders are available. |
| 63 |
var encoder runtime.Encoder = s |
| 64 |
if opts.EncoderDecoratorFn != nil { |
| 65 |
encoder = opts.EncoderDecoratorFn(encoder) |
| 66 |
} |
| 67 |
decoders := []runtime.Decoder{ |
| 68 |
// selected decoder as the primary |
| 69 |
s, |
| 70 |
// universal deserializer as a fallback |
| 71 |
opts.StorageSerializer.UniversalDeserializer(), |
| 72 |
// base64-wrapped universal deserializer as a last resort. |
| 73 |
// this allows reading base64-encoded protobuf, which should only exist if etcd2+protobuf was used at some point. |
| 74 |
// data written that way could exist in etcd2, or could have been migrated to etcd3. |
| 75 |
// TODO: flag this type of data if we encounter it, require migration (read to decode, write to persist using a supported encoder), and remove in 1.8 |
| 76 |
runtime.NewBase64Serializer(nil, opts.StorageSerializer.UniversalDeserializer()), |
| 77 |
} |
| 78 |
if opts.DecoderDecoratorFn != nil { |
| 79 |
decoders = opts.DecoderDecoratorFn(decoders) |
| 80 |
} |
| 81 |
|
| 82 |
encodeVersioner := runtime.NewMultiGroupVersioner( |
| 83 |
opts.StorageVersion, |
| 84 |
schema.GroupKind{Group: opts.StorageVersion.Group}, |
| 85 |
schema.GroupKind{Group: opts.MemoryVersion.Group}, |
| 86 |
) |
| 87 |
|
| 88 |
// Ensure the storage receives the correct version. |
| 89 |
encoder = opts.StorageSerializer.EncoderForVersion( |
| 90 |
encoder, |
| 91 |
encodeVersioner, |
| 92 |
) |
| 93 |
decoder := opts.StorageSerializer.DecoderToVersion( |
| 94 |
recognizer.NewDecoder(decoders...), |
| 95 |
runtime.NewCoercingMultiGroupVersioner( |
| 96 |
opts.MemoryVersion, |
| 97 |
schema.GroupKind{Group: opts.MemoryVersion.Group}, |
| 98 |
schema.GroupKind{Group: opts.StorageVersion.Group}, |
| 99 |
), |
| 100 |
) |
| 101 |
|
| 102 |
return runtime.NewCodec(encoder, decoder), encodeVersioner, nil |
| 103 |
} |