Skip to content

Commit 36713c6

Browse files
authored
Don't rely on buggy metaObject Kind (#1324) (#1357)
* Don't rely on buggy metaObject Kind A bug in our client implementation may clear the object's Kind on certain scenarios. See kubernetes-sigs/controller-runtime#406. Let's avoid that by fixing a constant Kind returned by a method call on the resource.
1 parent f8f9ed3 commit 36713c6

File tree

9 files changed

+44
-12
lines changed

9 files changed

+44
-12
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
)
1212

13-
const APMServerContainerName = "apm-server"
13+
const (
14+
APMServerContainerName = "apm-server"
15+
Kind = "ApmServer"
16+
)
1417

1518
// ApmServerSpec defines the desired state of ApmServer
1619
type ApmServerSpec struct {
@@ -155,3 +158,9 @@ func (as *ApmServer) ElasticsearchAuth() commonv1alpha1.ElasticsearchAuth {
155158
func (as *ApmServer) SecureSettings() *commonv1alpha1.SecretRef {
156159
return as.Spec.SecureSettings
157160
}
161+
162+
// Kind can technically be retrieved from metav1.Object, but there is a bug preventing us to retrieve it
163+
// see https://github.com/kubernetes-sigs/controller-runtime/issues/406
164+
func (as *ApmServer) Kind() string {
165+
return Kind
166+
}

operators/pkg/apis/elasticsearch/v1alpha1/elasticsearch_types.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
)
1212

13-
const ElasticsearchContainerName = "elasticsearch"
13+
const (
14+
ElasticsearchContainerName = "elasticsearch"
15+
Kind = "Elasticsearch"
16+
)
1417

1518
// ElasticsearchSpec defines the desired state of Elasticsearch
1619
type ElasticsearchSpec struct {
@@ -265,6 +268,12 @@ func (e Elasticsearch) SecureSettings() *commonv1alpha1.SecretRef {
265268
return e.Spec.SecureSettings
266269
}
267270

271+
// Kind can technically be retrieved from metav1.Object, but there is a bug preventing us to retrieve it
272+
// see https://github.com/kubernetes-sigs/controller-runtime/issues/406
273+
func (e Elasticsearch) Kind() string {
274+
return Kind
275+
}
276+
268277
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
269278

270279
// ElasticsearchList contains a list of Elasticsearch clusters

operators/pkg/apis/kibana/v1alpha1/kibana_types.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import (
1111
commonv1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/common/v1alpha1"
1212
)
1313

14-
const KibanaContainerName = "kibana"
14+
const (
15+
KibanaContainerName = "kibana"
16+
Kind = "Kibana"
17+
)
1518

1619
// KibanaSpec defines the desired state of Kibana
1720
type KibanaSpec struct {
@@ -112,6 +115,12 @@ func (k *Kibana) SecureSettings() *commonv1alpha1.SecretRef {
112115
return k.Spec.SecureSettings
113116
}
114117

118+
// Kind can technically be retrieved from metav1.Object, but there is a bug preventing us to retrieve it
119+
// see https://github.com/kubernetes-sigs/controller-runtime/issues/406
120+
func (k *Kibana) Kind() string {
121+
return Kind
122+
}
123+
115124
// +genclient
116125
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
117126

operators/pkg/controller/apmserver/apmserver_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,6 @@ func (r *ReconcileApmServer) updateStatus(state State) (reconcile.Result, error)
452452
// finalizersFor returns the list of finalizers applying to a given APM deployment
453453
func (r *ReconcileApmServer) finalizersFor(as apmv1alpha1.ApmServer) []finalizer.Finalizer {
454454
return []finalizer.Finalizer{
455-
keystore.Finalizer(k8s.ExtractNamespacedName(&as), r.dynamicWatches, "apmserver"),
455+
keystore.Finalizer(k8s.ExtractNamespacedName(&as), r.dynamicWatches, as.Kind()),
456456
}
457457
}

operators/pkg/controller/apmserver/pod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func newPodSpec(as *v1alpha1.ApmServer, p PodSpecParams) corev1.PodTemplateSpec
117117

118118
if p.keystoreResources != nil {
119119
dataVolume := keystore.DataVolume(
120-
strings.ToLower(as.Kind),
120+
strings.ToLower(as.Kind()),
121121
DataVolumePath,
122122
)
123123
builder.WithInitContainers(p.keystoreResources.InitContainer).

operators/pkg/controller/common/keystore/resources.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ package keystore
77
import (
88
"strings"
99

10-
commonv1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/common/v1alpha1"
11-
"github.com/elastic/cloud-on-k8s/operators/pkg/controller/common/watches"
12-
"github.com/elastic/cloud-on-k8s/operators/pkg/utils/k8s"
1310
corev1 "k8s.io/api/core/v1"
1411
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1512
"k8s.io/apimachinery/pkg/runtime"
1613
"k8s.io/client-go/tools/record"
1714
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
15+
16+
commonv1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/common/v1alpha1"
17+
"github.com/elastic/cloud-on-k8s/operators/pkg/controller/common/watches"
18+
"github.com/elastic/cloud-on-k8s/operators/pkg/utils/k8s"
1819
)
1920

2021
var log = logf.Log.WithName("keystore")
@@ -35,6 +36,9 @@ type HasKeystore interface {
3536
metav1.Object
3637
runtime.Object
3738
SecureSettings() *commonv1alpha1.SecretRef
39+
// Kind can technically be retrieved from metav1.Object, but there is a bug preventing us to retrieve it
40+
// see https://github.com/kubernetes-sigs/controller-runtime/issues/406
41+
Kind() string
3842
}
3943

4044
// NewResources optionally returns a volume and init container to include in pods,
@@ -60,7 +64,7 @@ func NewResources(
6064
// build an init container to create the keystore from the secure settings volume
6165
initContainer, err := initContainer(
6266
*secretVolume,
63-
strings.ToLower(hasKeystore.GetObjectKind().GroupVersionKind().Kind),
67+
strings.ToLower(hasKeystore.Kind()),
6468
initContainerParams,
6569
)
6670
if err != nil {

operators/pkg/controller/common/keystore/user_secret.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package keystore
66

77
import (
88
"fmt"
9+
"strings"
910

1011
commonv1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/common/v1alpha1"
1112
"github.com/elastic/cloud-on-k8s/operators/pkg/controller/common/events"
@@ -114,7 +115,7 @@ func watchSecureSettings(watched watches.DynamicWatches, secureSettingsRef *comm
114115
// Finalizer removes any dynamic watches on external user created secret.
115116
func Finalizer(namespacedName types.NamespacedName, watched watches.DynamicWatches, kind string) finalizer.Finalizer {
116117
return finalizer.Finalizer{
117-
Name: "secure-settings.finalizers." + kind + ".k8s.elastic.co",
118+
Name: "secure-settings.finalizers." + strings.ToLower(kind) + ".k8s.elastic.co",
118119
Execute: func() error {
119120
watched.Secrets.RemoveHandlerForKey(secureSettingsWatchName(namespacedName))
120121
return nil

operators/pkg/controller/elasticsearch/elasticsearch_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (r *ReconcileElasticsearch) finalizersFor(
294294
return []finalizer.Finalizer{
295295
reconciler.ExpectationsFinalizer(clusterName, r.podsExpectations),
296296
r.esObservers.Finalizer(clusterName),
297-
keystore.Finalizer(k8s.ExtractNamespacedName(&es), r.dynamicWatches, "elasticsearch"),
297+
keystore.Finalizer(k8s.ExtractNamespacedName(&es), r.dynamicWatches, es.Kind()),
298298
http.DynamicWatchesFinalizer(r.dynamicWatches, es.Name, esname.ESNamer),
299299
}
300300
}

operators/pkg/controller/kibana/kibana_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,6 @@ func (r *ReconcileKibana) updateStatus(state State) error {
216216
func (r *ReconcileKibana) finalizersFor(kb kibanav1alpha1.Kibana) []finalizer.Finalizer {
217217
return []finalizer.Finalizer{
218218
secretWatchFinalizer(kb, r.dynamicWatches),
219-
keystore.Finalizer(k8s.ExtractNamespacedName(&kb), r.dynamicWatches, "kibana"),
219+
keystore.Finalizer(k8s.ExtractNamespacedName(&kb), r.dynamicWatches, kb.Kind()),
220220
}
221221
}

0 commit comments

Comments
 (0)