Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add initial support for alertmanager external-url #622

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ spec:
required:
- key
x-kubernetes-map-type: atomic
externalURL:
type: string
description: ExternalURL is the external URL the managed Alertmanager will be available under. This is used for generating links back to the Alertmanager itself in fired alerts.
rules:
type: object
description: Rules specifies how the operator configures and deployes rule-evaluator.
Expand Down
1 change: 1 addition & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ ManagedAlertmanagerSpec holds configuration information for the managed Alertman
| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| configSecret | ConfigSecret refers to the name of a single-key Secret in the public namespace that holds the managed Alertmanager config file. | *corev1.SecretKeySelector | false |
| externalURL | ExternalURL is the external URL the managed Alertmanager will be available under. This is used for generating links back to the Alertmanager itself in fired alerts. | string | false |

[Back to TOC](#table-of-contents)

Expand Down
27 changes: 25 additions & 2 deletions e2e/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -74,6 +75,7 @@ route:
},
Key: "my-secret-key",
},
ExternalURL: "https://alertmanager.mycompany.com/",
},
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -108,15 +110,15 @@ func testAlertmanager(ctx context.Context, t *OperatorContext, spec *monitoringv
})
t.Run("deployed", t.subtest(func(ctx context.Context, t *OperatorContext) {
t.Parallel()
testAlertmanagerDeployed(ctx, t)
testAlertmanagerDeployed(ctx, t, spec)
}))
t.Run("config set", t.subtest(func(ctx context.Context, t *OperatorContext) {
t.Parallel()
testAlertmanagerConfig(ctx, t, secret, key)
}))
}

