Skip to content

Commit 19500e8

Browse files
authored
Merge pull request kubernetes#127524 from mjudeikis/mjudeikis/extend.group.manager
Add GroupLister interface to discovery GroupManager
2 parents 89f418f + ee55200 commit 19500e8

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/discovery/root.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package discovery
1818

1919
import (
20+
"context"
2021
"net/http"
2122
"sync"
2223

@@ -33,12 +34,21 @@ import (
3334
// GroupManager is an interface that allows dynamic mutation of the existing webservice to handle
3435
// API groups being added or removed.
3536
type GroupManager interface {
37+
GroupLister
38+
3639
AddGroup(apiGroup metav1.APIGroup)
3740
RemoveGroup(groupName string)
3841
ServeHTTP(resp http.ResponseWriter, req *http.Request)
3942
WebService() *restful.WebService
4043
}
4144

45+
// GroupLister knows how to list APIGroups for discovery.
46+
type GroupLister interface {
47+
// Groups returns APIGroups for discovery, filling in ServerAddressByClientCIDRs
48+
// based on data in req.
49+
Groups(ctx context.Context, req *http.Request) ([]metav1.APIGroup, error)
50+
}
51+
4252
// rootAPIsHandler creates a webservice serving api group discovery.
4353
// The list of APIGroups may change while the server is running because additional resources
4454
// are registered or removed. It is not safe to cache the values.
@@ -94,24 +104,40 @@ func (s *rootAPIsHandler) RemoveGroup(groupName string) {
94104
}
95105
}
96106

97-
func (s *rootAPIsHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
107+
func (s *rootAPIsHandler) Groups(ctx context.Context, req *http.Request) ([]metav1.APIGroup, error) {
98108
s.lock.RLock()
99109
defer s.lock.RUnlock()
100110

111+
return s.groupsLocked(ctx, req), nil
112+
}
113+
114+
// groupsLocked returns the APIGroupList discovery information for this handler.
115+
// The caller must hold the lock before invoking this method to avoid data races.
116+
func (s *rootAPIsHandler) groupsLocked(ctx context.Context, req *http.Request) []metav1.APIGroup {
117+
clientIP := utilnet.GetClientIP(req)
118+
serverCIDR := s.addresses.ServerAddressByClientCIDRs(clientIP)
119+
101120
orderedGroups := []metav1.APIGroup{}
102121
for _, groupName := range s.apiGroupNames {
103122
orderedGroups = append(orderedGroups, s.apiGroups[groupName])
104123
}
105124

106-
clientIP := utilnet.GetClientIP(req)
107-
serverCIDR := s.addresses.ServerAddressByClientCIDRs(clientIP)
108125
groups := make([]metav1.APIGroup, len(orderedGroups))
109126
for i := range orderedGroups {
110127
groups[i] = orderedGroups[i]
111128
groups[i].ServerAddressByClientCIDRs = serverCIDR
112129
}
113130

114-
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, resp, req, http.StatusOK, &metav1.APIGroupList{Groups: groups}, false)
131+
return groups
132+
}
133+
134+
func (s *rootAPIsHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
135+
s.lock.RLock()
136+
defer s.lock.RUnlock()
137+
138+
groupList := metav1.APIGroupList{Groups: s.groupsLocked(req.Context(), req)}
139+
140+
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, resp, req, http.StatusOK, &groupList, false)
115141
}
116142

117143
func (s *rootAPIsHandler) restfulHandle(req *restful.Request, resp *restful.Response) {

staging/src/k8s.io/apiserver/pkg/endpoints/discovery/root_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package discovery
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"fmt"
2223
"io/ioutil"
@@ -173,6 +174,13 @@ func TestDiscoveryOrdering(t *testing.T) {
173174
t.Fatalf("unexpected error: %v", err)
174175
}
175176

177+
// Check if internal groups listers returns the same group.
178+
groups, err := handler.Groups(context.TODO(), &http.Request{})
179+
if err != nil {
180+
t.Fatalf("unexpected error: %v", err)
181+
}
182+
assert.Len(t, groups, 6)
183+
176184
assert.Len(t, groupList.Groups, 6)
177185
assert.Equal(t, "x", groupList.Groups[0].Name)
178186
assert.Equal(t, "y", groupList.Groups[1].Name)

0 commit comments

Comments
 (0)