github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/package-server/apiserver/generic/storage.go (about) 1 // Copyright 2018 The Kubernetes Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package generic 16 17 import ( 18 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 19 "k8s.io/apimachinery/pkg/runtime" 20 "k8s.io/apimachinery/pkg/runtime/schema" 21 "k8s.io/apimachinery/pkg/runtime/serializer" 22 utilerrors "k8s.io/apimachinery/pkg/util/errors" 23 "k8s.io/apiserver/pkg/registry/rest" 24 generic "k8s.io/apiserver/pkg/server" 25 26 operators "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/install" 27 operatorsv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1" 28 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/provider" 29 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/storage" 30 ) 31 32 var ( 33 // Scheme contains the types needed by the resource metrics API. 34 Scheme = runtime.NewScheme() 35 // Codecs is a codec factory for serving the resource metrics API. 36 Codecs = serializer.NewCodecFactory(Scheme) 37 ) 38 39 func init() { 40 operators.Install(Scheme) 41 42 // we need to add the options to empty v1 43 // TODO fix the server code to avoid this 44 metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"}) 45 46 // TODO: keep the generic API server from wanting this 47 unversioned := schema.GroupVersion{Group: "", Version: "v1"} 48 Scheme.AddUnversionedTypes(unversioned, 49 &metav1.Status{}, 50 &metav1.APIVersions{}, 51 &metav1.APIGroupList{}, 52 &metav1.APIGroup{}, 53 &metav1.APIResourceList{}, 54 ) 55 } 56 57 // ProviderConfig holds the providers for packagemanifests. 58 type ProviderConfig struct { 59 Provider provider.PackageManifestProvider 60 } 61 62 // BuildStorage constructs APIGroupInfo for the packages.apps.redhat.com and packages.operators.coreos.com API groups. 63 func BuildStorage(providers *ProviderConfig) []generic.APIGroupInfo { 64 // Build storage for packages.operators.coreos.com 65 operatorInfo := generic.NewDefaultAPIGroupInfo(operatorsv1.Group, Scheme, metav1.ParameterCodec, Codecs) 66 operatorStorage := storage.NewStorage(operatorsv1.Resource("packagemanifests"), providers.Provider, Scheme) 67 iconStorage := storage.NewLogoStorage(operatorsv1.Resource("packagemanifests/icon"), providers.Provider) 68 operatorResources := map[string]rest.Storage{ 69 "packagemanifests": operatorStorage, 70 "packagemanifests/icon": iconStorage, 71 } 72 operatorInfo.VersionedResourcesStorageMap[operatorsv1.Version] = operatorResources 73 74 return []generic.APIGroupInfo{ 75 operatorInfo, 76 } 77 } 78 79 // InstallStorage builds the storage for the packages.apps.redhat.com and packages.operators.coreos.com API groups and then installs them into the given API server. 80 func InstallStorage(providers *ProviderConfig, server *generic.GenericAPIServer) error { 81 errs := []error{} 82 groups := BuildStorage(providers) 83 for i := 0; i < len(groups); i++ { 84 info := groups[i] 85 errs = append(errs, server.InstallAPIGroup(&info)) 86 } 87 88 return utilerrors.NewAggregate(errs) 89 }