Skip to content

Commit a8a9a85

Browse files
Merge pull request #1690 from simonpasquier/mon-2289
Bug 2100860: Pass user-defined Alertmanager service in shared configmap
2 parents 492c01d + ce42f37 commit a8a9a85

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

pkg/manifests/manifests.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,10 @@ func (f *Factory) ThanosQuerierRoute() (*routev1.Route, error) {
15161516
return r, nil
15171517
}
15181518

1519-
func (f *Factory) SharingConfig(promHost, amHost, thanosHost *url.URL) *v1.ConfigMap {
1519+
func (f *Factory) SharingConfig(
1520+
promHost, amHost, thanosHost *url.URL,
1521+
alertmanagerUserWorkloadHost, alertmanagerTenancyHost string,
1522+
) *v1.ConfigMap {
15201523
data := map[string]string{}
15211524

15221525
// Configmap keys need to include "public" to indicate that they are public values.
@@ -1532,6 +1535,8 @@ func (f *Factory) SharingConfig(promHost, amHost, thanosHost *url.URL) *v1.Confi
15321535
if thanosHost != nil {
15331536
data["thanosPublicURL"] = fmt.Sprintf("%s://%s", thanosHost.Scheme, thanosHost.Host)
15341537
}
1538+
data["alertmanagerUserWorkloadHost"] = alertmanagerUserWorkloadHost
1539+
data["alertmanagerTenancyHost"] = alertmanagerTenancyHost
15351540

15361541
return &v1.ConfigMap{
15371542
ObjectMeta: metav1.ObjectMeta{

pkg/manifests/manifests_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,13 +685,13 @@ func TestSharingConfig(t *testing.T) {
685685
t.Fatal(err)
686686
}
687687

688-
cm := f.SharingConfig(u, u, u)
688+
cm := f.SharingConfig(u, u, u, "alertmanager-main.openshift-monitoring.svc:9094", "alertmanager-main.openshift-monitoring.svc:9092")
689689
if cm.Namespace == "openshift-monitoring" {
690690
t.Fatalf("expecting namespace other than %q", "openshift-monitoring")
691691
}
692692
for k, v := range cm.Data {
693693
if !strings.Contains(k, "Public") {
694-
t.Fatalf("expecting key %q to contain 'Public'", k)
694+
continue
695695
}
696696
publicURL, err := url.Parse(v)
697697
if err != nil {

pkg/tasks/configsharing.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ package tasks
1616

1717
import (
1818
"context"
19+
"fmt"
1920
"net/url"
2021

22+
"github.com/pkg/errors"
23+
v1 "k8s.io/api/core/v1"
24+
2125
"github.com/openshift/cluster-monitoring-operator/pkg/client"
2226
"github.com/openshift/cluster-monitoring-operator/pkg/manifests"
23-
"github.com/pkg/errors"
2427
)
2528

2629
type ConfigSharingTask struct {
@@ -71,7 +74,49 @@ func (t *ConfigSharingTask) Run(ctx context.Context) error {
7174
return errors.Wrap(err, "failed to retrieve Thanos Querier host")
7275
}
7376

74-
cm := t.factory.SharingConfig(promURL, amURL, thanosURL)
77+
var (
78+
svc *v1.Service
79+
webPort, tenancyPort int
80+
)
81+
if t.config.UserWorkloadConfiguration.Alertmanager.Enabled {
82+
// User-defined alerts are routed to the UWM Alertmanager.
83+
svc, err = t.factory.AlertmanagerUserWorkloadService()
84+
if err != nil {
85+
return errors.Wrap(err, "initializing Alertmanager User Workload Service failed")
86+
}
87+
} else {
88+
// User-defined alerts are routed to the platform Alertmanager.
89+
svc, err = t.factory.AlertmanagerService()
90+
if err != nil {
91+
return errors.Wrap(err, "initializing Alertmanager Service failed")
92+
}
93+
}
94+
95+
for _, port := range svc.Spec.Ports {
96+
switch port.Name {
97+
case "web":
98+
webPort = int(port.Port)
99+
case "tenancy":
100+
tenancyPort = int(port.Port)
101+
}
102+
}
103+
104+
if webPort == 0 {
105+
return errors.New("failed to find Alertmanager web port")
106+
}
107+
108+
if tenancyPort == 0 {
109+
return errors.New("failed to find Alertmanager tenancy port")
110+
}
111+
112+
cm := t.factory.SharingConfig(
113+
promURL,
114+
amURL,
115+
thanosURL,
116+
fmt.Sprintf("%s.%s.svc:%d", svc.Name, svc.Namespace, webPort),
117+
fmt.Sprintf("%s.%s.svc:%d", svc.Name, svc.Namespace, tenancyPort),
118+
)
119+
75120
err = t.client.CreateOrUpdateConfigMap(ctx, cm)
76121
if err != nil {
77122
return errors.Wrapf(err, "reconciling %s/%s Config ConfigMap failed", cm.Namespace, cm.Name)

0 commit comments

Comments
 (0)