Skip to content

Commit

Permalink
Add introspecting summary to minimize the output for pods (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
haouc authored Jul 7, 2023
1 parent a19a933 commit 2d502cf
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 4 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ type IntrospectResponse struct {
CoolingResources []CoolDownResource
}

type IntrospectSummaryResponse struct {
UsedResourcesCount int
WarmResourcesCount int
CoolingResourcesCount int
}

func NewResourcePool(log logr.Logger, poolConfig *config.WarmPoolConfig, usedResources map[string]Resource,
warmResources map[string][]Resource, nodeName string, capacity int, isPDPool bool) Pool {
pool := &pool{
Expand Down
22 changes: 22 additions & 0 deletions pkg/provider/branch/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,28 @@ func (b *branchENIProvider) Introspect() interface{} {
return allResponse
}

func (b *branchENIProvider) IntrospectSummary() interface{} {
b.lock.RLock()
defer b.lock.RUnlock()

allResponse := make(map[string]trunk.IntrospectSummaryResponse)

for nodeName, trunkENI := range b.trunkENICache {
response := trunkENI.Introspect()
allResponse[nodeName] = changeToIntrospectSummary(response)
}
return allResponse
}

func changeToIntrospectSummary(details trunk.IntrospectResponse) trunk.IntrospectSummaryResponse {
return trunk.IntrospectSummaryResponse{
TrunkENIID: details.TrunkENIID,
InstanceID: details.InstanceID,
BranchENICount: len(details.PodToBranchENI),
DeleteQueueLen: len(details.DeleteQueue),
}
}

func (b *branchENIProvider) IntrospectNode(nodeName string) interface{} {
b.lock.RLock()
defer b.lock.RUnlock()
Expand Down
7 changes: 7 additions & 0 deletions pkg/provider/branch/trunk/trunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ type IntrospectResponse struct {
DeleteQueue []ENIDetails
}

type IntrospectSummaryResponse struct {
TrunkENIID string
InstanceID string
BranchENICount int
DeleteQueueLen int
}

// NewTrunkENI returns a new Trunk ENI interface.
func NewTrunkENI(logger logr.Logger, instance ec2.EC2Instance, helper api.EC2APIHelper) TrunkENI {

Expand Down
20 changes: 20 additions & 0 deletions pkg/provider/ip/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,26 @@ func (p *ipv4Provider) Introspect() interface{} {
return response
}

func (p *ipv4Provider) IntrospectSummary() interface{} {
p.lock.RLock()
defer p.lock.RUnlock()

response := make(map[string]pool.IntrospectSummaryResponse)
for nodeName, resource := range p.instanceProviderAndPool {
response[nodeName] = ChangeToIntrospectSummary(resource.resourcePool.Introspect())

}
return response
}

func ChangeToIntrospectSummary(details pool.IntrospectResponse) pool.IntrospectSummaryResponse {
return pool.IntrospectSummaryResponse{
WarmResourcesCount: len(details.WarmResources),
CoolingResourcesCount: len(details.CoolingResources),
UsedResourcesCount: len(details.UsedResources),
}
}

func (p *ipv4Provider) IntrospectNode(nodeName string) interface{} {
p.lock.RLock()
defer p.lock.RUnlock()
Expand Down
12 changes: 12 additions & 0 deletions pkg/provider/prefix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
rcHealthz "github.com/aws/amazon-vpc-resource-controller-k8s/pkg/healthz"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/pool"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider/ip"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider/ip/eni"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/utils"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/worker"
Expand Down Expand Up @@ -435,6 +436,17 @@ func (p *ipv4PrefixProvider) Introspect() interface{} {
return response
}

func (p *ipv4PrefixProvider) IntrospectSummary() interface{} {
p.lock.RLock()
defer p.lock.RUnlock()

response := make(map[string]pool.IntrospectSummaryResponse)
for nodeName, resource := range p.instanceProviderAndPool {
response[nodeName] = ip.ChangeToIntrospectSummary(resource.resourcePool.Introspect())
}
return response
}

func (p *ipv4PrefixProvider) IntrospectNode(node string) interface{} {
p.lock.RLock()
defer p.lock.RUnlock()
Expand Down
2 changes: 2 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ type ResourceProvider interface {
IntrospectNode(node string) interface{}
// GetHealthChecker provider a health check subpath for pinging provider
GetHealthChecker() healthz.Checker
// IntrospectSummary allows introspection of resources summary per node
IntrospectSummary() interface{}
}
26 changes: 24 additions & 2 deletions pkg/resource/introspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
)

const (
GetNodeResourcesPath = "/node/"
GetAllResourcesPath = "/resources"
GetNodeResourcesPath = "/node/"
GetAllResourcesPath = "/resources/all"
GetResourcesSummaryPath = "/resources/summary"
)

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

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

// ResourceSummaryHandler returns all the resources associated with the Node
func (i *IntrospectHandler) ResourceSummaryHandler(w http.ResponseWriter, r *http.Request) {
response := make(map[string]interface{})
for resourceName, provider := range i.ResourceManager.GetResourceProviders() {
data := provider.IntrospectSummary()
response[resourceName] = data
}

jsonData, err := json.MarshalIndent(response, "", "\t")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonData)
}

func (i *IntrospectHandler) SetupWithManager(mgr ctrl.Manager, healthzHanlder *rcHealthz.HealthzHandler) error {
// add health check on subpath for introspect controller
healthzHanlder.AddControllersHealthCheckers(
Expand Down
22 changes: 20 additions & 2 deletions pkg/resource/introspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"net/http/httptest"
"testing"

"github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/provider"
"github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/resource"
mock_provider "github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/provider"
mock_resource "github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/resource"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider"

Expand Down Expand Up @@ -90,6 +90,24 @@ func TestIntrospectHandler_ResourceHandler(t *testing.T) {
VerifyResponse(t, rr, mock.response)
}

func TestIntrospectHandler_SummaryHandler(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mock := NewMockIntrospectHandler(ctrl)

req, err := http.NewRequest("GET", GetAllResourcesPath+nodeName, nil)
assert.NoError(t, err)
rr := httptest.NewRecorder()

mock.mockManager.EXPECT().GetResourceProviders().
Return(map[string]provider.ResourceProvider{resourceName: mock.mockProvider})
mock.mockProvider.EXPECT().IntrospectSummary().Return(response)

mock.handler.ResourceSummaryHandler(rr, req)
VerifyResponse(t, rr, mock.response)
}

func VerifyResponse(t *testing.T, rr *httptest.ResponseRecorder, response map[string]string) {
got := &map[string]string{}
err := json.Unmarshal(rr.Body.Bytes(), got)
Expand Down

0 comments on commit 2d502cf

Please sign in to comment.