Skip to content

Commit 85cc17a

Browse files
committed
Merge branch 'v3' into add-token-file-watcher
2 parents 40cd06c + 1bdc156 commit 85cc17a

File tree

16 files changed

+551
-487
lines changed

16 files changed

+551
-487
lines changed

api/grpc/mpi/v1/command.pb.go

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

api/grpc/mpi/v1/command.pb.validate.go

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

api/grpc/mpi/v1/command.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ message ContainerInfo {
9090
string container_id = 1 [(buf.validate.field).string.uuid = true];
9191
// The name of the host
9292
string hostname = 2 [(buf.validate.field).string.address = true];
93+
// Release information of the container
94+
ReleaseInfo release_info = 3;
9395
}
9496

9597
// A response to a CreateConnectionRequest

docs/proto/protos.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ Container information
721721
| ----- | ---- | ----- | ----------- |
722722
| container_id | [string](#string) | | The identifier of the container |
723723
| hostname | [string](#string) | | The name of the host |
724+
| release_info | [ReleaseInfo](#mpi-v1-ReleaseInfo) | | Release information of the container |
724725

725726

726727

internal/datasource/host/info.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func (i *Info) ContainerInfo(ctx context.Context) *v1.Resource_ContainerInfo {
144144
ContainerInfo: &v1.ContainerInfo{
145145
ContainerId: i.getContainerID(),
146146
Hostname: hostname,
147+
ReleaseInfo: i.getReleaseInfo(ctx, i.osReleaseLocation),
147148
},
148149
}
149150
}

internal/datasource/host/info_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,20 @@ func TestInfo_IsContainer(t *testing.T) {
426426

427427
func TestInfo_ContainerInfo(t *testing.T) {
428428
ctx := context.Background()
429+
430+
osReleaseFile := helpers.CreateFileWithErrorCheck(t, os.TempDir(), "os-release")
431+
defer helpers.RemoveFileWithErrorCheck(t, osReleaseFile.Name())
432+
err := os.WriteFile(osReleaseFile.Name(), []byte(ubuntuReleaseInfo), os.ModeAppend)
433+
require.NoError(t, err)
434+
435+
releaseInfo := &v1.ReleaseInfo{
436+
Codename: "jammy",
437+
Id: "ubuntu",
438+
Name: "Ubuntu",
439+
VersionId: "22.04",
440+
Version: "22.04.5 LTS (Jammy Jellyfish)",
441+
}
442+
429443
tests := []struct {
430444
name string
431445
mountInfo string
@@ -501,8 +515,9 @@ func TestInfo_ContainerInfo(t *testing.T) {
501515

502516
execMock := &execfakes.FakeExecInterface{}
503517
execMock.HostnameReturns(test.expectHostname, nil)
518+
execMock.ReleaseInfoReturns(releaseInfo)
504519

505-
_, err := mountInfoFile.WriteString(test.mountInfo)
520+
_, err = mountInfoFile.WriteString(test.mountInfo)
506521
require.NoError(tt, err)
507522

508523
err = mountInfoFile.Close()
@@ -515,6 +530,7 @@ func TestInfo_ContainerInfo(t *testing.T) {
515530

516531
assert.Equal(tt, test.expectContainerID, containerInfo.ContainerInfo.GetContainerId())
517532
assert.Equal(tt, test.expectHostname, containerInfo.ContainerInfo.GetHostname())
533+
assert.Equal(t, releaseInfo, containerInfo.ContainerInfo.GetReleaseInfo())
518534
})
519535
}
520536
}

internal/resource/resource_plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (r *Resource) Process(ctx context.Context, msg *bus.Message) {
9494

9595
return
9696
}
97-
resource := r.resourceService.UpdateInstances(instanceList)
97+
resource := r.resourceService.UpdateInstances(ctx, instanceList)
9898

9999
r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ResourceUpdateTopic, Data: resource})
100100

@@ -107,7 +107,7 @@ func (r *Resource) Process(ctx context.Context, msg *bus.Message) {
107107

108108
return
109109
}
110-
resource := r.resourceService.DeleteInstances(instanceList)
110+
resource := r.resourceService.DeleteInstances(ctx, instanceList)
111111

112112
r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ResourceUpdateTopic, Data: resource})
113113

internal/resource/resource_service.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"strings"
1717
"sync"
1818

19+
"google.golang.org/protobuf/proto"
20+
1921
"github.com/nginxinc/nginx-plus-go-client/v2/client"
2022
"google.golang.org/protobuf/types/known/structpb"
2123

