Skip to content

Commit

Permalink
remove-generic-resource-from-alertmanager-config (#1641)
Browse files Browse the repository at this point in the history
* remove-generic-resource-from-alertmanager-config

* review

Signed-off-by: QuentinBisson <[email protected]>

* goimports...

---------

Signed-off-by: QuentinBisson <[email protected]>
  • Loading branch information
QuentinBisson authored Jun 6, 2024
1 parent 668ed6f commit ef180bc
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 248 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Internal rework to remove the use generic resource to ease out the migration to Mimir.
- Remove unused scrape_timeout inhibition.

## [4.76.0] - 2024-06-03
Expand Down
26 changes: 0 additions & 26 deletions service/controller/resource/alerting/alertmanagerconfig/client.go

This file was deleted.

39 changes: 39 additions & 0 deletions service/controller/resource/alerting/alertmanagerconfig/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package alertmanagerconfig

import (
"context"

"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/giantswarm/prometheus-meta-operator/v2/service/controller/resource/resourceutils"
)

func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
desired, err := r.toSecret()
if err != nil {
return microerror.Mask(err)
}

r.config.Logger.Debugf(ctx, "creating")
current, err := r.config.K8sClient.K8sClient().CoreV1().Secrets(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{})
if apierrors.IsNotFound(err) {
current, err = r.config.K8sClient.K8sClient().CoreV1().Secrets(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{})
}

if err != nil {
return microerror.Mask(err)
}

if r.hasChanged(current, desired) {
resourceutils.UpdateMeta(current, desired)
_, err = r.config.K8sClient.K8sClient().CoreV1().Secrets(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{})
if err != nil {
return microerror.Mask(err)
}
}
r.config.Logger.Debugf(ctx, "created")

return nil
}
24 changes: 24 additions & 0 deletions service/controller/resource/alerting/alertmanagerconfig/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package alertmanagerconfig

import (
"context"

"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
object := getObjectMeta()

r.config.Logger.Debugf(ctx, "deleting")
err := r.config.K8sClient.K8sClient().CoreV1().Secrets(object.GetNamespace()).Delete(ctx, object.GetName(), metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
// fall through
} else if err != nil {
return microerror.Mask(err)
}
r.config.Logger.Debugf(ctx, "deleted")

return nil
}
82 changes: 30 additions & 52 deletions service/controller/resource/alerting/alertmanagerconfig/resource.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package alertmanagerconfig

import (
"context"
"fmt"
"net/url"
"path"
Expand All @@ -14,7 +13,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/giantswarm/prometheus-meta-operator/v2/pkg/template"
"github.com/giantswarm/prometheus-meta-operator/v2/service/controller/resource/generic"
"github.com/giantswarm/prometheus-meta-operator/v2/service/key"
)

Expand All @@ -40,6 +38,10 @@ type Config struct {
SlackApiURL string
}

type Resource struct {
config Config
}

