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

Keep 'orchestrator.cluster.name' if 'kubeconfig' is not returned in GKE metadata #33418

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
- Fix race condition when stopping runners {pull}32433[32433]
- Fix concurrent map writes when system/process code called from reporter code {pull}32491[32491]
- Fix in AWS related services initialisation relying on custom endpoint resolver. {issue}32888[32888] {pull}32921[32921]
- Keep `orchestrator.cluster.name` if `kubeconfig` is not returned in GKE metadata. {pull}33418[33418]

*Auditbeat*

Expand Down
20 changes: 14 additions & 6 deletions libbeat/processors/add_cloud_metadata/provider_google_gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var gceMetadataFetcher = provider{
if !ok {
return
}
cloud.Put(key, path.Base(p))
_, _ = cloud.Put(key, path.Base(p))
}

if instance, ok := m["instance"].(map[string]interface{}); ok {
Expand Down Expand Up @@ -97,21 +97,29 @@ var gceMetadataFetcher = provider{
if kubeconfig, err := meta.GetValue("orchestrator.cluster.kubeconfig"); err == nil {
kubeConfig, ok := kubeconfig.(string)
if !ok {
meta.Delete("orchestrator.cluster.kubeconfig")
_ = meta.Delete("orchestrator.cluster.kubeconfig")
}
cc := &KubeConfig{}
err := yaml.Unmarshal([]byte(kubeConfig), cc)
if err != nil {
meta.Delete("orchestrator.cluster.kubeconfig")
_ = meta.Delete("orchestrator.cluster.kubeconfig")
}
if len(cc.Clusters) > 0 {
if cc.Clusters[0].Cluster.Server != "" {
meta.Delete("orchestrator.cluster.kubeconfig")
meta.Put("orchestrator.cluster.url", cc.Clusters[0].Cluster.Server)
_ = meta.Delete("orchestrator.cluster.kubeconfig")
_, _ = meta.Put("orchestrator.cluster.url", cc.Clusters[0].Cluster.Server)
}
}
} else {
meta.Delete("orchestrator")
_ = meta.Delete("orchestrator.cluster.kubeconfig")
}

clusterName, err := meta.GetValue("orchestrator.cluster.name")
if err != nil {
_ = meta.Delete("orchestrator")
}
if clusterName, ok := clusterName.(string); !ok || clusterName == "" {
_ = meta.Delete("orchestrator")
}

if project, ok := m["project"].(map[string]interface{}); ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ const gceK8sPartialMetadataV1 = `{
func initGCETestServer(resp string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/computeMetadata/v1/?recursive=true&alt=json" {
w.Write([]byte(resp))
_, _ = w.Write([]byte(resp))
return
}

Expand All @@ -308,7 +308,7 @@ func initGCETestServer(resp string) *httptest.Server {
}

func TestRetrieveGCEMetadata(t *testing.T) {
logp.TestingSetup()
_ = logp.TestingSetup()

server := initGCETestServer(gceMetadataV1)
defer server.Close()
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestRetrieveGCEMetadata(t *testing.T) {
}

func TestRetrieveGCEMetadataInK8s(t *testing.T) {
logp.TestingSetup()
_ = logp.TestingSetup()

server := initGCETestServer(gceK8sMetadataV1)
defer server.Close()
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestRetrieveGCEMetadataInK8s(t *testing.T) {
}

func TestRetrieveGCEMetadataInK8sNotOverriden(t *testing.T) {
logp.TestingSetup()
_ = logp.TestingSetup()

server := initGCETestServer(gceK8sMetadataV1)
defer server.Close()
Expand Down Expand Up @@ -475,7 +475,7 @@ func TestRetrieveGCEMetadataInK8sNotOverriden(t *testing.T) {
}

func TestRetrieveGCEMetadataInK8sPartial(t *testing.T) {
logp.TestingSetup()
_ = logp.TestingSetup()

server := initGCETestServer(gceK8sPartialMetadataV1)
defer server.Close()
Expand Down Expand Up @@ -518,6 +518,11 @@ func TestRetrieveGCEMetadataInK8sPartial(t *testing.T) {
"name": "GCE",
},
},
"orchestrator": mapstr.M{
"cluster": mapstr.M{
"name": "staging-marketing-k8s",
},
},
}
assert.Equal(t, expected, actual.Fields)
}