Skip to content

Commit

Permalink
Remove SetAvailableComponents method from interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsillydog committed Jan 14, 2025
1 parent 7893479 commit 9e2acf3
Show file tree
Hide file tree
Showing 8 changed files with 1 addition and 182 deletions.
14 changes: 0 additions & 14 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,4 @@ type OpAMPClient interface {
// If no error is returned, the channel returned will be closed after the specified
// message is sent.
SendCustomMessage(message *protobufs.CustomMessage) (messageSendingChannel chan struct{}, err error)

// SetAvailableComponents modifies the set of components that are available for configuration
// on the agent.
// May be called any time after Start(), including from the OnMessage handler.
// The new components will be sent with the next message to the server.
//
// If components is nil, types.ErrAvailableComponentsMissing will be returned.
// If components.Hash is nil or an empty []byte, types.ErrNoAvailableComponentHash will be returned.
// If the ReportsAvailableComponents capability is not set in StartSettings.Capabilities during Start(),
// types.ErrReportsAvailableComponentsNotSet will be returned.
//
// This method is subject to agent status compression - if components is not
// different from the cached agent state, this method is a no-op.
SetAvailableComponents(components *protobufs.AvailableComponents) error
}
103 changes: 0 additions & 103 deletions client/clientimpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2306,109 +2306,6 @@ func TestSetFlagsBeforeStart(t *testing.T) {
})
}

func TestSetAvailableComponents(t *testing.T) {
testCases := []struct {
desc string
capabilities protobufs.AgentCapabilities
testFunc func(t *testing.T, client OpAMPClient, srv *internal.MockServer)
}{
{
desc: "apply nil AvailableComponents",
capabilities: protobufs.AgentCapabilities_AgentCapabilities_ReportsAvailableComponents,
testFunc: func(t *testing.T, client OpAMPClient, _ *internal.MockServer) {
require.ErrorIs(t, client.SetAvailableComponents(nil), types.ErrAvailableComponentsMissing)
},
},
{
desc: "apply AvailableComponents with empty hash",
capabilities: protobufs.AgentCapabilities_AgentCapabilities_ReportsAvailableComponents,
testFunc: func(t *testing.T, client OpAMPClient, _ *internal.MockServer) {
require.ErrorIs(t, client.SetAvailableComponents(&protobufs.AvailableComponents{}), types.ErrNoAvailableComponentHash)
},
},
{
desc: "apply AvailableComponents without required capability",
testFunc: func(t *testing.T, client OpAMPClient, _ *internal.MockServer) {
require.ErrorIs(t, client.SetAvailableComponents(generateTestAvailableComponents()), types.ErrReportsAvailableComponentsNotSet)
},
},
{
desc: "apply AvailableComponents with cached AvailableComponents",
capabilities: protobufs.AgentCapabilities_AgentCapabilities_ReportsAvailableComponents,
testFunc: func(t *testing.T, client OpAMPClient, _ *internal.MockServer) {
require.NoError(t, client.SetAvailableComponents(generateTestAvailableComponents()))
},
},
{
desc: "apply AvailableComponents with new AvailableComponents",
capabilities: protobufs.AgentCapabilities_AgentCapabilities_ReportsAvailableComponents,
testFunc: func(t *testing.T, client OpAMPClient, srv *internal.MockServer) {
availableComponents := generateTestAvailableComponents()
availableComponents.Hash = []byte("different")
require.NoError(t, client.SetAvailableComponents(availableComponents))
srv.Expect(func(msg *protobufs.AgentToServer) *protobufs.ServerToAgent {
assert.EqualValues(t, 1, msg.SequenceNum)
msgAvailableComponents := msg.GetAvailableComponents()
require.NotNil(t, msgAvailableComponents)
require.Equal(t, msgAvailableComponents.GetHash(), availableComponents.GetHash())
require.Nil(t, msgAvailableComponents.GetComponents())
return nil
})
},
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
testClients(t, func(t *testing.T, client OpAMPClient) {

// Start a Server.
srv := internal.StartMockServer(t)
srv.EnableExpectMode()

availableComponents := generateTestAvailableComponents()

// Start a client.
settings := types.StartSettings{
OpAMPServerURL: "ws://" + srv.Endpoint,
Callbacks: types.Callbacks{
OnMessage: func(ctx context.Context, msg *types.MessageData) {},
},
Capabilities: tc.capabilities,
AvailableComponents: availableComponents,
}
prepareClient(t, &settings, client)

// Client --->
assert.NoError(t, client.Start(context.Background(), settings))

// ---> Server
srv.Expect(func(msg *protobufs.AgentToServer) *protobufs.ServerToAgent {
assert.EqualValues(t, 0, msg.SequenceNum)
msgAvailableComponents := msg.GetAvailableComponents()
if tc.capabilities&protobufs.AgentCapabilities_AgentCapabilities_ReportsAvailableComponents != 0 {
require.NotNil(t, msgAvailableComponents)
require.Equal(t, msgAvailableComponents.GetHash(), availableComponents.GetHash())
require.Nil(t, msgAvailableComponents.GetComponents())
} else {
require.Nil(t, msgAvailableComponents)
}
return nil
})

tc.testFunc(t, client, srv)

// Shutdown the Server.
srv.Close()

// Shutdown the client.
err := client.Stop(context.Background())
assert.NoError(t, err)
})
})
}
}