type NotificationTemplateData struct {
GrafanaAddress string
MimirEnabled bool
Expand All @@ -56,58 +58,34 @@ type AlertmanagerTemplateData struct {
MimirEnabled bool
}

func New(config Config) (*generic.Resource, error) {
clientFunc := func(namespace string) generic.Interface {
c := config.K8sClient.K8sClient().CoreV1().Secrets(namespace)
return wrappedClient{client: c}
}

c := generic.Config{
ClientFunc: clientFunc,
Logger: config.Logger,
Name: Name,
GetObjectMeta: func(ctx context.Context, v interface{}) (metav1.ObjectMeta, error) {
return getObjectMeta()
},
GetDesiredObject: func(ctx context.Context, v interface{}) (metav1.Object, error) {
return toSecret(config)
},
HasChangedFunc: hasChanged,
}

r, err := generic.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
func New(config Config) (*Resource, error) {
return &Resource{config}, nil
}

return r, nil
func (r *Resource) Name() string {
return Name
}

func getObjectMeta() (metav1.ObjectMeta, error) {
func getObjectMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: key.AlertmanagerSecretName(),
Namespace: key.MonitoringNamespace,
}, nil
}

func toSecret(config Config) (*corev1.Secret, error) {
objectMeta, err := getObjectMeta()
if err != nil {
return nil, microerror.Mask(err)
}
}

notificationTemplate, err := renderNotificationTemplate(templateDirectory, config)
func (r *Resource) toSecret() (*corev1.Secret, error) {
notificationTemplate, err := r.renderNotificationTemplate(templateDirectory)
if err != nil {
return nil, microerror.Mask(err)
}

alertmanagerConfigSecret, err := renderAlertmanagerConfig(templateDirectory, config)
alertmanagerConfigSecret, err := r.renderAlertmanagerConfig(templateDirectory)
if err != nil {
return nil, microerror.Mask(err)
}

secret := &corev1.Secret{
ObjectMeta: objectMeta,
ObjectMeta: getObjectMeta(),
Data: map[string][]byte{
"alertmanager.yaml": alertmanagerConfigSecret,
"notification-template.tmpl": notificationTemplate,
Expand All @@ -118,11 +96,11 @@ func toSecret(config Config) (*corev1.Secret, error) {
return secret, nil
}

func renderNotificationTemplate(templateDirectory string, config Config) ([]byte, error) {
func (r *Resource) renderNotificationTemplate(templateDirectory string) ([]byte, error) {
templateData := NotificationTemplateData{
GrafanaAddress: config.GrafanaAddress,
MimirEnabled: config.MimirEnabled,
PrometheusAddress: fmt.Sprintf("https://%s", config.BaseDomain),
GrafanaAddress: r.config.GrafanaAddress,
MimirEnabled: r.config.MimirEnabled,
PrometheusAddress: fmt.Sprintf("https://%s", r.config.BaseDomain),
}

data, err := template.RenderTemplate(templateData, path.Join(templateDirectory, notificationTemplatePath))
Expand All @@ -133,8 +111,8 @@ func renderNotificationTemplate(templateDirectory string, config Config) ([]byte
return data, nil
}

func renderAlertmanagerConfig(templateDirectory string, config Config) ([]byte, error) {
templateData, err := getTemplateData(config)
func (r *Resource) renderAlertmanagerConfig(templateDirectory string) ([]byte, error) {
templateData, err := r.getTemplateData()
if err != nil {
return nil, microerror.Mask(err)
}
Expand All @@ -147,23 +125,23 @@ func renderAlertmanagerConfig(templateDirectory string, config Config) ([]byte,
return data, nil
}

func getTemplateData(config Config) (*AlertmanagerTemplateData, error) {
func (r *Resource) getTemplateData() (*AlertmanagerTemplateData, error) {
opsgenieUrl, err := url.Parse("https://api.opsgenie.com/v2/heartbeats")
if err != nil {
return nil, err
}
proxyURL, err := config.Proxy(opsgenieUrl)
proxyURL, err := r.config.Proxy(opsgenieUrl)
if err != nil {
return nil, err
}

d := &AlertmanagerTemplateData{
Installation: config.Installation,
OpsgenieKey: config.OpsgenieKey,
Pipeline: config.Pipeline,
SlackApiToken: config.SlackApiToken,
SlackApiURL: config.SlackApiURL,
MimirEnabled: config.MimirEnabled,
Installation: r.config.Installation,
OpsgenieKey: r.config.OpsgenieKey,
Pipeline: r.config.Pipeline,
SlackApiToken: r.config.SlackApiToken,
SlackApiURL: r.config.SlackApiURL,
MimirEnabled: r.config.MimirEnabled,
}

if proxyURL != nil {
Expand All @@ -173,7 +151,7 @@ func getTemplateData(config Config) (*AlertmanagerTemplateData, error) {
return d, nil
}

func hasChanged(current, desired metav1.Object) bool {
func (r *Resource) hasChanged(current, desired metav1.Object) bool {
c := current.(*corev1.Secret)
d := desired.(*corev1.Secret)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ func TestRenderingOfAlertmanagerNotificationTemplateWithLegacyMonitoring(t *test
Installation: "test-installation",
GrafanaAddress: "https://grafana",
}
resource, err := New(config)
if err != nil {
t.Fatal(err)
}
testFunc = func(v interface{}) (interface{}, error) {
return renderNotificationTemplate(unittest.ProjectRoot(), config)
return resource.renderNotificationTemplate(unittest.ProjectRoot())
}
}

Expand Down Expand Up @@ -59,8 +63,12 @@ func TestRenderingOfAlertmanagerNotificationTemplateWithMimirEnabled(t *testing.
MimirEnabled: true,
BaseDomain: "prometheus.installation-prometheus.svc",
}
resource, err := New(config)
if err != nil {
t.Fatal(err)
}
testFunc = func(v interface{}) (interface{}, error) {
return renderNotificationTemplate(unittest.ProjectRoot(), config)
return resource.renderNotificationTemplate(unittest.ProjectRoot())
}
}

Expand Down Expand Up @@ -104,8 +112,12 @@ func TestRenderingOfAlertmanagerConfigWithLegacyMonitoring(t *testing.T) {
Pipeline: "testing",
SlackApiURL: "https://slack",
}
resource, err := New(config)
if err != nil {
t.Fatal(err)
}
testFunc = func(v interface{}) (interface{}, error) {
return renderAlertmanagerConfig(unittest.ProjectRoot(), config)
return resource.renderAlertmanagerConfig(unittest.ProjectRoot())
}
}

Expand Down Expand Up @@ -150,8 +162,12 @@ func TestRenderingOfAlertmanagerConfigWithMimirEnabled(t *testing.T) {
Pipeline: "testing",
SlackApiURL: "https://slack",
}
resource, err := New(config)
if err != nil {
t.Fatal(err)
}
testFunc = func(v interface{}) (interface{}, error) {
return renderAlertmanagerConfig(unittest.ProjectRoot(), config)
return resource.renderAlertmanagerConfig(unittest.ProjectRoot())
}
}

Expand Down Expand Up @@ -196,8 +212,12 @@ func TestRenderingOfAlertmanagerConfigSlackToken(t *testing.T) {
SlackApiURL: "https://slack",
SlackApiToken: "some-token",
}
resource, err := New(config)
if err != nil {
t.Fatal(err)
}
testFunc = func(v interface{}) (interface{}, error) {
return renderAlertmanagerConfig(unittest.ProjectRoot(), config)
return resource.renderAlertmanagerConfig(unittest.ProjectRoot())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/giantswarm/prometheus-meta-operator/v2/service/controller/resource/resourceutils"
)

func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
Expand All @@ -29,7 +31,7 @@ func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
}

if r.hasChanged(current, desired) {
updateMeta(current, desired)
resourceutils.UpdateMeta(current, desired)
_, err = r.client.MonitoringV1alpha1().AlertmanagerConfigs(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{})
if err != nil {
return microerror.Mask(err)
Expand All @@ -39,25 +41,3 @@ func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {

return nil
}

func updateMeta(c, d metav1.Object) {
d.SetGenerateName(c.GetGenerateName())
d.SetUID(c.GetUID())
d.SetResourceVersion(c.GetResourceVersion())
d.SetGeneration(c.GetGeneration())
d.SetSelfLink(c.GetSelfLink())
d.SetCreationTimestamp(c.GetCreationTimestamp())
d.SetDeletionTimestamp(c.GetDeletionTimestamp())
d.SetDeletionGracePeriodSeconds(c.GetDeletionGracePeriodSeconds())
// without this, it's impossible to change labels on resources
if len(d.GetLabels()) == 0 {
d.SetLabels(c.GetLabels())
}
// without this, it's impossible to change annotations on resources
if len(d.GetAnnotations()) == 0 {
d.SetAnnotations(c.GetAnnotations())
}
d.SetFinalizers(c.GetFinalizers())
d.SetOwnerReferences(c.GetOwnerReferences())
d.SetManagedFields(c.GetManagedFields())
}
Loading

0 comments on commit ef180bc

Please sign in to comment.