Skip to content

Commit

Permalink
Merge pull request #3 from PDOK/pingdom
Browse files Browse the repository at this point in the history
Add Pingdom provider
  • Loading branch information
rkettelerij authored Jun 21, 2024
2 parents f72a59d + 40a75d2 commit 2319bfd
Show file tree
Hide file tree
Showing 14 changed files with 762 additions and 275 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
go-version: '1.22'

- name: Make test
env:
PINGDOM_API_TOKEN: ${{ secrets.PINGDOM_API_TOKEN }}
run: |
make test
echo "removing generated code from coverage results"
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
[![Docker Pulls](https://img.shields.io/docker/pulls/pdok/uptime-operator.svg)](https://hub.docker.com/r/pdok/uptime-operator)

Kubernetes Operator to watch [Traefik](https://github.com/traefik/traefik) IngressRoute(s) and register these with a (SaaS) uptime monitoring provider.
Currently supported providers are:
- [Pingdom](https://www.pingdom.com/)
- Mock (for testing purposes)

Submit a PR when you wish to add another provider!

## Annotations

Expand All @@ -24,7 +29,7 @@ metadata:
uptime.pdok.nl/url: "https://site.example/service/wms/v1_0"
uptime.pdok.nl/tags: "metadata,separated,by,commas"
uptime.pdok.nl/request-headers: "Accept: application/json, Accept-Language: en"
uptime.pdok.nl/response-check-for-string-contains: "200 OK"
uptime.pdok.nl/response-check-for-string-contains: "It works!"
uptime.pdok.nl/response-check-for-string-not-contains: "NullPointerException"
```
Expand All @@ -49,6 +54,8 @@ USAGE:
<uptime-controller-manager> [OPTIONS]
OPTIONS:
-enable-deletes
Allow the operator to delete checks from the uptime provider when ingress routes are removed.
-enable-http2
If set, HTTP/2 will be enabled for the metrics and webhook servers.
-health-probe-bind-address string
Expand All @@ -63,6 +70,12 @@ OPTIONS:
If set the metrics endpoint is served securely.
-namespace value
Namespace(s) to watch for changes. Specify this flag multiple times for each namespace to watch. When not provided all namespaces will be watched.
-pingdom-alert-integration-ids value
One or more IDs of Pingdom integrations (like slack channels) to alert. Only applies when 'uptime-provider' is 'pingdom'
-pingdom-alert-user-ids value
One or more IDs of Pingdom users to alert. Only applies when 'uptime-provider' is 'pingdom'
-pingdom-api-token string
The API token to authenticate with Pingdom. Only applies when 'uptime-provider' is 'pingdom'
-slack-channel string
The Slack Channel ID for posting updates when uptime checks are mutated.
-slack-webhook-url string
Expand Down Expand Up @@ -96,6 +109,11 @@ Install [golangci-lint](https://golangci-lint.run/usage/install/) and run `golan
from the root.
(Don't run `make lint`, it uses an old version of golangci-lint.)

### Testing against real APIs

To test against (for example) the actual/real Pingdom API run `internal/service/providers/pingdom_test.go`
this test requires the `PINGDOM_API_TOKEN` env var to be set.

## Misc

### How to Contribute
Expand Down
121 changes: 82 additions & 39 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
"os"

"github.com/PDOK/uptime-operator/internal/service"
"github.com/PDOK/uptime-operator/internal/service/providers"
"github.com/PDOK/uptime-operator/internal/util"
"github.com/peterbourgon/ff"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/manager"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand Down Expand Up @@ -68,34 +70,107 @@ func main() {
var namespaces util.SliceFlag
var slackChannel string
var slackWebhookURL string
var enableDeletes bool
var uptimeProvider string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
var pingdomAPIToken string
var pingdomAlertUserIDs util.SliceFlag
var pingdomAlertIntegrationIDs util.SliceFlag
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080",
"The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081",
"The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&secureMetrics, "metrics-secure", false,
"If set the metrics endpoint is served securely.")
flag.BoolVar(&enableHTTP2, "enable-http2", false,
"If set, HTTP/2 will be enabled for the metrics and webhook servers.")
flag.BoolVar(&enableDeletes, "enable-deletes", false,
"Allow the operator to delete checks from the uptime provider when ingress routes are removed.")
flag.Var(&namespaces, "namespace", "Namespace(s) to watch for changes. "+
"Specify this flag multiple times for each namespace to watch. When not provided all namespaces will be watched.")
flag.StringVar(&slackChannel, "slack-channel", "", "The Slack Channel ID for posting updates when uptime checks are mutated.")
flag.StringVar(&slackWebhookURL, "slack-webhook-url", "", "The webhook URL required to post messages to the given Slack channel.")
flag.StringVar(&uptimeProvider, "uptime-provider", "mock", "Name of the (SaaS) uptime monitoring provider to use.")
flag.StringVar(&slackChannel, "slack-channel", "",
"The Slack Channel ID for posting updates when uptime checks are mutated.")
flag.StringVar(&slackWebhookURL, "slack-webhook-url", "",
"The webhook URL required to post messages to the given Slack channel.")
flag.StringVar(&uptimeProvider, "uptime-provider", "mock",
"Name of the (SaaS) uptime monitoring provider to use.")
flag.StringVar(&pingdomAPIToken, "pingdom-api-token", "",
"The API token to authenticate with Pingdom. Only applies when 'uptime-provider' is 'pingdom'")
flag.Var(&pingdomAlertUserIDs, "pingdom-alert-user-ids",
"One or more IDs of Pingdom users to alert. Only applies when 'uptime-provider' is 'pingdom'")
flag.Var(&pingdomAlertIntegrationIDs, "pingdom-alert-integration-ids",
"One or more IDs of Pingdom integrations (like slack channels) to alert. Only applies when 'uptime-provider' is 'pingdom'")

opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

if err := ff.Parse(flag.CommandLine, os.Args[1:], ff.WithEnvVarNoPrefix()); err != nil {
setupLog.Error(err, "unable to parse flags")
os.Exit(1)
}

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
mgr, err := createManager(enableHTTP2, metricsAddr, secureMetrics, probeAddr, enableLeaderElection, namespaces)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}

var uptimeProviderSettings any
if uptimeProvider == "pingdom" {
alertUserIDs, err := util.StringsToInts(pingdomAlertUserIDs)
if err != nil {
setupLog.Error(err, "Unable to parse 'pingdom-alert-user-ids' flag")
os.Exit(1)
}
alertIntegrationIDs, err := util.StringsToInts(pingdomAlertIntegrationIDs)
if err != nil {
setupLog.Error(err, "Unable to parse 'pingdom-alert-integration-ids' flag")
os.Exit(1)
}
uptimeProviderSettings = providers.PingdomSettings{
APIToken: pingdomAPIToken,
UserIDs: alertUserIDs,
IntegrationIDs: alertIntegrationIDs,
}
}

if err = (&controller.IngressRouteReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
UptimeCheckService: service.New(
service.WithProviderAndSettings(uptimeProvider, uptimeProviderSettings),
service.WithSlack(slackWebhookURL, slackChannel),
service.WithDeletes(enableDeletes),
),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "IngressRoute")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}

func createManager(enableHTTP2 bool, metricsAddr string, secureMetrics bool, probeAddr string,
enableLeaderElection bool, namespaces util.SliceFlag) (manager.Manager, error) {
// if the enable-http2 flag is false (the default), http/2 should be disabled
// due to its vulnerabilities. More specifically, disabling http/2 will
// prevent from being vulnerable to the HTTP/2 Stream Cancelation and
Expand Down Expand Up @@ -148,37 +223,5 @@ func main() {
managerOpts.Cache.DefaultNamespaces = namespacesToWatch
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), managerOpts)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}

if err = (&controller.IngressRouteReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
UptimeCheckService: service.New(
service.WithProviderName(uptimeProvider),
service.WithSlack(slackWebhookURL, slackChannel),
),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "IngressRoute")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
return ctrl.NewManager(ctrl.GetConfigOrDie(), managerOpts)
}
94 changes: 47 additions & 47 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
module github.com/PDOK/uptime-operator

