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

apimachinery: remove pre-apigroups import path logic #15727

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions pkg/api/legacy/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package legacy

import (
"fmt"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apimachinery"
"k8s.io/apimachinery/pkg/apimachinery/registered"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"
)

var (
accessor = meta.NewAccessor()
coreV1 = schema.GroupVersion{Group: "", Version: "v1"}
)

func InstallLegacy(group string, addToCore, addToCoreV1 func(*runtime.Scheme) error, rootScopedKinds sets.String, registry *registered.APIRegistrationManager, scheme *runtime.Scheme) {
interfacesFor := interfacesForGroup(group)

// install core V1 types temporarily into a local scheme to enumerate them
mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{coreV1}, interfacesFor)
localScheme := runtime.NewScheme()
if err := addToCoreV1(localScheme); err != nil {
panic(err)
}
for kind := range localScheme.KnownTypes(coreV1) {
scope := meta.RESTScopeNamespace
if rootScopedKinds.Has(kind) {
scope = meta.RESTScopeRoot
}
mapper.Add(coreV1.WithKind(kind), scope)
}

// register core v1 version. Should be done by kube (if the import dependencies are right).
registry.RegisterVersions([]schema.GroupVersion{coreV1})
if err := registry.EnableVersions(coreV1); err != nil {
panic(err)
}

// register types as core v1
if err := addToCore(scheme); err != nil {
panic(err)
}
if err := addToCoreV1(scheme); err != nil {
panic(err)
}

// add to group
legacyGroupMeta := apimachinery.GroupMeta{
GroupVersion: coreV1,
GroupVersions: []schema.GroupVersion{coreV1},
RESTMapper: mapper,
SelfLinker: runtime.SelfLinker(accessor),
InterfacesFor: interfacesFor,
}
if err := registry.RegisterGroup(legacyGroupMeta); err != nil {
panic(err)
}
}

func interfacesForGroup(group string) func(version schema.GroupVersion) (*meta.VersionInterfaces, error) {
return func(version schema.GroupVersion) (*meta.VersionInterfaces, error) {
switch version {
case coreV1:
return &meta.VersionInterfaces{
ObjectConvertor: kapi.Scheme,
MetadataAccessor: accessor,
}, nil

default:
g, _ := kapi.Registry.Group(group)
return nil, fmt.Errorf("unsupported storage version: %s (valid: %v)", version, g.GroupVersions)
}
}
}
34 changes: 0 additions & 34 deletions pkg/authorization/apis/authorization/install/apigroup.go

This file was deleted.

115 changes: 22 additions & 93 deletions pkg/authorization/apis/authorization/install/install.go
Original file line number Diff line number Diff line change
@@ -1,109 +1,38 @@
package install

import (
"fmt"

"github.com/golang/glog"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apimachinery"
"k8s.io/apimachinery/pkg/apimachinery/announced"
"k8s.io/apimachinery/pkg/apimachinery/registered"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"

"github.com/openshift/origin/pkg/api/legacy"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
authorizationapiv1 "github.com/openshift/origin/pkg/authorization/apis/authorization/v1"
)

const importPrefix = "github.com/openshift/origin/pkg/authorization/apis/authorization"

var accessor = meta.NewAccessor()

// availableVersions lists all known external versions for this group from most preferred to least preferred
var availableVersions = []schema.GroupVersion{authorizationapiv1.LegacySchemeGroupVersion}

func init() {
kapi.Registry.RegisterVersions(availableVersions)
externalVersions := []schema.GroupVersion{}
for _, v := range availableVersions {
if kapi.Registry.IsAllowedVersion(v) {
externalVersions = append(externalVersions, v)
}
}
if len(externalVersions) == 0 {
glog.Infof("No version is registered for group %v", authorizationapi.GroupName)
return
}

if err := kapi.Registry.EnableVersions(externalVersions...); err != nil {
panic(err)
}
if err := enableVersions(externalVersions); err != nil {
panic(err)
}

installApiGroup()
Install(kapi.GroupFactoryRegistry, kapi.Registry, kapi.Scheme)
legacy.InstallLegacy(authorizationapi.GroupName, authorizationapi.AddToSchemeInCoreGroup, authorizationapiv1.AddToSchemeInCoreGroup,
sets.NewString("ClusterRole", "ClusterRoleBinding", "ClusterPolicy", "ClusterPolicyBinding"),
kapi.Registry, kapi.Scheme,
)
}

