Skip to content

Commit

Permalink
Unify and simplify legacy api installation
Browse files Browse the repository at this point in the history
At the same time we get rid of the need for Store.ImportPrefix to filter the RESTMapper for each
legacy group. Before this commit we had overlapping RESTMapper, even inconsistent ones because
the root kinds were wrong outside of each legacy group.
  • Loading branch information
sttts committed Aug 15, 2017
1 parent fabc231 commit e999fe4
Show file tree
Hide file tree
Showing 26 changed files with 317 additions and 1,508 deletions.
86 changes: 86 additions & 0 deletions pkg/api/legacy/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package legacy

import (
"fmt"

"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"
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, groupFactoryRegistry announced.APIGroupFactoryRegistry, registry *registered.APIRegistrationManager, scheme *runtime.Scheme) {
groupMetaFactory, found := groupFactoryRegistry[group]
if !found {
panic(fmt.Sprintf("Group %q not found", group))
}

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) {
if groupMetaFactory.GroupArgs.IgnoredKinds.Has(kind) {
continue
}
scope := meta.RESTScopeNamespace
if groupMetaFactory.GroupArgs.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)
}
}
}
33 changes: 0 additions & 33 deletions pkg/authorization/apis/authorization/install/apigroup.go

This file was deleted.

110 changes: 19 additions & 91 deletions pkg/authorization/apis/authorization/install/install.go
Original file line number Diff line number Diff line change
@@ -1,107 +1,35 @@
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"
)

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()
}

// 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
Install(kapi.GroupFactoryRegistry, kapi.Registry, kapi.Scheme)
legacy.InstallLegacy(authorizationapi.GroupName, authorizationapi.AddToSchemeInCoreGroup, authorizationapiv1.AddToSchemeInCoreGroup, kapi.GroupFactoryRegistry, kapi.Registry, kapi.Scheme)
}

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, 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)
}
}
31 changes: 0 additions & 31 deletions pkg/build/apis/build/install/apigroup.go

This file was deleted.

Loading

0 comments on commit e999fe4

Please sign in to comment.