Skip to content

Commit

Permalink
fix listing servers from API
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <[email protected]>
  • Loading branch information
maksim-paskal committed Jan 9, 2024
1 parent 86191bc commit 07d7ff2
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,34 @@ func (api *ApplicationAPI) PatchClusterDeployment(ctx context.Context) error {
return nil
}

func (api *ApplicationAPI) listServers(ctx context.Context, labelSelector string) ([]*hcloud.Server, error) {
servers := make([]*hcloud.Server, 0)

opts := hcloud.ServerListOpts{
ListOpts: hcloud.ListOpts{
Page: 1,
LabelSelector: labelSelector,
},
}

for ctx.Err() == nil {
page, _, err := api.hcloudClient.Server.List(ctx, opts)
if err != nil {
return nil, errors.Wrap(err, "error in list servers")
}

if len(page) == 0 {
break
}

opts.ListOpts.Page++

servers = append(servers, page...)
}

return servers, nil
}

func (api *ApplicationAPI) ExecuteAdHoc(ctx context.Context, user string, command string, runOnMasters bool, runOnWorkers bool, copyNewScripts bool) { //nolint:funlen,lll,cyclop
log.Info("Executing adhoc...")

Expand All @@ -806,23 +834,21 @@ func (api *ApplicationAPI) ExecuteAdHoc(ctx context.Context, user string, comman
if runOnWorkers {
log.Info("Get worker nodes...")

workerServers, _, _ := api.hcloudClient.Server.List(ctx, hcloud.ServerListOpts{
ListOpts: hcloud.ListOpts{
LabelSelector: nodeGroupSelector,
},
})
workerServers, err := api.listServers(ctx, nodeGroupSelector)
if err != nil {
log.WithError(err).Error()
}

allServers = append(allServers, workerServers...)
}

if runOnMasters {
log.Info("Get master nodes...")

masterServers, _, _ := api.hcloudClient.Server.List(ctx, hcloud.ServerListOpts{
ListOpts: hcloud.ListOpts{
LabelSelector: api.getMasterLabels(),
},
})
masterServers, err := api.listServers(ctx, api.getMasterLabels())
if err != nil {
log.WithError(err).Error()
}

allServers = append(allServers, masterServers...)
}
Expand All @@ -833,6 +859,8 @@ func (api *ApplicationAPI) ExecuteAdHoc(ctx context.Context, user string, comman
return
}

log.Infof("Servers found: %d", len(allServers))

adhocStatus := make(map[string]string)

var (
Expand Down

0 comments on commit 07d7ff2

Please sign in to comment.