Skip to content

Commit

Permalink
Send k8s version during actions poll (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao authored Mar 30, 2022
1 parent e79561b commit 8d63c99
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
6 changes: 5 additions & 1 deletion actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ type ActionHandler interface {
func NewService(
log logrus.FieldLogger,
cfg Config,
k8sVersion string,
clientset *kubernetes.Clientset,
castaiClient castai.Client,
helmClient helm.Client,
) Service {
return &service{
log: log,
cfg: cfg,
k8sVersion: k8sVersion,
castaiClient: castaiClient,
startedActions: map[string]struct{}{},
actionHandlers: map[reflect.Type]ActionHandler{
Expand All @@ -64,6 +66,8 @@ type service struct {
cfg Config
castaiClient castai.Client

k8sVersion string

actionHandlers map[reflect.Type]ActionHandler

startedActionsWg sync.WaitGroup
Expand Down Expand Up @@ -114,7 +118,7 @@ func (s *service) doWork(ctx context.Context) error {
func (s *service) pollActions(ctx context.Context) ([]*castai.ClusterAction, error) {
ctx, cancel := context.WithTimeout(ctx, s.cfg.PollTimeout)
defer cancel()
actions, err := s.castaiClient.GetActions(ctx)
actions, err := s.castaiClient.GetActions(ctx, s.k8sVersion)
if err != nil {
return nil, err
}
Expand Down
9 changes: 8 additions & 1 deletion actions/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ func TestActions(t *testing.T) {
}

newTestService := func(handler ActionHandler, client castai.Client) *service {
svc := NewService(log, cfg, nil, client, nil).(*service)
svc := NewService(
log,
cfg,
"1.20.1",
nil,
client,
nil,
).(*service)
handlers := svc.actionHandlers
// Patch handlers with a mock one.
for k := range handlers {
Expand Down
10 changes: 6 additions & 4 deletions castai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
)

const (
headerAPIKey = "X-API-Key"
headerUserAgent = "User-Agent"
headerAPIKey = "X-API-Key"
headerUserAgent = "User-Agent"
headerKubernetesVersion = "X-K8s-Version"
)

type Client interface {
GetActions(ctx context.Context) ([]*ClusterAction, error)
GetActions(ctx context.Context, k8sVersion string) ([]*ClusterAction, error)
AckAction(ctx context.Context, actionID string, req *AckClusterActionRequest) error
SendLogs(ctx context.Context, req *LogEvent) error
SendAKSInitData(ctx context.Context, req *AKSInitDataRequest) error
Expand Down Expand Up @@ -84,11 +85,12 @@ func (c *client) SendLogs(ctx context.Context, req *LogEvent) error {
return nil
}

func (c *client) GetActions(ctx context.Context) ([]*ClusterAction, error) {
func (c *client) GetActions(ctx context.Context, k8sVersion string) ([]*ClusterAction, error) {
res := &GetClusterActionsResponse{}
resp, err := c.rest.R().
SetContext(ctx).
SetResult(res).
SetHeader(headerKubernetesVersion, k8sVersion).
Get(fmt.Sprintf("/v1/kubernetes/clusters/%s/actions", c.clusterID))
if err != nil {
return nil, fmt.Errorf("failed to request cluster-actions: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion castai/mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (m *mockClient) SendAKSInitData(ctx context.Context, req *castai.AKSInitDat
return nil
}

func (m *mockClient) GetActions(_ context.Context) ([]*castai.ClusterAction, error) {
func (m *mockClient) GetActions(_ context.Context, _ string) ([]*castai.ClusterAction, error) {
m.mu.Lock()
actions := m.Actions
m.mu.Unlock()
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ func run(
AckRetryWait: 1 * time.Second,
ClusterID: cfg.ClusterID,
}
svc := actions.NewService(log, actionsConfig, clientset, client, helmClient)
svc := actions.NewService(
log,
actionsConfig,
k8sVersion.Full(),
clientset,
client,
helmClient,
)

if cfg.LeaderElection.Enabled {
lock, err := newLeaseLock(clientset, cfg.LeaderElection.LockName, cfg.LeaderElection.Namespace)
Expand Down

0 comments on commit 8d63c99

Please sign in to comment.