Skip to content

Commit 0480720

Browse files
committed
alert-mgmt-01: k8s foundation and server bootstrap for alert management API
\nSigned-off-by: avlitman <alitman@redhat.com>\nSigned-off-by: Shirly Radco <sradco@redhat.com>\nSigned-off-by: machadovilaca <machadovilaca@gmail.com>\nCo-authored-by: Cursor AI Agent <cursor-ai@users.noreply.github.com>\n Made-with: Cursor
1 parent 7681f5e commit 0480720

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

cmd/plugin-backend.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import (
88
"strconv"
99
"strings"
1010

11-
server "github.com/openshift/monitoring-plugin/pkg"
1211
"github.com/sirupsen/logrus"
12+
13+
server "github.com/openshift/monitoring-plugin/pkg"
1314
)
1415

1516
var (
1617
portArg = flag.Int("port", 0, "server port to listen on (default: 9443)\nports 9444 and 9445 reserved for other use")
1718
certArg = flag.String("cert", "", "cert file path to enable TLS (disabled by default)")
1819
keyArg = flag.String("key", "", "private key file path to enable TLS (disabled by default)")
19-
featuresArg = flag.String("features", "", "enabled features, comma separated.\noptions: ['acm-alerting', 'incidents', 'dev-config', 'perses-dashboards']")
20+
featuresArg = flag.String("features", "", "enabled features, comma separated.\noptions: ['acm-alerting', 'incidents', 'dev-config', 'perses-dashboards', 'alert-management-api']")
2021
staticPathArg = flag.String("static-path", "", "static files path to serve frontend (default: './web/dist')")
2122
configPathArg = flag.String("config-path", "", "config files path (default: './config')")
2223
pluginConfigArg = flag.String("plugin-config-path", "", "plugin yaml configuration")

pkg/k8s/namespace.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ func (nm *namespaceManager) updateMonitoringNamespace(ns *corev1.Namespace) {
9090
nm.mu.Lock()
9191
defer nm.mu.Unlock()
9292

93-
nm.monitoringNamespaces[ns.Name] = true
93+
if ns.Labels != nil && ns.Labels[ClusterMonitoringLabel] == "true" {
94+
nm.monitoringNamespaces[ns.Name] = true
95+
} else {
96+
delete(nm.monitoringNamespaces, ns.Name)
97+
}
9498
}
9599

96100
func (nm *namespaceManager) removeMonitoringNamespace(name string) {

pkg/server.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/gorilla/handlers"
1414
"github.com/gorilla/mux"
15-
"github.com/openshift/monitoring-plugin/pkg/proxy"
1615
"github.com/sirupsen/logrus"
1716
"gopkg.in/yaml.v2"
1817
v1 "k8s.io/api/core/v1"
@@ -21,6 +20,8 @@ import (
2120
"k8s.io/client-go/kubernetes/scheme"
2221
"k8s.io/client-go/rest"
2322
"k8s.io/client-go/tools/record"
23+
24+
"github.com/openshift/monitoring-plugin/pkg/proxy"
2425
)
2526

2627
var log = logrus.WithField("module", "server")
@@ -56,10 +57,11 @@ type PluginConfig struct {
5657
type Feature string
5758

5859
const (
59-
AcmAlerting Feature = "acm-alerting"
60-
Incidents Feature = "incidents"
61-
DevConfig Feature = "dev-config"
62-
PersesDashboards Feature = "perses-dashboards"
60+
AcmAlerting Feature = "acm-alerting"
61+
Incidents Feature = "incidents"
62+
DevConfig Feature = "dev-config"
63+
PersesDashboards Feature = "perses-dashboards"
64+
AlertManagementAPI Feature = "alert-management-api"
6365
)
6466

6567
func (pluginConfig *PluginConfig) MarshalJSON() ([]byte, error) {
@@ -103,6 +105,8 @@ func (s *PluginServer) Shutdown(ctx context.Context) error {
103105

104106
func createHTTPServer(ctx context.Context, cfg *Config) (*http.Server, error) {
105107
acmMode := cfg.Features[AcmAlerting]
108+
alertManagementAPIMode := cfg.Features[AlertManagementAPI]
109+
106110
acmLocationsLength := len(cfg.AlertmanagerUrl) + len(cfg.ThanosQuerierUrl)
107111

108112
if acmLocationsLength > 0 && !acmMode {
@@ -116,15 +120,19 @@ func createHTTPServer(ctx context.Context, cfg *Config) (*http.Server, error) {
116120
return nil, fmt.Errorf("cannot set default port to reserved port %d", cfg.Port)
117121
}
118122

123+
var k8sconfig *rest.Config
124+
var err error
125+
119126
// Uncomment the following line for local development:
120-
// k8sconfig, err := clientcmd.BuildConfigFromFlags("", "$HOME/.kube/config")
127+
// k8sconfig, err = clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
128+
// if err != nil {
129+
// return nil, fmt.Errorf("cannot get kubeconfig from file: %w", err)
130+
// }
121131

122132
// Comment the following line for local development:
123133
var k8sclient *dynamic.DynamicClient
124-
if acmMode {
125-
126-
k8sconfig, err := rest.InClusterConfig()
127-
134+
if acmMode || alertManagementAPIMode {
135+
k8sconfig, err = rest.InClusterConfig()
128136
if err != nil {
129137
return nil, fmt.Errorf("cannot get in cluster config: %w", err)
130138
}

0 commit comments

Comments
 (0)