Skip to content

Commit

Permalink
feat: add agent version to snapshot delta (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenorgate authored Jul 4, 2024
1 parent 4bbc19c commit 16817b2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions internal/castai/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type AgentTelemetryResponse struct {
type Delta struct {
ClusterID string `json:"clusterId"`
ClusterVersion string `json:"clusterVersion"`
AgentVersion string `json:"agentVersion"`
FullSnapshot bool `json:"fullSnapshot"`
Items []*DeltaItem `json:"items"`
ContinuityToken string `json:"continuityToken"`
Expand Down
6 changes: 4 additions & 2 deletions internal/services/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ func CollectSingleSnapshot(ctx context.Context,

defer queue.ShutDown()

d := delta.New(log, clusterID, v.Full())
agentVersion := ctx.Value("agentVersion").(*config.AgentVersion)

d := delta.New(log, clusterID, v.Full(), agentVersion.Version)
go func() {
for {
i, _ := queue.Get()
Expand Down Expand Up @@ -231,7 +233,7 @@ func New(
castaiclient: castaiclient,
provider: provider,
cfg: cfg,
delta: delta.New(log, clusterID, v.Full()),
delta: delta.New(log, clusterID, v.Full(), agentVersion.Version),
queue: queue,
informers: handledInformers,
agentVersion: agentVersion,
Expand Down
17 changes: 14 additions & 3 deletions internal/services/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestController_ShouldReceiveDeltasBasedOnAvailableResources(t *testing.T) {
log.SetLevel(logrus.DebugLevel)

version.EXPECT().Full().Return("1.21+").MaxTimes(3)
agentVersion := &config.AgentVersion{Version: "1.2.3"}

clusterID := uuid.New()
var mockDiscovery *mock_discovery.MockDiscoveryInterface
Expand Down Expand Up @@ -166,6 +167,7 @@ func TestController_ShouldReceiveDeltasBasedOnAvailableResources(t *testing.T) {

require.Equal(t, clusterID, d.ClusterID)
require.Equal(t, "1.21+", d.ClusterVersion)
require.Equal(t, "1.2.3", d.AgentVersion)
require.True(t, d.FullSnapshot)
require.Len(t, d.Items, tt.expectedReceivedObjectsCount)

Expand All @@ -181,7 +183,6 @@ func TestController_ShouldReceiveDeltasBasedOnAvailableResources(t *testing.T) {
return nil
})

agentVersion := &config.AgentVersion{Version: "1.2.3"}
castaiclient.EXPECT().ExchangeAgentTelemetry(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().
Return(&castai.AgentTelemetryResponse{}, nil).
Do(func(ctx context.Context, clusterID string, req *castai.AgentTelemetryRequest) {
Expand Down Expand Up @@ -415,6 +416,8 @@ func TestController_ShouldKeepDeltaAfterDelete(t *testing.T) {

version.EXPECT().Full().Return("1.21+").MaxTimes(3)

agentVersion := &config.AgentVersion{Version: "1.2.3"}

clusterID := uuid.New()
log := logrus.New()

Expand All @@ -428,6 +431,7 @@ func TestController_ShouldKeepDeltaAfterDelete(t *testing.T) {

require.Equal(t, clusterID, d.ClusterID)
require.Equal(t, "1.21+", d.ClusterVersion)
require.Equal(t, "1.2.3", d.AgentVersion)
require.True(t, d.FullSnapshot)
require.Len(t, d.Items, 0)

Expand All @@ -445,6 +449,7 @@ func TestController_ShouldKeepDeltaAfterDelete(t *testing.T) {

require.Equal(t, clusterID, d.ClusterID)
require.Equal(t, "1.21+", d.ClusterVersion)
require.Equal(t, "1.2.3", d.AgentVersion)
require.False(t, d.FullSnapshot)
require.Len(t, d.Items, 1)

Expand All @@ -469,6 +474,7 @@ func TestController_ShouldKeepDeltaAfterDelete(t *testing.T) {

require.Equal(t, clusterID, d.ClusterID)
require.Equal(t, "1.21+", d.ClusterVersion)
require.Equal(t, "1.2.3", d.AgentVersion)
require.False(t, d.FullSnapshot)
require.Len(t, d.Items, 1)

Expand All @@ -482,7 +488,6 @@ func TestController_ShouldKeepDeltaAfterDelete(t *testing.T) {
return nil
})

agentVersion := &config.AgentVersion{Version: "1.2.3"}
castaiclient.EXPECT().ExchangeAgentTelemetry(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().
Return(&castai.AgentTelemetryResponse{}, nil).
Do(func(ctx context.Context, clusterID string, req *castai.AgentTelemetryRequest) {
Expand Down Expand Up @@ -968,8 +973,14 @@ func TestCollectSingleSnapshot(t *testing.T) {

mockctrl := gomock.NewController(t)
version := mock_version.NewMockInterface(mockctrl)
ctx := context.Background()

version.EXPECT().Full().Return("1.21+")
ctx = context.WithValue(ctx, "agentVersion", &config.AgentVersion{
GitCommit: "test",
GitRef: "test",
Version: "test",
})

var objs []runtime.Object
for i := range 10000 {
Expand All @@ -984,7 +995,7 @@ func TestCollectSingleSnapshot(t *testing.T) {
clientset := fake.NewSimpleClientset(objs...)

snapshot, err := CollectSingleSnapshot(
context.Background(),
ctx,
logrus.New(),
"123",
clientset,
Expand Down
5 changes: 4 additions & 1 deletion internal/services/controller/delta/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (

// New initializes the Delta struct which is used to collect cluster deltas, debounce them and map to CAST AI
// requests.
func New(log logrus.FieldLogger, clusterID, clusterVersion string) *Delta {
func New(log logrus.FieldLogger, clusterID, clusterVersion, agentVersion string) *Delta {
return &Delta{
log: log,
clusterID: clusterID,
clusterVersion: clusterVersion,
agentVersion: agentVersion,
FullSnapshot: true,
Cache: map[string]*Item{},
}
Expand All @@ -32,6 +33,7 @@ type Delta struct {
log logrus.FieldLogger
clusterID string
clusterVersion string
agentVersion string
FullSnapshot bool
Cache map[string]*Item
}
Expand Down Expand Up @@ -95,6 +97,7 @@ func (d *Delta) ToCASTAIRequest() *castai.Delta {
return &castai.Delta{
ClusterID: d.clusterID,
ClusterVersion: d.clusterVersion,
AgentVersion: d.agentVersion,
FullSnapshot: d.FullSnapshot,
Items: items,
}
Expand Down
4 changes: 3 additions & 1 deletion internal/services/controller/delta/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func TestDelta(t *testing.T) {
clusterID := uuid.New().String()
version := "1.18"
agentVersion := "1.2.3"

pod1 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: v1.NamespaceDefault, Name: "a"}}
pod1Updated := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: v1.NamespaceDefault, Name: "a", Labels: map[string]string{"a": "b"}}}
Expand Down Expand Up @@ -159,7 +160,7 @@ func TestDelta(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
d := New(logrus.New(), clusterID, version)
d := New(logrus.New(), clusterID, version, agentVersion)

for _, item := range test.items {
d.Add(item)
Expand All @@ -169,6 +170,7 @@ func TestDelta(t *testing.T) {

require.Equal(t, clusterID, got.ClusterID)
require.Equal(t, version, got.ClusterVersion)
require.Equal(t, agentVersion, got.AgentVersion)
require.True(t, got.FullSnapshot)
require.Equal(t, len(test.expected.Items), len(got.Items))
for _, expectedItem := range test.expected.Items {
Expand Down

0 comments on commit 16817b2

Please sign in to comment.