Skip to content

Commit

Permalink
add resource info
Browse files Browse the repository at this point in the history
  • Loading branch information
aphralG committed May 17, 2024
1 parent 1eac715 commit 6661358
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 138 deletions.
14 changes: 8 additions & 6 deletions internal/resource/resource_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type Resource struct {
messagePipe bus.MessagePipeInterface
resourceService resourceServiceInterface
resource *v1.Resource
}

func NewResource() *Resource {
Expand All @@ -29,6 +30,7 @@ func (r *Resource) Init(ctx context.Context, messagePipe bus.MessagePipeInterfac
slog.DebugContext(ctx, "Starting resource plugin")

r.messagePipe = messagePipe
r.resource = r.resourceService.GetResource(ctx)

return nil
}
Expand All @@ -52,19 +54,19 @@ func (r *Resource) Process(ctx context.Context, msg *bus.Message) {
slog.ErrorContext(ctx, "Unable to cast message payload to []*v1.Instance", "payload", msg.Data)
}

updatedResource := r.resourceService.AddInstance(instanceList)
r.resource = r.resourceService.AddInstances(instanceList)

r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ResourceUpdate, Data: updatedResource})
r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ResourceUpdate, Data: r.resource})

return
case bus.UpdatedInstances:
instanceList, ok := msg.Data.([]*v1.Instance)
if !ok {
slog.ErrorContext(ctx, "Unable to cast message payload to []*v1.Instance", "payload", msg.Data)
}
updatedResource := r.resourceService.UpdateInstance(instanceList)
r.resource = r.resourceService.UpdateInstances(instanceList)

r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ResourceUpdate, Data: updatedResource})
r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ResourceUpdate, Data: r.resource})

return

Expand All @@ -73,9 +75,9 @@ func (r *Resource) Process(ctx context.Context, msg *bus.Message) {
if !ok {
slog.ErrorContext(ctx, "Unable to cast message payload to []*v1.Instance", "payload", msg.Data)
}
updatedResource := r.resourceService.DeleteInstance(instanceList)
r.resource = r.resourceService.DeleteInstances(instanceList)

r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ResourceUpdate, Data: updatedResource})
r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ResourceUpdate, Data: r.resource})

