Skip to content

Commit

Permalink
Merge branch 'alertmanager-config' into alertmanager-test
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoBrigitte committed Dec 10, 2024
2 parents e44232e + cebac34 commit 15b297e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 19 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.10.0] - 2024-12-10

### Added

- Add Mimir Alertmanager datasource
- Add Mimir Alertmanager datasource.
- Add tenant ids field to the grafana organization CR to be able to support multiple tenants into one organization.

### Changed

- Removed organization OwnerReference on grafana-user-values configmap, this fixes an issue where the configmap is removed when the last organization is deleted which prevent Grafana from starting.

### Fixed

- Fix grafana organization deletion

## [0.9.1] - 2024-11-21

### Fixed
Expand Down Expand Up @@ -181,7 +188,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initialize project and create heartbeat for the installation.

[Unreleased]: https://github.com/giantswarm/observability-operator/compare/v0.9.1...HEAD
[Unreleased]: https://github.com/giantswarm/observability-operator/compare/v0.10.0...HEAD
[0.10.0]: https://github.com/giantswarm/observability-operator/compare/v0.9.1...v0.10.0
[0.9.1]: https://github.com/giantswarm/observability-operator/compare/v0.9.0...v0.9.1
[0.9.0]: https://github.com/giantswarm/observability-operator/compare/v0.8.1...v0.9.0
[0.8.1]: https://github.com/giantswarm/observability-operator/compare/v0.8.0...v0.8.1
Expand Down
13 changes: 12 additions & 1 deletion api/v1alpha1/grafanaorganization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ type GrafanaOrganizationSpec struct {
DisplayName string `json:"displayName"`

// Access rules defines user permissions for interacting with the organization in Grafana.
RBAC *RBAC `json:"rbac,omitempty"`
RBAC *RBAC `json:"rbac"`

// Tenants is a list of tenants that are associated with the Grafana organization.
// +kubebuilder:example={"giantswarm"}
Tenants []TenantID `json:"tenants,omitempty"`
}

// TenantID is a unique identifier for a tenant. It must be lowercase.
// +kubebuilder:validation:Pattern="^[a-z]*$"
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=63
type TenantID string

