Skip to content

Commit

Permalink
Use kubernetes/code-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha committed Sep 9, 2017
1 parent 6ca0b5a commit fd41124
Show file tree
Hide file tree
Showing 732 changed files with 341,436 additions and 1,562 deletions.
25 changes: 0 additions & 25 deletions api/install/install.go

This file was deleted.

31 changes: 0 additions & 31 deletions api/register_v1alpha1.go

This file was deleted.

14 changes: 14 additions & 0 deletions apis/monitoring/alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package monitoring

import "time"

type Alert interface {
GetName() string
GetNamespace() string
Command() string
GetCheckInterval() time.Duration
GetAlertInterval() time.Duration
IsValid() (bool, error)
GetNotifierSecretName() string
GetReceivers() []Receiver
}
67 changes: 67 additions & 0 deletions apis/monitoring/cluster_alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package monitoring

import (
"fmt"
"time"
)

const (
ResourceKindClusterAlert = "ClusterAlert"
ResourceNameClusterAlert = "cluster-alert"
ResourceTypeClusterAlert = "clusteralerts"
)

var _ Alert = &ClusterAlert{}

func (a ClusterAlert) GetName() string {
return a.Name
}

func (a ClusterAlert) GetNamespace() string {
return a.Namespace
}

func (a ClusterAlert) Command() string {
return string(a.Spec.Check)
}

func (a ClusterAlert) GetCheckInterval() time.Duration {
return a.Spec.CheckInterval.Duration
}

func (a ClusterAlert) GetAlertInterval() time.Duration {
return a.Spec.AlertInterval.Duration
}

func (a ClusterAlert) IsValid() (bool, error) {
cmd, ok := ClusterCommands[a.Spec.Check]
if !ok {
return false, fmt.Errorf("'%s' is not a valid cluster check command.", a.Spec.Check)
}
for k := range a.Spec.Vars {
if _, ok := cmd.Vars[k]; !ok {
return false, fmt.Errorf("Var '%s' is unsupported for check command %s.", k, a.Spec.Check)
}
}
for _, rcv := range a.Spec.Receivers {
found := false
for _, state := range cmd.States {
if state == rcv.State {
found = true
break
}
}
if !found {
return false, fmt.Errorf("State '%s' is unsupported for check command %s.", rcv.State, a.Spec.Check)
}
}
return true, nil
}

func (a ClusterAlert) GetNotifierSecretName() string {
return a.Spec.NotifierSecretName
}

func (a ClusterAlert) GetReceivers() []Receiver {
return a.Spec.Receivers
}
5 changes: 5 additions & 0 deletions apis/monitoring/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// package monitoring is the internal version of the API.

// +k8s:deepcopy-gen=package,register
// +groupName=monitoring.appscode.com
package monitoring
146 changes: 146 additions & 0 deletions apis/monitoring/icinga.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package monitoring

import (
"fmt"

"github.com/appscode/log"
"github.com/appscode/searchlight/data"
)

type CheckPod string

const (
CheckPodInfluxQuery CheckPod = "influx_query"
CheckPodStatus CheckPod = "pod_status"
CheckPodVolume CheckPod = "pod_volume"
CheckPodExec CheckPod = "pod_exec"
)

func (c CheckPod) IsValid() bool {
_, ok := PodCommands[c]
return ok
}

func ParseCheckPod(s string) (*CheckPod, error) {
cmd := CheckPod(s)
if _, ok := PodCommands[cmd]; !ok {
return nil, fmt.Errorf("Invalid pod check command %s", s)
}
return &cmd, nil
}

type CheckNode string

const (
CheckNodeInfluxQuery CheckNode = "influx_query"
CheckNodeVolume CheckNode = "node_volume"
CheckNodeStatus CheckNode = "node_status"
)

func (c CheckNode) IsValid() bool {
_, ok := NodeCommands[c]
return ok
}

func ParseCheckNode(s string) (*CheckNode, error) {
cmd := CheckNode(s)
if _, ok := NodeCommands[cmd]; !ok {
return nil, fmt.Errorf("Invalid node check command %s", s)
}
return &cmd, nil
}

type CheckCluster string

const (
CheckComponentStatus CheckCluster = "component_status"
CheckJsonPath CheckCluster = "json_path"
CheckNodeExists CheckCluster = "node_exists"
CheckPodExists CheckCluster = "pod_exists"
CheckEvent CheckCluster = "event"
CheckCACert CheckCluster = "ca_cert"
CheckHttp CheckCluster = "any_http"
CheckEnv CheckCluster = "env"
CheckDummy CheckCluster = "dummy"
//CheckICMP CheckCluster = "icmp"
//CheckDIG CheckCluster = "dig"
//CheckDNS CheckCluster = "dns"
)

func (c CheckCluster) IsValid() bool {
_, ok := ClusterCommands[c]
return ok
}

