-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/controlplane: split apart generic server part of instance.go
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
- Loading branch information
Showing
10 changed files
with
412 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright 2023 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" | ||
|
||
"k8s.io/apiserver/pkg/registry/generic" | ||
genericapiserver "k8s.io/apiserver/pkg/server" | ||
serverstorage "k8s.io/apiserver/pkg/server/storage" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// RESTStorageProvider is a factory type for REST storage. | ||
type RESTStorageProvider interface { | ||
GroupName() string | ||
NewRESTStorage(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter) (genericapiserver.APIGroupInfo, error) | ||
} | ||
|
||
// InstallAPIs will install the APIs for the restStorageProviders if they are enabled. | ||
func (s *Server) InstallAPIs(restStorageProviders ...RESTStorageProvider) error { | ||
nonLegacy := []*genericapiserver.APIGroupInfo{} | ||
|
||
// used later in the loop to filter the served resource by those that have expired. | ||
resourceExpirationEvaluator, err := genericapiserver.NewResourceExpirationEvaluator(*s.GenericAPIServer.Version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, restStorageBuilder := range restStorageProviders { | ||
groupName := restStorageBuilder.GroupName() | ||
apiGroupInfo, err := restStorageBuilder.NewRESTStorage(s.APIResourceConfigSource, s.RESTOptionsGetter) | ||
if err != nil { | ||
return fmt.Errorf("problem initializing API group %q: %w", groupName, err) | ||
} | ||
if len(apiGroupInfo.VersionedResourcesStorageMap) == 0 { | ||
// If we have no storage for any resource configured, this API group is effectively disabled. | ||
// This can happen when an entire API group, version, or development-stage (alpha, beta, GA) is disabled. | ||
klog.Infof("API group %q is not enabled, skipping.", groupName) | ||
continue | ||
} | ||
|
||
// Remove resources that serving kinds that are removed. | ||
// We do this here so that we don't accidentally serve versions without resources or openapi information that for kinds we don't serve. | ||
// This is a spot above the construction of individual storage handlers so that no sig accidentally forgets to check. | ||
resourceExpirationEvaluator.RemoveDeletedKinds(groupName, apiGroupInfo.Scheme, apiGroupInfo.VersionedResourcesStorageMap) | ||
if len(apiGroupInfo.VersionedResourcesStorageMap) == 0 { | ||
klog.V(1).Infof("Removing API group %v because it is time to stop serving it because it has no versions per APILifecycle.", groupName) | ||
continue | ||
} | ||
|
||
klog.V(1).Infof("Enabling API group %q.", groupName) | ||
|
||
if postHookProvider, ok := restStorageBuilder.(genericapiserver.PostStartHookProvider); ok { | ||
name, hook, err := postHookProvider.PostStartHook() | ||
if err != nil { | ||
return fmt.Errorf("error building PostStartHook: %w", err) | ||
} | ||
s.GenericAPIServer.AddPostStartHookOrDie(name, hook) | ||
} | ||
|
||
if len(groupName) == 0 { | ||
// the legacy group for core APIs is special that it is installed into /api via this special install method. | ||
if err := s.GenericAPIServer.InstallLegacyAPIGroup(genericapiserver.DefaultLegacyAPIPrefix, &apiGroupInfo); err != nil { | ||
return fmt.Errorf("error in registering legacy API: %w", err) | ||
} | ||
} else { | ||
// everything else goes to /apis | ||
nonLegacy = append(nonLegacy, &apiGroupInfo) | ||
} | ||
} | ||
|
||
if err := s.GenericAPIServer.InstallAPIGroups(nonLegacy...); err != nil { | ||
return fmt.Errorf("error in registering group versions: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.