Skip to content

Commit

Permalink
Merge pull request #334 from VictoriaMetrics/vmalert-notifier-discovery
Browse files Browse the repository at this point in the history
vmalert notifier discovery
  • Loading branch information
f41gh7 authored Sep 24, 2021
2 parents e479609 + 96c2538 commit 8ccd46a
Show file tree
Hide file tree
Showing 17 changed files with 615 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ manager: generate fmt vet

# Run against the configured Kubernetes cluster in ~/.kube/config
run: manager
WATCH_NAMESPACE="" ./bin/manager
./bin/manager

# Install CRDs into a cluster
install: manifests fix118 fix_crd_nulls kustomize
Expand Down
19 changes: 19 additions & 0 deletions api/v1beta1/additional.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,22 @@ func (cr *EmbeddedHPA) sanityCheck() error {
}
return nil
}

// DiscoverySelector can be used at CRD components discovery
type DiscoverySelector struct {
Namespace *NamespaceSelector `json:"namespaceSelector,omitempty"`
Labels *metav1.LabelSelector `json:"labelSelector,omitempty"`
}

func (ds *DiscoverySelector) AsListOptions() (*client.ListOptions, error) {
if ds.Labels == nil {
return &client.ListOptions{}, nil
}
s, err := metav1.LabelSelectorAsSelector(ds.Labels)
if err != nil {
return nil, err
}
return &client.ListOptions{
LabelSelector: s,
}, nil
}
27 changes: 23 additions & 4 deletions api/v1beta1/vmalert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,24 @@ type VMAlertDatasourceSpec struct {
// VMAlertNotifierSpec defines the notifier url for sending information about alerts
// +k8s:openapi-gen=true
type VMAlertNotifierSpec struct {
// AlertManager url. Required parameter. E.g. http://127.0.0.1:9093
URL string `json:"url"`
// AlertManager url. E.g. http://127.0.0.1:9093
// +optional
URL string `json:"url,omitempty"`
// Selector allows service discovery for alertmanager
// in this case all matched vmalertmanager replicas will be added into vmalert notifier.url
// as statefulset pod.fqdn
// +optional
Selector *DiscoverySelector `json:"selector,omitempty"`
// BasicAuth allow notifier to authenticate over basic authentication
// +optional
BasicAuth *BasicAuth `json:"basicAuth,omitempty"`
// TLSConfig describes tls configuration for notifier
TLSConfig *TLSConfig `json:"tlsConfig,omitempty"`
}

// AsMapKey - returns cr name with suffix for notifier token/auth maps.
// NotifierAsMapKey - returns cr name with suffix for notifier token/auth maps.
func (cr VMAlert) NotifierAsMapKey(i int) string {
return fmt.Sprintf("nodeScrape/%s/%s/%d", cr.Namespace, cr.Name, i)
return fmt.Sprintf("vmalert/%s/%s/%d", cr.Namespace, cr.Name, i)
}

// VMAgentRemoteReadSpec defines the remote storage configuration for VmAlert to read alerts from
Expand Down Expand Up @@ -444,6 +450,19 @@ func (cr *VMAlert) AsCRDOwner() []metav1.OwnerReference {
return crd.GetCRDAsOwner(crd.VMAlert)
}

func (cr *VMAlert) GetNotifierSelectors() []*DiscoverySelector {
var r []*DiscoverySelector
for _, n := range cr.Spec.Notifiers {
if n.Selector == nil {
continue
}
r = append(r, n.Selector)
}
if cr.Spec.Notifier != nil && cr.Spec.Notifier.Selector != nil {
r = append(r, cr.Spec.Notifier.Selector)
}
return r
}
func init() {
SchemeBuilder.Register(&VMAlert{}, &VMAlertList{})
}
19 changes: 19 additions & 0 deletions api/v1beta1/vmalertmanager_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,30 @@ func (cr *VMAlertmanager) AsURL() string {
return fmt.Sprintf("http://%s.%s.svc:9093", cr.PrefixedName(), cr.Namespace)
}

func (cr *VMAlertmanager) AsPodFQDN(idx int) string {
return fmt.Sprintf("http://%s-%d.%s.%s.svc:9093", cr.PrefixedName(), idx, cr.PrefixedName(), cr.Namespace)
}

// AsCRDOwner implements interface
func (cr *VMAlertmanager) AsCRDOwner() []metav1.OwnerReference {
return crd.GetCRDAsOwner(crd.VMAlertManager)
}

// AsNotifiers converts VMAlertmanager into VMAlertNotifierSpec
func (cr *VMAlertmanager) AsNotifiers() []VMAlertNotifierSpec {
var r []VMAlertNotifierSpec
replicaCount := 1
if cr.Spec.ReplicaCount != nil {
replicaCount = int(*cr.Spec.ReplicaCount)
}
for i := 0; i < replicaCount; i++ {
ns := VMAlertNotifierSpec{
URL: cr.AsPodFQDN(i),
}
r = append(r, ns)
}
return r
}
func init() {
SchemeBuilder.Register(&VMAlertmanager{}, &VMAlertmanagerList{})
}
16 changes: 16 additions & 0 deletions api/v1beta1/vmservicescrape_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ type NamespaceSelector struct {
MatchNames []string `json:"matchNames,omitempty"`
}

type nsMatcher interface {
GetNamespace() string
}

func (ns *NamespaceSelector) IsMatch(item nsMatcher) bool {
if ns.Any {
return true
}
for _, n := range ns.MatchNames {
if item.GetNamespace() == n {
return true
}
}
return false
}

// Endpoint defines a scrapeable endpoint serving Prometheus metrics.
// +k8s:openapi-gen=true
type Endpoint struct {
Expand Down
30 changes: 30 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

144 changes: 138 additions & 6 deletions config/crd/bases/operator.victoriametrics.com_vmalerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,74 @@ spec:
- key
type: object
type: object
selector:
description: Selector allows service discovery for alertmanager
in this case all matched vmalertmanager replicas will be added
into vmalert notifier.url as statefulset pod.fqdn
properties:
labelSelector:
description: A label selector is a label query over a set
of resources. The result of matchLabels and matchExpressions
are ANDed. An empty label selector matches all objects.
A null label selector matches no objects.
properties:
matchExpressions:
description: matchExpressions is a list of label selector
requirements. The requirements are ANDed.
items:
description: A label selector requirement is a selector
that contains values, a key, and an operator that
relates the key and values.
properties:
key:
description: key is the label key that the selector
applies to.
type: string
operator:
description: operator represents a key's relationship
to a set of values. Valid operators are In, NotIn,
Exists and DoesNotExist.
type: string
values:
description: values is an array of string values.
If the operator is In or NotIn, the values array
must be non-empty. If the operator is Exists or
DoesNotExist, the values array must be empty.
This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {key,value} pairs.
A single {key,value} in the matchLabels map is equivalent
to an element of matchExpressions, whose key field is
"key", the operator is "In", and the values array contains
only "value". The requirements are ANDed.
type: object
type: object
namespaceSelector:
description: NamespaceSelector is a selector for selecting
either all namespaces or a list of namespaces.
properties:
any:
description: Boolean describing whether all namespaces
are selected in contrast to a list restricting them.
type: boolean
matchNames:
description: List of namespace names.
items:
type: string
type: array
type: object
type: object
tlsConfig:
description: TLSConfig describes tls configuration for notifier
properties:
Expand Down Expand Up @@ -550,10 +618,8 @@ spec:
type: string
type: object
url:
description: AlertManager url. Required parameter. E.g. http://127.0.0.1:9093
description: AlertManager url. E.g. http://127.0.0.1:9093
type: string
required:
- url
type: object
notifiers:
description: Notifiers prometheus alertmanager endpoints. Required
Expand Down Expand Up @@ -613,6 +679,74 @@ spec:
- key
type: object
type: object
selector:
description: Selector allows service discovery for alertmanager
in this case all matched vmalertmanager replicas will be added
into vmalert notifier.url as statefulset pod.fqdn
properties:
labelSelector:
description: A label selector is a label query over a set
of resources. The result of matchLabels and matchExpressions
are ANDed. An empty label selector matches all objects.
A null label selector matches no objects.
properties:
matchExpressions:
description: matchExpressions is a list of label selector
requirements. The requirements are ANDed.
items:
description: A label selector requirement is a selector
that contains values, a key, and an operator that
relates the key and values.
properties:
key:
description: key is the label key that the selector
applies to.
type: string
operator:
description: operator represents a key's relationship
to a set of values. Valid operators are In,
NotIn, Exists and DoesNotExist.
type: string
values:
description: values is an array of string values.
If the operator is In or NotIn, the values array
must be non-empty. If the operator is Exists
or DoesNotExist, the values array must be empty.
This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {key,value} pairs.
A single {key,value} in the matchLabels map is equivalent
to an element of matchExpressions, whose key field
is "key", the operator is "In", and the values array
contains only "value". The requirements are ANDed.
type: object
type: object
namespaceSelector:
description: NamespaceSelector is a selector for selecting
either all namespaces or a list of namespaces.
properties:
any:
description: Boolean describing whether all namespaces
are selected in contrast to a list restricting them.
type: boolean
matchNames:
description: List of namespace names.
items:
type: string
type: array
type: object
type: object
tlsConfig:
description: TLSConfig describes tls configuration for notifier
properties:
Expand Down Expand Up @@ -741,10 +875,8 @@ spec:
type: string
type: object
url:
description: AlertManager url. Required parameter. E.g. http://127.0.0.1:9093
description: AlertManager url. E.g. http://127.0.0.1:9093
type: string
required:
- url
type: object
type: array
podDisruptionBudget:
Expand Down
Loading

0 comments on commit 8ccd46a

Please sign in to comment.