Skip to content

Commit

Permalink
monitoring and validations packages to fix import cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
thbkrkr committed Jul 7, 2021
1 parent b1bce09 commit 86ba8a2
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 62 deletions.
4 changes: 2 additions & 2 deletions pkg/apis/kibana/v1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"

commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/validations"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/version"
ulog "github.com/elastic/cloud-on-k8s/pkg/utils/log"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -108,5 +108,5 @@ func checkNoDowngrade(prev, curr *Kibana) field.ErrorList {
}

func checkMonitoring(k *Kibana) field.ErrorList {
return stackmon.Validate(k, k.Spec.Version)
return validations.Validate(k, k.Spec.Version)
}
3 changes: 2 additions & 1 deletion pkg/controller/common/stackmon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"text/template"

"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -36,7 +37,7 @@ type beatConfig struct {
volumes []volume.VolumeLike
}

func newBeatConfig(client k8s.Client, beatName string, resource HasMonitoring, associations []commonv1.Association, baseConfig string) (beatConfig, error) {
func newBeatConfig(client k8s.Client, beatName string, resource monitoring.HasMonitoring, associations []commonv1.Association, baseConfig string) (beatConfig, error) {
if len(associations) != 1 {
// should never happen because of the pre-creation validation
return beatConfig{}, errors.New("only one Elasticsearch reference is supported for Stack Monitoring")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package stackmon
package monitoring

import (
commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
)

// HasMonitoring is the interface implemented by an Elastic Stack application that supports Stack Monitoring ()
Expand All @@ -24,14 +25,14 @@ func IsMonitoringDefined(resource HasMonitoring) bool {
}

func IsMonitoringMetricsDefined(resource HasMonitoring) bool {
return areEsRefsDefined(resource.GetMonitoringMetricsRefs())
return AreEsRefsDefined(resource.GetMonitoringMetricsRefs())
}

func IsMonitoringLogsDefined(resource HasMonitoring) bool {
return areEsRefsDefined(resource.GetMonitoringLogsRefs())
return AreEsRefsDefined(resource.GetMonitoringLogsRefs())
}

func areEsRefsDefined(esRefs []commonv1.ObjectSelector) bool {
func AreEsRefsDefined(esRefs []commonv1.ObjectSelector) bool {
for _, ref := range esRefs {
if !ref.IsDefined() {
return false
Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/common/stackmon/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -45,16 +46,16 @@ func TestCAVolumeName(t *testing.T) {
},
}

name := caVolumeName(GetMonitoringMetricsAssociation(&es)[0])
name := caVolumeName(monitoring.GetMonitoringMetricsAssociation(&es)[0])
assert.LessOrEqual(t, len(name), maxVolumeNameLength)
assert.Equal(t, "es-monitoring-954c60-ca", name)

name = caVolumeName(GetMonitoringLogsAssociation(&es)[0])
name = caVolumeName(monitoring.GetMonitoringLogsAssociation(&es)[0])
assert.LessOrEqual(t, len(name), maxVolumeNameLength)
assert.Equal(t, "es-monitoring-954c60-ca", name)

es.Spec.Monitoring.Logs.ElasticsearchRefs[0].Name = "another-name"
newName := caVolumeName(GetMonitoringLogsAssociation(&es)[0])
newName := caVolumeName(monitoring.GetMonitoringLogsAssociation(&es)[0])
assert.NotEqual(t, name, newName)
assert.Equal(t, "es-monitoring-ae0f57-ca", newName)
}
11 changes: 6 additions & 5 deletions pkg/controller/common/stackmon/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package stackmon
import (
"hash"

"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"

Expand All @@ -21,7 +22,7 @@ import (
func NewMetricBeatSidecar(
client k8s.Client,
associationType commonv1.AssociationType,
resource HasMonitoring,
resource monitoring.HasMonitoring,
version string,
esNsn types.NamespacedName,
baseConfigTemplate string,
Expand All @@ -43,12 +44,12 @@ func NewMetricBeatSidecar(
return BeatSidecar{}, err
}
image := container.ImageRepository(container.MetricbeatImage, version)
return NewBeatSidecar(client, "metricbeat", image, resource, GetMonitoringMetricsAssociation(resource), baseConfig, sourceCaVolume)
return NewBeatSidecar(client, "metricbeat", image, resource, monitoring.GetMonitoringMetricsAssociation(resource), baseConfig, sourceCaVolume)
}

func NewFileBeatSidecar(client k8s.Client, resource HasMonitoring, version string, baseConfig string, additionalVolume volume.VolumeLike) (BeatSidecar, error) {
func NewFileBeatSidecar(client k8s.Client, resource monitoring.HasMonitoring, version string, baseConfig string, additionalVolume volume.VolumeLike) (BeatSidecar, error) {
image := container.ImageRepository(container.FilebeatImage, version)
return NewBeatSidecar(client, "filebeat", image, resource, GetMonitoringLogsAssociation(resource), baseConfig, additionalVolume)
return NewBeatSidecar(client, "filebeat", image, resource, monitoring.GetMonitoringLogsAssociation(resource), baseConfig, additionalVolume)
}

// BeatSidecar helps with building a beat sidecar container to monitor an Elastic Stack application. It focuses on
Expand All @@ -60,7 +61,7 @@ type BeatSidecar struct {
Volumes []corev1.Volume
}

func NewBeatSidecar(client k8s.Client, beatName string, image string, resource HasMonitoring,
func NewBeatSidecar(client k8s.Client, beatName string, image string, resource monitoring.HasMonitoring,
associations []commonv1.Association, baseConfig string, additionalVolume volume.VolumeLike,
) (BeatSidecar, error) {
// build the beat config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package stackmon
package validations

import (
"fmt"

"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/version"
"k8s.io/apimachinery/pkg/util/validation/field"
)
Expand All @@ -25,22 +26,22 @@ var (

// Validate validates that the resource version is supported for Stack Monitoring and that there is exactly one
// Elasticsearch reference defined to send monitoring data when Stack Monitoring is defined
func Validate(resource HasMonitoring, version string) field.ErrorList {
func Validate(resource monitoring.HasMonitoring, version string) field.ErrorList {
var errs field.ErrorList
if IsMonitoringDefined(resource) {
if monitoring.IsMonitoringDefined(resource) {
err := IsSupportedVersion(version)
if err != nil {
errs = append(errs, field.Invalid(field.NewPath("spec").Child("version"), version,
fmt.Sprintf(unsupportedVersionForStackMonitoringMsg, MinStackVersion)))
}
}
refs := resource.GetMonitoringMetricsRefs()
if areEsRefsDefined(refs) && len(refs) != 1 {
if monitoring.AreEsRefsDefined(refs) && len(refs) != 1 {
errs = append(errs, field.Invalid(field.NewPath("spec").Child("monitoring").Child("metrics").Child("elasticsearchRefs"),
refs, fmt.Sprintf(invalidStackMonitoringElasticsearchRefsMsg, "Metrics")))
}
refs = resource.GetMonitoringLogsRefs()
if areEsRefsDefined(refs) && len(refs) != 1 {
if monitoring.AreEsRefsDefined(refs) && len(refs) != 1 {
errs = append(errs, field.Invalid(field.NewPath("spec").Child("monitoring").Child("logs").Child("elasticsearchRefs"),
refs, fmt.Sprintf(invalidStackMonitoringElasticsearchRefsMsg, "Logs")))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package stackmon
package validations

import (
"testing"
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/elasticsearch/stackmon/beat_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/reconciler"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
)

Expand All @@ -25,7 +25,7 @@ var (

// ReconcileConfigSecrets reconciles the secrets holding beats configuration
func ReconcileConfigSecrets(client k8s.Client, es esv1.Elasticsearch) error {
if stackmon.IsMonitoringMetricsDefined(&es) {
if monitoring.IsMonitoringMetricsDefined(&es) {
b, err := Metricbeat(client, es)
if err != nil {
return err
Expand All @@ -36,7 +36,7 @@ func ReconcileConfigSecrets(client k8s.Client, es esv1.Elasticsearch) error {
}
}

if stackmon.IsMonitoringLogsDefined(&es) {
if monitoring.IsMonitoringLogsDefined(&es) {
b, err := Filebeat(client, es)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/elasticsearch/stackmon/es_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
package stackmon

import (
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
corev1 "k8s.io/api/core/v1"

commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon"
)

// MonitoringConfig returns the Elasticsearch settings to enable the collection of monitoring data
func MonitoringConfig(es esv1.Elasticsearch) commonv1.Config {
if !stackmon.IsMonitoringMetricsDefined(&es) {
if !monitoring.IsMonitoringMetricsDefined(&es) {
return commonv1.Config{}
}
return commonv1.Config{Data: map[string]interface{}{
Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/elasticsearch/stackmon/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/sha256"
"fmt"

"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
corev1 "k8s.io/api/core/v1"

commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
Expand Down Expand Up @@ -56,14 +57,14 @@ func Filebeat(client k8s.Client, es esv1.Elasticsearch) (stackmon.BeatSidecar, e
// in the Elasticsearch pod and injects the volumes for the beat configurations and the ES CA certificates.
func WithMonitoring(client k8s.Client, builder *defaults.PodTemplateBuilder, es esv1.Elasticsearch) (*defaults.PodTemplateBuilder, error) {
// no monitoring defined, skip
if !stackmon.IsMonitoringDefined(&es) {
if !monitoring.IsMonitoringDefined(&es) {
return builder, nil
}

configHash := sha256.New224()
volumes := make([]corev1.Volume, 0)

if stackmon.IsMonitoringMetricsDefined(&es) {
if monitoring.IsMonitoringMetricsDefined(&es) {
b, err := Metricbeat(client, es)
if err != nil {
return nil, err
Expand All @@ -74,7 +75,7 @@ func WithMonitoring(client k8s.Client, builder *defaults.PodTemplateBuilder, es
configHash.Write(b.ConfigHash.Sum(nil))
}

if stackmon.IsMonitoringLogsDefined(&es) {
if monitoring.IsMonitoringLogsDefined(&es) {
// enable Stack logging to write Elasticsearch logs to disk
builder.WithEnv(fileLogStyleEnvVar())

Expand Down
18 changes: 9 additions & 9 deletions pkg/controller/elasticsearch/stackmon/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ package stackmon
import (
"testing"

"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/defaults"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon"
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
)

Expand Down Expand Up @@ -85,7 +85,7 @@ func TestWithMonitoring(t *testing.T) {
name: "with metrics monitoring",
es: func() esv1.Elasticsearch {
sampleEs.Spec.Monitoring.Metrics.ElasticsearchRefs = monitoringEsRef
stackmon.GetMonitoringMetricsAssociation(&sampleEs)[0].SetAssociationConf(&monitoringAssocConf)
monitoring.GetMonitoringMetricsAssociation(&sampleEs)[0].SetAssociationConf(&monitoringAssocConf)
return sampleEs
},
containersLength: 2,
Expand All @@ -98,7 +98,7 @@ func TestWithMonitoring(t *testing.T) {
es: func() esv1.Elasticsearch {
sampleEs.Spec.Monitoring.Metrics.ElasticsearchRefs = nil
sampleEs.Spec.Monitoring.Logs.ElasticsearchRefs = monitoringEsRef
stackmon.GetMonitoringLogsAssociation(&sampleEs)[0].SetAssociationConf(&monitoringAssocConf)
monitoring.GetMonitoringLogsAssociation(&sampleEs)[0].SetAssociationConf(&monitoringAssocConf)
return sampleEs
},
containersLength: 2,
Expand All @@ -110,9 +110,9 @@ func TestWithMonitoring(t *testing.T) {
name: "with metrics and logs monitoring",
es: func() esv1.Elasticsearch {
sampleEs.Spec.Monitoring.Metrics.ElasticsearchRefs = monitoringEsRef
stackmon.GetMonitoringMetricsAssociation(&sampleEs)[0].SetAssociationConf(&monitoringAssocConf)
monitoring.GetMonitoringMetricsAssociation(&sampleEs)[0].SetAssociationConf(&monitoringAssocConf)
sampleEs.Spec.Monitoring.Logs.ElasticsearchRefs = monitoringEsRef
stackmon.GetMonitoringLogsAssociation(&sampleEs)[0].SetAssociationConf(&logsAssocConf)
monitoring.GetMonitoringLogsAssociation(&sampleEs)[0].SetAssociationConf(&logsAssocConf)
return sampleEs
},
containersLength: 3,
Expand All @@ -124,9 +124,9 @@ func TestWithMonitoring(t *testing.T) {
name: "with metrics and logs monitoring with different es ref",
es: func() esv1.Elasticsearch {
sampleEs.Spec.Monitoring.Metrics.ElasticsearchRefs = monitoringEsRef
stackmon.GetMonitoringMetricsAssociation(&sampleEs)[0].SetAssociationConf(&monitoringAssocConf)
monitoring.GetMonitoringMetricsAssociation(&sampleEs)[0].SetAssociationConf(&monitoringAssocConf)
sampleEs.Spec.Monitoring.Logs.ElasticsearchRefs = logsEsRef
stackmon.GetMonitoringLogsAssociation(&sampleEs)[0].SetAssociationConf(&logsAssocConf)
monitoring.GetMonitoringLogsAssociation(&sampleEs)[0].SetAssociationConf(&logsAssocConf)
return sampleEs
},
containersLength: 3,
Expand All @@ -147,14 +147,14 @@ func TestWithMonitoring(t *testing.T) {
assert.Equal(t, tc.esEnvVarsLength, len(builder.PodTemplate.Spec.Containers[0].Env))
assert.Equal(t, tc.podVolumesLength, len(builder.PodTemplate.Spec.Volumes))

if stackmon.IsMonitoringMetricsDefined(&es) {
if monitoring.IsMonitoringMetricsDefined(&es) {
for _, c := range builder.PodTemplate.Spec.Containers {
if c.Name == "metricbeat" {
assert.Equal(t, tc.beatVolumeMountsLength, len(c.VolumeMounts))
}
}
}
if stackmon.IsMonitoringLogsDefined(&es) {
if monitoring.IsMonitoringLogsDefined(&es) {
for _, c := range builder.PodTemplate.Spec.Containers {
if c.Name == "filebeat" {
assert.Equal(t, tc.beatVolumeMountsLength, len(c.VolumeMounts))
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/elasticsearch/validation/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon"
stackmon "github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/validations"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/version"
esversion "github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/version"
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/kibana/stackmon/beat_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

kbv1 "github.com/elastic/cloud-on-k8s/pkg/apis/kibana/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/reconciler"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/stackmon/monitoring"
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
)

Expand All @@ -25,7 +25,7 @@ var (

// ReconcileConfigSecrets reconciles the secrets holding beats configuration
func ReconcileConfigSecrets(client k8s.Client, kb kbv1.Kibana) error {
if stackmon.IsMonitoringMetricsDefined(&kb) {
if monitoring.IsMonitoringMetricsDefined(&kb) {
b, err := Metricbeat(client, kb)
if err != nil {
return err
Expand All @@ -36,7 +36,7 @@ func ReconcileConfigSecrets(client k8s.Client, kb kbv1.Kibana) error {
}
}

if stackmon.IsMonitoringLogsDefined(&kb) {
if monitoring.IsMonitoringLogsDefined(&kb) {
b, err := Filebeat(client, kb)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 86ba8a2

Please sign in to comment.