Skip to content

Commit e999fe4

Browse files
committed
Unify and simplify legacy api installation
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.
1 parent fabc231 commit e999fe4

File tree

26 files changed

+317
-1508
lines changed

26 files changed

+317
-1508
lines changed

pkg/api/legacy/install.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package legacy
2+
3+
import (
4+
"fmt"
5+
6+
"k8s.io/apimachinery/pkg/api/meta"
7+
"k8s.io/apimachinery/pkg/apimachinery"
8+
"k8s.io/apimachinery/pkg/apimachinery/announced"
9+
"k8s.io/apimachinery/pkg/apimachinery/registered"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
"k8s.io/apimachinery/pkg/runtime/schema"
12+
kapi "k8s.io/kubernetes/pkg/api"
13+
)
14+
15+
var (
16+
accessor = meta.NewAccessor()
17+
coreV1 = schema.GroupVersion{Group: "", Version: "v1"}
18+
)
19+
20+
func InstallLegacy(group string, addToCore, addToCoreV1 func(*runtime.Scheme) error, groupFactoryRegistry announced.APIGroupFactoryRegistry, registry *registered.APIRegistrationManager, scheme *runtime.Scheme) {
21+
groupMetaFactory, found := groupFactoryRegistry[group]
22+
if !found {
23+
panic(fmt.Sprintf("Group %q not found", group))
24+
}
25+
26+
interfacesFor := interfacesForGroup(group)
27+
28+
// install core V1 types temporarily into a local scheme to enumerate them
29+
mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{coreV1}, interfacesFor)
30+
localScheme := runtime.NewScheme()
31+
if err := addToCoreV1(localScheme); err != nil {
32+
panic(err)
33+
}
34+
for kind := range localScheme.KnownTypes(coreV1) {
35+
if groupMetaFactory.GroupArgs.IgnoredKinds.Has(kind) {
36+
continue
37+
}
38+
scope := meta.RESTScopeNamespace
39+
if groupMetaFactory.GroupArgs.RootScopedKinds.Has(kind) {
40+
scope = meta.RESTScopeRoot
41+
}
42+
mapper.Add(coreV1.WithKind(kind), scope)
43+
}
44+
45+
// register core v1 version. Should be done by kube (if the import dependencies are right).
46+
registry.RegisterVersions([]schema.GroupVersion{coreV1})
47+
if err := registry.EnableVersions(coreV1); err != nil {
48+
panic(err)
49+
}
50+
51+
// register types as core v1
52+
if err := addToCore(scheme); err != nil {
53+
panic(err)
54+
}
55+
if err := addToCoreV1(scheme); err != nil {
56+
panic(err)
57+
}
58+
59+
// add to group
60+
legacyGroupMeta := apimachinery.GroupMeta{
61+
GroupVersion: coreV1,
62+
GroupVersions: []schema.GroupVersion{coreV1},
63+
RESTMapper: mapper,
64+
SelfLinker: runtime.SelfLinker(accessor),
65+
InterfacesFor: interfacesFor,
66+
}
67+
if err := registry.RegisterGroup(legacyGroupMeta); err != nil {
68+
panic(err)
69+
}
70+
}
71+
72+
func interfacesForGroup(group string) func(version schema.GroupVersion) (*meta.VersionInterfaces, error) {
73+
return func(version schema.GroupVersion) (*meta.VersionInterfaces, error) {
74+
switch version {
75+
case coreV1:
76+
return &meta.VersionInterfaces{
77+
ObjectConvertor: kapi.Scheme,
78+
MetadataAccessor: accessor,
79+
}, nil
80+
81+
default:
82+
g, _ := kapi.Registry.Group(group)
83+
return nil, fmt.Errorf("unsupported storage version: %s (valid: %v)", version, g.GroupVersions)
84+
}
85+
}
86+
}

pkg/authorization/apis/authorization/install/apigroup.go

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 19 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,35 @@
11
package install
22

33
import (
4-
"fmt"
5-
6-
"github.com/golang/glog"
7-
8-
"k8s.io/apimachinery/pkg/api/meta"
9-
"k8s.io/apimachinery/pkg/apimachinery"
4+
"k8s.io/apimachinery/pkg/apimachinery/announced"
5+
"k8s.io/apimachinery/pkg/apimachinery/registered"
106
"k8s.io/apimachinery/pkg/runtime"
11-
"k8s.io/apimachinery/pkg/runtime/schema"
127
"k8s.io/apimachinery/pkg/util/sets"
138
kapi "k8s.io/kubernetes/pkg/api"
149

10+
"github.com/openshift/origin/pkg/api/legacy"
1511
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
1612
authorizationapiv1 "github.com/openshift/origin/pkg/authorization/apis/authorization/v1"
1713
)
1814

