Skip to content

Commit

Permalink
chore: add configure monitor group
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Nov 29, 2022
1 parent d07d5ab commit c389f02
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
8 changes: 8 additions & 0 deletions api/v1alpha1/uptrends.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ type UptrendsSpec struct {
Interval int `json:"interval"`
// Url of the Monitor.
Url string `json:"url"`
// MonitorGroup associates a monitor group.
Group MonitorGroup `json:"group"`
}

// MonitorGroup defines a monitor group.
type MonitorGroup struct {
// GUID is the id of the monitor group.
GUID string `json:"guid"`
}

//+kubebuilder:object:root=true
Expand Down
16 changes: 16 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions charts/uptrends/templates/crds/crd-uptrends.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ spec:
description:
description: Description of the Monitor.
type: string
group:
description: MonitorGroup associates a monitor group.
properties:
guid:
description: GUID is the id of the monitor group.
type: string
required:
- guid
type: object
interval:
description: Interval of the Monitor.
type: integer
Expand All @@ -53,6 +62,7 @@ spec:
type: string
required:
- description
- group
- interval
- name
- type
Expand Down
5 changes: 3 additions & 2 deletions examples/ingress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ metadata:
name: my-ingress
annotations:
uptrends.ionos-cloud.github.io/monitor.type: HTTPS
uptrends.ionos-cloud.github.io/monitor.interval: "5"
uptrends.ionos-cloud.github.io/monitor.interval: "1"
uptrends.ionos-cloud.github.io/monitor.guid: ""
spec:
rules:
- host: ionos.cloud
- host: ionos.com
http:
paths:
- pathType: Prefix
Expand Down
10 changes: 10 additions & 0 deletions manifests/crd/bases/operators.ionos-cloud.github.io_uptrends.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ spec:
description:
description: Description of the Monitor.
type: string
group:
description: MonitorGroup associates a monitor group.
properties:
guid:
description: GUID is the id of the monitor group.
type: string
required:
- guid
type: object
interval:
description: Interval of the Monitor.
type: integer
Expand All @@ -53,6 +62,7 @@ spec:
type: string
required:
- description
- group
- interval
- name
- type
Expand Down
10 changes: 10 additions & 0 deletions manifests/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ spec:
description:
description: Description of the Monitor.
type: string
group:
description: MonitorGroup associates a monitor group.
properties:
guid:
description: GUID is the id of the monitor group.
type: string
required:
- guid
type: object
interval:
description: Interval of the Monitor.
type: integer
Expand All @@ -54,6 +63,7 @@ spec:
type: string
required:
- description
- group
- interval
- name
- type
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (c *ingressReconciler) reconcileResources(ctx context.Context, in *networki
Name: fmt.Sprintf("%s - Uptime", r.Host),
Interval: 5,
Type: "HTTPS",
Group: v1alpha1.MonitorGroup{},
},
}

Expand All @@ -131,6 +132,10 @@ func (c *ingressReconciler) reconcileResources(ctx context.Context, in *networki
}
}

if v, ok := annotations["guid"]; ok {
monitor.Spec.Group.GUID = v
}

if monitor.Spec.Type == "HTTPS" {
monitor.Spec.Url = "https://" + r.Host
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/controller/monitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,35 @@ func (m *monitorReconcile) reconcileUpdate(ctx context.Context, mon *v1alpha1.Up
return err
}

groups, _, err := client.MonitorApi.MonitorGetMonitorGroups(
auth,
mon.Status.MonitorGuid,
)
if err != nil {
return err
}

gg := make(map[string]bool)
for _, g := range groups {
gg[g] = true
}

if mon.Spec.Group.GUID == "" { // remove from all groups
for k := range gg {
res, err := client.MonitorGroupApi.MonitorGroupRemoveMonitorFromMonitorGroup(auth, k, mon.Status.MonitorGuid)
if err != nil && res.StatusCode != http.StatusBadRequest { // remove
return err
}
}
}

if _, ok := gg[mon.Spec.Group.GUID]; !ok && mon.Spec.Group.GUID != "" { // this
_, err := client.MonitorGroupApi.MonitorGroupAddMonitorToMonitorGroup(auth, mon.Spec.Group.GUID, mon.Status.MonitorGuid)
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -188,6 +217,13 @@ func (m *monitorReconcile) reconcileCreate(ctx context.Context, mon *v1alpha1.Up
return err
}

if mon.Spec.Group.GUID != "" {
_, err := client.MonitorGroupApi.MonitorGroupAddMonitorToMonitorGroup(auth, mon.Spec.Group.GUID, up.MonitorGuid)
if err != nil {
return err
}
}

mon.SetFinalizers(finalizers.AddFinalizer(mon, v1alpha1.FinalizerName))
err = m.Update(ctx, mon)
if err != nil && !errors.IsNotFound(err) {
Expand Down

0 comments on commit c389f02

Please sign in to comment.