Skip to content

Commit

Permalink
refactor(api): Move Uptime Robot API types into api package
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 17, 2024
1 parent ac7e5e9 commit 0669738
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 253 deletions.
8 changes: 6 additions & 2 deletions api/v1/contact_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1

import (
"github.com/clevyr/uptime-robot-operator/internal/uptimerobot"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -31,7 +30,7 @@ type ContactSpec struct {
Account corev1.LocalObjectReference `json:"account,omitempty"`

// Contact configures the Uptime Robot monitor.
Contact uptimerobot.Contact `json:"contact"`
Contact ContactValues `json:"contact"`
}

// ContactStatus defines the observed state of Contact
Expand Down Expand Up @@ -66,6 +65,11 @@ type ContactList struct {
Items []Contact `json:"items"`
}

type ContactValues struct {
// FriendlyName sets the name that is shown in Uptime Robot.
FriendlyName string `json:"friendlyName"`
}

func init() {
SchemeBuilder.Register(&Contact{}, &ContactList{})
}
145 changes: 136 additions & 9 deletions api/v1/monitor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ limitations under the License.
package v1

import (
"github.com/clevyr/uptime-robot-operator/internal/uptimerobot"
"fmt"
"strings"
"time"

"github.com/clevyr/uptime-robot-operator/internal/uptimerobot/urtypes"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// MonitorContactRef attaches alert contacts. If blank, the default will be used.
type MonitorContactRef struct {
corev1.LocalObjectReference `json:",inline"`

uptimerobot.MonitorContactCommon `json:",inline"`
}

// MonitorSpec defines the desired state of Monitor
type MonitorSpec struct {
// Interval defines the reconcile interval.
Expand All @@ -44,7 +40,7 @@ type MonitorSpec struct {
Account corev1.LocalObjectReference `json:"account,omitempty"`

// Monitor configures the Uptime Robot monitor.
Monitor uptimerobot.Monitor `json:"monitor"`
Monitor MonitorValues `json:"monitor"`

// +kubebuilder:default:={{}}
Contacts []MonitorContactRef `json:"contacts,omitempty"`
Expand Down Expand Up @@ -78,6 +74,137 @@ type Monitor struct {
Status MonitorStatus `json:"status,omitempty"`
}

//+kubebuilder:object:generate=true
//+kubebuilder:validation:XValidation:rule="self.type != 'Keyword' || has(self.keyword)", message="Keyword config is required if type is Keyword"
//+kubebuilder:validation:XValidation:rule="self.type != 'Port' || has(self.port)", message="Port config is required if type is Port"

type MonitorValues struct {
// FriendlyName sets the name that is shown in Uptime Robot.
FriendlyName string `json:"friendlyName"`

// URL is the URL or IP to monitor, including the scheme.
URL string `json:"url"`

// Type chooses the monitor type.
//+kubebuilder:default:=HTTPS
Type urtypes.MonitorType `json:"type,omitempty"`

// Interval is the monitoring interval.
//+kubebuilder:default:="60s"
Interval *metav1.Duration `json:"interval,omitempty"`

// Status toggles pause status for the monitor. 0 is paused, 1 is running.
//+kubebuilder:default:=1
Status uint8 `json:"status,omitempty"`

// Timeout is the monitor timeout.
//+kubebuilder:default:="30s"
Timeout *metav1.Duration `json:"timeout,omitempty"`

// HTTPMethod defines the HTTP verb to use.
//+kubebuilder:default:="HEAD"
HTTPMethod urtypes.HTTPMethod `json:"httpMethod,omitempty"`

// POST configures POST, PUT, PATCH, DELETE, and OPTIONS requests.
POST *MonitorPOST `json:"post,omitempty"`

// Keyword provides configuration for the Keyword monitor type.
Keyword *MonitorKeyword `json:"keyword,omitempty"`

// Port provides configuration for the Port monitor type.
Port *MonitorPort `json:"port,omitempty"`

// Auth enables monitor auth.
Auth *MonitorAuth `json:"auth,omitempty"`
}

//+kubebuilder:object:generate=true

type MonitorKeyword struct {
Type urtypes.KeywordType `json:"type"`

//+kubebuilder:default:=false
CaseSensitive *bool `json:"caseSensitive,omitempty"`

Value string `json:"value"`
}

//+kubebuilder:validation:XValidation:rule="self.type != 'Custom' || has(self.number)", message="Number is required if type is Custom"
//+kubebuilder:validation:XValidation:rule="self.type == 'Custom' || !has(self.number)", message="Type must be Custom if Number is set"

type MonitorPort struct {
Type urtypes.PortType `json:"type"`

Number uint16 `json:"number,omitempty"`
}

type MonitorAuth struct {
//+kubebuilder:default:="Basic"
Type urtypes.MonitorAuthType `json:"type"`

Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`

SecretName string `json:"secretName,omitempty"`
UsernameKey string `json:"usernameKey,omitempty"`
PasswordKey string `json:"passwordKey,omitempty"`
}

type MonitorPOST struct {
// Type defines the format of data to be sent with POST, PUT, PATCH, DELETE, and OPTIONS requests.
//+kubebuilder:default:="KeyValue"
Type urtypes.POSTType `json:"postType,omitempty"`

// ContentType sets the Content-Type header for POST, PUT, PATCH, DELETE, and OPTIONS requests.
//+kubebuilder:default:="text/html"
ContentType urtypes.POSTContentType `json:"contentType,omitempty"`

// Value is the JSON form of data to be sent with POST, PUT, PATCH, DELETE, and OPTIONS requests.
Value string `json:"value,omitempty"`
}

// MonitorContactRef attaches alert contacts. If blank, the default will be used.
type MonitorContactRef struct {
corev1.LocalObjectReference `json:",inline"`

MonitorContactCommon `json:",inline"`
}

type MonitorContactCommon struct {
// Threshold defines the number of minutes to wait to notify.
// +kubebuilder:default:="1m"
Threshold metav1.Duration `json:"threshold,omitempty"`

// Recurrence defines the number of minutes between a repeat notification.
// A value of 0, disables repeat notifications.
Recurrence metav1.Duration `json:"recurrence,omitempty"`
}

type MonitorContact struct {
ID string `json:"id"`

MonitorContactCommon `json:",inline"`
}

func (m MonitorContact) String() string {
return fmt.Sprintf(
"%s_%d_%d",
m.ID,
int(m.MonitorContactCommon.Threshold.Round(time.Minute).Minutes()),
int(m.MonitorContactCommon.Recurrence.Round(time.Minute).Minutes()),
)
}

type MonitorContacts []MonitorContact

func (m MonitorContacts) String() string {
results := make([]string, 0, len(m))
for _, c := range m {
results = append(results, c.String())
}
return strings.Join(results, "-")
}

//+kubebuilder:object:root=true

// MonitorList contains a list of Monitor
Expand Down
Loading

0 comments on commit 0669738

Please sign in to comment.