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

Fix a problem with multiple APIs clobbering each other in registration. #28431

Merged
merged 2 commits into from
Jul 22, 2016
Merged
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
21 changes: 15 additions & 6 deletions pkg/apimachinery/registered/registered.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ var (
)

func init() {
loadKubeAPIVersions()
}

func loadKubeAPIVersions() {
// Env var KUBE_API_VERSIONS is a comma separated list of API versions that
// should be registered in the scheme.
kubeAPIVersions := os.Getenv("KUBE_API_VERSIONS")
Expand All @@ -70,6 +74,16 @@ func init() {
}
}

// Resets everything to clean room for the start of a test
func clearForTesting() {
Copy link
Member

Choose a reason for hiding this comment

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

ugh, this is awful, I really need to get my changes in so it's not necessary :(

registeredVersions = map[unversioned.GroupVersion]struct{}{}
thirdPartyGroupVersions = []unversioned.GroupVersion{}
enabledVersions = map[unversioned.GroupVersion]struct{}{}
groupMetaMap = map[string]*apimachinery.GroupMeta{}
envRequestedVersions = []unversioned.GroupVersion{}
loadKubeAPIVersions()
}

// RegisterVersions adds the given group versions to the list of registered group versions.
func RegisterVersions(availableVersions []unversioned.GroupVersion) {
for _, v := range availableVersions {
Expand Down Expand Up @@ -207,12 +221,7 @@ func AddThirdPartyAPIGroupVersions(gvs ...unversioned.GroupVersion) []unversione
}
RegisterVersions(filteredGVs)
EnableVersions(filteredGVs...)
next := make([]unversioned.GroupVersion, len(gvs))
for ix := range filteredGVs {
next[ix] = filteredGVs[ix]
}
thirdPartyGroupVersions = next

thirdPartyGroupVersions = append(thirdPartyGroupVersions, filteredGVs...)
return skippedGVs
}

Expand Down
72 changes: 72 additions & 0 deletions pkg/apimachinery/registered/registered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,78 @@ import (
"k8s.io/kubernetes/pkg/apimachinery"
)

func TestAddThirdPartyVersionsBasic(t *testing.T) {
clearForTesting()

registered := []unversioned.GroupVersion{
{
Group: "",
Version: "v1",
},
}
skipped := registered
thirdParty := []unversioned.GroupVersion{
{
Group: "company.com",
Version: "v1",
},
{
Group: "company.com",
Version: "v2",
},
}
gvs := append(registered, thirdParty...)

RegisterVersions(registered)
wasSkipped := AddThirdPartyAPIGroupVersions(gvs...)
if len(wasSkipped) != len(skipped) {
t.Errorf("Expected %v, found %v", skipped, wasSkipped)
}
for ix := range wasSkipped {
found := false
for _, gv := range skipped {
if gv.String() == wasSkipped[ix].String() {
found = true
break
}
}
if !found {
t.Errorf("Couldn't find %v in %v", wasSkipped[ix], skipped)
}
}
for _, gv := range thirdParty {
if !IsThirdPartyAPIGroupVersion(gv) {
t.Errorf("Expected %v to be third party.", gv)
}
}
}

func TestAddThirdPartyVersionsMultiple(t *testing.T) {
clearForTesting()

thirdParty := []unversioned.GroupVersion{
{
Group: "company.com",
Version: "v1",
},
{
Group: "company.com",
Version: "v2",
},
}
for _, gv := range thirdParty {
wasSkipped := AddThirdPartyAPIGroupVersions(gv)
if len(wasSkipped) != 0 {
t.Errorf("Expected length 0, found %v", wasSkipped)
}
}
for _, gv := range thirdParty {
if !IsThirdPartyAPIGroupVersion(gv) {
t.Errorf("Expected %v to be third party.", gv)
}
}
}

func TestAllPreferredGroupVersions(t *testing.T) {
testCases := []struct {
groupMetas []apimachinery.GroupMeta
Expand Down