/
store.go
113 lines (101 loc) · 4.42 KB
/
store.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
// Copyright 2023 The prometheus-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 prometheus
import (
"context"
"fmt"
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/prometheus-operator/prometheus-operator/pkg/assets"
)
func AddRemoteWritesToStore(ctx context.Context, store *assets.Store, namespace string, remotes []monv1.RemoteWriteSpec) error {
for i, remote := range remotes {
if err := ValidateRemoteWriteSpec(remote); err != nil {
return fmt.Errorf("remote write %d: %w", i, err)
}
key := fmt.Sprintf("remoteWrite/%d", i)
if err := store.AddBasicAuth(ctx, namespace, remote.BasicAuth, key); err != nil {
return fmt.Errorf("remote write %d: %w", i, err)
}
if err := store.AddOAuth2(ctx, namespace, remote.OAuth2, key); err != nil {
return fmt.Errorf("remote write %d: %w", i, err)
}
if err := store.AddTLSConfig(ctx, namespace, remote.TLSConfig); err != nil {
return fmt.Errorf("remote write %d: %w", i, err)
}
if err := store.AddAuthorizationCredentials(ctx, namespace, remote.Authorization, fmt.Sprintf("remoteWrite/auth/%d", i)); err != nil {
return fmt.Errorf("remote write %d: %w", i, err)
}
if err := store.AddSigV4(ctx, namespace, remote.Sigv4, key); err != nil {
return fmt.Errorf("remote write %d: %w", i, err)
}
if err := store.AddAzureOAuth(ctx, namespace, remote.AzureAD, key); err != nil {
return fmt.Errorf("remote write %d: %w", i, err)
}
}
return nil
}
func AddRemoteReadsToStore(ctx context.Context, store *assets.Store, namespace string, remotes []monv1.RemoteReadSpec) error {
for i, remote := range remotes {
if err := store.AddBasicAuth(ctx, namespace, remote.BasicAuth, fmt.Sprintf("remoteRead/%d", i)); err != nil {
return fmt.Errorf("remote read %d: %w", i, err)
}
if err := store.AddOAuth2(ctx, namespace, remote.OAuth2, fmt.Sprintf("remoteRead/%d", i)); err != nil {
return fmt.Errorf("remote read %d: %w", i, err)
}
if err := store.AddTLSConfig(ctx, namespace, remote.TLSConfig); err != nil {
return fmt.Errorf("remote read %d: %w", i, err)
}
if err := store.AddAuthorizationCredentials(ctx, namespace, remote.Authorization, fmt.Sprintf("remoteRead/auth/%d", i)); err != nil {
return fmt.Errorf("remote read %d: %w", i, err)
}
}
return nil
}
func AddAlertmanagerEndpointsToStore(ctx context.Context, store *assets.Store, namespace string, ams []monv1.AlertmanagerEndpoints) error {
for i, am := range ams {
if err := ValidateAlertmanagerEndpoints(am); err != nil {
return fmt.Errorf("alertmanager %d: %w", i, err)
}
if err := store.AddBasicAuth(ctx, namespace, am.BasicAuth, fmt.Sprintf("alertmanager/auth/%d", i)); err != nil {
return fmt.Errorf("alertmanager %d: %w", i, err)
}
if err := store.AddSafeAuthorizationCredentials(ctx, namespace, am.Authorization, fmt.Sprintf("alertmanager/auth/%d", i)); err != nil {
return fmt.Errorf("alertmanager %d: %w", i, err)
}
if err := store.AddSigV4(ctx, namespace, am.Sigv4, fmt.Sprintf("alertmanager/auth/%d", i)); err != nil {
return fmt.Errorf("alertmanager %d: %w", i, err)
}
}
return nil
}
func AddAPIServerConfigToStore(ctx context.Context, store *assets.Store, namespace string, config *monv1.APIServerConfig) error {
if config == nil {
return nil
}
if err := store.AddBasicAuth(ctx, namespace, config.BasicAuth, "apiserver"); err != nil {
return fmt.Errorf("apiserver config: %w", err)
}
if err := store.AddAuthorizationCredentials(ctx, namespace, config.Authorization, "apiserver/auth"); err != nil {
return fmt.Errorf("apiserver config: %w", err)
}
return nil
}
func AddScrapeClassesToStore(ctx context.Context, store *assets.Store, namespace string, scrapeClasses []monv1.ScrapeClass) error {
for _, scrapeClass := range scrapeClasses {
if err := store.AddTLSConfig(ctx, namespace, scrapeClass.TLSConfig); err != nil {
return fmt.Errorf("scrape class %q: %w", scrapeClass.Name, err)
}
}
return nil
}