getAggregatedAPIServerScheme: move group version logic internally

Signed-off-by: Monis Khan <mok@vmware.com>
This commit is contained in:
Monis Khan 2021-02-19 10:10:30 -05:00
parent f228f022f5
commit 62630d6449
No known key found for this signature in database
GPG Key ID: 52C90ADA01B269B8
2 changed files with 21 additions and 24 deletions

View File

@ -174,19 +174,11 @@ func getAggregatedAPIServerConfig(
startControllersPostStartHook func(context.Context), startControllersPostStartHook func(context.Context),
apiGroupSuffix string, apiGroupSuffix string,
) (*apiserver.Config, error) { ) (*apiserver.Config, error) {
loginConciergeAPIGroup, ok := groupsuffix.Replace(loginv1alpha1.GroupName, apiGroupSuffix) scheme, groupVersion := getAggregatedAPIServerScheme(apiGroupSuffix)
if !ok {
return nil, fmt.Errorf("cannot make api group from %s/%s", loginv1alpha1.GroupName, apiGroupSuffix)
}
scheme := getAggregatedAPIServerScheme(loginConciergeAPIGroup, apiGroupSuffix)
codecs := serializer.NewCodecFactory(scheme) codecs := serializer.NewCodecFactory(scheme)
defaultEtcdPathPrefix := fmt.Sprintf("/registry/%s", loginConciergeAPIGroup) // this is unused for now but it is a safe value that we could use in the future
groupVersion := schema.GroupVersion{ defaultEtcdPathPrefix := fmt.Sprintf("/pinniped-concierge-registry/%s", apiGroupSuffix)
Group: loginConciergeAPIGroup,
Version: loginv1alpha1.SchemeGroupVersion.Version,
}
recommendedOptions := genericoptions.NewRecommendedOptions( recommendedOptions := genericoptions.NewRecommendedOptions(
defaultEtcdPathPrefix, defaultEtcdPathPrefix,
@ -224,18 +216,23 @@ func getAggregatedAPIServerConfig(
return apiServerConfig, nil return apiServerConfig, nil
} }
func getAggregatedAPIServerScheme(loginConciergeAPIGroup, apiGroupSuffix string) *runtime.Scheme { func getAggregatedAPIServerScheme(apiGroupSuffix string) (*runtime.Scheme, schema.GroupVersion) {
// standard set up of the server side scheme // standard set up of the server side scheme
scheme := runtime.NewScheme() scheme := runtime.NewScheme()
// add the options to empty v1 // add the options to empty v1
metav1.AddToGroupVersion(scheme, metav1.Unversioned) metav1.AddToGroupVersion(scheme, metav1.Unversioned)
// nothing fancy is required if using the standard group // nothing fancy is required if using the standard group suffix
if loginConciergeAPIGroup == loginv1alpha1.GroupName { if apiGroupSuffix == "pinniped.dev" {
utilruntime.Must(loginv1alpha1.AddToScheme(scheme)) utilruntime.Must(loginv1alpha1.AddToScheme(scheme))
utilruntime.Must(loginapi.AddToScheme(scheme)) utilruntime.Must(loginapi.AddToScheme(scheme))
return scheme return scheme, loginv1alpha1.SchemeGroupVersion
}
loginConciergeAPIGroup, ok := groupsuffix.Replace(loginv1alpha1.GroupName, apiGroupSuffix)
if !ok {
panic(fmt.Errorf("cannot make api group from %s/%s", loginv1alpha1.GroupName, apiGroupSuffix)) // static input, impossible case
} }
// we need a temporary place to register our types to avoid double registering them // we need a temporary place to register our types to avoid double registering them
@ -309,5 +306,5 @@ func getAggregatedAPIServerScheme(loginConciergeAPIGroup, apiGroupSuffix string)
credentialRequest.Spec.Authenticator.APIGroup = &restoredGroup credentialRequest.Spec.Authenticator.APIGroup = &restoredGroup
}) })
return scheme return scheme, schema.GroupVersion{Group: loginConciergeAPIGroup, Version: loginv1alpha1.SchemeGroupVersion.Version}
} }

View File

@ -20,7 +20,6 @@ import (
loginapi "go.pinniped.dev/generated/latest/apis/concierge/login" loginapi "go.pinniped.dev/generated/latest/apis/concierge/login"
loginv1alpha1 "go.pinniped.dev/generated/latest/apis/concierge/login/v1alpha1" loginv1alpha1 "go.pinniped.dev/generated/latest/apis/concierge/login/v1alpha1"
"go.pinniped.dev/internal/groupsuffix"
) )
const knownGoodUsage = ` const knownGoodUsage = `
@ -129,6 +128,7 @@ func Test_getAggregatedAPIServerScheme(t *testing.T) {
name string name string
apiGroupSuffix string apiGroupSuffix string
want map[schema.GroupVersionKind]reflect.Type want map[schema.GroupVersionKind]reflect.Type
wantGroupVersion schema.GroupVersion
}{ }{
{ {
name: "regular api group", name: "regular api group",
@ -171,6 +171,7 @@ func Test_getAggregatedAPIServerScheme(t *testing.T) {
metav1.Unversioned.WithKind("UpdateOptions"): reflect.TypeOf(&metav1.UpdateOptions{}).Elem(), metav1.Unversioned.WithKind("UpdateOptions"): reflect.TypeOf(&metav1.UpdateOptions{}).Elem(),
metav1.Unversioned.WithKind("WatchEvent"): reflect.TypeOf(&metav1.WatchEvent{}).Elem(), metav1.Unversioned.WithKind("WatchEvent"): reflect.TypeOf(&metav1.WatchEvent{}).Elem(),
}, },
wantGroupVersion: regularGV,
}, },
{ {
name: "other api group", name: "other api group",
@ -213,16 +214,15 @@ func Test_getAggregatedAPIServerScheme(t *testing.T) {
metav1.Unversioned.WithKind("UpdateOptions"): reflect.TypeOf(&metav1.UpdateOptions{}).Elem(), metav1.Unversioned.WithKind("UpdateOptions"): reflect.TypeOf(&metav1.UpdateOptions{}).Elem(),
metav1.Unversioned.WithKind("WatchEvent"): reflect.TypeOf(&metav1.WatchEvent{}).Elem(), metav1.Unversioned.WithKind("WatchEvent"): reflect.TypeOf(&metav1.WatchEvent{}).Elem(),
}, },
wantGroupVersion: otherGV,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
tt := tt tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
loginConciergeAPIGroup, ok := groupsuffix.Replace("login.concierge.pinniped.dev", tt.apiGroupSuffix) scheme, gv := getAggregatedAPIServerScheme(tt.apiGroupSuffix)
require.True(t, ok)
scheme := getAggregatedAPIServerScheme(loginConciergeAPIGroup, tt.apiGroupSuffix)
require.Equal(t, tt.want, scheme.AllKnownTypes()) require.Equal(t, tt.want, scheme.AllKnownTypes())
require.Equal(t, tt.wantGroupVersion, gv)
// make a credential request like a client would send // make a credential request like a client would send
authenticationConciergeAPIGroup := "authentication.concierge." + tt.apiGroupSuffix authenticationConciergeAPIGroup := "authentication.concierge." + tt.apiGroupSuffix