// RBAC defines the RoleBasedAccessControl configuration for the Grafana organization.
// Each fields represents the mapping to a Grafana role:
//
Expand Down Expand Up @@ -62,6 +72,7 @@ type RBAC struct {
// GrafanaOrganizationStatus defines the observed state of GrafanaOrganization
type GrafanaOrganizationStatus struct {
// OrgID is the actual organisation ID in grafana.
// +optional
OrgID int64 `json:"orgID"`

// DataSources is a list of grafana data sources that are available to the Grafana organization.
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions config/crd/observability.giantswarm.io_grafanaorganizations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,22 @@ spec:
required:
- admins
type: object
tenants:
description: Tenants is a list of tenants that are associated with
the Grafana organization.
example:
- giantswarm
items:
description: TenantID is a unique identifier for a tenant. It must
be lowercase.
maxLength: 63
minLength: 1
pattern: ^[a-z]*$
type: string
type: array
required:
- displayName
- rbac
type: object
status:
description: GrafanaOrganizationStatus defines the observed state of GrafanaOrganization
Expand All @@ -104,8 +118,6 @@ spec:
description: OrgID is the actual organisation ID in grafana.
format: int64
type: integer
required:
- orgID
type: object
type: object
served: true
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/go-logr/logr v1.4.2
github.com/grafana/grafana-openapi-client-go v0.0.0-20241126111151-59d2d35e24eb
github.com/onsi/ginkgo/v2 v2.22.0
github.com/onsi/gomega v1.36.0
github.com/onsi/gomega v1.36.1
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.23
github.com/pkg/errors v0.9.1
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.78.2
Expand Down Expand Up @@ -80,7 +80,7 @@ require (
k8s.io/apiextensions-apiserver v0.31.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38 // indirect
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078
k8s.io/utils v0.0.0-20241210054802-24370beab758
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg=
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
github.com/onsi/gomega v1.36.0 h1:Pb12RlruUtj4XUuPUqeEWc6j5DkVVVA49Uf6YLfC95Y=
github.com/onsi/gomega v1.36.0/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/onsi/gomega v1.36.1 h1:bJDPBO7ibjxcbHMgSCoo4Yj18UWbKDlLwX1x9sybDcw=
github.com/onsi/gomega v1.36.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
Expand Down Expand Up @@ -760,8 +760,8 @@ k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38 h1:1dWzkmJrrprYvjGwh9kEUxmcUV/CtNU8QM7h1FLWQOo=
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38/go.mod h1:coRQXBK9NxO98XUv3ZD6AK3xzHCxV6+b7lrquKwaKzA=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4wbQ6cZIItJCSHno=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20241210054802-24370beab758 h1:sdbE21q2nlQtFh65saZY+rRM6x6aJJI8IUa1AmH/qa0=
k8s.io/utils v0.0.0-20241210054802-24370beab758/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
2 changes: 1 addition & 1 deletion helm/observability-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: observability-operator
description: The observability-operator manages the Giant Swarm observability platform.
home: https://github.com/giantswarm/observability-operator
icon: https://s.giantswarm.io/app-icons/giantswarm/1/light.svg
version: 0.9.1
version: 0.10.0
appVersion: 0.0.2
annotations:
application.giantswarm.io/team: "atlas"
Expand Down
11 changes: 9 additions & 2 deletions internal/controller/grafanaorganization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func SetupGrafanaOrganizationReconciler(mgr manager.Manager, conf config.Config)
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
GrafanaAPI: grafanaAPI,
AlertmanagerJob: alertmanager.New(conf),
AlertmanagerJob: alertmanager.New(conf, mgr.GetClient()),
}

err = r.SetupWithManager(mgr)
Expand Down Expand Up @@ -314,9 +314,16 @@ func (r GrafanaOrganizationReconciler) reconcileDelete(ctx context.Context, graf
return nil
}

// Delete organization in Grafana
var organization = grafana.Organization{
ID: grafanaOrganization.Status.OrgID,
Name: grafanaOrganization.Spec.DisplayName,
TenantID: grafanaOrganization.Name,
}

// Delete organization in Grafana if it exists
if grafanaOrganization.Status.OrgID > 0 {
err := grafana.DeleteByID(ctx, r.GrafanaAPI, grafanaOrganization.Status.OrgID)
err := grafana.DeleteOrganization(ctx, r.GrafanaAPI, organization)
if err != nil {
return errors.WithStack(err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ type configRequest struct {
AlertmanagerConfig string `yaml:"alertmanager_config"`
}

func New(conf pkgconfig.Config) Job {
func New(conf pkgconfig.Config, c client.Client) Job {
job := Job{
client: c,
alertmanagerURL: strings.TrimSuffix(conf.Monitoring.AlertmanagerURL, "/"),
alertmanagerSecret: client.ObjectKey{
Name: conf.Monitoring.AlertmanagerSecretName,
Expand Down
14 changes: 10 additions & 4 deletions pkg/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,22 @@ func UpdateOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI,
return &organization, nil
}

func DeleteByID(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, id int64) error {
func DeleteOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization Organization) error {
logger := log.FromContext(ctx)

logger.Info("deleting organization")
_, err := findByID(grafanaAPI, id)
_, err := findByID(grafanaAPI, organization.ID)
if err != nil {
logger.Error(err, fmt.Sprintf("failed to find organization with ID: %d", id))
if isNotFound(err) {
logger.Info("organization id was not found, skipping deletion")
// If the CR orgID does not exist in Grafana, then we create the organization
return nil
}
logger.Error(err, fmt.Sprintf("failed to find organization with ID: %d", organization.ID))
return errors.WithStack(err)
}

_, err = grafanaAPI.Orgs.DeleteOrgByID(id)
_, err = grafanaAPI.Orgs.DeleteOrgByID(organization.ID)
if err != nil {
logger.Error(err, "failed to delete organization")
return errors.WithStack(err)
Expand Down

0 comments on commit 15b297e

Please sign in to comment.