Skip to content

Commit

Permalink
Merge pull request #262 from stakater/migrate-to-operator
Browse files Browse the repository at this point in the history
Migrate to operator
  • Loading branch information
ahmedwaleedmalik authored Jul 28, 2020
2 parents 62e92bb + f97b21a commit 84c61fb
Show file tree
Hide file tree
Showing 130 changed files with 4,645 additions and 3,976 deletions.
2 changes: 1 addition & 1 deletion .VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version: v1.0.101
version: v2.0.0
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ vendor
\.vscode/
build/package/ingressmonitorcontroller
out
IngressMonitorController
/IngressMonitorController
/build/_output/bin/
/_output/
97 changes: 78 additions & 19 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type MonitorService interface {
GetByName(name string) (*Monitor, error)
Remove(m Monitor)
Setup(provider Provider)
Equal(oldMonitor Monitor, newMonitor Monitor) bool
}
```

Expand All @@ -51,6 +52,24 @@ func (mp *MonitorServiceProxy) OfType(mType string) MonitorServiceProxy {
}
```

Similarly, add a new case for your provider in ExtractConfig:

```go
func (mp *MonitorServiceProxy) ExtractConfig(spec endpointmonitorv1alpha1.EndpointMonitorSpec) interface{} {
var config interface{}

switch mp.monitorType {
case "UptimeRobot":
config = spec.UptimeRobotConfig
case "MyNewMonitor":
config = spec.MyNewMonitorConfig
default:
return config
}
return config
}
```

Note that the name you specify here for the case will be the key for your new monitor which you can add it in ConfigMap.

Also in case of handling custom api objects for the monitor api, you can create mappers that map from the api objects to the generic `Monitor` objects. The way you have to create these is to create a file named `monitorname-mappers.go` and add mapping functions in that file. An example of a mapping function is found below:
Expand All @@ -69,33 +88,73 @@ func UptimeMonitorMonitorToBaseMonitorMapper(uptimeMonitor UptimeMonitorMonitor)

## Naming Conventions

### Annotations
### Configuration

You should use the following format for annotations when there are monitor specific annotations:
You can add additional configuration in [endpointmonitor_types.go](./pkg/apis/endpointmonitor/v1alpha1/endpointmonitor_types.go)
And then handle it accordingly in your monitor's implementation.

```bash
<monitor-name>.monitor.stakater.com/<annotation-name>
```
#### Examples

You should use the following format for annotations when there are global annotations:
In [endpointmonitor_types.go](./pkg/apis/endpointmonitor/v1alpha1/endpointmonitor_types.go)

