From 460784275d4d64a63abcbfe838fa4a8cea0dcb3f Mon Sep 17 00:00:00 2001 From: Akos Veres Date: Thu, 7 Jul 2022 14:29:45 +0100 Subject: [PATCH] fix(check): Failing checks and default location (#18) * fix(check): Set MaxResponseTime correctly Previously we've been setting MaxResponseTime as the Frequency, MaxResponseTime is supposed to be in miliseconds, thus setting Frequency to 5, would make MaxResponseTime 5 miliseconds, the checklyhq backend didn't know what to do with this number so it was empty, created checks failed immediately. This fixes #17. * fix(check): Do not set location for checks Checks are tied to group, each group has location settings, we shouldn't set the default location to eu-west-1 anymore * test(debug): Add debug script for checkly checks --- .dockerignore | 1 + apis/checkly/v1alpha1/apicheck_types.go | 3 - .../checkly/v1alpha1/zz_generated.deepcopy.go | 7 +- .../bases/k8s.checklyhq.com_apichecks.yaml | 7 -- config/samples/checkly_v1alpha1_apicheck.yaml | 3 - controllers/checkly/apicheck_controller.go | 3 +- external/checkly/check.go | 3 +- external/checkly/check_test.go | 2 - hack/debug-checks.go | 64 +++++++++++++++++++ 9 files changed, 68 insertions(+), 25 deletions(-) create mode 100644 hack/debug-checks.go diff --git a/.dockerignore b/.dockerignore index b7138e5..568f8b1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,6 +2,7 @@ # Ignore build and test binaries. bin/ testbin/ +hack/ # direnv .envrc diff --git a/apis/checkly/v1alpha1/apicheck_types.go b/apis/checkly/v1alpha1/apicheck_types.go index 64f827d..e9b4a56 100644 --- a/apis/checkly/v1alpha1/apicheck_types.go +++ b/apis/checkly/v1alpha1/apicheck_types.go @@ -34,9 +34,6 @@ type ApiCheckSpec struct { // Muted determines if the created alert is muted or not, default false Muted bool `json:"muted,omitempty"` - // Locations determines the locations where the checks are run from, see https://www.checklyhq.com/docs/monitoring/global-locations/ for a list, use AWS Region codes, ex. eu-west-1 for Ireland - Locations []string `json:"locations,omitempty"` - // Endpoint determines which URL to monitor, ex. https://foo.bar/baz Endpoint string `json:"endpoint"` diff --git a/apis/checkly/v1alpha1/zz_generated.deepcopy.go b/apis/checkly/v1alpha1/zz_generated.deepcopy.go index d95fb96..ff60d77 100644 --- a/apis/checkly/v1alpha1/zz_generated.deepcopy.go +++ b/apis/checkly/v1alpha1/zz_generated.deepcopy.go @@ -30,7 +30,7 @@ func (in *ApiCheck) DeepCopyInto(out *ApiCheck) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) + out.Spec = in.Spec out.Status = in.Status } @@ -87,11 +87,6 @@ func (in *ApiCheckList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ApiCheckSpec) DeepCopyInto(out *ApiCheckSpec) { *out = *in - if in.Locations != nil { - in, out := &in.Locations, &out.Locations - *out = make([]string, len(*in)) - copy(*out, *in) - } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApiCheckSpec. diff --git a/config/crd/bases/k8s.checklyhq.com_apichecks.yaml b/config/crd/bases/k8s.checklyhq.com_apichecks.yaml index ae5b332..e848783 100644 --- a/config/crd/bases/k8s.checklyhq.com_apichecks.yaml +++ b/config/crd/bases/k8s.checklyhq.com_apichecks.yaml @@ -64,13 +64,6 @@ spec: description: Group determines in which group does the check belong to type: string - locations: - description: Locations determines the locations where the checks are - run from, see https://www.checklyhq.com/docs/monitoring/global-locations/ - for a list, use AWS Region codes, ex. eu-west-1 for Ireland - items: - type: string - type: array maxresponsetime: description: MaxResponseTime determines what the maximum number of miliseconds can pass before the check fails, default 15000 diff --git a/config/samples/checkly_v1alpha1_apicheck.yaml b/config/samples/checkly_v1alpha1_apicheck.yaml index 1137a75..d1996f5 100644 --- a/config/samples/checkly_v1alpha1_apicheck.yaml +++ b/config/samples/checkly_v1alpha1_apicheck.yaml @@ -9,6 +9,3 @@ spec: frequency: 10 # Default 5 muted: true # Default "false" group: "group-sample" - # locations: # Default eu-west-1 - # - eu-west-1 - # - ap-northeast-2 diff --git a/controllers/checkly/apicheck_controller.go b/controllers/checkly/apicheck_controller.go index d5edca3..a3647d4 100644 --- a/controllers/checkly/apicheck_controller.go +++ b/controllers/checkly/apicheck_controller.go @@ -139,8 +139,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c Name: apiCheck.Name, Namespace: apiCheck.Namespace, Frequency: apiCheck.Spec.Frequency, - MaxResponseTime: apiCheck.Spec.Frequency, - Locations: apiCheck.Spec.Locations, + MaxResponseTime: apiCheck.Spec.MaxResponseTime, Endpoint: apiCheck.Spec.Endpoint, SuccessCode: apiCheck.Spec.Success, ID: apiCheck.Status.ID, diff --git a/external/checkly/check.go b/external/checkly/check.go index a493ea9..b0d291b 100644 --- a/external/checkly/check.go +++ b/external/checkly/check.go @@ -30,7 +30,6 @@ type Check struct { Namespace string Frequency int MaxResponseTime int - Locations []string Endpoint string SuccessCode string GroupID int64 @@ -70,7 +69,7 @@ func checklyCheck(apiCheck Check) (check checkly.Check) { SSLCheck: false, LocalSetupScript: "", LocalTearDownScript: "", - Locations: checkValueArray(apiCheck.Locations, []string{"eu-west-1"}), + Locations: []string{}, Tags: []string{ apiCheck.Namespace, "checkly-operator", diff --git a/external/checkly/check_test.go b/external/checkly/check_test.go index a9a5a8f..fdf9c51 100644 --- a/external/checkly/check_test.go +++ b/external/checkly/check_test.go @@ -31,7 +31,6 @@ func TestChecklyCheck(t *testing.T) { Namespace: "bar", Frequency: 15, MaxResponseTime: 2000, - Locations: []string{"basement"}, Endpoint: "https://foo.bar/baz", SuccessCode: "200", Muted: true, @@ -84,7 +83,6 @@ func TestChecklyCheckActions(t *testing.T) { Namespace: "bar", Frequency: 15, MaxResponseTime: 2000, - Locations: []string{"basement"}, Endpoint: "https://foo.bar/baz", SuccessCode: "200", ID: "", diff --git a/hack/debug-checks.go b/hack/debug-checks.go new file mode 100644 index 0000000..75b9e40 --- /dev/null +++ b/hack/debug-checks.go @@ -0,0 +1,64 @@ +package main + +import ( + "context" + "errors" + "flag" + "fmt" + "os" + "time" + + "github.com/checkly/checkly-go-sdk" + ctrl "sigs.k8s.io/controller-runtime" +) + +// The script returns the data for a specific checkly check from the checklyhq API, +// it's meant to be used as a debug tool for issues with checks created. + +func main() { + + setupLog := ctrl.Log.WithName("setup") + + var checklyID string + + flag.StringVar(&checklyID, "c", "", "Specify the checkly check ID") + flag.Parse() + + if checklyID == "" { + setupLog.Error(errors.New("ChecklyID is empty"), "exiting due to missing information") + os.Exit(1) + } + + baseUrl := "https://api.checklyhq.com" + apiKey := os.Getenv("CHECKLY_API_KEY") + if apiKey == "" { + setupLog.Error(errors.New("checklyhq.com API key environment variable is undefined"), "checklyhq.com credentials missing") + os.Exit(1) + } + + accountId := os.Getenv("CHECKLY_ACCOUNT_ID") + if accountId == "" { + setupLog.Error(errors.New("checklyhq.com Account ID environment variable is undefined"), "checklyhq.com credentials missing") + os.Exit(1) + } + + client := checkly.NewClient( + baseUrl, + apiKey, + nil, //custom http client, defaults to http.DefaultClient + nil, //io.Writer to output debug messages + ) + + client.SetAccountId(accountId) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + + returnedCheck, err := client.Get(ctx, checklyID) + if err != nil { + setupLog.Error(err, "failed to get check") + os.Exit(1) + } + + fmt.Printf("%+v", returnedCheck) + +}