Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage factory should not hardcode special resources #32309

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 15 additions & 10 deletions pkg/genericapiserver/storage_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type DefaultStorageFactory struct {

Overrides map[unversioned.GroupResource]groupResourceOverrides

DefaultResourcePrefixes map[unversioned.GroupResource]string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this a part of overrides?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will make this cleaner

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having customizable defaults and overrides separate (like in this PR) is actually cleanest, IMO. Otherwise the caller will have to figure out if the user's flags imply a default change. If they're split like that, we can leave the code interpreting the flags in genericapiserver.


// DefaultMediaType is the media type used to store resources. If it is not set, "application/json" is used.
DefaultMediaType string

Expand Down Expand Up @@ -99,6 +101,17 @@ var _ StorageFactory = &DefaultStorageFactory{}

const AllResources = "*"

// specialDefaultResourcePrefixes are prefixes compiled into Kubernetes.
// TODO: move out of this package, it is not generic
var specialDefaultResourcePrefixes = map[unversioned.GroupResource]string{
unversioned.GroupResource{Group: "", Resource: "replicationControllers"}: "controllers",
unversioned.GroupResource{Group: "", Resource: "replicationcontrollers"}: "controllers",
unversioned.GroupResource{Group: "", Resource: "endpoints"}: "services/endpoints",
unversioned.GroupResource{Group: "", Resource: "nodes"}: "minions",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minion -> node rename: 2.5 years and counting

unversioned.GroupResource{Group: "", Resource: "services"}: "services/specs",
unversioned.GroupResource{Group: "extensions", Resource: "ingresses"}: "ingress",
}

func NewDefaultStorageFactory(config storagebackend.Config, defaultMediaType string, defaultSerializer runtime.StorageSerializer, resourceEncodingConfig ResourceEncodingConfig, resourceConfig APIResourceConfigSource) *DefaultStorageFactory {
if len(defaultMediaType) == 0 {
defaultMediaType = runtime.ContentTypeJSON
Expand All @@ -110,6 +123,7 @@ func NewDefaultStorageFactory(config storagebackend.Config, defaultMediaType str
DefaultSerializer: defaultSerializer,
ResourceEncodingConfig: resourceEncodingConfig,
APIResourceConfigSource: resourceConfig,
DefaultResourcePrefixes: specialDefaultResourcePrefixes,

newStorageCodecFn: NewStorageCodec,
}
Expand Down Expand Up @@ -283,21 +297,12 @@ func NewStorageCodec(storageMediaType string, ns runtime.StorageSerializer, stor
return runtime.NewCodec(encoder, decoder), nil
}

var specialDefaultResourcePrefixes = map[unversioned.GroupResource]string{
unversioned.GroupResource{Group: "", Resource: "replicationControllers"}: "controllers",
unversioned.GroupResource{Group: "", Resource: "replicationcontrollers"}: "controllers",
unversioned.GroupResource{Group: "", Resource: "endpoints"}: "services/endpoints",
unversioned.GroupResource{Group: "", Resource: "nodes"}: "minions",
unversioned.GroupResource{Group: "", Resource: "services"}: "services/specs",
unversioned.GroupResource{Group: "extensions", Resource: "ingresses"}: "ingress",
}

func (s *DefaultStorageFactory) ResourcePrefix(groupResource unversioned.GroupResource) string {
chosenStorageResource := s.getStorageGroupResource(groupResource)
groupOverride := s.Overrides[getAllResourcesAlias(chosenStorageResource)]
exactResourceOverride := s.Overrides[chosenStorageResource]

etcdResourcePrefix := specialDefaultResourcePrefixes[chosenStorageResource]
etcdResourcePrefix := s.DefaultResourcePrefixes[chosenStorageResource]
if len(groupOverride.etcdResourcePrefix) > 0 {
etcdResourcePrefix = groupOverride.etcdResourcePrefix
}
Expand Down