func ParseCheckCluster(s string) (*CheckCluster, error) {
cmd := CheckCluster(s)
if _, ok := ClusterCommands[cmd]; !ok {
return nil, fmt.Errorf("Invalid cluster check command %s", s)
}
return &cmd, nil
}

type IcingaCommand struct {
Name string
Vars map[string]data.CommandVar
States []string
}

var (
PodCommands map[CheckPod]IcingaCommand
NodeCommands map[CheckNode]IcingaCommand
ClusterCommands map[CheckCluster]IcingaCommand
)

func init() {
ClusterCommands = map[CheckCluster]IcingaCommand{}
clusterChecks, err := data.LoadClusterChecks()
if err != nil {
log.Fatal(err)
}
for _, cmd := range clusterChecks.Command {
vars := make(map[string]data.CommandVar)
for _, v := range cmd.Vars {
vars[v.Name] = v
}
ClusterCommands[CheckCluster(cmd.Name)] = IcingaCommand{
Name: cmd.Name,
Vars: vars,
States: cmd.States,
}
}

NodeCommands = map[CheckNode]IcingaCommand{}
nodeChecks, err := data.LoadNodeChecks()
if err != nil {
log.Fatal(err)
}
for _, cmd := range nodeChecks.Command {
vars := make(map[string]data.CommandVar)
for _, v := range cmd.Vars {
vars[v.Name] = v
}
NodeCommands[CheckNode(cmd.Name)] = IcingaCommand{
Name: cmd.Name,
Vars: vars,
States: cmd.States,
}
}

PodCommands = map[CheckPod]IcingaCommand{}
podChecks, err := data.LoadPodChecks()
if err != nil {
log.Fatal(err)
}
for _, cmd := range podChecks.Command {
vars := make(map[string]data.CommandVar)
for _, v := range cmd.Vars {
vars[v.Name] = v
}
PodCommands[CheckPod(cmd.Name)] = IcingaCommand{
Name: cmd.Name,
Vars: vars,
States: cmd.States,
}
}
}
26 changes: 26 additions & 0 deletions apis/monitoring/install/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package install

import (
tapi "github.com/appscode/searchlight/apis/monitoring"
"github.com/appscode/searchlight/apis/monitoring/v1alpha1"
"k8s.io/apimachinery/pkg/apimachinery/announced"
"k8s.io/apimachinery/pkg/apimachinery/registered"
"k8s.io/apimachinery/pkg/runtime"
)

// Install registers the API group and adds types to a scheme
func Install(groupFactoryRegistry announced.APIGroupFactoryRegistry, registry *registered.APIRegistrationManager, scheme *runtime.Scheme) {
if err := announced.NewGroupMetaFactory(
&announced.GroupMetaFactoryArgs{
GroupName: tapi.GroupName,
VersionPreferenceOrder: []string{v1alpha1.SchemeGroupVersion.Version},
ImportPrefix: "github.com/appscode/searchlight/apis/monitoring",
AddInternalObjectsToScheme: tapi.AddToScheme,
},
announced.VersionToSchemeFunc{
v1alpha1.SchemeGroupVersion.Version: v1alpha1.AddToScheme,
},
).Announce(groupFactoryRegistry).RegisterAndEnable(registry, scheme); err != nil {
panic(err)
}
}
67 changes: 67 additions & 0 deletions apis/monitoring/node_alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package monitoring

import (
"fmt"
"time"
)

const (
ResourceKindNodeAlert = "NodeAlert"
ResourceNameNodeAlert = "node-alert"
ResourceTypeNodeAlert = "nodealerts"
)

var _ Alert = &NodeAlert{}

func (a NodeAlert) GetName() string {
return a.Name
}

func (a NodeAlert) GetNamespace() string {
return a.Namespace
}

func (a NodeAlert) Command() string {
return string(a.Spec.Check)
}

func (a NodeAlert) GetCheckInterval() time.Duration {
return a.Spec.CheckInterval.Duration
}

func (a NodeAlert) GetAlertInterval() time.Duration {
return a.Spec.AlertInterval.Duration
}

func (a NodeAlert) IsValid() (bool, error) {
cmd, ok := NodeCommands[a.Spec.Check]
if !ok {
return false, fmt.Errorf("%s is not a valid node check command.", a.Spec.Check)
}
for k := range a.Spec.Vars {
if _, ok := cmd.Vars[k]; !ok {
return false, fmt.Errorf("Var %s is unsupported for check command %s.", k, a.Spec.Check)
}
}
for _, rcv := range a.Spec.Receivers {
found := false
for _, state := range cmd.States {
if state == rcv.State {
found = true
break
}
}
if !found {
return false, fmt.Errorf("State %s is unsupported for check command %s.", rcv.State, a.Spec.Check)
}
}
return true, nil
}

func (a NodeAlert) GetNotifierSecretName() string {
return a.Spec.NotifierSecretName
}

func (a NodeAlert) GetReceivers() []Receiver {
return a.Spec.Receivers
}
Loading

0 comments on commit fd41124

Please sign in to comment.