Skip to content

Commit

Permalink
Merge pull request #429 from weaveworks/alerts
Browse files Browse the repository at this point in the history
Implement canary alerts and alert providers
  • Loading branch information
stefanprodan authored Feb 11, 2020
2 parents 963a9af + fd85a34 commit ad68ca3
Show file tree
Hide file tree
Showing 46 changed files with 1,960 additions and 388 deletions.
2 changes: 2 additions & 0 deletions artifacts/flagger/account.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ rules:
- canaries/status
- metrictemplates
- metrictemplates/status
- alertproviders
- alertproviders/status
verbs: ["*"]
- apiGroups:
- networking.istio.io
Expand Down
59 changes: 59 additions & 0 deletions artifacts/flagger/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,62 @@ spec:
query:
description: Query of this metric template
type: string
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: alertproviders.flagger.app
annotations:
helm.sh/resource-policy: keep
spec:
group: flagger.app
version: v1beta1
versions:
- name: v1beta1
served: true
storage: true
names:
plural: alertproviders
singular: alertprovider
kind: AlertProvider
categories:
- all
scope: Namespaced
subresources:
status: {}
additionalPrinterColumns:
- name: Type
type: string
JSONPath: .spec.type
validation:
openAPIV3Schema:
properties:
spec:
oneOf:
- required:
- type
- address
- required:
- type
- secretRef
properties:
type:
description: Type of this provider
type: string
enum:
- slack
- msteams
- discord
- rocket
address:
description: Hook URL address of this provider
type: string
secretRef:
description: Kubernetes secret reference containing the provider address
type: object
required:
- name
properties:
name:
description: Name of the Kubernetes secret
type: string
59 changes: 59 additions & 0 deletions charts/flagger/templates/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,63 @@ spec:
query:
description: Query of this metric template
type: string
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: alertproviders.flagger.app
annotations:
helm.sh/resource-policy: keep
spec:
group: flagger.app
version: v1beta1
versions:
- name: v1beta1
served: true
storage: true
names:
plural: alertproviders
singular: alertprovider
kind: AlertProvider
categories:
- all
scope: Namespaced
subresources:
status: {}
additionalPrinterColumns:
- name: Type
type: string
JSONPath: .spec.type
validation:
openAPIV3Schema:
properties:
spec:
oneOf:
- required:
- type
- address
- required:
- type
- secretRef
properties:
type:
description: Type of this provider
type: string
enum:
- slack
- msteams
- discord
- rocket
address:
description: Hook URL address of this provider
type: string
secretRef:
description: Kubernetes secret reference containing the provider address
type: object
required:
- name
properties:
name:
description: Name of the Kubernetes secret
type: string
{{- end }}
2 changes: 2 additions & 0 deletions charts/flagger/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ rules:
- canaries/status
- metrictemplates
- metrictemplates/status
- alertproviders
- alertproviders/status
verbs: ["*"]
- apiGroups:
- networking.istio.io
Expand Down
56 changes: 38 additions & 18 deletions cmd/flagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func main() {

verifyCRDs(flaggerClient, logger)
verifyKubernetesVersion(kubeClient, logger)
infos := startInformers(flaggerClient, logger, stopCh)

labels := strings.Split(selectorLabels, ",")
if len(labels) < 1 {
Expand Down Expand Up @@ -157,23 +158,6 @@ func main() {
// start HTTP server
go server.ListenAndServe(port, 3*time.Second, logger, stopCh)

// start informers
flaggerInformerFactory := informers.NewSharedInformerFactoryWithOptions(flaggerClient, time.Second*30, informers.WithNamespace(namespace))

logger.Info("Waiting for canary informer cache to sync")
canaryInformer := flaggerInformerFactory.Flagger().V1beta1().Canaries()
go canaryInformer.Informer().Run(stopCh)
if ok := cache.WaitForNamedCacheSync("flagger", stopCh, canaryInformer.Informer().HasSynced); !ok {
logger.Fatalf("failed to wait for cache to sync")
}

logger.Info("Waiting for metric template informer cache to sync")
metricInformer := flaggerInformerFactory.Flagger().V1beta1().MetricTemplates()
go metricInformer.Informer().Run(stopCh)
if ok := cache.WaitForNamedCacheSync("flagger", stopCh, metricInformer.Informer().HasSynced); !ok {
logger.Fatalf("failed to wait for cache to sync")
}

routerFactory := router.NewFactory(cfg, kubeClient, flaggerClient, ingressAnnotationsPrefix, logger, meshClient)

var configTracker canary.Tracker
Expand All @@ -193,7 +177,7 @@ func main() {
kubeClient,
meshClient,
flaggerClient,
canaryInformer,
infos,
controlLoopInterval,
logger,
notifierClient,
Expand Down Expand Up @@ -237,6 +221,37 @@ func main() {
}
}

func startInformers(flaggerClient clientset.Interface, logger *zap.SugaredLogger, stopCh <-chan struct{}) controller.Informers {
flaggerInformerFactory := informers.NewSharedInformerFactoryWithOptions(flaggerClient, time.Second*30, informers.WithNamespace(namespace))

logger.Info("Waiting for canary informer cache to sync")
canaryInformer := flaggerInformerFactory.Flagger().V1beta1().Canaries()
go canaryInformer.Informer().Run(stopCh)
if ok := cache.WaitForNamedCacheSync("flagger", stopCh, canaryInformer.Informer().HasSynced); !ok {
logger.Fatalf("failed to wait for cache to sync")
}

logger.Info("Waiting for metric template informer cache to sync")
metricInformer := flaggerInformerFactory.Flagger().V1beta1().MetricTemplates()
go metricInformer.Informer().Run(stopCh)
if ok := cache.WaitForNamedCacheSync("flagger", stopCh, metricInformer.Informer().HasSynced); !ok {
logger.Fatalf("failed to wait for cache to sync")
}

logger.Info("Waiting for alert provider informer cache to sync")
alertInformer := flaggerInformerFactory.Flagger().V1beta1().AlertProviders()
go alertInformer.Informer().Run(stopCh)
if ok := cache.WaitForNamedCacheSync("flagger", stopCh, alertInformer.Informer().HasSynced); !ok {
logger.Fatalf("failed to wait for cache to sync")
}

return controller.Informers{
CanaryInformer: canaryInformer,
MetricInformer: metricInformer,
AlertInformer: alertInformer,
}
}

func startLeaderElection(ctx context.Context, run func(), ns string, kubeClient kubernetes.Interface, logger *zap.SugaredLogger) {
configMapName := "flagger-leader-election"
id, err := os.Hostname()
Expand Down Expand Up @@ -322,6 +337,11 @@ func verifyCRDs(flaggerClient clientset.Interface, logger *zap.SugaredLogger) {
if err != nil {
logger.Fatalf("MetricTemplate CRD is not registered %v", err)
}

_, err = flaggerClient.FlaggerV1beta1().AlertProviders(namespace).List(metav1.ListOptions{Limit: 1})
if err != nil {
logger.Fatalf("AlertProvider CRD is not registered %v", err)
}
}

func verifyKubernetesVersion(kubeClient kubernetes.Interface, logger *zap.SugaredLogger) {
Expand Down
59 changes: 59 additions & 0 deletions kustomize/base/flagger/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,62 @@ spec:
query:
description: Query of this metric template
type: string
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: alertproviders.flagger.app
annotations:
helm.sh/resource-policy: keep
spec:
group: flagger.app
version: v1beta1
versions:
- name: v1beta1
served: true
storage: true
names:
plural: alertproviders
singular: alertprovider
kind: AlertProvider
categories:
- all
scope: Namespaced
subresources:
status: {}
additionalPrinterColumns:
- name: Type
type: string
JSONPath: .spec.type
validation:
openAPIV3Schema:
properties:
spec:
oneOf:
- required:
- type
- address
- required:
- type
- secretRef
properties:
type:
description: Type of this provider
type: string
enum:
- slack
- msteams
- discord
- rocket
address:
description: Hook URL address of this provider
type: string
secretRef:
description: Kubernetes secret reference containing the provider address
type: object
required:
- name
properties:
name:
description: Name of the Kubernetes secret
type: string
2 changes: 2 additions & 0 deletions kustomize/base/flagger/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ rules:
- canaries/status
- metrictemplates
- metrictemplates/status
- alertproviders
- alertproviders/status
verbs: ["*"]
- apiGroups:
- networking.istio.io
Expand Down
Loading

0 comments on commit ad68ca3

Please sign in to comment.