-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from prometheus-operator/chore/adding-alertman…
…ager-builder [CHORE] adding alertmanager builder
- Loading branch information
Showing
4 changed files
with
346 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2024 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 cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/prometheus-operator/poctl/internal/k8sutil" | ||
"github.com/prometheus-operator/poctl/internal/log" | ||
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/client/applyconfiguration/monitoring/v1" | ||
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned" | ||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
applyConfigMetav1 "k8s.io/client-go/applyconfigurations/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
// servicemonitorCmd represents the servicemonitor command | ||
var ( | ||
serviceName string | ||
namespace string | ||
port string | ||
servicemonitorCmd = &cobra.Command{ | ||
Use: "servicemonitor", | ||
Short: "Create a service monitor object", | ||
Long: `Create a service monitor object based on user input parameters or taking as source of truth a kubernetes service`, | ||
RunE: runServiceMonitor, | ||
} | ||
) | ||
|
||
func runServiceMonitor(_ *cobra.Command, args []string) error { | ||
logger, err := log.NewLogger() | ||
if err != nil { | ||
fmt.Println(err) | ||
return err | ||
} | ||
|
||
//TODO(nicolastakashi): Replace it when the PR #6623 is merged | ||
restConfig, err := k8sutil.GetRestConfig(logger, kubeconfig) | ||
if err != nil { | ||
logger.With("error", err.Error()).Error("error while getting kubeconfig") | ||
return err | ||
} | ||
|
||
kclient, err := kubernetes.NewForConfig(restConfig) | ||
if err != nil { | ||
logger.With("error", err.Error()).Error("error while creating k8s client") | ||
return err | ||
} | ||
|
||
mclient, err := monitoringclient.NewForConfig(restConfig) | ||
if err != nil { | ||
logger.With("error", err.Error()).Error("error while creating Prometheus Operator client") | ||
return err | ||
} | ||
|
||
if serviceName == "" { | ||
return errors.New("service name is required") | ||
} | ||
|
||
err = createFromService(context.Background(), kclient, mclient, namespace, serviceName, port) | ||
if err != nil { | ||
logger.With("error", err.Error()).Error("error while creating service monitor") | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func createFromService( | ||
ctx context.Context, | ||
k8sClient *kubernetes.Clientset, | ||
mClient *monitoringclient.Clientset, | ||
namespace string, | ||
serviceName string, | ||
port string) error { | ||
|
||
service, err := k8sClient.CoreV1().Services(namespace).Get(ctx, serviceName, metav1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error while getting service %s: %v", serviceName, err) | ||
} | ||
|
||
svcMonitor := &monitoringv1.ServiceMonitorApplyConfiguration{ | ||
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{ | ||
Kind: ptr.To("ServiceMonitor"), | ||
APIVersion: ptr.To("monitoring.coreos.com/v1"), | ||
}, | ||
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{ | ||
Name: ptr.To(serviceName), | ||
Namespace: ptr.To(namespace), | ||
Labels: service.Labels, | ||
}, | ||
Spec: &monitoringv1.ServiceMonitorSpecApplyConfiguration{ | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: service.Spec.Selector, | ||
}, | ||
}, | ||
} | ||
|
||
for _, p := range service.Spec.Ports { | ||
if port != "" && p.Name != port { | ||
continue | ||
} | ||
|
||
svcMonitor.Spec.Endpoints = append(svcMonitor.Spec.Endpoints, monitoringv1.EndpointApplyConfiguration{ | ||
HonorLabels: ptr.To(true), | ||
Port: ptr.To(p.Name), | ||
}) | ||
} | ||
|
||
_, err = mClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, svcMonitor, k8sutil.ApplyOption) | ||
if err != nil { | ||
return fmt.Errorf("error while creating service monitor %s: %v", serviceName, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
createCmd.AddCommand(servicemonitorCmd) | ||
servicemonitorCmd.Flags().StringVarP(&serviceName, "service", "s", "", "Service name to create the service monitor from") | ||
servicemonitorCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "Namespace of the service") | ||
servicemonitorCmd.Flags().StringVarP(&port, "port", "p", "", "Port of the service") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright 2024 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 builder | ||
|
||
import ( | ||
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/client/applyconfiguration/monitoring/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
applyConfigCorev1 "k8s.io/client-go/applyconfigurations/core/v1" | ||
applyConfigMetav1 "k8s.io/client-go/applyconfigurations/meta/v1" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
type AlertManagerBuilder struct { | ||
labels map[string]string | ||
labelSelectors map[string]string | ||
namespace string | ||
manifets AlertManagerManifests | ||
} | ||
|
||
type AlertManagerManifests struct { | ||
ServiceAccount *applyConfigCorev1.ServiceAccountApplyConfiguration | ||
AlertManager *monitoringv1.AlertmanagerApplyConfiguration | ||
Service *applyConfigCorev1.ServiceApplyConfiguration | ||
ServiceMonitor *monitoringv1.ServiceMonitorApplyConfiguration | ||
} | ||
|
||
const AlertManagerName = "alertmanager" | ||
|
||
func NewAlertManager(namespace string) *AlertManagerBuilder { | ||
return &AlertManagerBuilder{ | ||
labels: map[string]string{ | ||
"alertmanager": AlertManagerName, | ||
}, | ||
labelSelectors: map[string]string{ | ||
"alertmanager": AlertManagerName, | ||
}, | ||
namespace: namespace, | ||
} | ||
} | ||
|
||
func (a *AlertManagerBuilder) WithServiceAccount() *AlertManagerBuilder { | ||
a.manifets.ServiceAccount = &applyConfigCorev1.ServiceAccountApplyConfiguration{ | ||
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{ | ||
Kind: ptr.To("ServiceAccount"), | ||
APIVersion: ptr.To("v1"), | ||
}, | ||
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{ | ||
Name: ptr.To(AlertManagerName), | ||
Labels: a.labels, | ||
Namespace: ptr.To(a.namespace), | ||
}, | ||
} | ||
return a | ||
} | ||
|
||
func (a *AlertManagerBuilder) WithAlertManager() *AlertManagerBuilder { | ||
a.manifets.AlertManager = &monitoringv1.AlertmanagerApplyConfiguration{ | ||
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{ | ||
Kind: ptr.To("Alertmanager"), | ||
APIVersion: ptr.To("monitoring.coreos.com/v1"), | ||
}, | ||
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{ | ||
Name: ptr.To(AlertManagerName), | ||
Labels: a.labels, | ||
Namespace: ptr.To(a.namespace), | ||
}, | ||
Spec: &monitoringv1.AlertmanagerSpecApplyConfiguration{ | ||
ServiceAccountName: a.manifets.ServiceAccount.Name, | ||
Replicas: ptr.To(int32(1)), | ||
AlertmanagerConfigSelector: &metav1.LabelSelector{}, | ||
AlertmanagerConfigNamespaceSelector: &metav1.LabelSelector{}, | ||
}, | ||
} | ||
return a | ||
} | ||
|
||
func (a *AlertManagerBuilder) WithService() *AlertManagerBuilder { | ||
a.manifets.Service = &applyConfigCorev1.ServiceApplyConfiguration{ | ||
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{ | ||
Kind: ptr.To("Service"), | ||
APIVersion: ptr.To("v1"), | ||
}, | ||
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{ | ||
Name: ptr.To(AlertManagerName), | ||
Labels: a.labels, | ||
Namespace: ptr.To(a.namespace), | ||
}, | ||
Spec: &applyConfigCorev1.ServiceSpecApplyConfiguration{ | ||
Ports: []applyConfigCorev1.ServicePortApplyConfiguration{ | ||
{ | ||
Name: ptr.To("http-web"), | ||
Port: ptr.To(int32(9093)), | ||
TargetPort: ptr.To(intstr.FromInt32(9093)), | ||
Protocol: ptr.To(corev1.ProtocolTCP), | ||
}, | ||
{ | ||
Name: ptr.To("reloader-web"), | ||
Port: ptr.To(int32(8080)), | ||
TargetPort: ptr.To(intstr.FromString("reloader-web")), | ||
}, | ||
}, | ||
Selector: a.labelSelectors, | ||
}, | ||
} | ||
return a | ||
} | ||
|
||
func (a *AlertManagerBuilder) WithServiceMonitor() *AlertManagerBuilder { | ||
a.manifets.ServiceMonitor = &monitoringv1.ServiceMonitorApplyConfiguration{ | ||
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{ | ||
Kind: ptr.To("ServiceMonitor"), | ||
APIVersion: ptr.To("monitoring.coreos.com/v1"), | ||
}, | ||
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{ | ||
Name: ptr.To(AlertManagerName), | ||
Labels: a.labels, | ||
Namespace: ptr.To(a.namespace), | ||
}, | ||
Spec: &monitoringv1.ServiceMonitorSpecApplyConfiguration{ | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: a.labelSelectors, | ||
}, | ||
Endpoints: []monitoringv1.EndpointApplyConfiguration{ | ||
{ | ||
HonorLabels: ptr.To(true), | ||
Port: ptr.To("http-web"), | ||
}, | ||
}, | ||
}, | ||
} | ||
return a | ||
} | ||
|
||
func (a *AlertManagerBuilder) Build() AlertManagerManifests { | ||
return a.manifets | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters