From d8e343f1bee35f7f2abedf9e86516a60aa811327 Mon Sep 17 00:00:00 2001 From: Akos Veres Date: Tue, 19 Jul 2022 17:11:03 +0300 Subject: [PATCH] feat(tags): Turn all labels into tags (#20) 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. --- config/samples/checkly_v1alpha1_apicheck.yaml | 3 +- config/samples/checkly_v1alpha1_group.yaml | 2 ++ controllers/checkly/apicheck_controller.go | 1 + controllers/checkly/group_controller.go | 1 + external/checkly/api_common.go | 11 ++++++ external/checkly/api_common_test.go | 17 +++++++++ external/checkly/check.go | 36 ++++++++++--------- external/checkly/group.go | 26 +++++++------- 8 files changed, 67 insertions(+), 30 deletions(-) diff --git a/config/samples/checkly_v1alpha1_apicheck.yaml b/config/samples/checkly_v1alpha1_apicheck.yaml index d1996f5..146ae4d 100644 --- a/config/samples/checkly_v1alpha1_apicheck.yaml +++ b/config/samples/checkly_v1alpha1_apicheck.yaml @@ -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" diff --git a/config/samples/checkly_v1alpha1_group.yaml b/config/samples/checkly_v1alpha1_group.yaml index 136701e..878137d 100644 --- a/config/samples/checkly_v1alpha1_group.yaml +++ b/config/samples/checkly_v1alpha1_group.yaml @@ -2,6 +2,8 @@ apiVersion: k8s.checklyhq.com/v1alpha1 kind: Group metadata: name: group-sample + labels: + environment: "local" spec: locations: - eu-west-1 diff --git a/controllers/checkly/apicheck_controller.go b/controllers/checkly/apicheck_controller.go index 959a523..2b66a40 100644 --- a/controllers/checkly/apicheck_controller.go +++ b/controllers/checkly/apicheck_controller.go @@ -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, } // ///////////////////////////// diff --git a/controllers/checkly/group_controller.go b/controllers/checkly/group_controller.go index b284229..6498087 100644 --- a/controllers/checkly/group_controller.go +++ b/controllers/checkly/group_controller.go @@ -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, } // ///////////////////////////// diff --git a/external/checkly/api_common.go b/external/checkly/api_common.go index f2f2d0f..e6cc0ea 100644 --- a/external/checkly/api_common.go +++ b/external/checkly/api_common.go @@ -16,6 +16,8 @@ limitations under the License. package external +import "fmt" + func checkValueString(x string, y string) (value string) { if x == "" { value = y @@ -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 +} diff --git a/external/checkly/api_common_test.go b/external/checkly/api_common_test.go index 279028b..6de4749 100644 --- a/external/checkly/api_common_test.go +++ b/external/checkly/api_common_test.go @@ -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) + } + } + +} diff --git a/external/checkly/check.go b/external/checkly/check.go index 1315c4a..5bb775b 100644 --- a/external/checkly/check.go +++ b/external/checkly/check.go @@ -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) { @@ -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{ @@ -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, diff --git a/external/checkly/group.go b/external/checkly/group.go index a936790..ada9334 100644 --- a/external/checkly/group.go +++ b/external/checkly/group.go @@ -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{ @@ -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{},