```bash
monitor.stakater.com/<annotation-name>
```yaml
// UptimeRobotConfig defines the configuration for UptimeRobot Monitor Provider
type UptimeRobotConfig struct {
// The uptimerobot alertContacts to be associated with this monitor
// +optional
AlertContacts string `json:"alertContacts,omitempty"`

// The uptimerobot check interval in seconds
// +kubebuilder:validation:Minimum=60
// +optional
Interval int `json:"interval,omitempty"`

// Specify maintenanceWindows i.e. once or recurring “do-not-monitor periods”
// +optional
MaintenanceWindows string `json:"maintenanceWindows,omitempty"`

// The uptimerobot monitor type (http or keyword)
// +kubebuilder:validation:Enum=http;keyword
// +optional
MonitorType string `json:"monitorType,omitempty"`

// Alert if value exist (yes) or doesn't exist (no) (Only if monitor-type is keyword)
// +kubebuilder:validation:Enum=yes;no
// +optional
KeywordExists string `json:"keywordExists,omitempty"`

// keyword to check on URL (e.g.'search' or '404') (Only if monitor-type is keyword)
// +optional
KeywordValue string `json:"keywordValue,omitempty"`

// The uptimerobot public status page ID to add this monitor to
// +optional
StatusPages string `json:"statusPages,omitempty"`
}
```

#### Examples
And then handle this configuration as handled in `processProviderConfig` in [uptime-monitor.go](./pkg/monitors/uptimerobot/uptime-monitor.go)

For example you're adding support for a new monitor service named `alertme`, it's specific annotations would look like the following:
# Development

```bash
alertme.monitor.stakater.com/some-key
```
## Dependencies:
1. GoLang v1.14.x
2. kubectl
3. operator-sdk v0.15.x

In case of a global annotation, lets say you want to create 1 for disabling deletion of specific monitors, it would look like so:
## Running Operator Locally

```bash
monitor.stakater.com/keep-on-delete
```
1. Create a namespace `test`
2. Create a secret with name `imc-config` and add your desired config in there
3. Run `make run-local`

**NOTE**: Ensure that all required resources are re-generated

# Creating a New Release

1. Update CSV version in `./hack/update-resources.sh`
2. Update image tag in `./deploy/operator.yaml`(look for `image:`)
3. Update dependencies and resources by running `./hack/update-resources.sh` script from root directory

# Testing

Expand All @@ -105,7 +164,7 @@ Tests require a Kubernetes instance to talk to with a `test` namespace created,

```bash
# while still in the root folder, configure test setup
$ export CONFIG_FILE_PATH=$(pwd)/configs/testConfigs/test-config.yaml
$ export CONFIG_FILE_PATH=$(pwd)/examples/configs/test-config.yaml
# update the apikey and alertContacts in this file and the config_test.go file (`correctTestAPIKey` and `correctTestAlertContacts` contstants)
$ minikube start
$ kubectl create namespace test
Expand Down Expand Up @@ -135,5 +194,5 @@ providers:
password: <your-account-password>
accountEmail: <multi-auth-account-email>
enableMonitorDeletion: true
monitorNameTemplate: "{{.IngressName}}-{{.Namespace}}"
monitorNameTemplate: "{{.Name}}-{{.Namespace}}"
```
6 changes: 3 additions & 3 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env groovy
@Library('github.com/stakater/stakater-pipeline-library@v2.16.24') _
@Library('github.com/stakater/stakater-pipeline-library@v2.16.34') _

goBuildViaGoReleaser {
publicChartRepositoryURL = 'https://stakater.github.io/stakater-charts'
publicChartGitURL = 'git@github.com:stakater/stakater-charts.git'
toolsImage = 'stakater/pipeline-tools:v2.0.18'
verify = true
renderChart = false
}
30 changes: 25 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# note: call scripts from /scripts

.PHONY: default build builder-image binary-image test stop clean-images clean push apply deploy helm-template helm-install
.PHONY: default verify build builder-image binary-image test stop clean-images clean push apply deploy helm-template helm-install

BUILDER ?= ingressmonitorcontroller-builder
BINARY ?= IngressMonitorController
DOCKER_IMAGE ?= stakater/ingressmonitorcontroller

# GOLANGCI_LINT env
GOLANGCI_LINT = _output/tools/golangci-lint
GOLANGCI_LINT_CACHE = $(PWD)/_output/golangci-lint-cache
GOLANGCI_LINT_VERSION = v1.24

# Default value "dev"
DOCKER_TAG ?= dev
REPOSITORY = ${DOCKER_IMAGE}:${DOCKER_TAG}
Expand All @@ -15,6 +20,8 @@ BUILD=

GOCMD = go
GOFLAGS ?= $(GOFLAGS:)
GOMAINPACKAGE=./cmd/manager

LDFLAGS =

HELMPATH= deployments/kubernetes/chart/ingressmonitorcontroller
Expand All @@ -27,16 +34,29 @@ install:
"$(GOCMD)" mod download

build:
"$(GOCMD)" build ${GOFLAGS} ${LDFLAGS} -o "${BINARY}"
"$(GOCMD)" build ${GOFLAGS} ${LDFLAGS} -o "${BINARY}" $(GOMAINPACKAGE)

verify-fmt:
./hack/verify-gofmt.sh

$(GOLANGCI_LINT):
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(dir $@) v1.24.0

verify-golangci-lint: $(GOLANGCI_LINT)
GOLANGCI_LINT_CACHE=$(GOLANGCI_LINT_CACHE) $(GOLANGCI_LINT) run --timeout=300s ./cmd/... ./pkg/... ./test/...

verify: verify-fmt verify-golangci-lint

builder-image:
@docker build --network host -t "${BUILDER}" -f build/package/Dockerfile.build .
@docker build --network host -t "${BUILDER}" -f build/Dockerfile .

binary-image: builder-image
@docker run --network host --rm "${BUILDER}" | docker build --network host -t "${REPOSITORY}" -f Dockerfile.run -

test:
"$(GOCMD)" test -v ./...
GOFLAGS="-count=1" "$(GOCMD)" test -v ./...

run-local:
./hack/run-local.sh

stop:
@docker stop "${BINARY}"
Expand Down
Loading

0 comments on commit 84c61fb

Please sign in to comment.