func testAlertmanagerDeployed(ctx context.Context, t *OperatorContext) {
func testAlertmanagerDeployed(ctx context.Context, t *OperatorContext, spec *monitoringv1.ManagedAlertmanagerSpec) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func testAlertmanagerDeployed(ctx context.Context, t *OperatorContext, spec *monitoringv1.ManagedAlertmanagerSpec) {
func testAlertmanagerDeployed(ctx context.Context, t *OperatorContext, config *monitoringv1.ManagedAlertmanagerSpec) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super small nit, but I was quite confused thinking initially this variable as used down there is Alertmanager spec not alertmanager configuration spec (kind of) - a bit more explicitness might help

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure no prob. Changed it to config.

err := wait.Poll(time.Second, 1*time.Minute, func() (bool, error) {
var ss appsv1.StatefulSet
if err := t.Client().Get(ctx, client.ObjectKey{Namespace: t.namespace, Name: operator.NameAlertmanager}, &ss); err != nil {
Expand All @@ -135,6 +137,27 @@ func testAlertmanagerDeployed(ctx context.Context, t *OperatorContext) {
return false, fmt.Errorf("unexpected annotations (-want, +got): %s", diff)
}

// If spec is empty, no need to assert EXTRA_ARGS.
if spec == nil {
return true, nil
}
var wantArgs []string
for _, c := range ss.Spec.Template.Spec.Containers {
if c.Name != "alertmanager" {
continue
}
// We're mainly interested in the dynamic flags but checking the entire set including
// the static ones is ultimately simpler.
if externalURL := spec.ExternalURL; externalURL != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit confused again - can't we prepare wantArgs before looping through Alertmanager statetful set containers? It feel like you do it inside this loop for a reason, but I can't find any 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was honestly just following convention elsewhere 1, 2 in our e2e tests without thinking too much about it.

I'm tempted to leave it this way for convention, where a separate PR can concisely and atomically do a cleanup (added TODOs). WDYT?

wantArgs = append(wantArgs, fmt.Sprintf("--web.external-url=%q", spec.ExternalURL))
}

if diff := cmp.Diff(strings.Join(wantArgs, " "), getEnvVar(c.Env, "EXTRA_ARGS")); diff != "" {
return false, fmt.Errorf("unexpected flags (-want, +got): %s", diff)
}
return true, nil
}

return true, nil
})
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions manifests/setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,9 @@ spec:
required:
- key
x-kubernetes-map-type: atomic
externalURL:
type: string
description: ExternalURL is the external URL the managed Alertmanager will be available under. This is used for generating links back to the Alertmanager itself in fired alerts.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it wouldn't be easier to put exact flag description. It's essentially more than just alert link, it also fixes UI when reverse proxies are used. WDYT?

Suggested change
description: ExternalURL is the external URL the managed Alertmanager will be available under. This is used for generating links back to the Alertmanager itself in fired alerts.
description: The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy). Used for generating relative and absolute links back to Alertmanager itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Alertmanager. If omitted, relevant URL components will be derived automatically.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM. Done.

rules:
type: object
description: Rules specifies how the operator configures and deployes rule-evaluator.
Expand Down
4 changes: 4 additions & 0 deletions pkg/operator/apis/monitoring/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ type ManagedAlertmanagerSpec struct {
// ConfigSecret refers to the name of a single-key Secret in the public namespace that
// holds the managed Alertmanager config file.
ConfigSecret *corev1.SecretKeySelector `json:"configSecret,omitempty"`
// ExternalURL is the external URL the managed Alertmanager will be
// available under. This is used for generating links back to the
// Alertmanager itself in fired alerts.
ExternalURL string `json:"externalURL,omitempty"`
}

// AlertmanagerEndpoints defines a selection of a single Endpoints object
Expand Down
53 changes: 53 additions & 0 deletions pkg/operator/operator_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,15 @@ func (r *operatorConfigReconciler) Reconcile(ctx context.Context, req reconcile.
return reconcile.Result{}, fmt.Errorf("ensure rule-evaluator config: %w", err)
}

// Ensure the alertmanager configuration is pulled from the spec.
if err := r.ensureAlertmanagerConfigSecret(ctx, config.ManagedAlertmanager); err != nil {
return reconcile.Result{}, fmt.Errorf("ensure alertmanager config secret: %w", err)
}

if err := r.ensureAlertmanagerStatefulSet(ctx, config.ManagedAlertmanager); err != nil {
return reconcile.Result{}, fmt.Errorf("ensure alertmanager statefulset: %w", err)
}

// Mirror the fetched secret data to where the rule-evaluator can
// mount and access.
if err := r.ensureRuleEvaluatorSecrets(ctx, secretData); err != nil {
Expand Down Expand Up @@ -383,6 +388,54 @@ func (r *operatorConfigReconciler) ensureAlertmanagerConfigSecret(ctx context.Co
return nil
}

// ensureAlertmanagerStatefulSet configures the managed Alertmanager instance
// to reflect the provided spec.
func (r *operatorConfigReconciler) ensureAlertmanagerStatefulSet(ctx context.Context, spec *monitoringv1.ManagedAlertmanagerSpec) error {
if spec == nil {
return nil
}

logger, _ := logr.FromContext(ctx)

var sset appsv1.StatefulSet
err := r.client.Get(ctx, client.ObjectKey{Namespace: r.opts.OperatorNamespace, Name: NameAlertmanager}, &sset)
// Some users deliberately not want to run the alertmanager.
// Only emit a warning but don't cause retries
// as this logic gets re-triggered anyway if the StatefulSet is created later.
if apierrors.IsNotFound(err) {
logger.Error(err, "Alertmanager StatefulSet does not exist")
return nil
}
if err != nil {
return err
}

var flags []string
if externalURL := spec.ExternalURL; externalURL != "" {
flags = append(flags, fmt.Sprintf("--web.external-url=%q", externalURL))
}

// Set EXTRA_ARGS envvar in alertmanager container.
for i, c := range sset.Spec.Template.Spec.Containers {
if c.Name != "alertmanager" {
continue
}
var repl []corev1.EnvVar

for _, ev := range c.Env {
if ev.Name != "EXTRA_ARGS" {
repl = append(repl, ev)
}
}
repl = append(repl, corev1.EnvVar{Name: "EXTRA_ARGS", Value: strings.Join(flags, " ")})

sset.Spec.Template.Spec.Containers[i].Env = repl
}

// Upsert alertmanager StatefulSet.
return r.client.Update(ctx, &sset)
}

// ensureRuleEvaluatorDeployment reconciles the Deployment for rule-evaluator.
func (r *operatorConfigReconciler) ensureRuleEvaluatorDeployment(ctx context.Context, spec *monitoringv1.RuleEvaluatorSpec) error {
logger, _ := logr.FromContext(ctx)
Expand Down