Skip to content

Commit c2a30b2

Browse files
committed
dont return full check config
1 parent 5ace3c0 commit c2a30b2

File tree

4 files changed

+25
-59
lines changed

4 files changed

+25
-59
lines changed

pkg/aggregate/aggregate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func Handler(w nethttp.ResponseWriter, req *nethttp.Request) {
9191
Name: c.Name,
9292
Type: c.Type,
9393
Description: c.Description,
94-
Endpoint: c.CheckConf.GetEndpoint(),
94+
Endpoint: c.Endpoint,
9595
ServerURL: "local",
9696
Health: map[string]CheckHealth{
9797
localServerId: {c.Latency, c.Uptime},
@@ -125,7 +125,7 @@ func Handler(w nethttp.ResponseWriter, req *nethttp.Request) {
125125
Name: c.Name,
126126
Type: c.Type,
127127
Description: c.Description,
128-
Endpoint: c.CheckConf.GetEndpoint(),
128+
Endpoint: c.Endpoint,
129129
ServerURL: serverURL,
130130
Health: map[string]CheckHealth{
131131
serverId: {c.Latency, c.Uptime},

pkg/api.go

+3-52
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package pkg
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"strings"
76
"time"
87

98
"github.com/flanksource/canary-checker/api/external"
109
v1 "github.com/flanksource/canary-checker/api/v1"
1110
"github.com/flanksource/commons/console"
12-
"github.com/mitchellh/mapstructure"
13-
"github.com/pkg/errors"
1411
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1512
)
1613

@@ -45,16 +42,15 @@ type CheckStatus struct {
4542
}
4643

4744
type Check struct {
48-
Key string
45+
Key string `json:"key"`
4946
Type string `json:"type"`
5047
Name string `json:"name"`
5148
Description string `json:"description"`
49+
Endpoint string `json:"endpoint"`
5250
Uptime string `json:"uptime"`
5351
Latency string `json:"latency"`
5452
Statuses []CheckStatus `json:"checkStatuses" mapstructure:"-"`
55-
// CheckConf is the configuration
56-
CheckConf external.Check `json:"checkConf" mapstructure:"-"`
57-
CheckCanary *v1.Canary `json:"-"`
53+
CheckCanary *v1.Canary `json:"-"`
5854
}
5955

6056
type Checks []Check
@@ -78,51 +74,6 @@ func (c Check) GetDescription() string {
7874
return c.Description
7975
}
8076

81-
func (c *Check) UnmarshalJSON(data []byte) error {
82-
// Unmarshalling checkStatuses because of custom JSONTime
83-
var objmap map[string]json.RawMessage
84-
if err := json.Unmarshal(data, &objmap); err != nil {
85-
return errors.Wrapf(err, "unmarshal map[string]json.RawMessage error")
86-
}
87-
88-
var statuses []CheckStatus
89-
if err := json.Unmarshal(objmap["checkStatuses"], &statuses); err != nil {
90-
return errors.Wrapf(err, "unmarshal statuses error")
91-
}
92-
93-
// Unmarshalling to interface to getting proper CheckConf type
94-
var m map[string]interface{}
95-
if err := json.Unmarshal(data, &m); err != nil {
96-
return errors.Wrapf(err, "unmarshal map[string]interface{} error")
97-
}
98-
99-
checkType := fmt.Sprintf("%v", m["type"])
100-
101-
var checkConf external.Check
102-
for _, _c := range v1.AllChecks {
103-
c := _c
104-
if c.GetType() == checkType {
105-
checkConf = c
106-
}
107-
}
108-
if checkConf == nil {
109-
return fmt.Errorf("external check type not found %s", checkType)
110-
}
111-
112-
if err := mapstructure.Decode(m["checkConf"], &checkConf); err != nil {
113-
return errors.Wrapf(err, "external check mapstructure err")
114-
}
115-
116-
// Decode rest of check fields
117-
if err := mapstructure.Decode(m, c); err != nil {
118-
return errors.Wrapf(err, "check mapstructure err")
119-
}
120-
121-
c.CheckConf = checkConf
122-
c.Statuses = statuses
123-
return nil
124-
}
125-
12677
type Config struct {
12778
HTTP []v1.HTTPCheck `yaml:"http,omitempty" json:"http,omitempty"`
12879
DNS []v1.DNSCheck `yaml:"dns,omitempty" json:"dns,omitempty"`

pkg/api/api.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ func TriggerCheckHandler(w http.ResponseWriter, req *http.Request) {
107107
return
108108
}
109109

110-
result := checker.Check(check.CheckConf)
110+
conf := cache.GetConfig(td.CheckKey)
111+
if conf == nil {
112+
http.Error(w, "Check config not found", http.StatusNotFound)
113+
return
114+
}
115+
result := checker.Check(conf)
111116
cache.AddCheck(*check.CheckCanary, result)
112117
metrics.Record(*check.CheckCanary, result)
113118
} else {

pkg/cache/cache.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"sync"
66
"time"
77

8+
"github.com/flanksource/canary-checker/api/external"
89
v1 "github.com/flanksource/canary-checker/api/v1"
910
"github.com/flanksource/canary-checker/pkg"
1011
"github.com/flanksource/canary-checker/pkg/metrics"
@@ -14,12 +15,18 @@ import (
1415
var Size = 5
1516

1617
type cache struct {
17-
Checks map[string]pkg.Check
18-
mtx sync.Mutex
18+
Checks map[string]pkg.Check
19+
CheckConfigs map[string]external.Check
20+
mtx sync.Mutex
1921
}
2022

2123
var Cache = &cache{
22-
Checks: make(map[string]pkg.Check),
24+
Checks: make(map[string]pkg.Check),
25+
CheckConfigs: make(map[string]external.Check),
26+
}
27+
28+
func GetConfig(key string) external.Check {
29+
return Cache.CheckConfigs[key]
2330
}
2431

2532
func AddCheck(check v1.Canary, result *pkg.CheckResult) *pkg.Check {
@@ -41,6 +48,7 @@ func (c *cache) RemoveCheck(checks v1.Canary) {
4148
key := checks.GetKey(check)
4249
logger.Errorf("removing %s", key)
4350
delete(c.Checks, key)
51+
delete(c.CheckConfigs, key)
4452
}
4553
}
4654

@@ -54,7 +62,9 @@ func (c *cache) InitCheck(checks v1.Canary) {
5462
Type: check.GetType(),
5563
Name: checks.ID(),
5664
Description: check.GetDescription(),
65+
Endpoint: check.GetEndpoint(),
5766
}
67+
c.CheckConfigs[key] = check
5868
}
5969
}
6070

@@ -71,7 +81,7 @@ func (c *cache) AddCheck(checks v1.Canary, result *pkg.CheckResult) *pkg.Check {
7181
Type: result.Check.GetType(),
7282
Name: checks.ID(),
7383
Description: checks.GetDescription(result.Check),
74-
CheckConf: result.Check,
84+
Endpoint: result.Check.GetEndpoint(),
7585
CheckCanary: &checks,
7686
Statuses: []pkg.CheckStatus{
7787
{

0 commit comments

Comments
 (0)