Skip to content

Commit

Permalink
Don't rely on buggy metaObject Kind
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sebgl committed Jul 22, 2019
1 parent 571d678 commit d8c90f4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
11 changes: 10 additions & 1 deletion operators/pkg/apis/apm/v1alpha1/apmserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const APMServerContainerName = "apm-server"
const (
APMServerContainerName = "apm-server"
Kind = "ApmServer"
)

// ApmServerSpec defines the desired state of ApmServer
type ApmServerSpec struct {
Expand Down Expand Up @@ -154,3 +157,9 @@ func (as *ApmServer) ElasticsearchAuth() commonv1alpha1.ElasticsearchAuth {
func (as *ApmServer) SecureSettings() *commonv1alpha1.SecretRef {
return as.Spec.SecureSettings
}

// Kind can technically be retrieved from metav1.Object, but there is a bug preventing us to retrieve it
// see https://github.com/kubernetes-sigs/controller-runtime/issues/406
func (as *ApmServer) Kind() string {
return Kind
}
11 changes: 10 additions & 1 deletion operators/pkg/apis/elasticsearch/v1alpha1/elasticsearch_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const ElasticsearchContainerName = "elasticsearch"
const (
ElasticsearchContainerName = "elasticsearch"
Kind = "Elasticsearch"
)

// ElasticsearchSpec defines the desired state of Elasticsearch
type ElasticsearchSpec struct {
Expand Down Expand Up @@ -260,6 +263,12 @@ func (e Elasticsearch) SecureSettings() *commonv1alpha1.SecretRef {
return e.Spec.SecureSettings
}

// Kind can technically be retrieved from metav1.Object, but there is a bug preventing us to retrieve it
// see https://github.com/kubernetes-sigs/controller-runtime/issues/406
func (e Elasticsearch) Kind() string {
return Kind
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ElasticsearchList contains a list of Elasticsearch clusters
Expand Down
11 changes: 10 additions & 1 deletion operators/pkg/apis/kibana/v1alpha1/kibana_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
commonv1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/common/v1alpha1"
)

const KibanaContainerName = "kibana"
const (
KibanaContainerName = "kibana"
Kind = "Kibana"
)

// KibanaSpec defines the desired state of Kibana
type KibanaSpec struct {
Expand Down Expand Up @@ -107,6 +110,12 @@ func (k *Kibana) SecureSettings() *commonv1alpha1.SecretRef {
return k.Spec.SecureSettings
}

// Kind can technically be retrieved from metav1.Object, but there is a bug preventing us to retrieve it
// see https://github.com/kubernetes-sigs/controller-runtime/issues/406
func (k *Kibana) Kind() string {
return Kind
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand Down
2 changes: 1 addition & 1 deletion operators/pkg/controller/apmserver/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func newPodSpec(as *v1alpha1.ApmServer, p PodSpecParams) corev1.PodTemplateSpec

if p.keystoreResources != nil {
dataVolume := keystore.DataVolume(
strings.ToLower(as.Kind),
strings.ToLower(as.Kind()),
DataVolumePath,
)
builder.WithInitContainers(p.keystoreResources.InitContainer).
Expand Down
10 changes: 7 additions & 3 deletions operators/pkg/controller/common/keystore/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
package keystore

import (
"strings"

commonv1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/common/v1alpha1"
"github.com/elastic/cloud-on-k8s/operators/pkg/controller/common/watches"
"github.com/elastic/cloud-on-k8s/operators/pkg/utils/k8s"

"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -35,6 +36,9 @@ type HasKeystore interface {
metav1.Object
runtime.Object
SecureSettings() *commonv1alpha1.SecretRef
// Kind can technically be retrieved from metav1.Object, but there is a bug preventing us to retrieve it
// see https://github.com/kubernetes-sigs/controller-runtime/issues/406
Kind() string
}

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

0 comments on commit d8c90f4

Please sign in to comment.