@@ -42,8 +44,8 @@ const (
4244

4345
type resourceServiceInterface interface {
4446
AddInstances(instanceList []*mpi.Instance) *mpi.Resource
45-
UpdateInstances(instanceList []*mpi.Instance) *mpi.Resource
46-
DeleteInstances(instanceList []*mpi.Instance) *mpi.Resource
47+
UpdateInstances(ctx context.Context, instanceList []*mpi.Instance) *mpi.Resource
48+
DeleteInstances(ctx context.Context, instanceList []*mpi.Instance) *mpi.Resource
4749
ApplyConfig(ctx context.Context, instanceID string) error
4850
Instance(instanceID string) *mpi.Instance
4951
GetHTTPUpstreamServers(ctx context.Context, instance *mpi.Instance, upstreams string) ([]client.UpstreamServer,
@@ -126,32 +128,45 @@ func (r *ResourceService) RemoveOperator(instanceList []*mpi.Instance) {
126128
}
127129
}
128130

129-
func (r *ResourceService) UpdateInstances(instanceList []*mpi.Instance) *mpi.Resource {
131+
func (r *ResourceService) UpdateInstances(ctx context.Context, instanceList []*mpi.Instance) *mpi.Resource {
130132
r.resourceMutex.Lock()
131133
defer r.resourceMutex.Unlock()
132134

133135
for _, updatedInstance := range instanceList {
134-
for _, instance := range r.resource.GetInstances() {
135-
if updatedInstance.GetInstanceMeta().GetInstanceId() == instance.GetInstanceMeta().GetInstanceId() {
136-
instance.InstanceMeta = updatedInstance.GetInstanceMeta()
137-
instance.InstanceRuntime = updatedInstance.GetInstanceRuntime()
138-
instance.InstanceConfig = updatedInstance.GetInstanceConfig()
136+
resourceCopy, ok := proto.Clone(r.resource).(*mpi.Resource)
137+
if ok {
138+
for _, instance := range resourceCopy.GetInstances() {
139+
if updatedInstance.GetInstanceMeta().GetInstanceId() == instance.GetInstanceMeta().GetInstanceId() {
140+
instance.InstanceMeta = updatedInstance.GetInstanceMeta()
141+
instance.InstanceRuntime = updatedInstance.GetInstanceRuntime()
142+
instance.InstanceConfig = updatedInstance.GetInstanceConfig()
143+
}
139144
}
145+
r.resource = resourceCopy
146+
} else {
147+
slog.WarnContext(ctx, "Unable to clone resource while updating instances", "resource",
148+
r.resource, "instances", instanceList)
140149
}
141150
}
142151

143152
return r.resource
144153
}
145154

146-
func (r *ResourceService) DeleteInstances(instanceList []*mpi.Instance) *mpi.Resource {
155+
func (r *ResourceService) DeleteInstances(ctx context.Context, instanceList []*mpi.Instance) *mpi.Resource {
147156
r.resourceMutex.Lock()
148157
defer r.resourceMutex.Unlock()
149158

150159
for _, deletedInstance := range instanceList {
151-
for index, instance := range r.resource.GetInstances() {
152-
if deletedInstance.GetInstanceMeta().GetInstanceId() == instance.GetInstanceMeta().GetInstanceId() {
153-
r.resource.Instances = append(r.resource.Instances[:index], r.resource.GetInstances()[index+1:]...)
160+
resourceCopy, ok := proto.Clone(r.resource).(*mpi.Resource)
161+
if ok {
162+
for index, instance := range resourceCopy.GetInstances() {
163+
if deletedInstance.GetInstanceMeta().GetInstanceId() == instance.GetInstanceMeta().GetInstanceId() {
164+
r.resource.Instances = append(r.resource.Instances[:index], r.resource.GetInstances()[index+1:]...)
165+
}
154166
}
167+
} else {
168+
slog.WarnContext(ctx, "Unable to clone resource while deleting instances", "resource",
169+
r.resource, "instances", instanceList)
155170
}
156171
}
157172
r.RemoveOperator(instanceList)

internal/resource/resource_service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestResourceService_UpdateInstance(t *testing.T) {
104104
t.Run(test.name, func(tt *testing.T) {
105105
resourceService := NewResourceService(ctx, types.AgentConfig())
106106
resourceService.resource.Instances = []*v1.Instance{protos.GetNginxOssInstance([]string{})}
107-
resource := resourceService.UpdateInstances(test.instanceList)
107+
resource := resourceService.UpdateInstances(ctx, test.instanceList)
108108
assert.Equal(tt, test.resource.GetInstances(), resource.GetInstances())
109109
})
110110
}
@@ -141,7 +141,7 @@ func TestResourceService_DeleteInstance(t *testing.T) {
141141
protos.GetNginxOssInstance([]string{}),
142142
protos.GetNginxPlusInstance([]string{}),
143143
}
144-
resource := resourceService.DeleteInstances(test.instanceList)
144+
resource := resourceService.DeleteInstances(ctx, test.instanceList)
145145
assert.Equal(tt, test.resource.GetInstances(), resource.GetInstances())
146146
})
147147
}

internal/resource/resourcefakes/fake_resource_service_interface.go

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

0 commit comments

Comments
 (0)