Skip to content

Commit

Permalink
Merge pull request #28414 from brendandburns/thirdparty
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

Allow multiple APIs to register for the same API Group

Fixes #23831

@kubernetes/sig-api-machinery 

[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/.github/PULL_REQUEST_TEMPLATE.md?pixel)]()
  • Loading branch information
k8s-merge-robot authored Jul 22, 2016
2 parents f55206a + ca9a61b commit 63e23a2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 14 deletions.
18 changes: 16 additions & 2 deletions pkg/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,13 @@ func (m *Master) ListThirdPartyResources() []string {
return result
}

func (m *Master) hasThirdPartyResourceStorage(path string) bool {
m.thirdPartyResourcesLock.Lock()
defer m.thirdPartyResourcesLock.Unlock()
_, found := m.thirdPartyResources[path]
return found
}

func (m *Master) addThirdPartyResourceStorage(path string, storage *thirdpartyresourcedataetcd.REST, apiGroup unversioned.APIGroup) {
m.thirdPartyResourcesLock.Lock()
defer m.thirdPartyResourcesLock.Unlock()
Expand All @@ -731,12 +738,19 @@ func (m *Master) InstallThirdPartyResource(rsrc *extensions.ThirdPartyResource)
Version: rsrc.Versions[0].Name,
Kind: kind,
})
path := makeThirdPartyPath(group)

thirdparty := m.thirdpartyapi(group, kind, rsrc.Versions[0].Name, plural.Resource)

// If storage exists, this group has already been added, just update
// the group with the new API
if m.hasThirdPartyResourceStorage(path) {
return thirdparty.UpdateREST(m.HandlerContainer)
}

if err := thirdparty.InstallREST(m.HandlerContainer); err != nil {
glog.Fatalf("Unable to setup thirdparty api: %v", err)
glog.Errorf("Unable to setup thirdparty api: %v", err)
}
path := makeThirdPartyPath(group)
groupVersion := unversioned.GroupVersionForDiscovery{
GroupVersion: group + "/" + rsrc.Versions[0].Name,
Version: rsrc.Versions[0].Name,
Expand Down
74 changes: 62 additions & 12 deletions pkg/master/master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,27 +552,77 @@ type FooList struct {
}

func initThirdParty(t *testing.T, version, name string) (*Master, *etcdtesting.EtcdTestServer, *httptest.Server, *assert.Assertions) {
return initThirdPartyMultiple(t, []string{version}, []string{name})
}

func initThirdPartyMultiple(t *testing.T, versions, names []string) (*Master, *etcdtesting.EtcdTestServer, *httptest.Server, *assert.Assertions) {
master, etcdserver, _, assert := newMaster(t)
api := &extensions.ThirdPartyResource{
ObjectMeta: api.ObjectMeta{
Name: name,
},
Versions: []extensions.APIVersion{
{
Name: version,
},
},
}
_, master.ServiceClusterIPRange, _ = net.ParseCIDR("10.0.0.0/24")

if !assert.NoError(master.InstallThirdPartyResource(api)) {
t.FailNow()
for ix := range names {
api := &extensions.ThirdPartyResource{
ObjectMeta: api.ObjectMeta{
Name: names[ix],
},
Versions: []extensions.APIVersion{
{
Name: versions[ix],
},
},
}
err := master.InstallThirdPartyResource(api)
if !assert.NoError(err) {
t.Logf("Failed to install API: %v", err)
t.FailNow()
}
}

server := httptest.NewServer(master.HandlerContainer.ServeMux)
return master, etcdserver, server, assert
}

func TestInstallMultipleAPIs(t *testing.T) {
names := []string{"foo.company.com", "bar.company.com"}
versions := []string{"v1", "v1"}

_, etcdserver, server, assert := initThirdPartyMultiple(t, versions, names)
defer server.Close()
defer etcdserver.Terminate(t)
for ix := range names {
kind, group, err := thirdpartyresourcedata.ExtractApiGroupAndKind(
&extensions.ThirdPartyResource{ObjectMeta: api.ObjectMeta{Name: names[ix]}})
assert.NoError(err, "Failed to extract group & kind")

plural, _ := meta.KindToResource(unversioned.GroupVersionKind{
Group: group,
Version: versions[ix],
Kind: kind,
})

resp, err := http.Get(
fmt.Sprintf("%s/apis/%s/%s/namespaces/default/%s", server.URL, group, versions[ix], plural.Resource))
if !assert.NoError(err, "Failed to do HTTP GET") {
return
}
defer resp.Body.Close()

assert.Equal(http.StatusOK, resp.StatusCode)

data, err := ioutil.ReadAll(resp.Body)
assert.NoError(err)

obj := map[string]interface{}{}
if err = json.Unmarshal(data, &obj); err != nil {
assert.NoError(err, fmt.Sprintf("unexpected error: %v", err))
}
kindOut, found := obj["kind"]
if !found {
t.Errorf("Missing 'kind' in %v", obj)
}
assert.Equal(kindOut, kind+"List")
}
}

func TestInstallThirdPartyAPIList(t *testing.T) {
for _, version := range versionsToTest {
testInstallThirdPartyAPIListVersion(t, version)
Expand Down

0 comments on commit 63e23a2

Please sign in to comment.