-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathconfigsharing.go
132 lines (113 loc) · 3.62 KB
/
configsharing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2018 The Cluster Monitoring Operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tasks
import (
"context"
"errors"
"fmt"
"net/url"
v1 "k8s.io/api/core/v1"
"github.com/openshift/cluster-monitoring-operator/pkg/client"
"github.com/openshift/cluster-monitoring-operator/pkg/manifests"
)
type ConfigSharingTask struct {
client *client.Client
factory *manifests.Factory
config *manifests.Config
}
func NewConfigSharingTask(client *client.Client, factory *manifests.Factory, config *manifests.Config) *ConfigSharingTask {
return &ConfigSharingTask{
client: client,
factory: factory,
config: config,
}
}
func (t *ConfigSharingTask) Run(ctx context.Context) error {
var amURL, promURL, thanosURL *url.URL
hasRoutes, err := t.client.HasRouteCapability(ctx)
if err != nil {
return fmt.Errorf("checking for Route capability failed: %w", err)
}
if hasRoutes {
promRoute, err := t.factory.PrometheusK8sAPIRoute()
if err != nil {
return fmt.Errorf("initializing Prometheus Route failed: %w", err)
}
promURL, err = t.client.GetRouteURL(ctx, promRoute)
if err != nil {
return fmt.Errorf("failed to retrieve Prometheus host: %w", err)
}
if t.config.ClusterMonitoringConfiguration.AlertmanagerMainConfig.IsEnabled() {
amRoute, err := t.factory.AlertmanagerRoute()
if err != nil {
return fmt.Errorf("initializing Alertmanager Route failed: %w", err)
}
amURL, err = t.client.GetRouteURL(ctx, amRoute)
if err != nil {
return fmt.Errorf("failed to retrieve Alertmanager host: %w", err)
}
}
thanosRoute, err := t.factory.ThanosQuerierRoute()
if err != nil {
return fmt.Errorf("initializing Thanos Querier Route failed: %w", err)
}
thanosURL, err = t.client.GetRouteURL(ctx, thanosRoute)
if err != nil {
return fmt.Errorf("failed to retrieve Thanos Querier host: %w", err)
}
}
var (
svc *v1.Service
webPort, tenancyPort int
)
if t.config.UserWorkloadConfiguration.Alertmanager.Enabled {
// User-defined alerts are routed to the UWM Alertmanager.
svc, err = t.factory.AlertmanagerUserWorkloadService()
if err != nil {
return fmt.Errorf("initializing Alertmanager User Workload Service failed: %w", err)
}
} else {
// User-defined alerts are routed to the platform Alertmanager.
svc, err = t.factory.AlertmanagerService()
if err != nil {
return fmt.Errorf("initializing Alertmanager Service failed: %w", err)
}
}
for _, port := range svc.Spec.Ports {
switch port.Name {
case "web":
webPort = int(port.Port)
case "tenancy":
tenancyPort = int(port.Port)
}
}
if webPort == 0 {
return errors.New("failed to find Alertmanager web port")
}
if tenancyPort == 0 {
return errors.New("failed to find Alertmanager tenancy port")
}
cm := t.factory.SharingConfig(
promURL,
amURL,
thanosURL,
fmt.Sprintf("%s.%s.svc:%d", svc.Name, svc.Namespace, webPort),
fmt.Sprintf("%s.%s.svc:%d", svc.Name, svc.Namespace, tenancyPort),
)
err = t.client.CreateOrUpdateConfigMap(ctx, cm)
if err != nil {
return fmt.Errorf("reconciling %s/%s Config ConfigMap failed: %w", cm.Namespace, cm.Name, err)
}
return nil
}