Skip to content

Commit f9c01e8

Browse files
authored
APM: remove "output" element and add elasticsearchRef (#1345) (#1354)
1 parent 6fbc2ff commit f9c01e8

File tree

13 files changed

+117
-171
lines changed

13 files changed

+117
-171
lines changed

operators/config/crds/apm_v1alpha1_apmserver.yaml

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,60 @@ spec:
5050
config:
5151
description: Config represents the APM configuration.
5252
type: object
53+
elasticsearch:
54+
description: Elasticsearch configures how the APM server connects to
55+
Elasticsearch
56+
properties:
57+
auth:
58+
description: Auth configures authentication for APM Server to use.
59+
properties:
60+
inline:
61+
description: Inline is auth provided as plaintext inline credentials.
62+
properties:
63+
password:
64+
description: Password is the password to use.
65+
type: string
66+
username:
67+
description: User is the username to use.
68+
type: string
69+
required:
70+
- username
71+
- password
72+
type: object
73+
secret:
74+
description: SecretKeyRef is a secret that contains the credentials
75+
to use.
76+
type: object
77+
type: object
78+
hosts:
79+
description: Hosts are the URLs of the output Elasticsearch nodes.
80+
items:
81+
type: string
82+
type: array
83+
ssl:
84+
description: SSL configures TLS-related configuration for Elasticsearch
85+
properties:
86+
certificateAuthorities:
87+
description: CertificateAuthorities is a secret that contains
88+
a `tls.crt` entry that contain certificates for server verifications.
89+
properties:
90+
secretName:
91+
type: string
92+
type: object
93+
type: object
94+
type: object
95+
elasticsearchRef:
96+
description: ElasticsearchRef references an Elasticsearch resource in
97+
the Kubernetes cluster. If the namespace is not specified, the current
98+
resource namespace will be used.
99+
properties:
100+
name:
101+
type: string
102+
namespace:
103+
type: string
104+
required:
105+
- name
106+
type: object
53107
featureFlags:
54108
description: FeatureFlags are apm-specific flags that enable or disable
55109
specific experimental features
@@ -114,65 +168,6 @@ spec:
114168
must have.
115169
format: int32
116170
type: integer
117-
output:
118-
properties:
119-
elasticsearch:
120-
description: Elasticsearch configures the Elasticsearch output
121-
properties:
122-
auth:
123-
description: Auth configures authentication for APM Server to
124-
use.
125-
properties:
126-
inline:
127-
description: Inline is auth provided as plaintext inline
128-
credentials.
129-
properties:
130-
password:
131-
description: Password is the password to use.
132-
type: string
133-
username:
134-
description: User is the username to use.
135-
type: string
136-
required:
137-
- username
138-
- password
139-
type: object
140-
secret:
141-
description: SecretKeyRef is a secret that contains the
142-
credentials to use.
143-
type: object
144-
type: object
145-
hosts:
146-
description: Hosts are the URLs of the output Elasticsearch
147-
nodes.
148-
items:
149-
type: string
150-
type: array
151-
ref:
152-
description: ElasticsearchRef allows users to reference a Elasticsearch
153-
cluster inside k8s to automatically derive the other fields.
154-
properties:
155-
name:
156-
type: string
157-
namespace:
158-
type: string
159-
required:
160-
- name
161-
type: object
162-
ssl:
163-
description: SSL configures TLS-related configuration for Elasticsearch
164-
properties:
165-
certificateAuthorities:
166-
description: CertificateAuthorities is a secret that contains
167-
a `tls.crt` entry that contain certificates for server
168-
verifications.
169-
properties:
170-
secretName:
171-
type: string
172-
type: object
173-
type: object
174-
type: object
175-
type: object
176171
podTemplate:
177172
description: PodTemplate can be used to propagate configuration to APM
178173
Server pods. This allows specifying custom annotations, labels, environment

operators/config/samples/apm/apm_es_kibana.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ metadata:
1616
spec:
1717
version: "7.2.0"
1818
nodeCount: 1
19-
output:
20-
elasticsearch:
21-
ref:
22-
name: elasticsearch-sample
23-
namespace: default
19+
elasticsearchRef:
20+
name: "elasticsearch-sample"
2421
---
2522
apiVersion: kibana.k8s.elastic.co/v1alpha1
2623
kind: Kibana

operators/pkg/apis/apm/v1alpha1/apmserver_types.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ type ApmServerSpec struct {
2929
// HTTP contains settings for HTTP.
3030
HTTP commonv1alpha1.HTTPConfig `json:"http,omitempty"`
3131

32+
// ElasticsearchRef references an Elasticsearch resource in the Kubernetes cluster.
33+
// If the namespace is not specified, the current resource namespace will be used.
34+
ElasticsearchRef commonv1alpha1.ObjectSelector `json:"elasticsearchRef,omitempty"`
35+
36+
// Elasticsearch configures how the APM server connects to Elasticsearch
3237
// +optional
33-
Output Output `json:"output,omitempty"`
38+
Elasticsearch ElasticsearchOutput `json:"elasticsearch,omitempty"`
3439

3540
// PodTemplate can be used to propagate configuration to APM Server pods.
3641
// This allows specifying custom annotations, labels, environment variables,
@@ -49,17 +54,8 @@ type ApmServerSpec struct {
4954
FeatureFlags commonv1alpha1.FeatureFlags `json:"featureFlags,omitempty"`
5055
}
5156

52-
// Output contains output configuration for supported outputs
53-
type Output struct {
54-
// Elasticsearch configures the Elasticsearch output
55-
// +optional
56-
Elasticsearch ElasticsearchOutput `json:"elasticsearch,omitempty"`
57-
}
58-
5957
// Elasticsearch contains configuration for the Elasticsearch output
6058
type ElasticsearchOutput struct {
61-
// ElasticsearchRef allows users to reference a Elasticsearch cluster inside k8s to automatically derive the other fields.
62-
ElasticsearchRef *commonv1alpha1.ObjectSelector `json:"ref,omitempty"`
6359

6460
// Hosts are the URLs of the output Elasticsearch nodes.
6561
Hosts []string `json:"hosts,omitempty"`
@@ -153,7 +149,7 @@ func (as *ApmServer) IsMarkedForDeletion() bool {
153149
}
154150

155151
func (as *ApmServer) ElasticsearchAuth() commonv1alpha1.ElasticsearchAuth {
156-
return as.Spec.Output.Elasticsearch.Auth
152+
return as.Spec.Elasticsearch.Auth
157153
}
158154

159155
func (as *ApmServer) SecureSettings() *commonv1alpha1.SecretRef {

operators/pkg/apis/apm/v1alpha1/zz_generated.deepcopy.go

Lines changed: 2 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operators/pkg/controller/apmserver/apmserver_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func (r *ReconcileApmServer) deploymentParams(
309309
_, _ = configChecksum.Write([]byte(params.keystoreResources.Version))
310310
}
311311

312-
esCASecretName := as.Spec.Output.Elasticsearch.SSL.CertificateAuthorities.SecretName
312+
esCASecretName := as.Spec.Elasticsearch.SSL.CertificateAuthorities.SecretName
313313
if esCASecretName != "" {
314314
// TODO: use apmServerCa to generate cert for deployment
315315

operators/pkg/controller/apmserver/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ func NewConfigFromSpec(c k8s.Client, as v1alpha1.ApmServer) (*settings.Canonical
4545
}
4646

4747
outputCfg := settings.NewCanonicalConfig()
48-
if as.Spec.Output.Elasticsearch.IsConfigured() {
48+
if as.Spec.Elasticsearch.IsConfigured() {
4949
// Get username and password
5050
username, password, err := association.ElasticsearchAuthSettings(c, &as)
5151
if err != nil {
5252
return nil, err
5353
}
5454
outputCfg = settings.MustCanonicalConfig(
5555
map[string]interface{}{
56-
"output.elasticsearch.hosts": as.Spec.Output.Elasticsearch.Hosts,
56+
"output.elasticsearch.hosts": as.Spec.Elasticsearch.Hosts,
5757
"output.elasticsearch.username": username,
5858
"output.elasticsearch.password": password,
5959
"output.elasticsearch.ssl.certificate_authorities": []string{filepath.Join(CertificatesDir, certificates.CertFileName)},

operators/pkg/controller/apmserverelasticsearchassociation/apmserverelasticsearchassociation_controller.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,16 @@ func resultFromStatus(status commonv1alpha1.AssociationStatus) reconcile.Result
208208
}
209209

210210
func (r *ReconcileApmServerElasticsearchAssociation) reconcileInternal(apmServer apmtype.ApmServer) (commonv1alpha1.AssociationStatus, error) {
211-
assocKey := k8s.ExtractNamespacedName(&apmServer)
212211
// no auto-association nothing to do
213-
elasticsearchRef := apmServer.Spec.Output.Elasticsearch.ElasticsearchRef
214-
if elasticsearchRef == nil {
212+
elasticsearchRef := apmServer.Spec.ElasticsearchRef
213+
if !elasticsearchRef.IsDefined() {
215214
return "", nil
216215
}
217-
216+
if elasticsearchRef.Namespace == "" {
217+
// no namespace provided: default to the APM server namespace
218+
elasticsearchRef.Namespace = apmServer.Namespace
219+
}
220+
assocKey := k8s.ExtractNamespacedName(&apmServer)
218221
// Make sure we see events from Elasticsearch using a dynamic watch
219222
// will become more relevant once we refactor user handling to CRDs and implement
220223
// syncing of user credentials across namespaces
@@ -244,8 +247,6 @@ func (r *ReconcileApmServerElasticsearchAssociation) reconcileInternal(apmServer
244247
}
245248

246249
var expectedEsConfig apmtype.ElasticsearchOutput
247-
expectedEsConfig.ElasticsearchRef = apmServer.Spec.Output.Elasticsearch.ElasticsearchRef
248-
249250
// TODO: look up public certs secret name from the ES cluster resource instead of relying on naming convention
250251
var publicCertsSecret corev1.Secret
251252
publicCertsSecretKey := http.PublicCertsSecretRef(
@@ -261,8 +262,8 @@ func (r *ReconcileApmServerElasticsearchAssociation) reconcileInternal(apmServer
261262
expectedEsConfig.Auth.SecretKeyRef = clearTextSecretKeySelector(apmServer)
262263

263264
// TODO: this is a bit rough
264-
if !reflect.DeepEqual(apmServer.Spec.Output.Elasticsearch, expectedEsConfig) {
265-
apmServer.Spec.Output.Elasticsearch = expectedEsConfig
265+
if !reflect.DeepEqual(apmServer.Spec.Elasticsearch, expectedEsConfig) {
266+
apmServer.Spec.Elasticsearch = expectedEsConfig
266267
log.Info("Updating Apm Server spec with Elasticsearch output configuration", "namespace", apmServer.Namespace, "as_name", apmServer.Name)
267268
if err := r.Update(&apmServer); err != nil {
268269
return commonv1alpha1.AssociationPending, err
@@ -289,7 +290,7 @@ func deleteOrphanedResources(c k8s.Client, apm apmtype.ApmServer) error {
289290

290291
for _, s := range secrets.Items {
291292
controlledBy := metav1.IsControlledBy(&s, &apm)
292-
if controlledBy && !apm.Spec.Output.Elasticsearch.ElasticsearchRef.IsDefined() {
293+
if controlledBy && !apm.Spec.ElasticsearchRef.IsDefined() {
293294
log.Info("Deleting secret", "namespace", s.Namespace, "secret_name", s.Name, "as_name", apm.Name)
294295
if err := c.Delete(&s); err != nil {
295296
return err

operators/pkg/controller/apmserverelasticsearchassociation/apmserverelasticsearchassociation_controller_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,7 @@ func Test_deleteOrphanedResources(t *testing.T) {
7777
Name: "as",
7878
Namespace: "default",
7979
},
80-
Spec: apmtype.ApmServerSpec{
81-
Output: apmtype.Output{
82-
Elasticsearch: apmtype.ElasticsearchOutput{
83-
ElasticsearchRef: nil,
84-
},
85-
},
86-
},
80+
Spec: apmtype.ApmServerSpec{},
8781
},
8882
initialObjects: []runtime.Object{
8983
&corev1.Secret{

operators/pkg/controller/apmserverelasticsearchassociation/user.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@ func apmUserObjectName(assocName string) string {
3434

3535
// userKey is the namespaced name to identify the customer user resource created by the controller.
3636
func userKey(apm apmtype.ApmServer) *types.NamespacedName {
37-
38-
ref := apm.Spec.Output.Elasticsearch.ElasticsearchRef
39-
if ref == nil {
37+
esRef := apm.Spec.ElasticsearchRef
38+
if !esRef.IsDefined() {
4039
return nil
4140
}
41+
42+
esNamespace := esRef.Namespace
43+
if esNamespace == "" {
44+
// no namespace given, default to APM's one
45+
esNamespace = apm.Namespace
46+
}
4247
return &types.NamespacedName{
43-
Namespace: ref.Namespace,
48+
Namespace: esNamespace,
4449
Name: userName(apm),
4550
}
4651
}
@@ -76,7 +81,7 @@ func reconcileEsUser(c k8s.Client, s *runtime.Scheme, apm apmtype.ApmServer, es
7681
secretLabels := labels.NewLabels(apm.Name)
7782
secretLabels[AssociationLabelName] = apm.Name
7883
// add ES labels
79-
for k, v := range label.NewLabels(apm.Spec.Output.Elasticsearch.ElasticsearchRef.NamespacedName()) {
84+
for k, v := range label.NewLabels(apm.Spec.ElasticsearchRef.NamespacedName()) {
8085
secretLabels[k] = v
8186
}
8287
secKey := secretKey(apm)
@@ -120,7 +125,7 @@ func reconcileEsUser(c k8s.Client, s *runtime.Scheme, apm apmtype.ApmServer, es
120125
}
121126

122127
// analogous to the secret: the user goes on the Elasticsearch side of the association, we apply the ES labels for visibility
123-
userLabels := common.NewLabels(apm.Spec.Output.Elasticsearch.ElasticsearchRef.NamespacedName())
128+
userLabels := common.NewLabels(apm.Spec.ElasticsearchRef.NamespacedName())
124129
userLabels[AssociationLabelName] = apm.Name
125130
userLabels[AssociationLabelNamespace] = apm.Namespace
126131
expectedEsUser := &corev1.Secret{

operators/pkg/controller/apmserverelasticsearchassociation/user_test.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,11 @@ var apmFixture = apmtype.ApmServer{
3838
Namespace: "default",
3939
},
4040
Spec: apmtype.ApmServerSpec{
41-
Output: apmtype.Output{
42-
Elasticsearch: apmtype.ElasticsearchOutput{
43-
ElasticsearchRef: &commonv1alpha1.ObjectSelector{
44-
Name: "es",
45-
Namespace: "default",
46-
},
47-
},
41+
ElasticsearchRef: commonv1alpha1.ObjectSelector{
42+
Name: "es",
43+
Namespace: "default",
4844
},
45+
Elasticsearch: apmtype.ElasticsearchOutput{},
4946
},
5047
}
5148

@@ -186,14 +183,11 @@ func Test_reconcileEsUser(t *testing.T) {
186183
Namespace: "ns-2",
187184
},
188185
Spec: apmtype.ApmServerSpec{
189-
Output: apmtype.Output{
190-
Elasticsearch: apmtype.ElasticsearchOutput{
191-
ElasticsearchRef: &commonv1alpha1.ObjectSelector{
192-
Name: "es",
193-
Namespace: "ns-1",
194-
},
195-
},
186+
ElasticsearchRef: commonv1alpha1.ObjectSelector{
187+
Name: "es",
188+
Namespace: "ns-1",
196189
},
190+
Elasticsearch: apmtype.ElasticsearchOutput{},
197191
},
198192
},
199193
},

0 commit comments

Comments
 (0)