Skip to content

Commit 4a28c5c

Browse files
robbaveybarkbay
andauthored
Add Selector to LogstashStatus (elastic#6854)
Add Selector to LogstashStatus: while running some tests with the VerticalPodAutoscaler, I was unable to make it work with the Logstash CRD, which appears to be due to the lack of a selector in the LogstashStatus object. This commit adds the missing Selector. Co-authored-by: Michael Morello <[email protected]>
1 parent ebffcc3 commit 4a28c5c

File tree

7 files changed

+35
-9
lines changed

7 files changed

+35
-9
lines changed

Diff for: config/crds/v1/all-crds.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -9815,11 +9815,15 @@ spec:
98159815
in the Logstash specification.
98169816
format: int64
98179817
type: integer
9818+
selector:
9819+
type: string
98189820
version:
98199821
description: 'Version of the stack resource currently running. During
98209822
version upgrades, multiple versions may run in parallel: this value
98219823
specifies the lowest version currently running.'
98229824
type: string
9825+
required:
9826+
- selector
98239827
type: object
98249828
type: object
98259829
served: true
@@ -9828,7 +9832,7 @@ spec:
98289832
scale:
98299833
labelSelectorPath: .status.selector
98309834
specReplicasPath: .spec.count
9831-
statusReplicasPath: .status.count
9835+
statusReplicasPath: .status.expectedNodes
98329836
status: {}
98339837
---
98349838
apiVersion: apiextensions.k8s.io/v1

Diff for: config/crds/v1/bases/logstash.k8s.elastic.co_logstashes.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -8224,11 +8224,15 @@ spec:
82248224
in the Logstash specification.
82258225
format: int64
82268226
type: integer
8227+
selector:
8228+
type: string
82278229
version:
82288230
description: 'Version of the stack resource currently running. During
82298231
version upgrades, multiple versions may run in parallel: this value
82308232
specifies the lowest version currently running.'
82318233
type: string
8234+
required:
8235+
- selector
82328236
type: object
82338237
type: object
82348238
served: true
@@ -8237,5 +8241,5 @@ spec:
82378241
scale:
82388242
labelSelectorPath: .status.selector
82398243
specReplicasPath: .spec.count
8240-
statusReplicasPath: .status.count
8244+
statusReplicasPath: .status.expectedNodes
82418245
status: {}

Diff for: deploy/eck-operator/charts/eck-operator-crds/templates/all-crds.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -9869,11 +9869,15 @@ spec:
98699869
in the Logstash specification.
98709870
format: int64
98719871
type: integer
9872+
selector:
9873+
type: string
98729874
version:
98739875
description: 'Version of the stack resource currently running. During
98749876
version upgrades, multiple versions may run in parallel: this value
98759877
specifies the lowest version currently running.'
98769878
type: string
9879+
required:
9880+
- selector
98779881
type: object
98789882
type: object
98799883
served: true
@@ -9882,7 +9886,7 @@ spec:
98829886
scale:
98839887
labelSelectorPath: .status.selector
98849888
specReplicasPath: .spec.count
9885-
statusReplicasPath: .status.count
9889+
statusReplicasPath: .status.expectedNodes
98869890
status: {}
98879891
---
98889892
apiVersion: apiextensions.k8s.io/v1

Diff for: docs/reference/api-docs.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,7 @@ LogstashStatus defines the observed state of Logstash
19631963
| *`expectedNodes`* __integer__ |
19641964
| *`availableNodes`* __integer__ |
19651965
| *`observedGeneration`* __integer__ | ObservedGeneration is the most recent generation observed for this Logstash instance. It corresponds to the metadata generation, which is updated on mutation by the API Server. If the generation observed in status diverges from the generation in metadata, the Logstash controller has not yet processed the changes contained in the Logstash specification.
1966+
| *`selector`* __string__ |
19661967
|===
19671968

19681969

Diff for: pkg/apis/logstash/v1alpha1/logstash_types.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ type LogstashStatus struct {
129129

130130
// MonitoringAssociationStatus is the status of any auto-linking to monitoring Elasticsearch clusters.
131131
MonitoringAssociationStatus commonv1.AssociationStatusMap `json:"monitoringAssociationStatus,omitempty"`
132+
133+
Selector string `json:"selector"`
132134
}
133135

134136
// +kubebuilder:object:root=true
@@ -141,7 +143,7 @@ type LogstashStatus struct {
141143
// +kubebuilder:printcolumn:name="expected",type="integer",JSONPath=".status.expectedNodes",description="Expected nodes"
142144
// +kubebuilder:printcolumn:name="age",type="date",JSONPath=".metadata.creationTimestamp"
143145
// +kubebuilder:printcolumn:name="version",type="string",JSONPath=".status.version",description="Logstash version"
144-
// +kubebuilder:subresource:scale:specpath=.spec.count,statuspath=.status.count,selectorpath=.status.selector
146+
// +kubebuilder:subresource:scale:specpath=.spec.count,statuspath=.status.expectedNodes,selectorpath=.status.selector
145147
// +kubebuilder:storageversion
146148
type Logstash struct {
147149
metav1.TypeMeta `json:",inline"`

Diff for: pkg/controller/logstash/logstash_controller_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func TestReconcileLogstash_Reconcile(t *testing.T) {
208208
ExpectedNodes: 1,
209209
AvailableNodes: 1,
210210
ObservedGeneration: 2,
211+
Selector: "common.k8s.elastic.co/type=logstash,logstash.k8s.elastic.co/name=testLogstash",
211212
},
212213
},
213214
wantErr: false,
@@ -314,6 +315,7 @@ func TestReconcileLogstash_Reconcile(t *testing.T) {
314315
ExpectedNodes: 1,
315316
AvailableNodes: 1,
316317
ObservedGeneration: 2,
318+
Selector: "common.k8s.elastic.co/type=logstash,logstash.k8s.elastic.co/name=testLogstash",
317319
},
318320
},
319321
wantErr: false,
@@ -412,6 +414,7 @@ func TestReconcileLogstash_Reconcile(t *testing.T) {
412414
ExpectedNodes: 1,
413415
AvailableNodes: 1,
414416
ObservedGeneration: 2,
417+
Selector: "common.k8s.elastic.co/type=logstash,logstash.k8s.elastic.co/name=testLogstash",
415418
},
416419
},
417420
wantErr: false,

