Skip to content

Commit 2d502cf

Browse files
authored
Add introspecting summary to minimize the output for pods (#276)
1 parent a19a933 commit 2d502cf

File tree

9 files changed

+127
-4
lines changed

9 files changed

+127
-4
lines changed

mocks/amazon-vcp-resource-controller-k8s/pkg/provider/mock_provider.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/pool/pool.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ type IntrospectResponse struct {
104104
CoolingResources []CoolDownResource
105105
}
106106

107+
type IntrospectSummaryResponse struct {
108+
UsedResourcesCount int
109+
WarmResourcesCount int
110+
CoolingResourcesCount int
111+
}
112+
107113
func NewResourcePool(log logr.Logger, poolConfig *config.WarmPoolConfig, usedResources map[string]Resource,
108114
warmResources map[string][]Resource, nodeName string, capacity int, isPDPool bool) Pool {
109115
pool := &pool{

pkg/provider/branch/provider.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,28 @@ func (b *branchENIProvider) Introspect() interface{} {
496496
return allResponse
497497
}
498498

499+
func (b *branchENIProvider) IntrospectSummary() interface{} {
500+
b.lock.RLock()
501+
defer b.lock.RUnlock()
502+
503+
allResponse := make(map[string]trunk.IntrospectSummaryResponse)
504+
505+
for nodeName, trunkENI := range b.trunkENICache {
506+
response := trunkENI.Introspect()
507+
allResponse[nodeName] = changeToIntrospectSummary(response)
508+
}
509+
return allResponse
510+
}
511+
512+
func changeToIntrospectSummary(details trunk.IntrospectResponse) trunk.IntrospectSummaryResponse {
513+
return trunk.IntrospectSummaryResponse{
514+
TrunkENIID: details.TrunkENIID,
515+
InstanceID: details.InstanceID,
516+
BranchENICount: len(details.PodToBranchENI),
517+
DeleteQueueLen: len(details.DeleteQueue),
518+
}
519+
}
520+
499521
func (b *branchENIProvider) IntrospectNode(nodeName string) interface{} {
500522
b.lock.RLock()
501523
defer b.lock.RUnlock()

pkg/provider/branch/trunk/trunk.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ type IntrospectResponse struct {
130130
DeleteQueue []ENIDetails
131131
}
132132

133+
type IntrospectSummaryResponse struct {
134+
TrunkENIID string
135+
InstanceID string
136+
BranchENICount int
137+
DeleteQueueLen int
138+
}
139+
133140
// NewTrunkENI returns a new Trunk ENI interface.
134141
func NewTrunkENI(logger logr.Logger, instance ec2.EC2Instance, helper api.EC2APIHelper) TrunkENI {
135142

pkg/provider/ip/provider.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,26 @@ func (p *ipv4Provider) Introspect() interface{} {
460460
return response
461461
}
462462

463+
func (p *ipv4Provider) IntrospectSummary() interface{} {
464+
p.lock.RLock()
465+
defer p.lock.RUnlock()
466+
467+
response := make(map[string]pool.IntrospectSummaryResponse)
468+
for nodeName, resource := range p.instanceProviderAndPool {
469+
response[nodeName] = ChangeToIntrospectSummary(resource.resourcePool.Introspect())
470+
471+
}
472+
return response
473+
}
474+
475+
func ChangeToIntrospectSummary(details pool.IntrospectResponse) pool.IntrospectSummaryResponse {
476+
return pool.IntrospectSummaryResponse{
477+
WarmResourcesCount: len(details.WarmResources),
478+
CoolingResourcesCount: len(details.CoolingResources),
479+
UsedResourcesCount: len(details.UsedResources),
480+
}
481+
}
482+
463483
func (p *ipv4Provider) IntrospectNode(nodeName string) interface{} {
464484
p.lock.RLock()
465485
defer p.lock.RUnlock()

pkg/provider/prefix/provider.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
rcHealthz "github.com/aws/amazon-vpc-resource-controller-k8s/pkg/healthz"
2929
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/pool"
3030
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider"
31+
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider/ip"
3132
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider/ip/eni"
3233
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/utils"
3334
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/worker"
@@ -435,6 +436,17 @@ func (p *ipv4PrefixProvider) Introspect() interface{} {
435436
return response
436437
}
437438

439+
func (p *ipv4PrefixProvider) IntrospectSummary() interface{} {
440+
p.lock.RLock()
441+
defer p.lock.RUnlock()
442+
443+
response := make(map[string]pool.IntrospectSummaryResponse)
444+
for nodeName, resource := range p.instanceProviderAndPool {
445+
response[nodeName] = ip.ChangeToIntrospectSummary(resource.resourcePool.Introspect())
446+
}
447+
return response
448+
}
449+
438450
func (p *ipv4PrefixProvider) IntrospectNode(node string) interface{} {
439451
p.lock.RLock()
440452
defer p.lock.RUnlock()

pkg/provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ type ResourceProvider interface {
4242
IntrospectNode(node string) interface{}
4343
// GetHealthChecker provider a health check subpath for pinging provider
4444
GetHealthChecker() healthz.Checker
45+
// IntrospectSummary allows introspection of resources summary per node
46+
IntrospectSummary() interface{}
4547
}

pkg/resource/introspect.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525
)
2626

2727
const (
28-
GetNodeResourcesPath = "/node/"
29-
GetAllResourcesPath = "/resources"
28+
GetNodeResourcesPath = "/node/"
29+
GetAllResourcesPath = "/resources/all"
30+
GetResourcesSummaryPath = "/resources/summary"
3031
)
3132

3233
type IntrospectHandler struct {
@@ -42,6 +43,7 @@ func (i *IntrospectHandler) Start(_ context.Context) error {
4243
mux := http.NewServeMux()
4344
mux.HandleFunc(GetAllResourcesPath, i.ResourceHandler)
4445
mux.HandleFunc(GetNodeResourcesPath, i.NodeResourceHandler)
46+
mux.HandleFunc(GetResourcesSummaryPath, i.ResourceSummaryHandler)
4547

4648
// Should this be a fatal error?
4749
err := http.ListenAndServe(i.BindAddress, mux)
@@ -95,6 +97,26 @@ func (i *IntrospectHandler) NodeResourceHandler(w http.ResponseWriter, r *http.R
9597
w.Write(jsonData)
9698
}
9799

100+
// ResourceSummaryHandler returns all the resources associated with the Node
101+
func (i *IntrospectHandler) ResourceSummaryHandler(w http.ResponseWriter, r *http.Request) {
102+
response := make(map[string]interface{})
103+
for resourceName, provider := range i.ResourceManager.GetResourceProviders() {
104+
data := provider.IntrospectSummary()
105+
response[resourceName] = data
106+
}
107+
108+
jsonData, err := json.MarshalIndent(response, "", "\t")
109+
if err != nil {
110+
w.WriteHeader(http.StatusInternalServerError)
111+
w.Write([]byte(err.Error()))
112+
return
113+
}
114+
115+
w.Header().Set("content-type", "application/json")
116+
w.WriteHeader(http.StatusOK)
117+
w.Write(jsonData)
118+
}
119+
98120
func (i *IntrospectHandler) SetupWithManager(mgr ctrl.Manager, healthzHanlder *rcHealthz.HealthzHandler) error {
99121
// add health check on subpath for introspect controller
100122
healthzHanlder.AddControllersHealthCheckers(

pkg/resource/introspect_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"net/http/httptest"
2020
"testing"
2121

22-
"github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/provider"
23-
"github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/resource"
22+
mock_provider "github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/provider"
23+
mock_resource "github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/resource"
2424
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config"
2525
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider"
2626

@@ -90,6 +90,24 @@ func TestIntrospectHandler_ResourceHandler(t *testing.T) {
9090
VerifyResponse(t, rr, mock.response)
9191
}
9292

93+
func TestIntrospectHandler_SummaryHandler(t *testing.T) {
94+
ctrl := gomock.NewController(t)
95+
defer ctrl.Finish()
96+
97+
mock := NewMockIntrospectHandler(ctrl)
98+
99+
req, err := http.NewRequest("GET", GetAllResourcesPath+nodeName, nil)
100+
assert.NoError(t, err)
101+
rr := httptest.NewRecorder()
102+
103+
mock.mockManager.EXPECT().GetResourceProviders().
104+
Return(map[string]provider.ResourceProvider{resourceName: mock.mockProvider})
105+
mock.mockProvider.EXPECT().IntrospectSummary().Return(response)
106+
107+
mock.handler.ResourceSummaryHandler(rr, req)
108+
VerifyResponse(t, rr, mock.response)
109+
}
110+
93111
func VerifyResponse(t *testing.T, rr *httptest.ResponseRecorder, response map[string]string) {
94112
got := &map[string]string{}
95113
err := json.Unmarshal(rr.Body.Bytes(), got)

0 commit comments

Comments
 (0)