19-
var accessor = meta.NewAccessor()
20-
21-
// availableVersions lists all known external versions for this group from most preferred to least preferred
22-
var availableVersions = []schema.GroupVersion{authorizationapiv1.LegacySchemeGroupVersion}
23-
2415
func init() {
25-
kapi.Registry.RegisterVersions(availableVersions)
26-
externalVersions := []schema.GroupVersion{}
27-
for _, v := range availableVersions {
28-
if kapi.Registry.IsAllowedVersion(v) {
29-
externalVersions = append(externalVersions, v)
30-
}
31-
}
32-
if len(externalVersions) == 0 {
33-
glog.Infof("No version is registered for group %v", authorizationapi.GroupName)
34-
return
35-
}
36-
37-
if err := kapi.Registry.EnableVersions(externalVersions...); err != nil {
38-
panic(err)
39-
}
40-
if err := enableVersions(externalVersions); err != nil {
41-
panic(err)
42-
}
43-
44-
installApiGroup()
45-
}
46-
47-
// TODO: enableVersions should be centralized rather than spread in each API
48-
// group.
49-
// We can combine registered.RegisterVersions, registered.EnableVersions and
50-
// registered.RegisterGroup once we have moved enableVersions there.
51-
func enableVersions(externalVersions []schema.GroupVersion) error {
52-
addVersionsToScheme(externalVersions...)
53-
preferredExternalVersion := externalVersions[0]
54-
55-
groupMeta := apimachinery.GroupMeta{
56-
GroupVersion: preferredExternalVersion,
57-
GroupVersions: externalVersions,
58-
RESTMapper: newRESTMapper(externalVersions),
59-
SelfLinker: runtime.SelfLinker(accessor),
60-
InterfacesFor: interfacesFor,
61-
}
62-
63-
if err := kapi.Registry.RegisterGroup(groupMeta); err != nil {
64-
return err
65-
}
66-
return nil
16+
Install(kapi.GroupFactoryRegistry, kapi.Registry, kapi.Scheme)
17+
legacy.InstallLegacy(authorizationapi.GroupName, authorizationapi.AddToSchemeInCoreGroup, authorizationapiv1.AddToSchemeInCoreGroup, kapi.GroupFactoryRegistry, kapi.Registry, kapi.Scheme)
6718
}
6819

69-
func addVersionsToScheme(externalVersions ...schema.GroupVersion) {
70-
// add the internal version to Scheme
71-
authorizationapi.AddToSchemeInCoreGroup(kapi.Scheme)
72-
// add the enabled external versions to Scheme
73-
for _, v := range externalVersions {
74-
if !kapi.Registry.IsEnabledVersion(v) {
75-
glog.Errorf("Version %s is not enabled, so it will not be added to the Scheme.", v)
76-
continue
77-
}
78-
switch v {
79-
case authorizationapiv1.LegacySchemeGroupVersion:
80-
authorizationapiv1.AddToSchemeInCoreGroup(kapi.Scheme)
81-
82-
default:
83-
glog.Errorf("Version %s is not known, so it will not be added to the Scheme.", v)
84-
continue
85-
}
86-
}
87-
}
88-
89-
func newRESTMapper(externalVersions []schema.GroupVersion) meta.RESTMapper {
90-
rootScoped := sets.NewString("ClusterRole", "ClusterRoleBinding", "ClusterPolicy", "ClusterPolicyBinding")
91-
ignoredKinds := sets.NewString()
92-
return meta.NewDefaultRESTMapperFromScheme(externalVersions, interfacesFor, ignoredKinds, rootScoped, kapi.Scheme)
93-
}
94-
95-
func interfacesFor(version schema.GroupVersion) (*meta.VersionInterfaces, error) {
96-
switch version {
97-
case authorizationapiv1.LegacySchemeGroupVersion:
98-
return &meta.VersionInterfaces{
99-
ObjectConvertor: kapi.Scheme,
100-
MetadataAccessor: accessor,
101-
}, nil
102-
103-
default:
104-
g, _ := kapi.Registry.Group(authorizationapi.GroupName)
105-
return nil, fmt.Errorf("unsupported storage version: %s (valid: %v)", version, g.GroupVersions)
20+
// Install registers the API group and adds types to a scheme
21+
func Install(groupFactoryRegistry announced.APIGroupFactoryRegistry, registry *registered.APIRegistrationManager, scheme *runtime.Scheme) {
22+
if err := announced.NewGroupMetaFactory(
23+
&announced.GroupMetaFactoryArgs{
24+
GroupName: authorizationapi.GroupName,
25+
VersionPreferenceOrder: []string{authorizationapiv1.SchemeGroupVersion.Version},
26+
AddInternalObjectsToScheme: authorizationapi.AddToScheme,
27+
RootScopedKinds: sets.NewString("ClusterRole", "ClusterRoleBinding", "ClusterPolicy", "ClusterPolicyBinding", "SubjectAccessReview", "ResourceAccessReview"),
28+
},
29+
announced.VersionToSchemeFunc{
30+
authorizationapiv1.SchemeGroupVersion.Version: authorizationapiv1.AddToScheme,
31+
},
32+
).Announce(groupFactoryRegistry).RegisterAndEnable(registry, scheme); err != nil {
33+
panic(err)
10634
}
10735
}

pkg/build/apis/build/install/apigroup.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)