8000 feat(sdk): add ctx on WorkerList() (#3405) · ovh/cds@cd8441b · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit cd8441b

Browse files
authored
feat(sdk): add ctx on WorkerList() (#3405)

Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ func (ui *Termui) execLoadData() error {
131131
}
132132
ui.elapsedStatus = time.Since(start)
133133

134+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
135+
defer cancel()
134136
start = time.Now()
135-
ui.workers, err = client.WorkerList()
137+
ui.workers, err = client.WorkerList(ctx)
136138
if err != nil {
137139
return err
138140
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"strings"
7+
"time"
68

79
"github.com/spf13/cobra"
810

@@ -29,7 +31,9 @@ var workerListCmd = cli.Command{
2931
}
3032

3133
func workerListRun(v cli.Values) (cli.ListResult, error) {
32-
workers, err := client.WorkerList()
34+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
35+
defer cancel()
36+
workers, err := client.WorkerList(ctx)
3337
if err != nil {
3438
return nil, err
3539
}
@@ -52,7 +56,9 @@ $ cdsctl worker disable $(cdsctl worker list)`,
5256
func workerDisableRun(v cli.Values) error {
5357
names := v.GetStringSlice("name")
5458

55-
workers, err := client.WorkerList()
59+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
60+
defer cancel()
61+
workers, err := client.WorkerList(ctx)
5662
if err != nil {
5763
return err
5864
}
@@ -63,7 +69,7 @@ func workerDisableRun(v cli.Values) error {
6369
if w.ID == n || strings.ToLower(w.Name) == strings.ToLower(n) {
6470
found = true
6571
fmt.Printf("Disabling worker %s [status %s]... ", cli.Magenta(w.Name), w.Status)
66-
if err := client.WorkerDisable(w.ID); err != nil {
72+
if err := client.WorkerDisable(context.Background(), w.ID); err != nil {
6773
fmt.Printf("Error disabling worker %s : %s\n", w.ID, err)
6874
} else {
6975
fmt.Printf("Done\n")
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"os"
8+
"time"
79

810
"github.com/ovh/cds/sdk/cdsclient"
911
)
@@ -27,7 +29,9 @@ func main() {
2729

2830
// go on https://godoc.org/github.com/ovh/cds/sdk/cdsclient to
2931
// see all available funcs
30-
workers, err := client.WorkerList()
32+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
33+
defer cancel()
34+
workers, err := client.WorkerList(ctx)
3135
if err != nil {
3236
fmt.Fprintf(os.Stderr, "Error while getting workers:%s", err)
3337
os.Exit(1)
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Start(ctx context.Context, serviceName string, w http.ResponseWriter, req *
4040
}
4141

4242
var span *trace.Span
43-
rootSpanContext, hasSpanContext := DefaultFormat.SpanContextFromRequest(req)
43+
rootSpanContext, hasSpanContext := tracingutils.DefaultFormat.SpanContextFromRequest(req)
4444

4545
var pkey string
4646
var ok bool
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package observability
22

33
import (
4-
"go.opencensus.io/plugin/ochttp/propagation/b3"
5-
"go.opencensus.io/trace/propagation"
6-
74
"github.com/ovh/cds/sdk"
85
)
96

@@ -17,8 +14,6 @@ const (
1714
StatusCodeAttribute = "http.status_code"
1815
)
1916

20-
var DefaultFormat propagation.HTTPFormat = &b3.HTTPFormat{}
21-
2217
// Configuration is the global tracing configuration
2318
type Configuration struct {
2419
Enable bool `json:"enable"`
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net/url"
1313
"time"
1414

15-
"github.com/ovh/cds/engine/api/observability"
1615
"github.com/ovh/cds/sdk"
1716
"github.com/ovh/cds/sdk/log"
1817
"github.com/ovh/cds/sdk/tracingutils"
@@ -167,7 +166,7 @@ func doRequest(ctx context.Context, httpURL string, hash string, method, path st
167166
spanCtx, ok := tracingutils.ContextToSpanContext(ctx)
168167
log.Debug("setup tracing = %v (%v) on request to %s", ok, spanCtx, req.URL.String())
169168
if ok {
170-
observability.DefaultFormat.SpanContextToRequest(spanCtx, req)
169+
tracingutils.DefaultFormat.SpanContextToRequest(spanCtx, req)
171170
}
172171

173172
req.Header.Set("Connection", "close")
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package kubernetes
22

33
import (
4+
"context"
45
"fmt"
56
"strconv"
7+
"time"
68

79
apiv1 "k8s.io/api/core/v1"
810
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -63,9 +65,12 @@ func (h *HatcheryKubernetes) getServicesLogs() error {
6365

6466
if len(servicesLogs) > 0 {
6567
// Do call api
66-
if err := h.Client.QueueServiceLogs(servicesLogs); err != nil {
68+
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
69+
if err := h.Client.QueueServiceLogs(ctx, servicesLogs); err != nil {
70+
cancel()
6771
return fmt.Errorf("Hatchery> Swarm> Cannot send service logs : %v", err)
6872
}
73+
cancel()
6974
}
7075

7176
return nil
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ func (h *HatcheryLocal) killAwolWorkers() error {
390390
h.Lock()
391391
defer h.Unlock()
392392

393-
apiWorkers, err := h.CDSClient().WorkerList()
393+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
394+
defer cancel()
395+
apiWorkers, err := h.CDSClient().WorkerList(ctx)
394396
if err != nil {
395397
return err
396398
}
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,9 @@ func (h *HatcheryMarathon) startKillAwolWorkerRoutine() {
508508
}
509509

510510
func (h *HatcheryMarathon) killDisabledWorkers() error {
511-
workers, err := h.CDSClient().WorkerList()
511+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
512+
defer cancel()
513+
workers, err := h.CDSClient().WorkerList(ctx)
512514
if err != nil {
513515
return< 10000 /span> err
514516
}
@@ -539,7 +541,9 @@ func (h *HatcheryMarathon) killDisabledWorkers() error {
539541
}
540542

541543
func (h *HatcheryMarathon) killAwolWorkers() error {
542-
workers, err := h.CDSClient().WorkerList()
544+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
545+
defer cancel()
546+
workers, err := h.CDSClient().WorkerList(ctx)
543547
if err != nil {
544548
return err
545549
}
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ func (h *HatcheryOpenstack) updateServerList() {
223223
}
224224

225225
func (h *HatcheryOpenstack) killAwolServers() {
226-
workers, err := h.CDSClient().WorkerList()
226+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
227+
defer cancel()
228+
workers, err := h.CDSClient().WorkerList(ctx)
227229
now := time.Now().Unix()
228230
if err != nil {
229231
log.Warning("killAwolServers> Cannot fetch worker list: %s", err)
@@ -383,7 +385,9 @@ func (h *HatcheryOpenstack) killErrorServers() {
383385
}
384386

385387
func (h *HatcheryOpenstack) killDisabledWorkers() {
386-
workers, err := h.CDSClient().WorkerList()
388+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
389+
defer cancel()
390+
workers, err := h.CDSClient().WorkerList(ctx)
387391
if err != nil {
388392
log.Warning("killDisabledWorkers> Cannot fetch worker list: %s", err)
389393
return