Skip to content

Commit

Permalink
Simplify adding SumRate to Grafana templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioifg94 committed Apr 28, 2022
1 parent f224c8b commit 9e3e092
Show file tree
Hide file tree
Showing 26 changed files with 89 additions and 133 deletions.
4 changes: 4 additions & 0 deletions controllers/apps/apimanager_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
grafanav1alpha1 "github.com/integr8ly/grafana-operator/v3/pkg/apis/integreatly/v1alpha1"
appsv1 "github.com/openshift/api/apps/v1"
configv1 "github.com/openshift/api/config/v1"
consolev1 "github.com/openshift/api/console/v1"
imagev1 "github.com/openshift/api/image/v1"
routev1 "github.com/openshift/api/route/v1"
Expand Down Expand Up @@ -113,6 +114,9 @@ var _ = BeforeSuite(func(done Done) {
err = grafanav1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = configv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Expand Down
1 change: 1 addition & 0 deletions doc/prometheusrules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [System App](system-app.yaml)
* [System Sidekiq](system-sidekiq.yaml)
* [3scale Kube State Metrics](threescale-kube-state-metrics.yaml)
* [3scale Kube State Metrics (Openshift <4.9)](threescale-kube-state-metrics-pre49.yaml)
* [Zync](zync.yaml)
* [Zync QUE](zync-que.yaml)

Expand Down
6 changes: 5 additions & 1 deletion pkg/3scale/amp/cmd/prometheusrules.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (

var prometheusRulesNamespace string

// Compatibility with Openshift <4.9
var compatPre49 bool

// templateCmd represents the template command
var prometheusRulesCmd = &cobra.Command{
Use: getPrometheusRulesUsage(),
Expand Down Expand Up @@ -62,7 +65,7 @@ func runPrometheusRulesCommand(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Factory %s not found", prName)
}

prometheusRulesObj := factory.PrometheusRule(prometheusRulesNamespace)
prometheusRulesObj := factory.PrometheusRule(compatPre49, prometheusRulesNamespace)

serializer := json.NewSerializerWithOptions(json.DefaultMetaFactory, nil, nil,
json.SerializerOptions{Yaml: true, Pretty: true, Strict: true})
Expand All @@ -71,6 +74,7 @@ func runPrometheusRulesCommand(cmd *cobra.Command, args []string) error {

func init() {
prometheusRulesCmd.PersistentFlags().StringVar(&prometheusRulesNamespace, "namespace", "", "Namespace to be used when generating the prometheus rules")
prometheusRulesCmd.PersistentFlags().BoolVar(&compatPre49, "compat", false, "Generate rules compatible with Openshift releases prior to 4.9")
prometheusRulesCmd.MarkFlagRequired("namespace")
rootCmd.AddCommand(prometheusRulesCmd)
}
21 changes: 10 additions & 11 deletions pkg/3scale/amp/component/apicast_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/3scale/3scale-operator/pkg/assets"
"github.com/3scale/3scale-operator/pkg/common"
"github.com/3scale/3scale-operator/pkg/helper"
)

func (apicast *Apicast) ApicastProductionPodMonitor() *monitoringv1.PodMonitor {
Expand Down Expand Up @@ -52,12 +51,12 @@ func (apicast *Apicast) ApicastStagingPodMonitor() *monitoringv1.PodMonitor {
}
}

func (apicast *Apicast) ApicastMainAppGrafanaDashboard(mutateTemplateData helper.TemplateDataMutation) *grafanav1alpha1.GrafanaDashboard {
data := mutateTemplateData(&struct {
Namespace string
func (apicast *Apicast) ApicastMainAppGrafanaDashboard(sumRate string) *grafanav1alpha1.GrafanaDashboard {
data := &struct {
Namespace, SumRate string
}{
apicast.Options.Namespace,
})
apicast.Options.Namespace, sumRate,
}

return &grafanav1alpha1.GrafanaDashboard{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -71,12 +70,12 @@ func (apicast *Apicast) ApicastMainAppGrafanaDashboard(mutateTemplateData helper
}
}

func (apicast *Apicast) ApicastServicesGrafanaDashboard(mutateTemplateData helper.TemplateDataMutation) *grafanav1alpha1.GrafanaDashboard {
data := mutateTemplateData(&struct {
Namespace string
func (apicast *Apicast) ApicastServicesGrafanaDashboard(sumRate string) *grafanav1alpha1.GrafanaDashboard {
data := &struct {
Namespace, SumRate string
}{
apicast.Options.Namespace,
})
apicast.Options.Namespace, sumRate,
}
return &grafanav1alpha1.GrafanaDashboard{
ObjectMeta: metav1.ObjectMeta{
Name: "apicast-services",
Expand Down
11 changes: 5 additions & 6 deletions pkg/3scale/amp/component/backend_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/3scale/3scale-operator/pkg/assets"
"github.com/3scale/3scale-operator/pkg/common"
"github.com/3scale/3scale-operator/pkg/helper"
"github.com/coreos/prometheus-operator/pkg/apis/monitoring"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
grafanav1alpha1 "github.com/integr8ly/grafana-operator/v3/pkg/apis/integreatly/v1alpha1"
Expand Down Expand Up @@ -51,12 +50,12 @@ func (backend *Backend) BackendWorkerPodMonitor() *monitoringv1.PodMonitor {
}
}

func (backend *Backend) BackendGrafanaDashboard(templateMutation helper.TemplateDataMutation) *grafanav1alpha1.GrafanaDashboard {
data := templateMutation(&struct {
Namespace string
func (backend *Backend) BackendGrafanaDashboard(sumRate string) *grafanav1alpha1.GrafanaDashboard {
data := &struct {
Namespace, SumRate string
}{
backend.Options.Namespace,
})
backend.Options.Namespace, sumRate,
}
return &grafanav1alpha1.GrafanaDashboard{
ObjectMeta: metav1.ObjectMeta{
Name: "backend",
Expand Down
21 changes: 10 additions & 11 deletions pkg/3scale/amp/component/generic_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (

"github.com/3scale/3scale-operator/pkg/assets"
"github.com/3scale/3scale-operator/pkg/common"
"github.com/3scale/3scale-operator/pkg/helper"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
grafanav1alpha1 "github.com/integr8ly/grafana-operator/v3/pkg/apis/integreatly/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

func KubernetesResourcesByNamespaceGrafanaDashboard(templateDataMutation helper.TemplateDataMutation, ns string, appLabel string) *grafanav1alpha1.GrafanaDashboard {
data := templateDataMutation(&struct {
Namespace string
func KubernetesResourcesByNamespaceGrafanaDashboard(sumRate, ns, appLabel string) *grafanav1alpha1.GrafanaDashboard {
data := &struct {
Namespace, SumRate string
}{
ns,
})
ns, sumRate,
}
return &grafanav1alpha1.GrafanaDashboard{
ObjectMeta: metav1.ObjectMeta{
Name: "kubernetes-resources-by-namespace",
Expand All @@ -33,12 +32,12 @@ func KubernetesResourcesByNamespaceGrafanaDashboard(templateDataMutation helper.
}
}

func KubernetesResourcesByPodGrafanaDashboard(templateDataMutation helper.TemplateDataMutation, ns string, appLabel string) *grafanav1alpha1.GrafanaDashboard {
data := templateDataMutation(&struct {
Namespace string
func KubernetesResourcesByPodGrafanaDashboard(sumRate, ns, appLabel string) *grafanav1alpha1.GrafanaDashboard {
data := &struct {
Namespace, SumRate string
}{
ns,
})
ns, sumRate,
}
return &grafanav1alpha1.GrafanaDashboard{
ObjectMeta: metav1.ObjectMeta{
Name: "kubernetes-resources-by-pod",
Expand Down
11 changes: 5 additions & 6 deletions pkg/3scale/amp/component/system_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/3scale/3scale-operator/pkg/assets"
"github.com/3scale/3scale-operator/pkg/common"
"github.com/3scale/3scale-operator/pkg/helper"
)

func (system *System) SystemSidekiqPodMonitor() *monitoringv1.PodMonitor {
Expand Down Expand Up @@ -79,12 +78,12 @@ func (system *System) SystemAppPodMonitor() *monitoringv1.PodMonitor {
}
}

func (system *System) SystemGrafanaDashboard(templateDataMutation helper.TemplateDataMutation) *grafanav1alpha1.GrafanaDashboard {
data := templateDataMutation(&struct {
Namespace string
func (system *System) SystemGrafanaDashboard(sumRate string) *grafanav1alpha1.GrafanaDashboard {
data := &struct {
Namespace, SumRate string
}{
system.Options.Namespace,
})
system.Options.Namespace, sumRate,
}
return &grafanav1alpha1.GrafanaDashboard{
ObjectMeta: metav1.ObjectMeta{
Name: "system",
Expand Down
11 changes: 5 additions & 6 deletions pkg/3scale/amp/component/zync_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/3scale/3scale-operator/pkg/assets"
"github.com/3scale/3scale-operator/pkg/common"
"github.com/3scale/3scale-operator/pkg/helper"
)

func (zync *Zync) ZyncPodMonitor() *monitoringv1.PodMonitor {
Expand Down Expand Up @@ -52,12 +51,12 @@ func (zync *Zync) ZyncQuePodMonitor() *monitoringv1.PodMonitor {
}
}

func (zync *Zync) ZyncGrafanaDashboard(templateDataMutation helper.TemplateDataMutation) *grafanav1alpha1.GrafanaDashboard {
data := templateDataMutation(&struct {
Namespace string
func (zync *Zync) ZyncGrafanaDashboard(sumRate string) *grafanav1alpha1.GrafanaDashboard {
data := &struct {
Namespace, SumRate string
}{
zync.Options.Namespace,
})
zync.Options.Namespace, sumRate,
}
return &grafanav1alpha1.GrafanaDashboard{
ObjectMeta: metav1.ObjectMeta{
Name: "zync",
Expand Down
6 changes: 3 additions & 3 deletions pkg/3scale/amp/operator/apicast_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ func (r *ApicastReconciler) Reconcile() (reconcile.Result, error) {
return reconcile.Result{}, err
}

grafanaTemplateDataMutation, err := helper.SumRateTemplateDataMutation(r.Context(), r.Client())
sumRate, err := helper.SumRateForOpenshiftVersion(r.Context(), r.Client())
if err != nil {
return reconcile.Result{}, err
}

err = r.ReconcileGrafanaDashboard(apicast.ApicastMainAppGrafanaDashboard(grafanaTemplateDataMutation), reconcilers.GenericGrafanaDashboardsMutator)
err = r.ReconcileGrafanaDashboard(apicast.ApicastMainAppGrafanaDashboard(sumRate), reconcilers.GenericGrafanaDashboardsMutator)
if err != nil {
return reconcile.Result{}, err
}

err = r.ReconcileGrafanaDashboard(apicast.ApicastServicesGrafanaDashboard(grafanaTemplateDataMutation), reconcilers.GenericGrafanaDashboardsMutator)
err = r.ReconcileGrafanaDashboard(apicast.ApicastServicesGrafanaDashboard(sumRate), reconcilers.GenericGrafanaDashboardsMutator)
if err != nil {
return reconcile.Result{}, err
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/3scale/amp/operator/apicast_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
grafanav1alpha1 "github.com/integr8ly/grafana-operator/v3/pkg/apis/integreatly/v1alpha1"
appsv1 "github.com/openshift/api/apps/v1"
configv1 "github.com/openshift/api/config/v1"
imagev1 "github.com/openshift/api/image/v1"
routev1 "github.com/openshift/api/route/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -89,6 +90,9 @@ func TestApicastReconciler(t *testing.T) {
if err := grafanav1alpha1.AddToScheme(s); err != nil {
t.Fatal(err)
}
if err := configv1.AddToScheme(s); err != nil {
t.Fatal(err)
}

// Create a fake client to mock API calls.
cl := fake.NewFakeClient(objs...)
Expand Down Expand Up @@ -253,6 +257,9 @@ func TestApicastReconcilerCustomPolicyParts(t *testing.T) {
if err := grafanav1alpha1.AddToScheme(s); err != nil {
t.Fatal(err)
}
if err := configv1.AddToScheme(s); err != nil {
t.Fatal(err)
}

// Create a fake client to mock API calls.
cl := fake.NewFakeClient(objs...)
Expand Down Expand Up @@ -465,6 +472,9 @@ func TestApicastReconcilerTracingConfigParts(t *testing.T) {
if err := grafanav1alpha1.AddToScheme(s); err != nil {
t.Fatal(err)
}
if err := configv1.AddToScheme(s); err != nil {
t.Fatal(err)
}

// Create a fake client to mock API calls.
cl := fake.NewFakeClient(objs...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/3scale/amp/operator/backend_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func (r *BackendReconciler) Reconcile() (reconcile.Result, error) {
return reconcile.Result{}, err
}

grafanaTemplateMutation, err := helper.SumRateTemplateDataMutation(r.Context(), r.Client())
sumRate, err := helper.SumRateForOpenshiftVersion(r.Context(), r.Client())
if err != nil {
return reconcile.Result{}, err
}

err = r.ReconcileGrafanaDashboard(backend.BackendGrafanaDashboard(grafanaTemplateMutation), reconcilers.GenericGrafanaDashboardsMutator)
err = r.ReconcileGrafanaDashboard(backend.BackendGrafanaDashboard(sumRate), reconcilers.GenericGrafanaDashboardsMutator)
if err != nil {
return reconcile.Result{}, err
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/3scale/amp/operator/generic_monitoring_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,13 @@ func (r *GenericMonitoringReconciler) Reconcile() (reconcile.Result, error) {
return reconcile.Result{}, err
}

grafanaTemplateDataMutation := helper.AddSumRateField(sumRate)
if err != nil {
return reconcile.Result{}, err
}

grafanaDashboard := component.KubernetesResourcesByNamespaceGrafanaDashboard(grafanaTemplateDataMutation, r.apiManager.Namespace, *r.apiManager.Spec.AppLabel)
grafanaDashboard := component.KubernetesResourcesByNamespaceGrafanaDashboard(sumRate, r.apiManager.Namespace, *r.apiManager.Spec.AppLabel)
err = r.ReconcileGrafanaDashboard(grafanaDashboard, reconcilers.GenericGrafanaDashboardsMutator)
if err != nil {
return reconcile.Result{}, err
}

grafanaDashboard = component.KubernetesResourcesByPodGrafanaDashboard(grafanaTemplateDataMutation, r.apiManager.Namespace, *r.apiManager.Spec.AppLabel)
grafanaDashboard = component.KubernetesResourcesByPodGrafanaDashboard(sumRate, r.apiManager.Namespace, *r.apiManager.Spec.AppLabel)
err = r.ReconcileGrafanaDashboard(grafanaDashboard, reconcilers.GenericGrafanaDashboardsMutator)
if err != nil {
return reconcile.Result{}, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/3scale/amp/operator/system_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ func (r *SystemReconciler) Reconcile() (reconcile.Result, error) {
return reconcile.Result{}, err
}

grafanaTemplateDataMutation, err := helper.SumRateTemplateDataMutation(r.Context(), r.Client())
sumRate, err := helper.SumRateForOpenshiftVersion(r.Context(), r.Client())
if err != nil {
return reconcile.Result{}, err
}

err = r.ReconcileGrafanaDashboard(system.SystemGrafanaDashboard(grafanaTemplateDataMutation), reconcilers.GenericGrafanaDashboardsMutator)
err = r.ReconcileGrafanaDashboard(system.SystemGrafanaDashboard(sumRate), reconcilers.GenericGrafanaDashboardsMutator)
if err != nil {
return reconcile.Result{}, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/3scale/amp/operator/system_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
grafanav1alpha1 "github.com/integr8ly/grafana-operator/v3/pkg/apis/integreatly/v1alpha1"
appsv1 "github.com/openshift/api/apps/v1"
configv1 "github.com/openshift/api/config/v1"
imagev1 "github.com/openshift/api/image/v1"
routev1 "github.com/openshift/api/route/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -54,6 +55,9 @@ func TestSystemReconcilerCreate(t *testing.T) {
if err := grafanav1alpha1.AddToScheme(s); err != nil {
t.Fatal(err)
}
if err := configv1.AddToScheme(s); err != nil {
t.Fatal(err)
}

// Create a fake client to mock API calls.
cl := fake.NewFakeClient(objs...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/3scale/amp/operator/zync_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ func (r *ZyncReconciler) Reconcile() (reconcile.Result, error) {
return reconcile.Result{}, err
}

templateDataMutation, err := helper.SumRateTemplateDataMutation(r.Context(), r.Client())
sumRate, err := helper.SumRateForOpenshiftVersion(r.Context(), r.Client())
if err != nil {
return reconcile.Result{}, err
}

err = r.ReconcileGrafanaDashboard(zync.ZyncGrafanaDashboard(templateDataMutation), reconcilers.GenericGrafanaDashboardsMutator)
err = r.ReconcileGrafanaDashboard(zync.ZyncGrafanaDashboard(sumRate), reconcilers.GenericGrafanaDashboardsMutator)
if err != nil {
return reconcile.Result{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/3scale/amp/prometheusrules/apicast_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (b *ApicastPrometheusRuleFactory) Type() string {
return "apicast"
}

func (b *ApicastPrometheusRuleFactory) PrometheusRule(ns string) *monitoringv1.PrometheusRule {
func (b *ApicastPrometheusRuleFactory) PrometheusRule(_ bool, ns string) *monitoringv1.PrometheusRule {
options, err := apicastOptions(ns)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/3scale/amp/prometheusrules/backend_listener_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (b *BackendListenerPrometheusRuleFactory) Type() string {
return "backend-listener"
}

func (b *BackendListenerPrometheusRuleFactory) PrometheusRule(ns string) *monitoringv1.PrometheusRule {
func (b *BackendListenerPrometheusRuleFactory) PrometheusRule(_ bool, ns string) *monitoringv1.PrometheusRule {
options, err := backendOptions(ns)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/3scale/amp/prometheusrules/backend_worker_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (b *BackendWorkerPrometheusRuleFactory) Type() string {
return "backend-worker"
}

func (b *BackendWorkerPrometheusRuleFactory) PrometheusRule(ns string) *monitoringv1.PrometheusRule {
func (b *BackendWorkerPrometheusRuleFactory) PrometheusRule(_ bool, ns string) *monitoringv1.PrometheusRule {
options, err := backendOptions(ns)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/3scale/amp/prometheusrules/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type PrometheusRuleFactory interface {
PrometheusRule(ns string) *monitoringv1.PrometheusRule
PrometheusRule(compatPre49 bool, ns string) *monitoringv1.PrometheusRule
Type() string
}

Expand Down
Loading

0 comments on commit 9e3e092

Please sign in to comment.