Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Stack Monitoring] Elasticsearch monitoring with Metricbeat and Filebeat as sidecars #4528

Merged
merged 86 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
2c1619a
Stack monitoring for Elasticsearch
thbkrkr May 27, 2021
e621963
Regenerate
thbkrkr May 27, 2021
1839614
Model with only one associationi es->es
thbkrkr May 31, 2021
cd1e21e
Regenerate
thbkrkr May 31, 2021
cc931cd
Restore hash-based annotation name
thbkrkr Jun 1, 2021
e018707
Simple stack monitoring E2E test
thbkrkr Jun 3, 2021
a27f910
Move beats config in embedded files
thbkrkr Jun 3, 2021
b298efc
Renaming
thbkrkr Jun 3, 2021
29e3cd9
Typo
thbkrkr Jun 3, 2021
964bedd
Revert bad replace all
thbkrkr Jun 3, 2021
9a2a5e3
Avoid to use configmap subpath
thbkrkr Jun 3, 2021
3f5d8ee
containerImage func godoc
thbkrkr Jun 3, 2021
064fd50
Introduce WithContainers to avoid weirdness with container defaulter
thbkrkr Jun 7, 2021
76f4f27
Wording
thbkrkr Jun 7, 2021
9f16362
Remove unused code
thbkrkr Jun 7, 2021
27ab36e
Sort imports
thbkrkr Jun 7, 2021
02400fd
Reject unsupported version
thbkrkr Jun 8, 2021
fc4c782
Naming
thbkrkr Jun 8, 2021
d3d97ab
Add stack mon doc link in es spec doc
thbkrkr Jun 8, 2021
0a7528d
Typo
thbkrkr Jun 8, 2021
069eace
Merge remote-tracking branch 'upstream/master' into stack-monitoring
thbkrkr Jun 8, 2021
75fcde3
go fmt
thbkrkr Jun 8, 2021
dfe83f5
doc
thbkrkr Jun 8, 2021
ac6d4f9
Dedicated role
thbkrkr Jun 8, 2021
ff98c86
Update Test_aggregateRoles
thbkrkr Jun 10, 2021
d8b8fc0
Merge remote-tracking branch 'upstream/master' into stack-monitoring
thbkrkr Jun 16, 2021
450492c
Renaming
thbkrkr Jun 16, 2021
e01836d
End of revert about configmap subpath
thbkrkr Jun 16, 2021
46ff83a
Move ES monitoring config in stackmon package
thbkrkr Jun 16, 2021
b3fcdc6
Review's input
thbkrkr Jun 16, 2021
337e96e
Generate
thbkrkr Jun 16, 2021
4abcc0c
Revert
thbkrkr Jun 16, 2021
cc48fd1
Fix monitoring config
thbkrkr Jun 17, 2021
074d760
Add unit tests
thbkrkr Jun 17, 2021
9346b4a
Use GetCASecretName()
thbkrkr Jun 17, 2021
6c96979
lint
thbkrkr Jun 17, 2021
4d62d07
Move warning comment
thbkrkr Jun 18, 2021
b2cfbce
Unit tests for fullContainerImage
thbkrkr Jun 18, 2021
ce855c0
Future proof elasticsearchRefs for Elastic Agent
thbkrkr Jun 21, 2021
86e9fbd
Add kubebuilder annotations
thbkrkr Jun 22, 2021
9a81c1c
Adjust association name
thbkrkr Jun 22, 2021
3fb22c4
Refactoring validation
thbkrkr Jun 22, 2021
6b9c2d3
Cleaning beat yaml config
thbkrkr Jun 22, 2021
482784a
Refactoring beat config in secrets
thbkrkr Jun 22, 2021
2d1c67e
godoc
thbkrkr Jun 23, 2021
a2fd5d2
Put monitoringXxxEnvVars funcs side by side
thbkrkr Jun 23, 2021
048f8e4
Update descriptions for API doc
thbkrkr Jun 23, 2021
7b249bd
Apply review's input for API doc
thbkrkr Jun 23, 2021
eb9de17
Run generate
thbkrkr Jun 23, 2021
541ab64
Fix receiver name
thbkrkr Jun 24, 2021
175bcf3
Factorize ElasticsearchConfigAnnotationName shared with agent
thbkrkr Jun 24, 2021
ac8cbb2
Adjust association type and labels
thbkrkr Jun 24, 2021
a52d1f1
Typo and new line
thbkrkr Jun 24, 2021
e52d785
Decouple beat_config.go and es_config.go
thbkrkr Jun 24, 2021
a9b4e49
Adjust TargetEsCaCertMountPathFormat
thbkrkr Jun 24, 2021
ffa0aa0
Reuse IsStackMonitoringDefined
thbkrkr Jun 24, 2021
6218a47
Move and rename fileLogStyleEnvVar
thbkrkr Jun 24, 2021
170f106
Adjust comments in filebeat.yml
thbkrkr Jun 24, 2021
00042a6
Document TestEsStackMonitoring
thbkrkr Jun 24, 2021
f6975f3
Add CheckBeatSidecars to TestESStackMonitoring
thbkrkr Jun 24, 2021
cf5c596
Revert fullContainerImage
thbkrkr Jun 24, 2021
742fe9f
Unit tests for Validate
thbkrkr Jun 24, 2021
d45f5a2
Unit tests for WithMonitoring
thbkrkr Jun 24, 2021
05dcd97
Remove useless health=green query param
thbkrkr Jun 24, 2021
12f3bd4
Unit test TestPodTemplateBuilder_WithContainers
thbkrkr Jun 24, 2021
8e72f7c
Support container override with pod template
thbkrkr Jun 24, 2021
2f5b796
common/stackmon package
thbkrkr Jul 1, 2021
a9584a2
ES impl for HasMonitoring
thbkrkr Jul 1, 2021
e60c0d9
Refactoring of elasticsearch/stackmon
thbkrkr Jul 1, 2021
7737cef
Remove redundant with_monitoring unit tests with container_test.go
thbkrkr Jul 1, 2021
5b92f6d
Dedicated es user to collect metrics
thbkrkr Jul 1, 2021
f1139cc
Adjust dedicated monitoring role
thbkrkr Jul 1, 2021
6e788c8
Adjust unit tests following the addition of user elastic-internal-mon…
thbkrkr Jul 1, 2021
f531e93
Use 7.14.0-SNAPSHOT as MinStackVersion
thbkrkr Jul 5, 2021
1b035c9
BeatSidecar constructor and public fields
thbkrkr Jul 5, 2021
f1a45c9
Simplify HasMonitoring interface with metav1.Object
thbkrkr Jul 5, 2021
324bb05
Files renaming
thbkrkr Jul 5, 2021
d7b7b13
indentation
thbkrkr Jul 5, 2021
07c1bdd
Accumulate validation errors
thbkrkr Jul 5, 2021
718eed6
Remove redundant code
thbkrkr Jul 5, 2021
b49530f
Update ES secrets check in E2E tests
thbkrkr Jul 5, 2021
f4759fd
Fix FilebeatImage
thbkrkr Jul 5, 2021
c26faa5
Better control of volume names
thbkrkr Jul 5, 2021
fc508c8
Merge remote-tracking branch 'upstream/master' into stack-monitoring
thbkrkr Jul 5, 2021
31c2465
Run generate
thbkrkr Jul 5, 2021
ccd7e12
Update E2E test Kibana CheckStatus
thbkrkr Jul 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func registerControllers(mgr manager.Manager, params operator.Parameters, access
{name: "BEAT-KB", registerFunc: associationctl.AddBeatKibana},
{name: "AGENT-ES", registerFunc: associationctl.AddAgentES},
{name: "EMS-ES", registerFunc: associationctl.AddMapsES},
{name: "ES-ES", registerFunc: associationctl.AddEsEs},
{name: "ES-MONITORING", registerFunc: associationctl.AddEsMonitoring},
}

