From bd41389754e88ffe6ffd4d3b276644f3110b67c5 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Sat, 11 Feb 2017 11:59:17 +0100 Subject: [PATCH] k8s.io/apiserver: move server storage code into subpackage --- .../apiserver/pkg/server/storage/doc.go | 18 ++++ .../server/{ => storage}/resource_config.go | 2 +- .../{ => storage}/resource_config_test.go | 2 +- .../{ => storage}/resource_encoding_config.go | 2 +- .../pkg/server/storage/storage_codec.go | 94 +++++++++++++++++++ .../server/{ => storage}/storage_factory.go | 67 +------------ .../{ => storage}/storage_factory_test.go | 2 +- 7 files changed, 117 insertions(+), 70 deletions(-) create mode 100644 staging/src/k8s.io/apiserver/pkg/server/storage/doc.go rename staging/src/k8s.io/apiserver/pkg/server/{ => storage}/resource_config.go (99%) rename staging/src/k8s.io/apiserver/pkg/server/{ => storage}/resource_config_test.go (99%) rename staging/src/k8s.io/apiserver/pkg/server/{ => storage}/resource_encoding_config.go (99%) create mode 100644 staging/src/k8s.io/apiserver/pkg/server/storage/storage_codec.go rename staging/src/k8s.io/apiserver/pkg/server/{ => storage}/storage_factory.go (82%) rename staging/src/k8s.io/apiserver/pkg/server/{ => storage}/storage_factory_test.go (99%) diff --git a/staging/src/k8s.io/apiserver/pkg/server/storage/doc.go b/staging/src/k8s.io/apiserver/pkg/server/storage/doc.go new file mode 100644 index 00000000000..71d4e2be7d4 --- /dev/null +++ b/staging/src/k8s.io/apiserver/pkg/server/storage/doc.go @@ -0,0 +1,18 @@ +/* +Copyright 2015 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package storage contains the plumbing to setup the etcd storage of the apiserver. +package storage diff --git a/staging/src/k8s.io/apiserver/pkg/server/resource_config.go b/staging/src/k8s.io/apiserver/pkg/server/storage/resource_config.go similarity index 99% rename from staging/src/k8s.io/apiserver/pkg/server/resource_config.go rename to staging/src/k8s.io/apiserver/pkg/server/storage/resource_config.go index 06be1a55fe9..da232569a91 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/resource_config.go +++ b/staging/src/k8s.io/apiserver/pkg/server/storage/resource_config.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package server +package storage import ( "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/staging/src/k8s.io/apiserver/pkg/server/resource_config_test.go b/staging/src/k8s.io/apiserver/pkg/server/storage/resource_config_test.go similarity index 99% rename from staging/src/k8s.io/apiserver/pkg/server/resource_config_test.go rename to staging/src/k8s.io/apiserver/pkg/server/storage/resource_config_test.go index 576b24a89c5..4eb9b544c56 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/resource_config_test.go +++ b/staging/src/k8s.io/apiserver/pkg/server/storage/resource_config_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package server +package storage import ( "testing" diff --git a/staging/src/k8s.io/apiserver/pkg/server/resource_encoding_config.go b/staging/src/k8s.io/apiserver/pkg/server/storage/resource_encoding_config.go similarity index 99% rename from staging/src/k8s.io/apiserver/pkg/server/resource_encoding_config.go rename to staging/src/k8s.io/apiserver/pkg/server/storage/resource_encoding_config.go index f00ca91e52f..78cb865b744 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/resource_encoding_config.go +++ b/staging/src/k8s.io/apiserver/pkg/server/storage/resource_encoding_config.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package server +package storage import ( "k8s.io/apimachinery/pkg/apimachinery/registered" diff --git a/staging/src/k8s.io/apiserver/pkg/server/storage/storage_codec.go b/staging/src/k8s.io/apiserver/pkg/server/storage/storage_codec.go new file mode 100644 index 00000000000..c815851d343 --- /dev/null +++ b/staging/src/k8s.io/apiserver/pkg/server/storage/storage_codec.go @@ -0,0 +1,94 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package storage + +import ( + "fmt" + "mime" + + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/runtime/serializer/recognizer" + "k8s.io/apiserver/pkg/storage/storagebackend" + + "github.com/golang/glog" +) + +// StorageCodecConfig are the arguments passed to newStorageCodecFn +type StorageCodecConfig struct { + StorageMediaType string + StorageSerializer runtime.StorageSerializer + StorageVersion schema.GroupVersion + MemoryVersion schema.GroupVersion + Config storagebackend.Config + + EncoderDecoratorFn func(runtime.Encoder) runtime.Encoder + DecoderDecoratorFn func([]runtime.Decoder) []runtime.Decoder +} + +// NewStorageCodec assembles a storage codec for the provided storage media type, the provided serializer, and the requested +// storage and memory versions. +func NewStorageCodec(opts StorageCodecConfig) (runtime.Codec, error) { + mediaType, _, err := mime.ParseMediaType(opts.StorageMediaType) + if err != nil { + return nil, fmt.Errorf("%q is not a valid mime-type", opts.StorageMediaType) + } + serializer, ok := runtime.SerializerInfoForMediaType(opts.StorageSerializer.SupportedMediaTypes(), mediaType) + if !ok { + return nil, fmt.Errorf("unable to find serializer for %q", opts.StorageMediaType) + } + + s := serializer.Serializer + + // etcd2 only supports string data - we must wrap any result before returning + // TODO: storagebackend should return a boolean indicating whether it supports binary data + if !serializer.EncodesAsText && (opts.Config.Type == storagebackend.StorageTypeUnset || opts.Config.Type == storagebackend.StorageTypeETCD2) { + glog.V(4).Infof("Wrapping the underlying binary storage serializer with a base64 encoding for etcd2") + s = runtime.NewBase64Serializer(s) + } + + // Give callers the opportunity to wrap encoders and decoders. For decoders, each returned decoder will + // be passed to the recognizer so that multiple decoders are available. + var encoder runtime.Encoder = s + if opts.EncoderDecoratorFn != nil { + encoder = opts.EncoderDecoratorFn(encoder) + } + decoders := []runtime.Decoder{s, opts.StorageSerializer.UniversalDeserializer()} + if opts.DecoderDecoratorFn != nil { + decoders = opts.DecoderDecoratorFn(decoders) + } + + // Ensure the storage receives the correct version. + encoder = opts.StorageSerializer.EncoderForVersion( + encoder, + runtime.NewMultiGroupVersioner( + opts.StorageVersion, + schema.GroupKind{Group: opts.StorageVersion.Group}, + schema.GroupKind{Group: opts.MemoryVersion.Group}, + ), + ) + decoder := opts.StorageSerializer.DecoderToVersion( + recognizer.NewDecoder(decoders...), + runtime.NewMultiGroupVersioner( + opts.MemoryVersion, + schema.GroupKind{Group: opts.MemoryVersion.Group}, + schema.GroupKind{Group: opts.StorageVersion.Group}, + ), + ) + + return runtime.NewCodec(encoder, decoder), nil +} diff --git a/staging/src/k8s.io/apiserver/pkg/server/storage_factory.go b/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory.go similarity index 82% rename from staging/src/k8s.io/apiserver/pkg/server/storage_factory.go rename to staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory.go index b40306bf740..5a6e8b88272 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/storage_factory.go +++ b/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package server +package storage import ( "fmt" @@ -77,18 +77,6 @@ type DefaultStorageFactory struct { newStorageCodecFn func(opts StorageCodecConfig) (codec runtime.Codec, err error) } -// StorageCodecConfig are the arguments passed to newStorageCodecFn -type StorageCodecConfig struct { - StorageMediaType string - StorageSerializer runtime.StorageSerializer - StorageVersion schema.GroupVersion - MemoryVersion schema.GroupVersion - Config storagebackend.Config - - EncoderDecoratorFn func(runtime.Encoder) runtime.Encoder - DecoderDecoratorFn func([]runtime.Decoder) []runtime.Decoder -} - type groupResourceOverrides struct { // etcdLocation contains the list of "special" locations that are used for particular GroupResources // These are merged on top of the StorageConfig when requesting the storage.Interface for a given GroupResource @@ -278,59 +266,6 @@ func (s *DefaultStorageFactory) Backends() []string { return backends.List() } -// NewStorageCodec assembles a storage codec for the provided storage media type, the provided serializer, and the requested -// storage and memory versions. -func NewStorageCodec(opts StorageCodecConfig) (runtime.Codec, error) { - mediaType, _, err := mime.ParseMediaType(opts.StorageMediaType) - if err != nil { - return nil, fmt.Errorf("%q is not a valid mime-type", opts.StorageMediaType) - } - serializer, ok := runtime.SerializerInfoForMediaType(opts.StorageSerializer.SupportedMediaTypes(), mediaType) - if !ok { - return nil, fmt.Errorf("unable to find serializer for %q", opts.StorageMediaType) - } - - s := serializer.Serializer - - // etcd2 only supports string data - we must wrap any result before returning - // TODO: storagebackend should return a boolean indicating whether it supports binary data - if !serializer.EncodesAsText && (opts.Config.Type == storagebackend.StorageTypeUnset || opts.Config.Type == storagebackend.StorageTypeETCD2) { - glog.V(4).Infof("Wrapping the underlying binary storage serializer with a base64 encoding for etcd2") - s = runtime.NewBase64Serializer(s) - } - - // Give callers the opportunity to wrap encoders and decoders. For decoders, each returned decoder will - // be passed to the recognizer so that multiple decoders are available. - var encoder runtime.Encoder = s - if opts.EncoderDecoratorFn != nil { - encoder = opts.EncoderDecoratorFn(encoder) - } - decoders := []runtime.Decoder{s, opts.StorageSerializer.UniversalDeserializer()} - if opts.DecoderDecoratorFn != nil { - decoders = opts.DecoderDecoratorFn(decoders) - } - - // Ensure the storage receives the correct version. - encoder = opts.StorageSerializer.EncoderForVersion( - encoder, - runtime.NewMultiGroupVersioner( - opts.StorageVersion, - schema.GroupKind{Group: opts.StorageVersion.Group}, - schema.GroupKind{Group: opts.MemoryVersion.Group}, - ), - ) - decoder := opts.StorageSerializer.DecoderToVersion( - recognizer.NewDecoder(decoders...), - runtime.NewMultiGroupVersioner( - opts.MemoryVersion, - schema.GroupKind{Group: opts.MemoryVersion.Group}, - schema.GroupKind{Group: opts.StorageVersion.Group}, - ), - ) - - return runtime.NewCodec(encoder, decoder), nil -} - func (s *DefaultStorageFactory) ResourcePrefix(groupResource schema.GroupResource) string { chosenStorageResource := s.getStorageGroupResource(groupResource) groupOverride := s.Overrides[getAllResourcesAlias(chosenStorageResource)] diff --git a/staging/src/k8s.io/apiserver/pkg/server/storage_factory_test.go b/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory_test.go similarity index 99% rename from staging/src/k8s.io/apiserver/pkg/server/storage_factory_test.go rename to staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory_test.go index b644a23a6dd..72ea1a03744 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/storage_factory_test.go +++ b/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package server +package storage import ( "os"