func generateTestAvailableComponents() *protobufs.AvailableComponents {
return &protobufs.AvailableComponents{
Hash: []byte("fake-hash"),
Expand Down
5 changes: 0 additions & 5 deletions client/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ func (c *httpClient) SendCustomMessage(message *protobufs.CustomMessage) (messag
return c.common.SendCustomMessage(message)
}

// SetAvailableComponents implements OpAMPClient.SetAvailableComponents
func (c *httpClient) SetAvailableComponents(components *protobufs.AvailableComponents) error {
return c.common.SetAvailableComponents(components)
}

func (c *httpClient) runUntilStopped(ctx context.Context) {
// Start the HTTP sender. This will make request/responses with retries for
// failures and will wait with configured polling interval if there is nothing
Expand Down
40 changes: 0 additions & 40 deletions client/internal/clientcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,43 +454,3 @@ func (c *ClientCommon) SendCustomMessage(message *protobufs.CustomMessage) (mess

return sendingChan, nil
}

// SetAvailableComponents sends a message to the server with the available components for the agent
func (c *ClientCommon) SetAvailableComponents(components *protobufs.AvailableComponents) error {
if c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_ReportsAvailableComponents == 0 {
return types.ErrReportsAvailableComponentsNotSet
}

if components == nil {
return types.ErrAvailableComponentsMissing
}

if len(components.Hash) == 0 {
return types.ErrNoAvailableComponentHash
}

// implement agent status compression, don't send the message if it hasn't changed from the previous message
availableComponentsChanged := !proto.Equal(c.ClientSyncedState.AvailableComponents(), components)

if availableComponentsChanged {
if err := c.ClientSyncedState.SetAvailableComponents(components); err != nil {
return err
}

// initially, do not send the full component state - just send the hash.
// full state is available on request from the server using the corresponding ServerToAgent flag
availableComponents := &protobufs.AvailableComponents{
Hash: c.ClientSyncedState.AvailableComponents().GetHash(),
}

c.sender.NextMessage().Update(
func(msg *protobufs.AgentToServer) {
msg.AvailableComponents = availableComponents
},
)

c.sender.ScheduleSend()
}

return nil
}
4 changes: 1 addition & 3 deletions client/internal/receivedprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func (r *receivedProcessor) ProcessReceivedMessage(ctx context.Context, msg *pro
r.logger.Errorf(ctx, "cannot processed received flags:%v", err)
}

msgData := &types.MessageData{
Flags: protobufs.ServerToAgentFlags(msg.Flags),
}
msgData := &types.MessageData{}

if msg.RemoteConfig != nil {
if r.hasCapability(protobufs.AgentCapabilities_AgentCapabilities_AcceptsRemoteConfig) {
Expand Down
3 changes: 0 additions & 3 deletions client/types/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ type MessageData struct {

// CustomMessage contains a custom message sent by the server.
CustomMessage *protobufs.CustomMessage

// Flags contains any flags sent by the server.
Flags protobufs.ServerToAgentFlags
}

// Callbacks contains functions that are executed when the client encounters
Expand Down
9 changes: 0 additions & 9 deletions client/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,4 @@ var (
// ErrCustomMessagePending is returned by SendCustomMessage when called before the previous
// message has been sent.
ErrCustomMessagePending = errors.New("custom message already set")

// ErrReportsAvailableComponentsNotSet is returned by SetAvailableComponents without the ReportsAvailableComponents capability set
ErrReportsAvailableComponentsNotSet = errors.New("ReportsAvailableComponents capability is not set")

// ErrAvailableComponentsMissing is returned by SetAvailableComponents when called with a nil message
ErrAvailableComponentsMissing = errors.New("AvailableComponents is nil")

// ErrNoAvailableComponentHash is returned by SetAvailableComponents when called with a message with an empty hash
ErrNoAvailableComponentHash = errors.New("AvailableComponents.Hash is empty")
)
5 changes: 0 additions & 5 deletions client/wsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ func (c *wsClient) SendCustomMessage(message *protobufs.CustomMessage) (messageS
return c.common.SendCustomMessage(message)
}

// SetAvailableComponents implements OpAMPClient.SetAvailableComponents
func (c *wsClient) SetAvailableComponents(components *protobufs.AvailableComponents) error {
return c.common.SetAvailableComponents(components)
}

func viaReq(resps []*http.Response) []*http.Request {
reqs := make([]*http.Request, 0, len(resps))
for _, resp := range resps {
Expand Down

0 comments on commit 9e2acf3

Please sign in to comment.