for _, c := range assocControllers {
Expand Down
6 changes: 3 additions & 3 deletions config/crds/all-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2167,9 +2167,9 @@ spec:
description: Image is the Elasticsearch Docker image to deploy.
type: string
monitoring:
description: Monitoring enables you to extract log and stack monitoring
description: Monitoring enables you to extract log and Stack Monitoring
metrics of this Elasticsearch cluster. Metricbeat and Filebeat are
deployed in the same pod as sidecar and each one send data to one
deployed in the same Pod as sidecars and each one send data to one
or two different monitoring Elasticsearch clusters running in the
same Kubernetes cluster.
properties:
Expand Down Expand Up @@ -2203,7 +2203,7 @@ spec:
elasticsearchRef:
description: ElasticsearchRef is a reference to a monitoring
Elasticsearch cluster running in the same Kubernetes cluster
dedicated to receiving stack monitoring metrics.
dedicated to receiving Stack Monitoring metrics.
properties:
name:
description: Name of the Kubernetes object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ spec:
description: Image is the Elasticsearch Docker image to deploy.
type: string
monitoring:
description: Monitoring enables you to extract log and stack monitoring metrics of this Elasticsearch cluster. Metricbeat and Filebeat are deployed in the same pod as sidecar and each one send data to one or two different monitoring Elasticsearch clusters running in the same Kubernetes cluster.
description: Monitoring enables you to extract log and Stack Monitoring metrics of this Elasticsearch cluster. Metricbeat and Filebeat are deployed in the same Pod as sidecars and each one send data to one or two different monitoring Elasticsearch clusters running in the same Kubernetes cluster.
properties:
logs:
properties:
Expand All @@ -266,7 +266,7 @@ spec:
metrics:
properties:
elasticsearchRef:
description: ElasticsearchRef is a reference to a monitoring Elasticsearch cluster running in the same Kubernetes cluster dedicated to receiving stack monitoring metrics.
description: ElasticsearchRef is a reference to a monitoring Elasticsearch cluster running in the same Kubernetes cluster dedicated to receiving Stack Monitoring metrics.
properties:
name:
description: Name of the Kubernetes object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2197,9 +2197,9 @@ spec:
description: Image is the Elasticsearch Docker image to deploy.
type: string
monitoring:
description: Monitoring enables you to extract log and stack monitoring
description: Monitoring enables you to extract log and Stack Monitoring
metrics of this Elasticsearch cluster. Metricbeat and Filebeat are
deployed in the same pod as sidecar and each one send data to one
deployed in the same Pod as sidecars and each one send data to one
or two different monitoring Elasticsearch clusters running in the
same Kubernetes cluster.
properties:
Expand Down Expand Up @@ -2233,7 +2233,7 @@ spec:
elasticsearchRef:
description: ElasticsearchRef is a reference to a monitoring
Elasticsearch cluster running in the same Kubernetes cluster
dedicated to receiving stack monitoring metrics.
dedicated to receiving Stack Monitoring metrics.
properties:
name:
description: Name of the Kubernetes object.
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/api-docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ ElasticsearchSpec holds the specification of an Elasticsearch cluster.
| *`serviceAccountName`* __string__ | ServiceAccountName is used to check access from the current resource to a resource (eg. a remote Elasticsearch cluster) in a different namespace. Can only be used if ECK is enforcing RBAC on references.
| *`remoteClusters`* __xref:{anchor_prefix}-github-com-elastic-cloud-on-k8s-pkg-apis-elasticsearch-v1-remotecluster[$$RemoteCluster$$] array__ | RemoteClusters enables you to establish uni-directional connections to a remote Elasticsearch cluster.
| *`volumeClaimDeletePolicy`* __xref:{anchor_prefix}-github-com-elastic-cloud-on-k8s-pkg-apis-elasticsearch-v1-volumeclaimdeletepolicy[$$VolumeClaimDeletePolicy$$]__ | VolumeClaimDeletePolicy sets the policy for handling deletion of PersistentVolumeClaims for all NodeSets. Possible values are DeleteOnScaledownOnly and DeleteOnScaledownAndClusterDeletion. Defaults to DeleteOnScaledownAndClusterDeletion.
| *`monitoring`* __xref:{anchor_prefix}-github-com-elastic-cloud-on-k8s-pkg-apis-elasticsearch-v1-monitoring[$$Monitoring$$]__ | Monitoring enables you to extract log and stack monitoring metrics of this Elasticsearch cluster. Metricbeat and Filebeat are deployed in the same pod as sidecar and each one send data to one or two different monitoring Elasticsearch clusters running in the same Kubernetes cluster.
| *`monitoring`* __xref:{anchor_prefix}-github-com-elastic-cloud-on-k8s-pkg-apis-elasticsearch-v1-monitoring[$$Monitoring$$]__ | Monitoring enables you to extract log and Stack Monitoring metrics of this Elasticsearch cluster. Metricbeat and Filebeat are deployed in the same Pod as sidecars and each one send data to one or two different monitoring Elasticsearch clusters running in the same Kubernetes cluster.
|===


Expand Down Expand Up @@ -956,7 +956,7 @@ FileRealmSource references users to create in the Elasticsearch cluster.
[cols="25a,75a", options="header"]
|===
| Field | Description
| *`elasticsearchRef`* __xref:{anchor_prefix}-github-com-elastic-cloud-on-k8s-pkg-apis-common-v1-objectselector[$$ObjectSelector$$]__ | ElasticsearchRef is a reference to a monitoring Elasticsearch cluster running in the same Kubernetes cluster dedicated to receiving stack monitoring metrics.
| *`elasticsearchRef`* __xref:{anchor_prefix}-github-com-elastic-cloud-on-k8s-pkg-apis-common-v1-objectselector[$$ObjectSelector$$]__ | ElasticsearchRef is a reference to a monitoring Elasticsearch cluster running in the same Kubernetes cluster dedicated to receiving Stack Monitoring metrics.
|===


Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/common/v1/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (asm AssociationStatusMap) Single() (AssociationStatus, error) {
return result, nil
}

// AllEstablished returns true iff all Associations have AssociationEstablished status, false otherwise.
// AllEstablished returns true if all Associations have AssociationEstablished status, false otherwise.
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
func (asm AssociationStatusMap) AllEstablished() bool {
for _, status := range asm {
if status != AssociationEstablished {
Expand Down
46 changes: 21 additions & 25 deletions pkg/apis/elasticsearch/v1/elasticsearch_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ type ElasticsearchSpec struct {
// +kubebuilder:validation:Enum=DeleteOnScaledownOnly;DeleteOnScaledownAndClusterDeletion
VolumeClaimDeletePolicy VolumeClaimDeletePolicy `json:"volumeClaimDeletePolicy,omitempty"`

// Monitoring enables you to extract log and stack monitoring metrics of this Elasticsearch cluster.
// Metricbeat and Filebeat are deployed in the same pod as sidecar and each one send data to one or two different
// Monitoring enables you to extract log and Stack Monitoring metrics of this Elasticsearch cluster.
// Metricbeat and Filebeat are deployed in the same Pod as sidecars and each one send data to one or two different
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
// monitoring Elasticsearch clusters running in the same Kubernetes cluster.
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
// +kubebuilder:validation:Optional
Monitoring Monitoring `json:"monitoring,omitempty"`
Expand All @@ -93,7 +93,7 @@ type Monitoring struct {

type MetricsMonitoring struct {
// ElasticsearchRef is a reference to a monitoring Elasticsearch cluster running in the same Kubernetes cluster
// dedicated to receiving stack monitoring metrics.
// dedicated to receiving Stack Monitoring metrics.
ElasticsearchRef commonv1.ObjectSelector `json:"elasticsearchRef,omitempty"`
}

Expand All @@ -103,16 +103,16 @@ type LogsMonitoring struct {
ElasticsearchRef commonv1.ObjectSelector `json:"elasticsearchRef,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want the API to have slices/arrays of elasticsearchRef to future proof it for Elastic Agent

Copy link
Contributor Author

@thbkrkr thbkrkr Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to support the list of elasticsearchRef in the stackmon package with a validation to only support one elasticsearchRef for now. ce855c0

}

// EsEsAssociation helps to manage the Elasticsearch+Metricbeat+Filebeat <-> Elasticsearch(es) association
type EsEsAssociation struct {
// EsMonitoringAssociation helps to manage Elasticsearch+Metricbeat+Filebeat <-> Elasticsearch(es) associations
type EsMonitoringAssociation struct {
*Elasticsearch
// ref is the namespaced name of the Elasticsearch used in Association
// ref is the namespaced name of the Elasticsearch referenced in the Association
ref types.NamespacedName
}

var _ commonv1.Association = &EsEsAssociation{}
var _ commonv1.Association = &EsMonitoringAssociation{}

func (eea *EsEsAssociation) Associated() commonv1.Associated {
func (eea *EsMonitoringAssociation) Associated() commonv1.Associated {
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
if eea == nil {
return nil
}
Expand All @@ -122,7 +122,7 @@ func (eea *EsEsAssociation) Associated() commonv1.Associated {
return eea.Elasticsearch
}

func (eea *EsEsAssociation) AssociationConfAnnotationName() string {
func (eea *EsMonitoringAssociation) AssociationConfAnnotationName() string {
// annotation key should be stable to allow Elasticsearch Controller only pick up the ones it expects,
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
// based on ElasticsearchRefs

Expand All @@ -140,18 +140,18 @@ func (eea *EsEsAssociation) AssociationConfAnnotationName() string {
)
}

func (eea *EsEsAssociation) AssociationType() commonv1.AssociationType {
func (eea *EsMonitoringAssociation) AssociationType() commonv1.AssociationType {
return commonv1.ElasticsearchAssociationType
}

func (eea *EsEsAssociation) AssociationRef() commonv1.ObjectSelector {
func (eea *EsMonitoringAssociation) AssociationRef() commonv1.ObjectSelector {
return commonv1.ObjectSelector{
Name: eea.ref.Name,
Namespace: eea.ref.Namespace,
}
}

func (eea *EsEsAssociation) AssociationConf() *commonv1.AssociationConf {
func (eea *EsMonitoringAssociation) AssociationConf() *commonv1.AssociationConf {
if eea.AssocConfs == nil {
return nil
}
Expand All @@ -163,7 +163,7 @@ func (eea *EsEsAssociation) AssociationConf() *commonv1.AssociationConf {
return &assocConf
}

func (eea *EsEsAssociation) SetAssociationConf(assocConf *commonv1.AssociationConf) {
func (eea *EsMonitoringAssociation) SetAssociationConf(assocConf *commonv1.AssociationConf) {
if eea.AssocConfs == nil {
eea.AssocConfs = make(map[types.NamespacedName]commonv1.AssociationConf)
}
Expand All @@ -172,7 +172,7 @@ func (eea *EsEsAssociation) SetAssociationConf(assocConf *commonv1.AssociationCo
}
}

func (eea *EsEsAssociation) AssociationID() string {
func (eea *EsMonitoringAssociation) AssociationID() string {
return fmt.Sprintf("%s-%s", eea.ref.Namespace, eea.ref.Name)
}

Expand All @@ -188,14 +188,14 @@ func (es *Elasticsearch) GetAssociations() []commonv1.Association {
associations := make([]commonv1.Association, 0)
ref := es.Spec.Monitoring.Metrics.ElasticsearchRef
if ref.IsDefined() {
associations = append(associations, &EsEsAssociation{
associations = append(associations, &EsMonitoringAssociation{
Elasticsearch: es,
ref: ref.WithDefaultNamespace(es.Namespace).NamespacedName(),
})
}
ref = es.Spec.Monitoring.Logs.ElasticsearchRef
if ref.IsDefined() {
associations = append(associations, &EsEsAssociation{
associations = append(associations, &EsMonitoringAssociation{
Elasticsearch: es,
ref: ref.WithDefaultNamespace(es.Namespace).NamespacedName(),
})
Expand All @@ -206,23 +206,23 @@ func (es *Elasticsearch) GetAssociations() []commonv1.Association {
func (es *Elasticsearch) GetMonitoringMetricsAssociation() commonv1.Association {
ref := es.Spec.Monitoring.Metrics.ElasticsearchRef
if ref.IsDefined() {
return &EsEsAssociation{
return &EsMonitoringAssociation{
Elasticsearch: es,
ref: ref.WithDefaultNamespace(es.Namespace).NamespacedName(),
}
}
return &EsEsAssociation{}
return &EsMonitoringAssociation{}
}

func (es *Elasticsearch) GetMonitoringLogsAssociation() commonv1.Association {
ref := es.Spec.Monitoring.Logs.ElasticsearchRef
if ref.IsDefined() {
return &EsEsAssociation{
return &EsMonitoringAssociation{
Elasticsearch: es,
ref: ref.WithDefaultNamespace(es.Namespace).NamespacedName(),
}
}
return &EsEsAssociation{}
return &EsMonitoringAssociation{}
}

func (es *Elasticsearch) AssociationStatusMap(typ commonv1.AssociationType) commonv1.AssociationStatusMap {
Expand All @@ -242,10 +242,6 @@ func (es *Elasticsearch) SetAssociationStatusMap(typ commonv1.AssociationType, s
return nil
}

/*func (es *Elasticsearch) AssociationID() string {
return commonv1.SingletonAssociationID
}*/

// VolumeClaimDeletePolicy describes the delete policy for handling PersistentVolumeClaims that hold Elasticsearch data.
// Inspired by https://github.com/kubernetes/enhancements/pull/2440
type VolumeClaimDeletePolicy string
Expand Down Expand Up @@ -541,7 +537,7 @@ type ElasticsearchStatus struct {
}

type ZenDiscoveryStatus struct {
MinimueeasterNodes int `json:"minimueeasterNodes,omitempty"`
MinimumMasterNodes int `json:"minimumMasterNodes,omitempty"`
}

// IsDegraded returns true if the current status is worse than the previous.
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/elasticsearch/v1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ import (
)

const (
// EsEsAssociationLabelName marks resources created by this controller for easier retrieval.
EsEsAssociationLabelName = "esesassociation.k8s.elastic.co/name"
// EsEsAssociationLabelNamespace marks resources created by this controller for easier retrieval.
EsEsAssociationLabelNamespace = "esesassociation.k8s.elastic.co/namespace"
// EsEsAssociationLabelType marks the type of association.
EsEsAssociationLabelType = "esesassociation.k8s.elastic.co/type"
// EsMonitoringAssociationLabelName marks resources created by this controller for easier retrieval.
EsMonitoringAssociationLabelName = "esmonitoringassociation.k8s.elastic.co/name"
// EsMonitoringAssociationLabelNamespace marks resources created by this controller for easier retrieval.
EsMonitoringAssociationLabelNamespace = "esmonitoringassociation.k8s.elastic.co/namespace"
// EsMonitoringAssociationLabelType marks the type of association.
EsMonitoringAssociationLabelType = "esmonitoringassociation.k8s.elastic.co/type"
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved

// BeatBuiltinRole is the name of the built-in role for the Metricbeat/Filebeat system user.
BeatBuiltinRole = "superuser" // FIXME: create a dedicated role?
)

func AddEsEs(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params operator.Parameters) error {
// AddEsMonitoring reconciles an association between two Elasticsearch clusters for Stack Monitoring.
// Beats are configured to collect monitoring metrics and logs data of the associated Elasticsearch and send
// them to the Elasticsearch referenced in the association.
func AddEsMonitoring(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params operator.Parameters) error {
return association.AddAssociationController(mgr, accessReviewer, params, association.AssociationInfo{
AssociatedObjTemplate: func() commonv1.Associated { return &esv1.Elasticsearch{} },
ElasticsearchRef: func(c k8s.Client, association commonv1.Association) (bool, commonv1.ObjectSelector, error) {
Expand All @@ -39,17 +42,17 @@ func AddEsEs(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params ope
ExternalServiceURL: getElasticsearchExternalURL,
AssociationType: commonv1.ElasticsearchAssociationType,
AssociatedNamer: esv1.ESNamer,
AssociationName: "es-es",
AssociationName: "es-mon",
AssociatedShortName: "es",
Labels: func(associated types.NamespacedName) map[string]string {
return map[string]string{
EsEsAssociationLabelName: associated.Name,
EsEsAssociationLabelNamespace: associated.Namespace,
EsEsAssociationLabelType: commonv1.ElasticsearchAssociationType,
EsMonitoringAssociationLabelName: associated.Name,
EsMonitoringAssociationLabelNamespace: associated.Namespace,
EsMonitoringAssociationLabelType: commonv1.ElasticsearchAssociationType,
}
},
AssociationConfAnnotationNameBase: commonv1.ElasticsearchConfigAnnotationNameBase,
UserSecretSuffix: "beat-user",
UserSecretSuffix: "beat-mon-user",
ESUserRole: func(associated commonv1.Associated) (string, error) {
return BeatBuiltinRole, nil
},
Expand Down
35 changes: 24 additions & 11 deletions pkg/controller/common/defaults/pod_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,27 @@ func NewPodTemplateBuilder(base corev1.PodTemplateSpec, containerName string) *P
return builder.setDefaults()
}

// getContainer retrieves the existing Container from the pod template
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
func (b *PodTemplateBuilder) getContainer() *corev1.Container {
for i, c := range b.PodTemplate.Spec.Containers {
if c.Name == b.containerName {
return &b.PodTemplate.Spec.Containers[i]
}
}
return nil
}
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
func (b *PodTemplateBuilder) setContainerDefaulter() {
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
b.containerDefaulter = container.NewDefaulter(b.getContainer())
}

// setDefaults sets up a default Container in the pod template,
// and disables service account token auto mount.
func (b *PodTemplateBuilder) setDefaults() *PodTemplateBuilder {
// retrieve the existing Container from the pod template
getContainer := func() *corev1.Container {
for i, c := range b.PodTemplate.Spec.Containers {
if c.Name == b.containerName {
return &b.PodTemplate.Spec.Containers[i]
}
}
return nil
}
userContainer := getContainer()
userContainer := b.getContainer()
if userContainer == nil {
// create the default Container if not provided by the user
b.PodTemplate.Spec.Containers = append(b.PodTemplate.Spec.Containers, corev1.Container{Name: b.containerName})
b.containerDefaulter = container.NewDefaulter(getContainer())
b.setContainerDefaulter()
} else {
b.containerDefaulter = container.NewDefaulter(userContainer)
}
Expand Down Expand Up @@ -179,6 +183,15 @@ func (b *PodTemplateBuilder) WithTerminationGracePeriod(period int64) *PodTempla
return b
}

// WithContainer appends the given containers to the list of containers belonging to the pod.
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
// It also ensures that the base container defaulter still points to the container in the list because append()
// create a new slice.
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
func (b *PodTemplateBuilder) WithContainers(containers ...corev1.Container) *PodTemplateBuilder {
b.PodTemplate.Spec.Containers = append(b.PodTemplate.Spec.Containers, containers...)
b.setContainerDefaulter()
return b
}

// WithInitContainerDefaults sets default values for the current init containers.
//
// Defaults:
Expand Down
Loading