return
default:
Expand Down
11 changes: 8 additions & 3 deletions internal/resource/resource_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func TestResource_Process(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
fakeResourceService := &resourcefakes.FakeResourceServiceInterface{}
fakeResourceService.AddInstanceReturns(protos.GetHostResource())
fakeResourceService.UpdateInstanceReturns(test.resource)
fakeResourceService.DeleteInstanceReturns(test.resource)
fakeResourceService.AddInstancesReturns(protos.GetHostResource())
fakeResourceService.UpdateInstancesReturns(test.resource)
fakeResourceService.DeleteInstancesReturns(test.resource)
messagePipe := bus.NewFakeMessagePipe()

resourcePlugin := NewResource()
Expand Down Expand Up @@ -124,11 +124,16 @@ func TestResource_Info(t *testing.T) {

func TestResource_Init(t *testing.T) {
ctx := context.Background()
resource := protos.GetContainerizedResource()

resourceService := resourcefakes.FakeResourceServiceInterface{}
resourceService.GetResourceReturns(resource)

messagePipe := bus.NewFakeMessagePipe()
messagePipe.RunWithoutInit(ctx)

resourcePlugin := NewResource()
resourcePlugin.resourceService = &resourceService
err := resourcePlugin.Init(ctx, messagePipe)
require.NoError(t, err)

Expand Down
40 changes: 31 additions & 9 deletions internal/resource/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@
package resource

import (
"context"
"sync"

"github.com/nginx/agent/v3/internal/datasource/host"

"github.com/nginx/agent/v3/api/grpc/mpi/v1"
)

//go:generate go run github.com/maxbrunsfeld/counterfeiter/[email protected] -generate
//counterfeiter:generate . resourceServiceInterface
type resourceServiceInterface interface {
AddInstance(instanceList []*v1.Instance) *v1.Resource
UpdateInstance(instanceList []*v1.Instance) *v1.Resource
DeleteInstance(instanceList []*v1.Instance) *v1.Resource
AddInstances(instanceList []*v1.Instance) *v1.Resource
UpdateInstances(instanceList []*v1.Instance) *v1.Resource
DeleteInstances(instanceList []*v1.Instance) *v1.Resource
GetResource(ctx context.Context) *v1.Resource
}

type ResourceService struct {
info host.InfoInterface
resource *v1.Resource
resourceMutex sync.Mutex
}
Expand All @@ -30,19 +35,21 @@ func NewResourceService() *ResourceService {
Instances: []*v1.Instance{},
},
resourceMutex: sync.Mutex{},
info: host.NewInfo(),
}
}

func (r *ResourceService) AddInstance(instanceList []*v1.Instance) *v1.Resource {
func (r *ResourceService) AddInstances(instanceList []*v1.Instance) *v1.Resource {
r.resourceMutex.Lock()
defer r.resourceMutex.Unlock()
r.resource.Instances = append(r.resource.GetInstances(), instanceList...)
r.resourceMutex.Unlock()

return r.resource
}

func (r *ResourceService) UpdateInstance(instanceList []*v1.Instance) *v1.Resource {
func (r *ResourceService) UpdateInstances(instanceList []*v1.Instance) *v1.Resource {
r.resourceMutex.Lock()
defer r.resourceMutex.Unlock()

for _, updatedInstance := range instanceList {
for _, instance := range r.resource.GetInstances() {
Expand All @@ -53,13 +60,13 @@ func (r *ResourceService) UpdateInstance(instanceList []*v1.Instance) *v1.Resour
}
}
}
r.resourceMutex.Unlock()

return r.resource
}

func (r *ResourceService) DeleteInstance(instanceList []*v1.Instance) *v1.Resource {
func (r *ResourceService) DeleteInstances(instanceList []*v1.Instance) *v1.Resource {
r.resourceMutex.Lock()
defer r.resourceMutex.Unlock()

for _, deletedInstance := range instanceList {
for index, instance := range r.resource.GetInstances() {
Expand All @@ -69,7 +76,22 @@ func (r *ResourceService) DeleteInstance(instanceList []*v1.Instance) *v1.Resour
}
}

r.resourceMutex.Unlock()
return r.resource
}

func (r *ResourceService) GetResource(ctx context.Context) *v1.Resource {
r.resourceMutex.Lock()
defer r.resourceMutex.Unlock()

if r.info.IsContainer() {
r.resource.Info = r.info.ContainerInfo()
r.resource.ResourceId = r.resource.GetContainerInfo().GetContainerId()
r.resource.Instances = []*v1.Instance{}
} else {
r.resource.Info = r.info.HostInfo(ctx)
r.resource.ResourceId = r.resource.GetHostInfo().GetHostId()
r.resource.Instances = []*v1.Instance{}
}

return r.resource
}
59 changes: 56 additions & 3 deletions internal/resource/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
package resource

import (
"context"
"testing"

"github.com/nginx/agent/v3/internal/datasource/host/hostfakes"

"github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/test/protos"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -46,7 +49,7 @@ func TestResourceService_AddInstance(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
resourceService := NewResourceService()
resource := resourceService.AddInstance(test.instanceList)
resource := resourceService.AddInstances(test.instanceList)
assert.Equal(tt, test.resource.GetInstances(), resource.GetInstances())
})
}
Expand Down Expand Up @@ -88,7 +91,7 @@ func TestResourceService_UpdateInstance(t *testing.T) {
t.Run(test.name, func(tt *testing.T) {
resourceService := NewResourceService()
resourceService.resource.Instances = []*v1.Instance{protos.GetNginxOssInstance([]string{})}
resource := resourceService.UpdateInstance(test.instanceList)
resource := resourceService.UpdateInstances(test.instanceList)
assert.Equal(tt, test.resource.GetInstances(), resource.GetInstances())
})
}
Expand Down Expand Up @@ -123,8 +126,58 @@ func TestResourceService_DeleteInstance(t *testing.T) {
protos.GetNginxOssInstance([]string{}),
protos.GetNginxPlusInstance([]string{}),
}
resource := resourceService.DeleteInstance(test.instanceList)
resource := resourceService.DeleteInstances(test.instanceList)
assert.Equal(tt, test.resource.GetInstances(), resource.GetInstances())
})
}
}

func TestResourceService_GetResource(t *testing.T) {
ctx := context.Background()

testCases := []struct {
isContainer bool
expectedResource *v1.Resource
}{
{
isContainer: true,
expectedResource: protos.GetContainerizedResource(),
},
{
isContainer: false,
expectedResource: protos.GetHostResource(),
},
}
for _, tc := range testCases {
mockInfo := &hostfakes.FakeInfoInterface{}
if tc.isContainer {
mockInfo.ContainerInfoReturns(
&v1.Resource_ContainerInfo{
ContainerInfo: tc.expectedResource.GetContainerInfo(),
},
)
} else {
mockInfo.HostInfoReturns(
&v1.Resource_HostInfo{
HostInfo: tc.expectedResource.GetHostInfo(),
},
)
}

mockInfo.IsContainerReturns(tc.isContainer)

resourceService := NewResourceService()
resourceService.info = mockInfo
resourceService.resource = tc.expectedResource

resource := resourceService.GetResource(ctx)
assert.Equal(t, tc.expectedResource.GetResourceId(), resource.GetResourceId())
assert.Empty(t, resource.GetInstances())

if tc.isContainer {
assert.Equal(t, tc.expectedResource.GetContainerInfo(), resource.GetContainerInfo())
} else {
assert.Equal(t, tc.expectedResource.GetHostInfo(), resource.GetHostInfo())
}
}
}
Loading

0 comments on commit 6661358

Please sign in to comment.