Diff for: pkg/controller/logstash/reconcile.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010

1111
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/logstash/sset"
1212

13+
appsv1 "k8s.io/api/apps/v1"
1314
corev1 "k8s.io/api/core/v1"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416
"k8s.io/client-go/kubernetes/scheme"
1517
"sigs.k8s.io/controller-runtime/pkg/client"
1618
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -49,26 +51,32 @@ func reconcileStatefulSet(params Params, podTemplate corev1.PodTemplateSpec) (*r
4951

5052
var status logstashv1alpha1.LogstashStatus
5153

52-
if status, err = calculateStatus(&params, reconciled.Status.ReadyReplicas, reconciled.Status.Replicas); err != nil {
54+
if status, err = calculateStatus(&params, reconciled); err != nil {
5355
results.WithError(errors.Wrap(err, "while calculating status"))
5456
}
5557
return results, status
5658
}
5759

5860
// calculateStatus will calculate a new status from the state of the pods within the k8s cluster
5961
// and will return any error encountered.
60-
func calculateStatus(params *Params, ready, desired int32) (logstashv1alpha1.LogstashStatus, error) {
62+
func calculateStatus(params *Params, sset appsv1.StatefulSet) (logstashv1alpha1.LogstashStatus, error) {
6163
logstash := params.Logstash
6264
status := params.Status
63-
6465
pods, err := k8s.PodsMatchingLabels(params.Client, logstash.Namespace, map[string]string{NameLabelName: logstash.Name})
6566
if err != nil {
6667
return status, err
6768
}
6869

70+
if sset.Spec.Selector != nil {
71+
selector, err := metav1.LabelSelectorAsSelector(sset.Spec.Selector)
72+
if err != nil {
73+
return logstashv1alpha1.LogstashStatus{}, err
74+
}
75+
status.Selector = selector.String()
76+
}
6977
status.Version = common.LowestVersionFromPods(params.Context, status.Version, pods, VersionLabelName)
70-
status.AvailableNodes = ready
71-
status.ExpectedNodes = desired
78+
status.AvailableNodes = sset.Status.ReadyReplicas
79+
status.ExpectedNodes = sset.Status.Replicas
7280
return status, nil
7381
}
7482

0 commit comments

Comments
 (0)