Skip to content

Commit

Permalink
Keep 'orchestrator.cluster.name' if 'kubeconfig' is not returned in G…
Browse files Browse the repository at this point in the history
…KE metadata (#33418)

* keep   if  is not returned in GKE metadata

Signed-off-by: Tetiana Kravchenko <[email protected]>

* add PR number

Signed-off-by: Tetiana Kravchenko <[email protected]>

* keep   if  is not returned in GKE metadata

Signed-off-by: Tetiana Kravchenko <[email protected]>

* add PR number

Signed-off-by: Tetiana Kravchenko <[email protected]>

* remove orchestrator if cluster name is mepty

Signed-off-by: Tetiana Kravchenko <[email protected]>

* changes needed to pass golangci-lint

Signed-off-by: Tetiana Kravchenko <[email protected]>

Signed-off-by: Tetiana Kravchenko <[email protected]>
(cherry picked from commit 6c01bc6)
  • Loading branch information
tetianakravchenko authored and mergify[bot] committed Nov 2, 2022
1 parent 0693623 commit d73bc56
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]

*Affecting all Beats*

- Fix namespacing for agent self-monitoring, CPU no longer reports as zero. {pull}32336[32336]
- Fix namespacing on self-monitoring {pull}32336[32336]
- Expand fields in `decode_json_fields` if target is set. {issue}31712[31712] {pull}32010[32010]
- 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
15 changes: 10 additions & 5 deletions libbeat/processors/add_cloud_metadata/provider_google_gce_test.go
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)
}

0 comments on commit d73bc56

Please sign in to comment.