-
Notifications
You must be signed in to change notification settings - Fork 10
/
clusterchecks.go
115 lines (96 loc) · 3.36 KB
/
clusterchecks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package clusteragent
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/clusteragent/clusterchecks/types"
"k8s.io/klog/v2"
)
const (
dcaClusterChecksPath = "api/v1/clusterchecks"
dcaClusterChecksStatusPath = dcaClusterChecksPath + "/status"
dcaClusterChecksConfigsPath = dcaClusterChecksPath + "/configs"
)
// PostClusterCheckStatus is called by the clustercheck config provider
func (c *DCAClient) PostClusterCheckStatus(identifier string, status types.NodeStatus) (types.StatusResponse, error) {
// Retry on the main URL if the leader fails
willRetry := c.leaderClient.hasLeader()
result, err := c.doPostClusterCheckStatus(identifier, status)
if err != nil && willRetry {
klog.V(5).Infof("Got error on leader, retrying via the service: %s", err)
c.leaderClient.resetURL()
return c.doPostClusterCheckStatus(identifier, status)
}
return result, err
}
func (c *DCAClient) doPostClusterCheckStatus(identifier string, status types.NodeStatus) (types.StatusResponse, error) {
var response types.StatusResponse
queryBody, err := json.Marshal(status)
if err != nil {
return response, err
}
// https://host:port/api/v1/clusterchecks/status/{identifier}
rawURL := c.leaderClient.buildURL(dcaClusterChecksStatusPath, identifier)
req, err := http.NewRequest("POST", rawURL, bytes.NewBuffer(queryBody))
if err != nil {
return response, err
}
req.Header = c.clusterAgentAPIRequestHeaders
resp, err := c.leaderClient.Do(req)
if err != nil {
return response, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return response, fmt.Errorf("unexpected response: %d - %s", resp.StatusCode, resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
err = json.Unmarshal(b, &response)
return response, err
}
// GetClusterCheckConfigs is called by the clustercheck config provider
func (c *DCAClient) GetClusterCheckConfigs(identifier string) (types.ConfigResponse, error) {
// Retry on the main URL if the leader fails
willRetry := c.leaderClient.hasLeader()
result, err := c.doGetClusterCheckConfigs(identifier)
if err != nil && willRetry {
klog.V(5).Infof("Got error on leader, retrying via the service: %s", err)
c.leaderClient.resetURL()
return c.doGetClusterCheckConfigs(identifier)
}
return result, err
}
func (c *DCAClient) doGetClusterCheckConfigs(identifier string) (types.ConfigResponse, error) {
var configs types.ConfigResponse
var err error
// https://host:port/api/v1/clusterchecks/configs/{identifier}
rawURL := c.leaderClient.buildURL(dcaClusterChecksConfigsPath, identifier)
req, err := http.NewRequest("GET", rawURL, nil)
if err != nil {
return configs, err
}
req.Header = c.clusterAgentAPIRequestHeaders
resp, err := c.leaderClient.Do(req)
if err != nil {
return configs, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return configs, fmt.Errorf("unexpected response: %d - %s", resp.StatusCode, resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return configs, err
}
err = json.Unmarshal(b, &configs)
return configs, err
}