go 1.22
go 1.22.4

require (
github.com/onsi/ginkgo/v2 v2.17.1
github.com/onsi/gomega v1.32.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/peterbourgon/ff v1.7.1
github.com/slack-go/slack v0.12.5
github.com/traefik/traefik/v2 v2.11.0
golang.org/x/tools v0.17.0
k8s.io/apimachinery v0.29.3
k8s.io/client-go v0.29.3
sigs.k8s.io/controller-runtime v0.17.3
github.com/slack-go/slack v0.13.0
github.com/stretchr/testify v1.9.0
github.com/traefik/traefik/v2 v2.11.5
golang.org/x/tools v0.22.0
k8s.io/apimachinery v0.30.2
k8s.io/client-go v0.30.2
sigs.k8s.io/controller-runtime v0.18.4
)

replace github.com/abbot/go-http-auth => github.com/abbot/go-http-auth v0.4.0 // for github.com/traefik/traefik/v2

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-acme/lego/v4 v4.15.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-acme/lego/v4 v4.17.4 // indirect
github.com/go-jose/go-jose/v4 v4.0.2 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20230817174616-7a8ec2ada47b // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/google/pprof v0.0.0-20240618054019-d3b898a103f8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/miekg/dns v1.1.58 // indirect
github.com/miekg/dns v1.1.61 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.54.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/traefik/paerser v0.2.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.3 // indirect
k8s.io/apiextensions-apiserver v0.29.2 // indirect
k8s.io/component-base v0.29.2 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
k8s.io/api v0.30.2 // indirect
k8s.io/apiextensions-apiserver v0.30.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240521193020-835d969ad83a // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
Loading

0 comments on commit 2319bfd

Please sign in to comment.