2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
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 apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
2020-08-10 17:43:49 +00:00
|
|
|
"sort"
|
2019-01-12 04:58:27 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
goopenapispec "github.com/go-openapi/spec"
|
2019-08-30 18:33:25 +00:00
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
apiextensionshelpers "k8s.io/apiextensions-apiserver/pkg/apihelpers"
|
|
|
|
apiextensionsinternal "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
|
|
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/apiextensions-apiserver/pkg/apiserver/conversion"
|
2019-08-30 18:33:25 +00:00
|
|
|
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
|
|
|
|
structuraldefaulting "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting"
|
|
|
|
schemaobjectmeta "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/objectmeta"
|
|
|
|
structuralpruning "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning"
|
|
|
|
apiservervalidation "k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
|
2020-03-26 21:07:15 +00:00
|
|
|
informers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/v1"
|
|
|
|
listers "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/apiextensions-apiserver/pkg/controller/establish"
|
|
|
|
"k8s.io/apiextensions-apiserver/pkg/controller/finalizer"
|
2019-09-27 21:51:53 +00:00
|
|
|
"k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/apiextensions-apiserver/pkg/crdserverscheme"
|
|
|
|
"k8s.io/apiextensions-apiserver/pkg/registry/customresource"
|
|
|
|
"k8s.io/apiextensions-apiserver/pkg/registry/customresource/tableconvertor"
|
2019-08-30 18:33:25 +00:00
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
2019-08-30 18:33:25 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/protobuf"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/versioning"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
2019-08-05 18:39:53 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2019-08-30 18:33:25 +00:00
|
|
|
utilwaitgroup "k8s.io/apimachinery/pkg/util/waitgroup"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/apimachinery/pkg/version"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/apiserver/pkg/admission"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/apiserver/pkg/authorization/authorizer"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/apiserver/pkg/endpoints/handlers"
|
2019-08-30 18:33:25 +00:00
|
|
|
"k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
|
|
|
|
"k8s.io/apiserver/pkg/endpoints/metrics"
|
|
|
|
apirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2019-08-30 18:33:25 +00:00
|
|
|
"k8s.io/apiserver/pkg/features"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/apiserver/pkg/registry/generic"
|
|
|
|
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
|
2019-08-30 18:33:25 +00:00
|
|
|
genericfilters "k8s.io/apiserver/pkg/server/filters"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/apiserver/pkg/storage/storagebackend"
|
|
|
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
2019-09-27 21:51:53 +00:00
|
|
|
utilopenapi "k8s.io/apiserver/pkg/util/openapi"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/apiserver/pkg/util/webhook"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/apiserver/pkg/warning"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/client-go/scale"
|
|
|
|
"k8s.io/client-go/scale/scheme/autoscalingv1"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-09-27 21:51:53 +00:00
|
|
|
"k8s.io/kube-openapi/pkg/util/proto"
|
2020-12-01 01:06:26 +00:00
|
|
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
|
|
|
"k8s.io/kube-openapi/pkg/validation/strfmt"
|
|
|
|
"k8s.io/kube-openapi/pkg/validation/validate"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// crdHandler serves the `/apis` endpoint.
|
|
|
|
// This is registered as a filter so that it never collides with any explicitly registered endpoints
|
|
|
|
type crdHandler struct {
|
|
|
|
versionDiscoveryHandler *versionDiscoveryHandler
|
|
|
|
groupDiscoveryHandler *groupDiscoveryHandler
|
|
|
|
|
|
|
|
customStorageLock sync.Mutex
|
|
|
|
// customStorage contains a crdStorageMap
|
|
|
|
// atomic.Value has a very good read performance compared to sync.RWMutex
|
|
|
|
// see https://gist.github.com/dim/152e6bf80e1384ea72e17ac717a5000a
|
|
|
|
// which is suited for most read and rarely write cases
|
|
|
|
customStorage atomic.Value
|
|
|
|
|
|
|
|
crdLister listers.CustomResourceDefinitionLister
|
2019-09-27 21:51:53 +00:00
|
|
|
hasSynced func() bool
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
delegate http.Handler
|
|
|
|
restOptionsGetter generic.RESTOptionsGetter
|
|
|
|
admission admission.Interface
|
|
|
|
|
|
|
|
establishingController *establish.EstablishingController
|
|
|
|
|
|
|
|
// MasterCount is used to implement sleep to improve
|
|
|
|
// CRD establishing process for HA clusters.
|
|
|
|
masterCount int
|
|
|
|
|
|
|
|
converterFactory *conversion.CRConverterFactory
|
2019-04-07 17:07:55 +00:00
|
|
|
|
|
|
|
// so that we can do create on update.
|
|
|
|
authorizer authorizer.Authorizer
|
2019-08-30 18:33:25 +00:00
|
|
|
|
|
|
|
// request timeout we should delay storage teardown for
|
|
|
|
requestTimeout time.Duration
|
2019-09-27 21:51:53 +00:00
|
|
|
|
|
|
|
// minRequestTimeout applies to CR's list/watch calls
|
|
|
|
minRequestTimeout time.Duration
|
|
|
|
|
|
|
|
// staticOpenAPISpec is used as a base for the schema of CR's for the
|
|
|
|
// purpose of managing fields, it is how CR handlers get the structure
|
|
|
|
// of TypeMeta and ObjectMeta
|
2020-12-01 01:06:26 +00:00
|
|
|
staticOpenAPISpec *goopenapispec.Swagger
|
2019-10-16 05:42:28 +00:00
|
|
|
|
|
|
|
// The limit on the request size that would be accepted and decoded in a write request
|
|
|
|
// 0 means no limit.
|
|
|
|
maxRequestBodyBytes int64
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// crdInfo stores enough information to serve the storage for the custom resource
|
|
|
|
type crdInfo struct {
|
|
|
|
// spec and acceptedNames are used to compare against if a change is made on a CRD. We only update
|
|
|
|
// the storage if one of these changes.
|
2020-03-26 21:07:15 +00:00
|
|
|
spec *apiextensionsv1.CustomResourceDefinitionSpec
|
|
|
|
acceptedNames *apiextensionsv1.CustomResourceDefinitionNames
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// Deprecated per version
|
|
|
|
deprecated map[string]bool
|
|
|
|
|
|
|
|
// Warnings per version
|
|
|
|
warnings map[string][]string
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// Storage per version
|
|
|
|
storages map[string]customresource.CustomResourceStorage
|
|
|
|
|
|
|
|
// Request scope per version
|
2019-08-30 18:33:25 +00:00
|
|
|
requestScopes map[string]*handlers.RequestScope
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
// Scale scope per version
|
2019-08-30 18:33:25 +00:00
|
|
|
scaleRequestScopes map[string]*handlers.RequestScope
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
// Status scope per version
|
2019-08-30 18:33:25 +00:00
|
|
|
statusRequestScopes map[string]*handlers.RequestScope
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
// storageVersion is the CRD version used when storing the object in etcd.
|
|
|
|
storageVersion string
|
2019-08-30 18:33:25 +00:00
|
|
|
|
|
|
|
waitGroup *utilwaitgroup.SafeWaitGroup
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// crdStorageMap goes from customresourcedefinition to its storage
|
|
|
|
type crdStorageMap map[types.UID]*crdInfo
|
|
|
|
|
|
|
|
func NewCustomResourceDefinitionHandler(
|
|
|
|
versionDiscoveryHandler *versionDiscoveryHandler,
|
|
|
|
groupDiscoveryHandler *groupDiscoveryHandler,
|
|
|
|
crdInformer informers.CustomResourceDefinitionInformer,
|
|
|
|
delegate http.Handler,
|
|
|
|
restOptionsGetter generic.RESTOptionsGetter,
|
|
|
|
admission admission.Interface,
|
|
|
|
establishingController *establish.EstablishingController,
|
|
|
|
serviceResolver webhook.ServiceResolver,
|
|
|
|
authResolverWrapper webhook.AuthenticationInfoResolverWrapper,
|
2019-04-07 17:07:55 +00:00
|
|
|
masterCount int,
|
2019-08-30 18:33:25 +00:00
|
|
|
authorizer authorizer.Authorizer,
|
2019-09-27 21:51:53 +00:00
|
|
|
requestTimeout time.Duration,
|
|
|
|
minRequestTimeout time.Duration,
|
2020-12-01 01:06:26 +00:00
|
|
|
staticOpenAPISpec *goopenapispec.Swagger,
|
2019-10-16 05:42:28 +00:00
|
|
|
maxRequestBodyBytes int64) (*crdHandler, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
ret := &crdHandler{
|
|
|
|
versionDiscoveryHandler: versionDiscoveryHandler,
|
|
|
|
groupDiscoveryHandler: groupDiscoveryHandler,
|
|
|
|
customStorage: atomic.Value{},
|
|
|
|
crdLister: crdInformer.Lister(),
|
2019-09-27 21:51:53 +00:00
|
|
|
hasSynced: crdInformer.Informer().HasSynced,
|
2019-01-12 04:58:27 +00:00
|
|
|
delegate: delegate,
|
|
|
|
restOptionsGetter: restOptionsGetter,
|
|
|
|
admission: admission,
|
|
|
|
establishingController: establishingController,
|
|
|
|
masterCount: masterCount,
|
2019-04-07 17:07:55 +00:00
|
|
|
authorizer: authorizer,
|
2019-08-30 18:33:25 +00:00
|
|
|
requestTimeout: requestTimeout,
|
2019-09-27 21:51:53 +00:00
|
|
|
minRequestTimeout: minRequestTimeout,
|
|
|
|
staticOpenAPISpec: staticOpenAPISpec,
|
2019-10-16 05:42:28 +00:00
|
|
|
maxRequestBodyBytes: maxRequestBodyBytes,
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
crdInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
2019-08-30 18:33:25 +00:00
|
|
|
AddFunc: ret.createCustomResourceDefinition,
|
2019-01-12 04:58:27 +00:00
|
|
|
UpdateFunc: ret.updateCustomResourceDefinition,
|
|
|
|
DeleteFunc: func(obj interface{}) {
|
|
|
|
ret.removeDeadStorage()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
crConverterFactory, err := conversion.NewCRConverterFactory(serviceResolver, authResolverWrapper)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.converterFactory = crConverterFactory
|
|
|
|
|
|
|
|
ret.customStorage.Store(crdStorageMap{})
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// watches are expected to handle storage disruption gracefully,
|
|
|
|
// both on the server-side (by terminating the watch connection)
|
|
|
|
// and on the client side (by restarting the watch)
|
|
|
|
var longRunningFilter = genericfilters.BasicLongRunningRequestCheck(sets.NewString("watch"), sets.NewString())
|
|
|
|
|
2019-08-05 18:39:53 +00:00
|
|
|
// possiblyAcrossAllNamespacesVerbs contains those verbs which can be per-namespace and across all
|
|
|
|
// namespaces for namespaces resources. I.e. for these an empty namespace in the requestInfo is fine.
|
|
|
|
var possiblyAcrossAllNamespacesVerbs = sets.NewString("list", "watch")
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
func (r *crdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx := req.Context()
|
|
|
|
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
|
|
|
|
if !ok {
|
2019-09-27 21:51:53 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewInternalError(fmt.Errorf("no RequestInfo found in the context")),
|
|
|
|
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req,
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !requestInfo.IsResourceRequest {
|
|
|
|
pathParts := splitPath(requestInfo.Path)
|
|
|
|
// only match /apis/<group>/<version>
|
|
|
|
// only registered under /apis
|
|
|
|
if len(pathParts) == 3 {
|
2019-09-27 21:51:53 +00:00
|
|
|
if !r.hasSynced() {
|
|
|
|
responsewriters.ErrorNegotiated(serverStartingError(), Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req)
|
|
|
|
return
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
r.versionDiscoveryHandler.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// only match /apis/<group>
|
|
|
|
if len(pathParts) == 2 {
|
2019-09-27 21:51:53 +00:00
|
|
|
if !r.hasSynced() {
|
|
|
|
responsewriters.ErrorNegotiated(serverStartingError(), Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req)
|
|
|
|
return
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
r.groupDiscoveryHandler.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.delegate.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
crdName := requestInfo.Resource + "." + requestInfo.APIGroup
|
|
|
|
crd, err := r.crdLister.Get(crdName)
|
|
|
|
if apierrors.IsNotFound(err) {
|
2019-09-27 21:51:53 +00:00
|
|
|
if !r.hasSynced() {
|
|
|
|
responsewriters.ErrorNegotiated(serverStartingError(), Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
r.delegate.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-09-27 21:51:53 +00:00
|
|
|
utilruntime.HandleError(err)
|
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewInternalError(fmt.Errorf("error resolving resource")),
|
|
|
|
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req,
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-05 18:39:53 +00:00
|
|
|
|
|
|
|
// if the scope in the CRD and the scope in request differ (with exception of the verbs in possiblyAcrossAllNamespacesVerbs
|
|
|
|
// for namespaced resources), pass request to the delegate, which is supposed to lead to a 404.
|
2020-03-26 21:07:15 +00:00
|
|
|
namespacedCRD, namespacedReq := crd.Spec.Scope == apiextensionsv1.NamespaceScoped, len(requestInfo.Namespace) > 0
|
2019-08-05 18:39:53 +00:00
|
|
|
if !namespacedCRD && namespacedReq {
|
|
|
|
r.delegate.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if namespacedCRD && !namespacedReq && !possiblyAcrossAllNamespacesVerbs.Has(requestInfo.Verb) {
|
|
|
|
r.delegate.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
if !apiextensionshelpers.HasServedCRDVersion(crd, requestInfo.APIVersion) {
|
2019-01-12 04:58:27 +00:00
|
|
|
r.delegate.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
2019-08-05 18:39:53 +00:00
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// There is a small chance that a CRD is being served because NamesAccepted condition is true,
|
|
|
|
// but it becomes "unserved" because another names update leads to a conflict
|
|
|
|
// and EstablishingController wasn't fast enough to put the CRD into the Established condition.
|
|
|
|
// We accept this as the problem is small and self-healing.
|
2020-03-26 21:07:15 +00:00
|
|
|
if !apiextensionshelpers.IsCRDConditionTrue(crd, apiextensionsv1.NamesAccepted) &&
|
|
|
|
!apiextensionshelpers.IsCRDConditionTrue(crd, apiextensionsv1.Established) {
|
2019-01-12 04:58:27 +00:00
|
|
|
r.delegate.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
terminating := apiextensionshelpers.IsCRDConditionTrue(crd, apiextensionsv1.Terminating)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
crdInfo, err := r.getOrCreateServingInfoFor(crd.UID, crd.Name)
|
|
|
|
if apierrors.IsNotFound(err) {
|
|
|
|
r.delegate.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
2019-09-27 21:51:53 +00:00
|
|
|
utilruntime.HandleError(err)
|
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewInternalError(fmt.Errorf("error resolving resource")),
|
|
|
|
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req,
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
if !hasServedCRDVersion(crdInfo.spec, requestInfo.APIVersion) {
|
|
|
|
r.delegate.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
deprecated := crdInfo.deprecated[requestInfo.APIVersion]
|
|
|
|
for _, w := range crdInfo.warnings[requestInfo.APIVersion] {
|
|
|
|
warning.AddWarning(req.Context(), "", w)
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
verb := strings.ToUpper(requestInfo.Verb)
|
|
|
|
resource := requestInfo.Resource
|
|
|
|
subresource := requestInfo.Subresource
|
|
|
|
scope := metrics.CleanScope(requestInfo)
|
|
|
|
supportedTypes := []string{
|
|
|
|
string(types.JSONPatchType),
|
|
|
|
string(types.MergePatchType),
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
if utilfeature.DefaultFeatureGate.Enabled(features.ServerSideApply) {
|
|
|
|
supportedTypes = append(supportedTypes, string(types.ApplyPatchType))
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
var handlerFunc http.HandlerFunc
|
2020-03-26 21:07:15 +00:00
|
|
|
subresources, err := apiextensionshelpers.GetSubresourcesForVersion(crd, requestInfo.APIVersion)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
2019-09-27 21:51:53 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewInternalError(fmt.Errorf("could not properly serve the subresource")),
|
|
|
|
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req,
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case subresource == "status" && subresources != nil && subresources.Status != nil:
|
2019-08-30 18:33:25 +00:00
|
|
|
handlerFunc = r.serveStatus(w, req, requestInfo, crdInfo, terminating, supportedTypes)
|
2019-01-12 04:58:27 +00:00
|
|
|
case subresource == "scale" && subresources != nil && subresources.Scale != nil:
|
2019-08-30 18:33:25 +00:00
|
|
|
handlerFunc = r.serveScale(w, req, requestInfo, crdInfo, terminating, supportedTypes)
|
2019-01-12 04:58:27 +00:00
|
|
|
case len(subresource) == 0:
|
2019-08-30 18:33:25 +00:00
|
|
|
handlerFunc = r.serveResource(w, req, requestInfo, crdInfo, terminating, supportedTypes)
|
2019-01-12 04:58:27 +00:00
|
|
|
default:
|
2019-09-27 21:51:53 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewNotFound(schema.GroupResource{Group: requestInfo.APIGroup, Resource: requestInfo.Resource}, requestInfo.Name),
|
|
|
|
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req,
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
if handlerFunc != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
handlerFunc = metrics.InstrumentHandlerFunc(verb, requestInfo.APIGroup, requestInfo.APIVersion, resource, subresource, scope, metrics.APIServerComponent, deprecated, "", handlerFunc)
|
2019-08-30 18:33:25 +00:00
|
|
|
handler := genericfilters.WithWaitGroup(handlerFunc, longRunningFilter, crdInfo.waitGroup)
|
|
|
|
handler.ServeHTTP(w, req)
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *crdHandler) serveResource(w http.ResponseWriter, req *http.Request, requestInfo *apirequest.RequestInfo, crdInfo *crdInfo, terminating bool, supportedTypes []string) http.HandlerFunc {
|
|
|
|
requestScope := crdInfo.requestScopes[requestInfo.APIVersion]
|
|
|
|
storage := crdInfo.storages[requestInfo.APIVersion].CustomResource
|
|
|
|
|
|
|
|
switch requestInfo.Verb {
|
|
|
|
case "get":
|
|
|
|
return handlers.GetResource(storage, storage, requestScope)
|
|
|
|
case "list":
|
|
|
|
forceWatch := false
|
2019-09-27 21:51:53 +00:00
|
|
|
return handlers.ListResource(storage, storage, requestScope, forceWatch, r.minRequestTimeout)
|
2019-01-12 04:58:27 +00:00
|
|
|
case "watch":
|
|
|
|
forceWatch := true
|
2019-09-27 21:51:53 +00:00
|
|
|
return handlers.ListResource(storage, storage, requestScope, forceWatch, r.minRequestTimeout)
|
2019-01-12 04:58:27 +00:00
|
|
|
case "create":
|
|
|
|
if terminating {
|
2019-09-27 21:51:53 +00:00
|
|
|
err := apierrors.NewMethodNotSupported(schema.GroupResource{Group: requestInfo.APIGroup, Resource: requestInfo.Resource}, requestInfo.Verb)
|
|
|
|
err.ErrStatus.Message = fmt.Sprintf("%v not allowed while custom resource definition is terminating", requestInfo.Verb)
|
|
|
|
responsewriters.ErrorNegotiated(err, Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req)
|
2019-01-12 04:58:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return handlers.CreateResource(storage, requestScope, r.admission)
|
|
|
|
case "update":
|
|
|
|
return handlers.UpdateResource(storage, requestScope, r.admission)
|
|
|
|
case "patch":
|
|
|
|
return handlers.PatchResource(storage, requestScope, r.admission, supportedTypes)
|
|
|
|
case "delete":
|
|
|
|
allowsOptions := true
|
|
|
|
return handlers.DeleteResource(storage, allowsOptions, requestScope, r.admission)
|
|
|
|
case "deletecollection":
|
|
|
|
checkBody := true
|
|
|
|
return handlers.DeleteCollection(storage, checkBody, requestScope, r.admission)
|
|
|
|
default:
|
2019-09-27 21:51:53 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewMethodNotSupported(schema.GroupResource{Group: requestInfo.APIGroup, Resource: requestInfo.Resource}, requestInfo.Verb),
|
|
|
|
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req,
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *crdHandler) serveStatus(w http.ResponseWriter, req *http.Request, requestInfo *apirequest.RequestInfo, crdInfo *crdInfo, terminating bool, supportedTypes []string) http.HandlerFunc {
|
|
|
|
requestScope := crdInfo.statusRequestScopes[requestInfo.APIVersion]
|
|
|
|
storage := crdInfo.storages[requestInfo.APIVersion].Status
|
|
|
|
|
|
|
|
switch requestInfo.Verb {
|
|
|
|
case "get":
|
|
|
|
return handlers.GetResource(storage, nil, requestScope)
|
|
|
|
case "update":
|
|
|
|
return handlers.UpdateResource(storage, requestScope, r.admission)
|
|
|
|
case "patch":
|
|
|
|
return handlers.PatchResource(storage, requestScope, r.admission, supportedTypes)
|
|
|
|
default:
|
2019-09-27 21:51:53 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewMethodNotSupported(schema.GroupResource{Group: requestInfo.APIGroup, Resource: requestInfo.Resource}, requestInfo.Verb),
|
|
|
|
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req,
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *crdHandler) serveScale(w http.ResponseWriter, req *http.Request, requestInfo *apirequest.RequestInfo, crdInfo *crdInfo, terminating bool, supportedTypes []string) http.HandlerFunc {
|
|
|
|
requestScope := crdInfo.scaleRequestScopes[requestInfo.APIVersion]
|
|
|
|
storage := crdInfo.storages[requestInfo.APIVersion].Scale
|
|
|
|
|
|
|
|
switch requestInfo.Verb {
|
|
|
|
case "get":
|
|
|
|
return handlers.GetResource(storage, nil, requestScope)
|
|
|
|
case "update":
|
|
|
|
return handlers.UpdateResource(storage, requestScope, r.admission)
|
|
|
|
case "patch":
|
|
|
|
return handlers.PatchResource(storage, requestScope, r.admission, supportedTypes)
|
|
|
|
default:
|
2019-09-27 21:51:53 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewMethodNotSupported(schema.GroupResource{Group: requestInfo.APIGroup, Resource: requestInfo.Resource}, requestInfo.Verb),
|
|
|
|
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, req,
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// createCustomResourceDefinition removes potentially stale storage so it gets re-created
|
|
|
|
func (r *crdHandler) createCustomResourceDefinition(obj interface{}) {
|
2020-03-26 21:07:15 +00:00
|
|
|
crd := obj.(*apiextensionsv1.CustomResourceDefinition)
|
2019-08-30 18:33:25 +00:00
|
|
|
r.customStorageLock.Lock()
|
|
|
|
defer r.customStorageLock.Unlock()
|
|
|
|
// this could happen if the create event is merged from create-update events
|
|
|
|
r.removeStorage_locked(crd.UID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateCustomResourceDefinition removes potentially stale storage so it gets re-created
|
2019-01-12 04:58:27 +00:00
|
|
|
func (r *crdHandler) updateCustomResourceDefinition(oldObj, newObj interface{}) {
|
2020-03-26 21:07:15 +00:00
|
|
|
oldCRD := oldObj.(*apiextensionsv1.CustomResourceDefinition)
|
|
|
|
newCRD := newObj.(*apiextensionsv1.CustomResourceDefinition)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
r.customStorageLock.Lock()
|
|
|
|
defer r.customStorageLock.Unlock()
|
|
|
|
|
|
|
|
// Add CRD to the establishing controller queue.
|
|
|
|
// For HA clusters, we want to prevent race conditions when changing status to Established,
|
|
|
|
// so we want to be sure that CRD is Installing at least for 5 seconds before Establishing it.
|
|
|
|
// TODO: find a real HA safe checkpointing mechanism instead of an arbitrary wait.
|
2020-03-26 21:07:15 +00:00
|
|
|
if !apiextensionshelpers.IsCRDConditionTrue(newCRD, apiextensionsv1.Established) &&
|
|
|
|
apiextensionshelpers.IsCRDConditionTrue(newCRD, apiextensionsv1.NamesAccepted) {
|
2019-01-12 04:58:27 +00:00
|
|
|
if r.masterCount > 1 {
|
|
|
|
r.establishingController.QueueCRD(newCRD.Name, 5*time.Second)
|
|
|
|
} else {
|
|
|
|
r.establishingController.QueueCRD(newCRD.Name, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
if oldCRD.UID != newCRD.UID {
|
|
|
|
r.removeStorage_locked(oldCRD.UID)
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
storageMap := r.customStorage.Load().(crdStorageMap)
|
|
|
|
oldInfo, found := storageMap[newCRD.UID]
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if apiequality.Semantic.DeepEqual(&newCRD.Spec, oldInfo.spec) && apiequality.Semantic.DeepEqual(&newCRD.Status.AcceptedNames, oldInfo.acceptedNames) {
|
|
|
|
klog.V(6).Infof("Ignoring customresourcedefinition %s update because neither spec, nor accepted names changed", oldCRD.Name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
klog.V(4).Infof("Updating customresourcedefinition %s", newCRD.Name)
|
|
|
|
r.removeStorage_locked(newCRD.UID)
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// removeStorage_locked removes the cached storage with the given uid as key from the storage map. This function
|
|
|
|
// updates r.customStorage with the cleaned-up storageMap and tears down the old storage.
|
|
|
|
// NOTE: Caller MUST hold r.customStorageLock to write r.customStorage thread-safely.
|
|
|
|
func (r *crdHandler) removeStorage_locked(uid types.UID) {
|
|
|
|
storageMap := r.customStorage.Load().(crdStorageMap)
|
|
|
|
if oldInfo, ok := storageMap[uid]; ok {
|
|
|
|
// Copy because we cannot write to storageMap without a race
|
|
|
|
// as it is used without locking elsewhere.
|
|
|
|
storageMap2 := storageMap.clone()
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// Remove from the CRD info map and store the map
|
|
|
|
delete(storageMap2, uid)
|
|
|
|
r.customStorage.Store(storageMap2)
|
|
|
|
|
|
|
|
// Tear down the old storage
|
|
|
|
go r.tearDown(oldInfo)
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeDeadStorage removes REST storage that isn't being used
|
|
|
|
func (r *crdHandler) removeDeadStorage() {
|
|
|
|
allCustomResourceDefinitions, err := r.crdLister.List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.customStorageLock.Lock()
|
|
|
|
defer r.customStorageLock.Unlock()
|
|
|
|
|
|
|
|
storageMap := r.customStorage.Load().(crdStorageMap)
|
|
|
|
// Copy because we cannot write to storageMap without a race
|
2020-03-26 21:07:15 +00:00
|
|
|
storageMap2 := make(crdStorageMap)
|
|
|
|
for _, crd := range allCustomResourceDefinitions {
|
|
|
|
if _, ok := storageMap[crd.UID]; ok {
|
|
|
|
storageMap2[crd.UID] = storageMap[crd.UID]
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r.customStorage.Store(storageMap2)
|
2019-08-30 18:33:25 +00:00
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
for uid, crdInfo := range storageMap {
|
|
|
|
if _, ok := storageMap2[uid]; !ok {
|
|
|
|
klog.V(4).Infof("Removing dead CRD storage for %s/%s", crdInfo.spec.Group, crdInfo.spec.Names.Kind)
|
|
|
|
go r.tearDown(crdInfo)
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait up to a minute for requests to drain, then tear down storage
|
|
|
|
func (r *crdHandler) tearDown(oldInfo *crdInfo) {
|
|
|
|
requestsDrained := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(requestsDrained)
|
|
|
|
// Allow time for in-flight requests with a handle to the old info to register themselves
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
// Wait for in-flight requests to drain
|
|
|
|
oldInfo.waitGroup.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(r.requestTimeout * 2):
|
|
|
|
klog.Warningf("timeout waiting for requests to drain for %s/%s, tearing down storage", oldInfo.spec.Group, oldInfo.spec.Names.Kind)
|
|
|
|
case <-requestsDrained:
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, storage := range oldInfo.storages {
|
|
|
|
// destroy only the main storage. Those for the subresources share cacher and etcd clients.
|
|
|
|
storage.CustomResource.DestroyFunc()
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCustomResourceListerCollectionDeleter returns the ListerCollectionDeleter of
|
|
|
|
// the given crd.
|
2020-03-26 21:07:15 +00:00
|
|
|
func (r *crdHandler) GetCustomResourceListerCollectionDeleter(crd *apiextensionsv1.CustomResourceDefinition) (finalizer.ListerCollectionDeleter, error) {
|
2019-08-30 18:33:25 +00:00
|
|
|
info, err := r.getOrCreateServingInfoFor(crd.UID, crd.Name)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return info.storages[info.storageVersion].CustomResource, nil
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// getOrCreateServingInfoFor gets the CRD serving info for the given CRD UID if the key exists in the storage map.
|
|
|
|
// Otherwise the function fetches the up-to-date CRD using the given CRD name and creates CRD serving info.
|
|
|
|
func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crdInfo, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
storageMap := r.customStorage.Load().(crdStorageMap)
|
2019-08-30 18:33:25 +00:00
|
|
|
if ret, ok := storageMap[uid]; ok {
|
2019-01-12 04:58:27 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r.customStorageLock.Lock()
|
|
|
|
defer r.customStorageLock.Unlock()
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// Get the up-to-date CRD when we have the lock, to avoid racing with updateCustomResourceDefinition.
|
|
|
|
// If updateCustomResourceDefinition sees an update and happens later, the storage will be deleted and
|
|
|
|
// we will re-create the updated storage on demand. If updateCustomResourceDefinition happens before,
|
|
|
|
// we make sure that we observe the same up-to-date CRD.
|
|
|
|
crd, err := r.crdLister.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
storageMap = r.customStorage.Load().(crdStorageMap)
|
|
|
|
if ret, ok := storageMap[crd.UID]; ok {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
storageVersion, err := apiextensionshelpers.GetCRDStorageVersion(crd)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scope/Storages per version.
|
2019-08-30 18:33:25 +00:00
|
|
|
requestScopes := map[string]*handlers.RequestScope{}
|
2019-01-12 04:58:27 +00:00
|
|
|
storages := map[string]customresource.CustomResourceStorage{}
|
2019-08-30 18:33:25 +00:00
|
|
|
statusScopes := map[string]*handlers.RequestScope{}
|
|
|
|
scaleScopes := map[string]*handlers.RequestScope{}
|
2020-08-10 17:43:49 +00:00
|
|
|
deprecated := map[string]bool{}
|
|
|
|
warnings := map[string][]string{}
|
2019-08-30 18:33:25 +00:00
|
|
|
|
|
|
|
equivalentResourceRegistry := runtime.NewEquivalentResourceRegistry()
|
|
|
|
|
|
|
|
structuralSchemas := map[string]*structuralschema.Structural{}
|
|
|
|
for _, v := range crd.Spec.Versions {
|
2020-03-26 21:07:15 +00:00
|
|
|
val, err := apiextensionshelpers.GetSchemaForVersion(crd, v.Name)
|
2019-08-30 18:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return nil, fmt.Errorf("the server could not properly serve the CR schema")
|
|
|
|
}
|
|
|
|
if val == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
internalValidation := &apiextensionsinternal.CustomResourceValidation{}
|
|
|
|
if err := apiextensionsv1.Convert_v1_CustomResourceValidation_To_apiextensions_CustomResourceValidation(val, internalValidation, nil); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed converting CRD validation to internal version: %v", err)
|
|
|
|
}
|
|
|
|
s, err := structuralschema.NewStructural(internalValidation.OpenAPIV3Schema)
|
|
|
|
if crd.Spec.PreserveUnknownFields == false && err != nil {
|
2019-09-27 21:51:53 +00:00
|
|
|
// This should never happen. If it does, it is a programming error.
|
|
|
|
utilruntime.HandleError(fmt.Errorf("failed to convert schema to structural: %v", err))
|
2019-08-30 18:33:25 +00:00
|
|
|
return nil, fmt.Errorf("the server could not properly serve the CR schema") // validation should avoid this
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
if crd.Spec.PreserveUnknownFields == false {
|
2019-09-27 21:51:53 +00:00
|
|
|
// we don't own s completely, e.g. defaults are not deep-copied. So better make a copy here.
|
|
|
|
s = s.DeepCopy()
|
|
|
|
|
|
|
|
if err := structuraldefaulting.PruneDefaults(s); err != nil {
|
|
|
|
// This should never happen. If it does, it is a programming error.
|
|
|
|
utilruntime.HandleError(fmt.Errorf("failed to prune defaults: %v", err))
|
|
|
|
return nil, fmt.Errorf("the server could not properly serve the CR schema") // validation should avoid this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
structuralSchemas[v.Name] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
openAPIModels, err := buildOpenAPIModelsForApply(r.staticOpenAPISpec, crd)
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("error building openapi models for %s: %v", crd.Name, err))
|
|
|
|
openAPIModels = nil
|
2019-08-30 18:33:25 +00:00
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
var typeConverter fieldmanager.TypeConverter = fieldmanager.DeducedTypeConverter{}
|
|
|
|
if openAPIModels != nil {
|
|
|
|
typeConverter, err = fieldmanager.NewTypeConverter(openAPIModels, crd.Spec.PreserveUnknownFields)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
safeConverter, unsafeConverter, err := r.converterFactory.NewConverter(crd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
for _, v := range crd.Spec.Versions {
|
|
|
|
// In addition to Unstructured objects (Custom Resources), we also may sometimes need to
|
|
|
|
// decode unversioned Options objects, so we delegate to parameterScheme for such types.
|
|
|
|
parameterScheme := runtime.NewScheme()
|
|
|
|
parameterScheme.AddUnversionedTypes(schema.GroupVersion{Group: crd.Spec.Group, Version: v.Name},
|
|
|
|
&metav1.ListOptions{},
|
|
|
|
&metav1.ExportOptions{},
|
|
|
|
&metav1.GetOptions{},
|
|
|
|
&metav1.DeleteOptions{},
|
|
|
|
)
|
|
|
|
parameterCodec := runtime.NewParameterCodec(parameterScheme)
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
resource := schema.GroupVersionResource{Group: crd.Spec.Group, Version: v.Name, Resource: crd.Status.AcceptedNames.Plural}
|
2019-01-12 04:58:27 +00:00
|
|
|
kind := schema.GroupVersionKind{Group: crd.Spec.Group, Version: v.Name, Kind: crd.Status.AcceptedNames.Kind}
|
2019-08-30 18:33:25 +00:00
|
|
|
equivalentResourceRegistry.RegisterKindFor(resource, "", kind)
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
typer := newUnstructuredObjectTyper(parameterScheme)
|
|
|
|
creator := unstructuredCreator{}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
validationSchema, err := apiextensionshelpers.GetSchemaForVersion(crd, v.Name)
|
2019-08-30 18:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return nil, fmt.Errorf("the server could not properly serve the CR schema")
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
var internalValidationSchema *apiextensionsinternal.CustomResourceValidation
|
|
|
|
if validationSchema != nil {
|
|
|
|
internalValidationSchema = &apiextensionsinternal.CustomResourceValidation{}
|
|
|
|
if err := apiextensionsv1.Convert_v1_CustomResourceValidation_To_apiextensions_CustomResourceValidation(validationSchema, internalValidationSchema, nil); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to convert CRD validation to internal version: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
validator, _, err := apiservervalidation.NewSchemaValidator(internalValidationSchema)
|
2019-08-30 18:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
var statusSpec *apiextensionsinternal.CustomResourceSubresourceStatus
|
2019-08-30 18:33:25 +00:00
|
|
|
var statusValidator *validate.SchemaValidator
|
2020-03-26 21:07:15 +00:00
|
|
|
subresources, err := apiextensionshelpers.GetSubresourcesForVersion(crd, v.Name)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return nil, fmt.Errorf("the server could not properly serve the CR subresources")
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
if subresources != nil && subresources.Status != nil {
|
2019-08-30 18:33:25 +00:00
|
|
|
equivalentResourceRegistry.RegisterKindFor(resource, "status", kind)
|
2020-03-26 21:07:15 +00:00
|
|
|
statusSpec = &apiextensionsinternal.CustomResourceSubresourceStatus{}
|
|
|
|
if err := apiextensionsv1.Convert_v1_CustomResourceSubresourceStatus_To_apiextensions_CustomResourceSubresourceStatus(subresources.Status, statusSpec, nil); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed converting CRD status subresource to internal version: %v", err)
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
// for the status subresource, validate only against the status schema
|
2020-03-26 21:07:15 +00:00
|
|
|
if internalValidationSchema != nil && internalValidationSchema.OpenAPIV3Schema != nil && internalValidationSchema.OpenAPIV3Schema.Properties != nil {
|
|
|
|
if statusSchema, ok := internalValidationSchema.OpenAPIV3Schema.Properties["status"]; ok {
|
2019-08-30 18:33:25 +00:00
|
|
|
openapiSchema := &spec.Schema{}
|
2019-12-12 01:27:03 +00:00
|
|
|
if err := apiservervalidation.ConvertJSONSchemaPropsWithPostProcess(&statusSchema, openapiSchema, apiservervalidation.StripUnsupportedFormatsPostProcess); err != nil {
|
2019-08-30 18:33:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-12 01:27:03 +00:00
|
|
|
statusValidator = validate.NewSchemaValidator(openapiSchema, nil, "", strfmt.Default)
|
2019-08-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
var scaleSpec *apiextensionsinternal.CustomResourceSubresourceScale
|
|
|
|
if subresources != nil && subresources.Scale != nil {
|
2019-08-30 18:33:25 +00:00
|
|
|
equivalentResourceRegistry.RegisterKindFor(resource, "scale", autoscalingv1.SchemeGroupVersion.WithKind("Scale"))
|
2020-03-26 21:07:15 +00:00
|
|
|
scaleSpec = &apiextensionsinternal.CustomResourceSubresourceScale{}
|
|
|
|
if err := apiextensionsv1.Convert_v1_CustomResourceSubresourceScale_To_apiextensions_CustomResourceSubresourceScale(subresources.Scale, scaleSpec, nil); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed converting CRD status subresource to internal version: %v", err)
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
columns, err := getColumnsForVersion(crd, v.Name)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return nil, fmt.Errorf("the server could not properly serve the CR columns")
|
|
|
|
}
|
|
|
|
table, err := tableconvertor.New(columns)
|
|
|
|
if err != nil {
|
|
|
|
klog.V(2).Infof("The CRD for %v has an invalid printer specification, falling back to default printing: %v", kind, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
storages[v.Name] = customresource.NewStorage(
|
2019-08-30 18:33:25 +00:00
|
|
|
resource.GroupResource(),
|
|
|
|
kind,
|
2019-01-12 04:58:27 +00:00
|
|
|
schema.GroupVersionKind{Group: crd.Spec.Group, Version: v.Name, Kind: crd.Status.AcceptedNames.ListKind},
|
|
|
|
customresource.NewStrategy(
|
|
|
|
typer,
|
2020-03-26 21:07:15 +00:00
|
|
|
crd.Spec.Scope == apiextensionsv1.NamespaceScoped,
|
2019-01-12 04:58:27 +00:00
|
|
|
kind,
|
2019-08-30 18:33:25 +00:00
|
|
|
validator,
|
|
|
|
statusValidator,
|
|
|
|
structuralSchemas,
|
2019-01-12 04:58:27 +00:00
|
|
|
statusSpec,
|
|
|
|
scaleSpec,
|
|
|
|
),
|
|
|
|
crdConversionRESTOptionsGetter{
|
2019-08-30 18:33:25 +00:00
|
|
|
RESTOptionsGetter: r.restOptionsGetter,
|
|
|
|
converter: safeConverter,
|
|
|
|
decoderVersion: schema.GroupVersion{Group: crd.Spec.Group, Version: v.Name},
|
|
|
|
encoderVersion: schema.GroupVersion{Group: crd.Spec.Group, Version: storageVersion},
|
|
|
|
structuralSchemas: structuralSchemas,
|
|
|
|
structuralSchemaGK: kind.GroupKind(),
|
2020-03-26 21:07:15 +00:00
|
|
|
preserveUnknownFields: crd.Spec.PreserveUnknownFields,
|
2019-01-12 04:58:27 +00:00
|
|
|
},
|
|
|
|
crd.Status.AcceptedNames.Categories,
|
|
|
|
table,
|
|
|
|
)
|
|
|
|
|
|
|
|
selfLinkPrefix := ""
|
|
|
|
switch crd.Spec.Scope {
|
2020-03-26 21:07:15 +00:00
|
|
|
case apiextensionsv1.ClusterScoped:
|
2019-01-12 04:58:27 +00:00
|
|
|
selfLinkPrefix = "/" + path.Join("apis", crd.Spec.Group, v.Name) + "/" + crd.Status.AcceptedNames.Plural + "/"
|
2020-03-26 21:07:15 +00:00
|
|
|
case apiextensionsv1.NamespaceScoped:
|
2019-01-12 04:58:27 +00:00
|
|
|
selfLinkPrefix = "/" + path.Join("apis", crd.Spec.Group, v.Name, "namespaces") + "/"
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
clusterScoped := crd.Spec.Scope == apiextensionsv1.ClusterScoped
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// CRDs explicitly do not support protobuf, but some objects returned by the API server do
|
|
|
|
negotiatedSerializer := unstructuredNegotiatedSerializer{
|
|
|
|
typer: typer,
|
|
|
|
creator: creator,
|
|
|
|
converter: safeConverter,
|
|
|
|
structuralSchemas: structuralSchemas,
|
|
|
|
structuralSchemaGK: kind.GroupKind(),
|
2020-03-26 21:07:15 +00:00
|
|
|
preserveUnknownFields: crd.Spec.PreserveUnknownFields,
|
2019-08-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
var standardSerializers []runtime.SerializerInfo
|
|
|
|
for _, s := range negotiatedSerializer.SupportedMediaTypes() {
|
|
|
|
if s.MediaType == runtime.ContentTypeProtobuf {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
standardSerializers = append(standardSerializers, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestScopes[v.Name] = &handlers.RequestScope{
|
2019-01-12 04:58:27 +00:00
|
|
|
Namer: handlers.ContextBasedNaming{
|
|
|
|
SelfLinker: meta.NewAccessor(),
|
|
|
|
ClusterScoped: clusterScoped,
|
|
|
|
SelfLinkPathPrefix: selfLinkPrefix,
|
|
|
|
},
|
2019-08-30 18:33:25 +00:00
|
|
|
Serializer: negotiatedSerializer,
|
|
|
|
ParameterCodec: parameterCodec,
|
|
|
|
StandardSerializers: standardSerializers,
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
Creater: creator,
|
|
|
|
Convertor: safeConverter,
|
2019-08-30 18:33:25 +00:00
|
|
|
Defaulter: unstructuredDefaulter{parameterScheme, structuralSchemas, kind.GroupKind()},
|
2019-01-12 04:58:27 +00:00
|
|
|
Typer: typer,
|
|
|
|
UnsafeConvertor: unsafeConverter,
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
EquivalentResourceMapper: equivalentResourceRegistry,
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
Resource: schema.GroupVersionResource{Group: crd.Spec.Group, Version: v.Name, Resource: crd.Status.AcceptedNames.Plural},
|
|
|
|
Kind: kind,
|
|
|
|
|
|
|
|
// a handler for a specific group-version of a custom resource uses that version as the in-memory representation
|
|
|
|
HubGroupVersion: kind.GroupVersion(),
|
|
|
|
|
|
|
|
MetaGroupVersion: metav1.SchemeGroupVersion,
|
|
|
|
|
|
|
|
TableConvertor: storages[v.Name].CustomResource,
|
2019-04-07 17:07:55 +00:00
|
|
|
|
|
|
|
Authorizer: r.authorizer,
|
2019-10-16 05:42:28 +00:00
|
|
|
|
|
|
|
MaxRequestBodyBytes: r.maxRequestBodyBytes,
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
if utilfeature.DefaultFeatureGate.Enabled(features.ServerSideApply) {
|
|
|
|
reqScope := *requestScopes[v.Name]
|
2019-12-12 01:27:03 +00:00
|
|
|
reqScope.FieldManager, err = fieldmanager.NewDefaultCRDFieldManager(
|
2020-12-01 01:06:26 +00:00
|
|
|
typeConverter,
|
2019-08-30 18:33:25 +00:00
|
|
|
reqScope.Convertor,
|
|
|
|
reqScope.Defaulter,
|
2019-12-12 01:27:03 +00:00
|
|
|
reqScope.Creater,
|
|
|
|
reqScope.Kind,
|
2019-08-30 18:33:25 +00:00
|
|
|
reqScope.HubGroupVersion,
|
2020-12-01 01:06:26 +00:00
|
|
|
false,
|
2019-08-30 18:33:25 +00:00
|
|
|
)
|
2019-09-27 21:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
requestScopes[v.Name] = &reqScope
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
// override scaleSpec subresource values
|
|
|
|
// shallow copy
|
2019-08-30 18:33:25 +00:00
|
|
|
scaleScope := *requestScopes[v.Name]
|
2019-01-12 04:58:27 +00:00
|
|
|
scaleConverter := scale.NewScaleConverter()
|
|
|
|
scaleScope.Subresource = "scale"
|
|
|
|
scaleScope.Serializer = serializer.NewCodecFactory(scaleConverter.Scheme())
|
|
|
|
scaleScope.Kind = autoscalingv1.SchemeGroupVersion.WithKind("Scale")
|
|
|
|
scaleScope.Namer = handlers.ContextBasedNaming{
|
|
|
|
SelfLinker: meta.NewAccessor(),
|
|
|
|
ClusterScoped: clusterScoped,
|
|
|
|
SelfLinkPathPrefix: selfLinkPrefix,
|
|
|
|
SelfLinkPathSuffix: "/scale",
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
// TODO(issues.k8s.io/82046): We can't effectively track ownership on scale requests yet.
|
|
|
|
scaleScope.FieldManager = nil
|
2019-08-30 18:33:25 +00:00
|
|
|
scaleScopes[v.Name] = &scaleScope
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
// override status subresource values
|
|
|
|
// shallow copy
|
2019-08-30 18:33:25 +00:00
|
|
|
statusScope := *requestScopes[v.Name]
|
2020-12-01 01:06:26 +00:00
|
|
|
if utilfeature.DefaultFeatureGate.Enabled(features.ServerSideApply) {
|
|
|
|
statusScope.FieldManager, err = fieldmanager.NewDefaultCRDFieldManager(
|
|
|
|
typeConverter,
|
|
|
|
statusScope.Convertor,
|
|
|
|
statusScope.Defaulter,
|
|
|
|
statusScope.Creater,
|
|
|
|
statusScope.Kind,
|
|
|
|
statusScope.HubGroupVersion,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
statusScope.Subresource = "status"
|
|
|
|
statusScope.Namer = handlers.ContextBasedNaming{
|
|
|
|
SelfLinker: meta.NewAccessor(),
|
|
|
|
ClusterScoped: clusterScoped,
|
|
|
|
SelfLinkPathPrefix: selfLinkPrefix,
|
|
|
|
SelfLinkPathSuffix: "/status",
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
statusScopes[v.Name] = &statusScope
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
if v.Deprecated {
|
|
|
|
deprecated[v.Name] = true
|
|
|
|
if utilfeature.DefaultFeatureGate.Enabled(features.WarningHeaders) {
|
|
|
|
if v.DeprecationWarning != nil {
|
|
|
|
warnings[v.Name] = append(warnings[v.Name], *v.DeprecationWarning)
|
|
|
|
} else {
|
|
|
|
warnings[v.Name] = append(warnings[v.Name], defaultDeprecationWarning(v.Name, crd.Spec))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret := &crdInfo{
|
|
|
|
spec: &crd.Spec,
|
|
|
|
acceptedNames: &crd.Status.AcceptedNames,
|
|
|
|
storages: storages,
|
|
|
|
requestScopes: requestScopes,
|
|
|
|
scaleRequestScopes: scaleScopes,
|
|
|
|
statusRequestScopes: statusScopes,
|
2020-08-10 17:43:49 +00:00
|
|
|
deprecated: deprecated,
|
|
|
|
warnings: warnings,
|
2019-01-12 04:58:27 +00:00
|
|
|
storageVersion: storageVersion,
|
2019-08-30 18:33:25 +00:00
|
|
|
waitGroup: &utilwaitgroup.SafeWaitGroup{},
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy because we cannot write to storageMap without a race
|
|
|
|
// as it is used without locking elsewhere.
|
|
|
|
storageMap2 := storageMap.clone()
|
|
|
|
|
|
|
|
storageMap2[crd.UID] = ret
|
|
|
|
r.customStorage.Store(storageMap2)
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func defaultDeprecationWarning(deprecatedVersion string, crd apiextensionsv1.CustomResourceDefinitionSpec) string {
|
|
|
|
msg := fmt.Sprintf("%s/%s %s is deprecated", crd.Group, deprecatedVersion, crd.Names.Kind)
|
|
|
|
|
|
|
|
var servedNonDeprecatedVersions []string
|
|
|
|
for _, v := range crd.Versions {
|
|
|
|
if v.Served && !v.Deprecated && version.CompareKubeAwareVersionStrings(deprecatedVersion, v.Name) < 0 {
|
|
|
|
servedNonDeprecatedVersions = append(servedNonDeprecatedVersions, v.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(servedNonDeprecatedVersions) == 0 {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
sort.Slice(servedNonDeprecatedVersions, func(i, j int) bool {
|
|
|
|
return version.CompareKubeAwareVersionStrings(servedNonDeprecatedVersions[i], servedNonDeprecatedVersions[j]) > 0
|
|
|
|
})
|
|
|
|
msg += fmt.Sprintf("; use %s/%s %s", crd.Group, servedNonDeprecatedVersions[0], crd.Names.Kind)
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
type unstructuredNegotiatedSerializer struct {
|
|
|
|
typer runtime.ObjectTyper
|
|
|
|
creator runtime.ObjectCreater
|
|
|
|
converter runtime.ObjectConvertor
|
2019-08-30 18:33:25 +00:00
|
|
|
|
|
|
|
structuralSchemas map[string]*structuralschema.Structural // by version
|
|
|
|
structuralSchemaGK schema.GroupKind
|
|
|
|
preserveUnknownFields bool
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s unstructuredNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInfo {
|
|
|
|
return []runtime.SerializerInfo{
|
|
|
|
{
|
|
|
|
MediaType: "application/json",
|
2019-08-30 18:33:25 +00:00
|
|
|
MediaTypeType: "application",
|
|
|
|
MediaTypeSubType: "json",
|
2019-01-12 04:58:27 +00:00
|
|
|
EncodesAsText: true,
|
|
|
|
Serializer: json.NewSerializer(json.DefaultMetaFactory, s.creator, s.typer, false),
|
|
|
|
PrettySerializer: json.NewSerializer(json.DefaultMetaFactory, s.creator, s.typer, true),
|
|
|
|
StreamSerializer: &runtime.StreamSerializerInfo{
|
|
|
|
EncodesAsText: true,
|
|
|
|
Serializer: json.NewSerializer(json.DefaultMetaFactory, s.creator, s.typer, false),
|
|
|
|
Framer: json.Framer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-08-30 18:33:25 +00:00
|
|
|
MediaType: "application/yaml",
|
|
|
|
MediaTypeType: "application",
|
|
|
|
MediaTypeSubType: "yaml",
|
|
|
|
EncodesAsText: true,
|
|
|
|
Serializer: json.NewYAMLSerializer(json.DefaultMetaFactory, s.creator, s.typer),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.kubernetes.protobuf",
|
|
|
|
MediaTypeType: "application",
|
|
|
|
MediaTypeSubType: "vnd.kubernetes.protobuf",
|
|
|
|
Serializer: protobuf.NewSerializer(s.creator, s.typer),
|
|
|
|
StreamSerializer: &runtime.StreamSerializerInfo{
|
|
|
|
Serializer: protobuf.NewRawSerializer(s.creator, s.typer),
|
|
|
|
Framer: protobuf.LengthDelimitedFramer,
|
|
|
|
},
|
2019-01-12 04:58:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s unstructuredNegotiatedSerializer) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder {
|
|
|
|
return versioning.NewCodec(encoder, nil, s.converter, Scheme, Scheme, Scheme, gv, nil, "crdNegotiatedSerializer")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s unstructuredNegotiatedSerializer) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder {
|
2019-08-30 18:33:25 +00:00
|
|
|
d := schemaCoercingDecoder{delegate: decoder, validator: unstructuredSchemaCoercer{structuralSchemas: s.structuralSchemas, structuralSchemaGK: s.structuralSchemaGK, preserveUnknownFields: s.preserveUnknownFields}}
|
|
|
|
return versioning.NewCodec(nil, d, runtime.UnsafeObjectConvertor(Scheme), Scheme, Scheme, unstructuredDefaulter{
|
|
|
|
delegate: Scheme,
|
|
|
|
structuralSchemas: s.structuralSchemas,
|
|
|
|
structuralSchemaGK: s.structuralSchemaGK,
|
|
|
|
}, nil, gv, "unstructuredNegotiatedSerializer")
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UnstructuredObjectTyper struct {
|
|
|
|
Delegate runtime.ObjectTyper
|
|
|
|
UnstructuredTyper runtime.ObjectTyper
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUnstructuredObjectTyper(Delegate runtime.ObjectTyper) UnstructuredObjectTyper {
|
|
|
|
return UnstructuredObjectTyper{
|
|
|
|
Delegate: Delegate,
|
|
|
|
UnstructuredTyper: crdserverscheme.NewUnstructuredObjectTyper(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t UnstructuredObjectTyper) ObjectKinds(obj runtime.Object) ([]schema.GroupVersionKind, bool, error) {
|
|
|
|
// Delegate for things other than Unstructured.
|
|
|
|
if _, ok := obj.(runtime.Unstructured); !ok {
|
|
|
|
return t.Delegate.ObjectKinds(obj)
|
|
|
|
}
|
|
|
|
return t.UnstructuredTyper.ObjectKinds(obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t UnstructuredObjectTyper) Recognizes(gvk schema.GroupVersionKind) bool {
|
|
|
|
return t.Delegate.Recognizes(gvk) || t.UnstructuredTyper.Recognizes(gvk)
|
|
|
|
}
|
|
|
|
|
|
|
|
type unstructuredCreator struct{}
|
|
|
|
|
|
|
|
func (c unstructuredCreator) New(kind schema.GroupVersionKind) (runtime.Object, error) {
|
|
|
|
ret := &unstructured.Unstructured{}
|
|
|
|
ret.SetGroupVersionKind(kind)
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type unstructuredDefaulter struct {
|
2019-08-30 18:33:25 +00:00
|
|
|
delegate runtime.ObjectDefaulter
|
|
|
|
structuralSchemas map[string]*structuralschema.Structural // by version
|
|
|
|
structuralSchemaGK schema.GroupKind
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d unstructuredDefaulter) Default(in runtime.Object) {
|
2019-08-30 18:33:25 +00:00
|
|
|
// Delegate for things other than Unstructured, and other GKs
|
|
|
|
u, ok := in.(runtime.Unstructured)
|
|
|
|
if !ok || u.GetObjectKind().GroupVersionKind().GroupKind() != d.structuralSchemaGK {
|
2019-01-12 04:58:27 +00:00
|
|
|
d.delegate.Default(in)
|
2019-08-30 18:33:25 +00:00
|
|
|
return
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
|
|
|
|
structuraldefaulting.Default(u.UnstructuredContent(), d.structuralSchemas[u.GetObjectKind().GroupVersionKind().Version])
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CRDRESTOptionsGetter struct {
|
|
|
|
StorageConfig storagebackend.Config
|
|
|
|
StoragePrefix string
|
|
|
|
EnableWatchCache bool
|
|
|
|
DefaultWatchCacheSize int
|
|
|
|
EnableGarbageCollection bool
|
|
|
|
DeleteCollectionWorkers int
|
|
|
|
CountMetricPollPeriod time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t CRDRESTOptionsGetter) GetRESTOptions(resource schema.GroupResource) (generic.RESTOptions, error) {
|
|
|
|
ret := generic.RESTOptions{
|
|
|
|
StorageConfig: &t.StorageConfig,
|
|
|
|
Decorator: generic.UndecoratedStorage,
|
|
|
|
EnableGarbageCollection: t.EnableGarbageCollection,
|
|
|
|
DeleteCollectionWorkers: t.DeleteCollectionWorkers,
|
|
|
|
ResourcePrefix: resource.Group + "/" + resource.Resource,
|
|
|
|
CountMetricPollPeriod: t.CountMetricPollPeriod,
|
|
|
|
}
|
|
|
|
if t.EnableWatchCache {
|
2020-08-10 17:43:49 +00:00
|
|
|
ret.Decorator = genericregistry.StorageWithCacher()
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clone returns a clone of the provided crdStorageMap.
|
|
|
|
// The clone is a shallow copy of the map.
|
|
|
|
func (in crdStorageMap) clone() crdStorageMap {
|
|
|
|
if in == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out := make(crdStorageMap, len(in))
|
|
|
|
for key, value := range in {
|
|
|
|
out[key] = value
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// crdConversionRESTOptionsGetter overrides the codec with one using the
|
|
|
|
// provided custom converter and custom encoder and decoder version.
|
|
|
|
type crdConversionRESTOptionsGetter struct {
|
|
|
|
generic.RESTOptionsGetter
|
2019-08-30 18:33:25 +00:00
|
|
|
converter runtime.ObjectConvertor
|
|
|
|
encoderVersion schema.GroupVersion
|
|
|
|
decoderVersion schema.GroupVersion
|
|
|
|
structuralSchemas map[string]*structuralschema.Structural // by version
|
|
|
|
structuralSchemaGK schema.GroupKind
|
|
|
|
preserveUnknownFields bool
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t crdConversionRESTOptionsGetter) GetRESTOptions(resource schema.GroupResource) (generic.RESTOptions, error) {
|
|
|
|
ret, err := t.RESTOptionsGetter.GetRESTOptions(resource)
|
|
|
|
if err == nil {
|
|
|
|
d := schemaCoercingDecoder{delegate: ret.StorageConfig.Codec, validator: unstructuredSchemaCoercer{
|
2019-08-30 18:33:25 +00:00
|
|
|
// drop invalid fields while decoding old CRs (before we haven't had any ObjectMeta validation)
|
|
|
|
dropInvalidMetadata: true,
|
2019-09-27 21:51:53 +00:00
|
|
|
repairGeneration: true,
|
2019-08-30 18:33:25 +00:00
|
|
|
structuralSchemas: t.structuralSchemas,
|
|
|
|
structuralSchemaGK: t.structuralSchemaGK,
|
|
|
|
preserveUnknownFields: t.preserveUnknownFields,
|
|
|
|
}}
|
|
|
|
c := schemaCoercingConverter{delegate: t.converter, validator: unstructuredSchemaCoercer{
|
|
|
|
structuralSchemas: t.structuralSchemas,
|
|
|
|
structuralSchemaGK: t.structuralSchemaGK,
|
|
|
|
preserveUnknownFields: t.preserveUnknownFields,
|
2019-01-12 04:58:27 +00:00
|
|
|
}}
|
|
|
|
ret.StorageConfig.Codec = versioning.NewCodec(
|
|
|
|
ret.StorageConfig.Codec,
|
|
|
|
d,
|
|
|
|
c,
|
|
|
|
&unstructuredCreator{},
|
|
|
|
crdserverscheme.NewUnstructuredObjectTyper(),
|
2019-08-30 18:33:25 +00:00
|
|
|
&unstructuredDefaulter{
|
|
|
|
delegate: Scheme,
|
|
|
|
structuralSchemaGK: t.structuralSchemaGK,
|
|
|
|
structuralSchemas: t.structuralSchemas,
|
|
|
|
},
|
2019-01-12 04:58:27 +00:00
|
|
|
t.encoderVersion,
|
|
|
|
t.decoderVersion,
|
|
|
|
"crdRESTOptions",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// schemaCoercingDecoder calls the delegate decoder, and then applies the Unstructured schema validator
|
|
|
|
// to coerce the schema.
|
|
|
|
type schemaCoercingDecoder struct {
|
|
|
|
delegate runtime.Decoder
|
|
|
|
validator unstructuredSchemaCoercer
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ runtime.Decoder = schemaCoercingDecoder{}
|
|
|
|
|
|
|
|
func (d schemaCoercingDecoder) Decode(data []byte, defaults *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
|
|
|
|
obj, gvk, err := d.delegate.Decode(data, defaults, into)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gvk, err
|
|
|
|
}
|
|
|
|
if u, ok := obj.(*unstructured.Unstructured); ok {
|
|
|
|
if err := d.validator.apply(u); err != nil {
|
|
|
|
return nil, gvk, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj, gvk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// schemaCoercingConverter calls the delegate converter and applies the Unstructured validator to
|
|
|
|
// coerce the schema.
|
|
|
|
type schemaCoercingConverter struct {
|
|
|
|
delegate runtime.ObjectConvertor
|
|
|
|
validator unstructuredSchemaCoercer
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ runtime.ObjectConvertor = schemaCoercingConverter{}
|
|
|
|
|
|
|
|
func (v schemaCoercingConverter) Convert(in, out, context interface{}) error {
|
|
|
|
if err := v.delegate.Convert(in, out, context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if u, ok := out.(*unstructured.Unstructured); ok {
|
|
|
|
if err := v.validator.apply(u); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v schemaCoercingConverter) ConvertToVersion(in runtime.Object, gv runtime.GroupVersioner) (runtime.Object, error) {
|
|
|
|
out, err := v.delegate.ConvertToVersion(in, gv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if u, ok := out.(*unstructured.Unstructured); ok {
|
|
|
|
if err := v.validator.apply(u); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v schemaCoercingConverter) ConvertFieldLabel(gvk schema.GroupVersionKind, label, value string) (string, string, error) {
|
|
|
|
return v.delegate.ConvertFieldLabel(gvk, label, value)
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// unstructuredSchemaCoercer adds to unstructured unmarshalling what json.Unmarshal does
|
|
|
|
// in addition for native types when decoding into Golang structs:
|
|
|
|
//
|
|
|
|
// - validating and pruning ObjectMeta
|
2020-12-01 01:06:26 +00:00
|
|
|
// - generic pruning of unknown fields following a structural schema
|
|
|
|
// - removal of non-defaulted non-nullable null map values.
|
2019-01-12 04:58:27 +00:00
|
|
|
type unstructuredSchemaCoercer struct {
|
|
|
|
dropInvalidMetadata bool
|
2019-09-27 21:51:53 +00:00
|
|
|
repairGeneration bool
|
2019-08-30 18:33:25 +00:00
|
|
|
|
|
|
|
structuralSchemas map[string]*structuralschema.Structural
|
|
|
|
structuralSchemaGK schema.GroupKind
|
|
|
|
preserveUnknownFields bool
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *unstructuredSchemaCoercer) apply(u *unstructured.Unstructured) error {
|
|
|
|
// save implicit meta fields that don't have to be specified in the validation spec
|
|
|
|
kind, foundKind, err := unstructured.NestedString(u.UnstructuredContent(), "kind")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apiVersion, foundApiVersion, err := unstructured.NestedString(u.UnstructuredContent(), "apiVersion")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
objectMeta, foundObjectMeta, err := schemaobjectmeta.GetObjectMeta(u.Object, v.dropInvalidMetadata)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// compare group and kind because also other object like DeleteCollection options pass through here
|
|
|
|
gv, err := schema.ParseGroupVersion(apiVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if gv.Group == v.structuralSchemaGK.Group && kind == v.structuralSchemaGK.Kind {
|
|
|
|
if !v.preserveUnknownFields {
|
|
|
|
// TODO: switch over pruning and coercing at the root to schemaobjectmeta.Coerce too
|
|
|
|
structuralpruning.Prune(u.Object, v.structuralSchemas[gv.Version], false)
|
2020-12-01 01:06:26 +00:00
|
|
|
structuraldefaulting.PruneNonNullableNullsWithoutDefaults(u.Object, v.structuralSchemas[gv.Version])
|
2019-08-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
if err := schemaobjectmeta.Coerce(nil, u.Object, v.structuralSchemas[gv.Version], false, v.dropInvalidMetadata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
// fixup missing generation in very old CRs
|
|
|
|
if v.repairGeneration && objectMeta.Generation == 0 {
|
|
|
|
objectMeta.Generation = 1
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// restore meta fields, starting clean
|
|
|
|
if foundKind {
|
|
|
|
u.SetKind(kind)
|
|
|
|
}
|
|
|
|
if foundApiVersion {
|
|
|
|
u.SetAPIVersion(apiVersion)
|
|
|
|
}
|
|
|
|
if foundObjectMeta {
|
2019-08-30 18:33:25 +00:00
|
|
|
if err := schemaobjectmeta.SetObjectMeta(u.Object, objectMeta); err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// hasServedCRDVersion returns true if the given version is in the list of CRD's versions and the Served flag is set.
|
2020-03-26 21:07:15 +00:00
|
|
|
func hasServedCRDVersion(spec *apiextensionsv1.CustomResourceDefinitionSpec, version string) bool {
|
2019-08-30 18:33:25 +00:00
|
|
|
for _, v := range spec.Versions {
|
|
|
|
if v.Name == version {
|
|
|
|
return v.Served
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
return false
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
|
|
|
|
// serverStartingError returns a ServiceUnavailble error with a retry-after time
|
|
|
|
func serverStartingError() error {
|
|
|
|
err := apierrors.NewServiceUnavailable("server is starting")
|
|
|
|
if err.ErrStatus.Details == nil {
|
|
|
|
err.ErrStatus.Details = &metav1.StatusDetails{}
|
|
|
|
}
|
|
|
|
if err.ErrStatus.Details.RetryAfterSeconds == 0 {
|
|
|
|
err.ErrStatus.Details.RetryAfterSeconds = int32(10)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildOpenAPIModelsForApply constructs openapi models from any validation schemas specified in the custom resource,
|
|
|
|
// and merges it with the models defined in the static OpenAPI spec.
|
|
|
|
// Returns nil models if the ServerSideApply feature is disabled, or the static spec is nil, or an error is encountered.
|
2020-12-01 01:06:26 +00:00
|
|
|
func buildOpenAPIModelsForApply(staticOpenAPISpec *goopenapispec.Swagger, crd *apiextensionsv1.CustomResourceDefinition) (proto.Models, error) {
|
2019-09-27 21:51:53 +00:00
|
|
|
if !utilfeature.DefaultFeatureGate.Enabled(features.ServerSideApply) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if staticOpenAPISpec == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
specs := []*goopenapispec.Swagger{}
|
2019-09-27 21:51:53 +00:00
|
|
|
for _, v := range crd.Spec.Versions {
|
2020-12-01 01:06:26 +00:00
|
|
|
// Defaults are not pruned here, but before being served.
|
|
|
|
s, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: false, StripValueValidation: true, StripNullable: true, AllowNonStructural: true})
|
2019-09-27 21:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
specs = append(specs, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
mergedOpenAPI, err := builder.MergeSpecs(staticOpenAPISpec, specs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
models, err := utilopenapi.ToProtoModels(mergedOpenAPI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return models, nil
|
|
|
|
}
|