Skip to content

Commit

Permalink
feat(tags): Turn all labels into tags (#20)
Browse files Browse the repository at this point in the history
In kubernetes we can apply labels to each resource, this can hold any
key/value pair, just like the tags in checkly checks. Using the new
feature from checklyhq.com where all tags become labels in the
prometheus endpoint, we can actually set tags from the labels of the
checks and groups which we set up.
  • Loading branch information
akosveres authored Jul 19, 2022
1 parent d3897e6 commit d8e343f
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 30 deletions.
3 changes: 2 additions & 1 deletion config/samples/checkly_v1alpha1_apicheck.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ apiVersion: k8s.checklyhq.com/v1alpha1
kind: ApiCheck
metadata:
name: apicheck-sample
labels:
service: "foo"
spec:
endpoint: "https://foo.bar/baz"
success: "200"
team: qux
frequency: 10 # Default 5
muted: true # Default "false"
group: "group-sample"
2 changes: 2 additions & 0 deletions config/samples/checkly_v1alpha1_group.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ apiVersion: k8s.checklyhq.com/v1alpha1
kind: Group
metadata:
name: group-sample
labels:
environment: "local"
spec:
locations:
- eu-west-1
Expand Down
1 change: 1 addition & 0 deletions controllers/checkly/apicheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
ID: apiCheck.Status.ID,
GroupID: group.Status.ID,
Muted: apiCheck.Spec.Muted,
Labels: apiCheck.Labels,
}

// /////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions controllers/checkly/group_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
Locations: group.Spec.Locations,
AlertChannels: group.Spec.AlertChannels,
ID: group.Status.ID,
Labels: group.Labels,
}

// /////////////////////////////
Expand Down
11 changes: 11 additions & 0 deletions external/checkly/api_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package external

import "fmt"

func checkValueString(x string, y string) (value string) {
if x == "" {
value = y
Expand All @@ -42,3 +44,12 @@ func checkValueArray(x []string, y []string) (value []string) {
}
return
}

func getTags(labels map[string]string) (tags []string) {

for k, v := range labels {
tags = append(tags, fmt.Sprintf("%s:%s", k, v))
}

return
}
17 changes: 17 additions & 0 deletions external/checkly/api_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,20 @@ func TestCheckValueArray(t *testing.T) {
}

}

func TestGetTags(t *testing.T) {
var data = make(map[string]string)
data["foo"] = "bar"

response := getTags(data)
if len(response) != 1 {
t.Errorf("Expected 1 item, got %d", len(response))
}

for _, v := range response {
if v != "foo:bar" {
t.Errorf("Expected foo:bar, got %s", v)
}
}

}
36 changes: 19 additions & 17 deletions external/checkly/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Check struct {
GroupID int64
ID string
Muted bool
Labels map[string]string
}

func checklyCheck(apiCheck Check) (check checkly.Check, err error) {
Expand All @@ -45,6 +46,10 @@ func checklyCheck(apiCheck Check) (check checkly.Check, err error) {
return
}

tags := getTags(apiCheck.Labels)
tags = append(tags, "checkly-operator")
tags = append(tags, apiCheck.Namespace)

alertSettings := checkly.AlertSettings{
EscalationType: checkly.RunBased,
RunBasedEscalation: checkly.RunBasedEscalation{
Expand All @@ -63,23 +68,20 @@ func checklyCheck(apiCheck Check) (check checkly.Check, err error) {
}

check = checkly.Check{
Name: apiCheck.Name,
Type: checkly.TypeAPI,
Frequency: checkValueInt(apiCheck.Frequency, 5),
DegradedResponseTime: 5000,
MaxResponseTime: checkValueInt(apiCheck.MaxResponseTime, 15000),
Activated: true,
Muted: apiCheck.Muted, // muted for development
ShouldFail: shouldFail,
DoubleCheck: false,
SSLCheck: false,
LocalSetupScript: "",
LocalTearDownScript: "",
Locations: []string{},
Tags: []string{
apiCheck.Namespace,
"checkly-operator",
},
Name: apiCheck.Name,
Type: checkly.TypeAPI,
Frequency: checkValueInt(apiCheck.Frequency, 5),
DegradedResponseTime: 5000,
MaxResponseTime: checkValueInt(apiCheck.MaxResponseTime, 15000),
Activated: true,
Muted: apiCheck.Muted, // muted for development
ShouldFail: shouldFail,
DoubleCheck: false,
SSLCheck: false,
LocalSetupScript: "",
LocalTearDownScript: "",
Locations: []string{},
Tags: tags,
AlertSettings: alertSettings,
UseGlobalAlertSettings: false,
GroupID: apiCheck.GroupID,
Expand Down
26 changes: 14 additions & 12 deletions external/checkly/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ type Group struct {
Locations []string
Activated bool
AlertChannels []string
Labels map[string]string
}

func checklyGroup(group Group) (check checkly.Group) {

tags := getTags(group.Labels)
tags = append(tags, "checkly-operator")
tags = append(tags, group.Namespace)

alertSettings := checkly.AlertSettings{
EscalationType: checkly.RunBased,
RunBasedEscalation: checkly.RunBasedEscalation{
Expand All @@ -52,18 +57,15 @@ func checklyGroup(group Group) (check checkly.Group) {
}

check = checkly.Group{
Name: group.Name,
Activated: true,
Muted: false, // muted for development
DoubleCheck: false,
LocalSetupScript: "",
LocalTearDownScript: "",
Concurrency: 2,
Locations: checkValueArray(group.Locations, []string{"eu-west-1"}),
Tags: []string{
group.Namespace,
"checkly-operator",
},
Name: group.Name,
Activated: true,
Muted: false, // muted for development
DoubleCheck: false,
LocalSetupScript: "",
LocalTearDownScript: "",
Concurrency: 2,
Locations: checkValueArray(group.Locations, []string{"eu-west-1"}),
Tags: tags,
AlertSettings: alertSettings,
UseGlobalAlertSettings: false,
AlertChannelSubscriptions: []checkly.AlertChannelSubscription{},
Expand Down

0 comments on commit d8e343f

Please sign in to comment.