// TODO: enableVersions should be centralized rather than spread in each API
// group.
// We can combine registered.RegisterVersions, registered.EnableVersions and
// registered.RegisterGroup once we have moved enableVersions there.
func enableVersions(externalVersions []schema.GroupVersion) error {
addVersionsToScheme(externalVersions...)
preferredExternalVersion := externalVersions[0]

groupMeta := apimachinery.GroupMeta{
GroupVersion: preferredExternalVersion,
GroupVersions: externalVersions,
RESTMapper: newRESTMapper(externalVersions),
SelfLinker: runtime.SelfLinker(accessor),
InterfacesFor: interfacesFor,
}

if err := kapi.Registry.RegisterGroup(groupMeta); err != nil {
return err
}
return nil
}

func addVersionsToScheme(externalVersions ...schema.GroupVersion) {
// add the internal version to Scheme
authorizationapi.AddToSchemeInCoreGroup(kapi.Scheme)
// add the enabled external versions to Scheme
for _, v := range externalVersions {
if !kapi.Registry.IsEnabledVersion(v) {
glog.Errorf("Version %s is not enabled, so it will not be added to the Scheme.", v)
continue
}
switch v {
case authorizationapiv1.LegacySchemeGroupVersion:
authorizationapiv1.AddToSchemeInCoreGroup(kapi.Scheme)

default:
glog.Errorf("Version %s is not known, so it will not be added to the Scheme.", v)
continue
}
}
}

func newRESTMapper(externalVersions []schema.GroupVersion) meta.RESTMapper {
rootScoped := sets.NewString("ClusterRole", "ClusterRoleBinding", "ClusterPolicy", "ClusterPolicyBinding")
ignoredKinds := sets.NewString()
return meta.NewDefaultRESTMapperFromScheme(externalVersions, interfacesFor, importPrefix, ignoredKinds, rootScoped, kapi.Scheme)
}

func interfacesFor(version schema.GroupVersion) (*meta.VersionInterfaces, error) {
switch version {
case authorizationapiv1.LegacySchemeGroupVersion:
return &meta.VersionInterfaces{
ObjectConvertor: kapi.Scheme,
MetadataAccessor: accessor,
}, nil

default:
g, _ := kapi.Registry.Group(authorizationapi.GroupName)
return nil, fmt.Errorf("unsupported storage version: %s (valid: %v)", version, g.GroupVersions)
// Install registers the API group and adds types to a scheme
func Install(groupFactoryRegistry announced.APIGroupFactoryRegistry, registry *registered.APIRegistrationManager, scheme *runtime.Scheme) {
if err := announced.NewGroupMetaFactory(
&announced.GroupMetaFactoryArgs{
GroupName: authorizationapi.GroupName,
VersionPreferenceOrder: []string{authorizationapiv1.SchemeGroupVersion.Version},
AddInternalObjectsToScheme: authorizationapi.AddToScheme,
RootScopedKinds: sets.NewString("ClusterRole", "ClusterRoleBinding", "ClusterPolicy", "ClusterPolicyBinding", "SubjectAccessReview", "ResourceAccessReview"),
},
announced.VersionToSchemeFunc{
authorizationapiv1.SchemeGroupVersion.Version: authorizationapiv1.AddToScheme,
},
).Announce(groupFactoryRegistry).RegisterAndEnable(registry, scheme); err != nil {
panic(err)
}
}
32 changes: 0 additions & 32 deletions pkg/build/apis/build/install/apigroup.go

This file was deleted.

Loading