Skip to content

Commit 88cb012

Browse files
committed
adding function to override sonarcube
1 parent a221dee commit 88cb012

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

internal/pkg/composable/providers/kubernetes/pod.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
"sync"
1010
"time"
1111

12-
"github.com/elastic/elastic-agent-autodiscover/utils"
13-
1412
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
1513
"github.com/elastic/elastic-agent-autodiscover/kubernetes/metadata"
14+
"github.com/elastic/elastic-agent-autodiscover/utils"
1615
c "github.com/elastic/elastic-agent-libs/config"
1716
"github.com/elastic/elastic-agent-libs/logp"
1817
"github.com/elastic/elastic-agent-libs/mapstr"
@@ -217,11 +216,7 @@ func (p *pod) emitRunning(pod *kubernetes.Pod) {
217216
if !p.managed {
218217
if ann, ok := data.mapping["annotations"]; ok {
219218
annotations, _ := ann.(mapstr.M)
220-
hints, incorrecthints := utils.GenerateHints(annotations, "", p.config.Prefix, true, allSupportedHints)
221-
for _, value := range incorrecthints { //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that have prefix co.elastic
222-
p.logger.Warnf("provided hint: %s/%s is not recognised as supported annotation for pod %s in namespace %s", p.config.Prefix, value, pod.Name, pod.ObjectMeta.Namespace)
223-
}
224-
219+
hints, _ := hintsCheck(annotations, "", p.config.Prefix, true, allSupportedHints, p.logger, pod)
225220
if len(hints) > 0 {
226221
p.logger.Debugf("Extracted hints are :%v", hints)
227222
hintsMapping := GenerateHintsMapping(hints, data.mapping, p.logger, "")
@@ -529,3 +524,12 @@ func updateProcessors(newprocessors []mapstr.M, processors []map[string]interfac
529524

530525
return processors
531526
}
527+
528+
// HintsCheck geenrates hints from provided annotations of the pod and logs any possible incorrect annotations that have been provided in the pod
529+
func hintsCheck(annotations mapstr.M, container string, prefix string, validate bool, allSupportedHints []string, logger *logp.Logger, pod *kubernetes.Pod) (mapstr.M, []string) {
530+
hints, incorrecthints := utils.GenerateHints(annotations, container, prefix, validate, allSupportedHints)
531+
for _, value := range incorrecthints { //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that have prefix co.elastic
532+
logger.Warnf("provided hint: %s/%s is not recognised as supported annotation for pod %s in namespace %s", prefix, value, pod.Name, pod.ObjectMeta.Namespace)
533+
}
534+
return hints, incorrecthints
535+
}

internal/pkg/composable/providers/kubernetes/pod_test.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
2424
"github.com/elastic/elastic-agent-autodiscover/kubernetes/metadata"
25-
"github.com/elastic/elastic-agent-autodiscover/utils"
2625
"github.com/elastic/elastic-agent-libs/mapstr"
2726

2827
c "github.com/elastic/elastic-agent-libs/config"
@@ -403,26 +402,26 @@ func TestGenerateHints(t *testing.T) {
403402
Status: kubernetes.PodStatus{PodIP: "127.0.0.5"},
404403
}
405404

406-
namespaceAnnotations := mapstr.M{
407-
"nsa": "nsb",
408-
}
409-
410-
data := generatePodData(pod, &podMeta{}, namespaceAnnotations)
405+
data := generatePodData(pod, &podMeta{}, mapstr.M{})
411406

412407
hints_result := mapstr.M{
413408
"hints": mapstr.M{
414-
"host": "${kubernetes.pod.ip}:6379",
415-
"package": "redis",
416-
"metricspath": "/metrics", // on purpose we have introduced a typo
417-
"period": "42s",
409+
"host": "${kubernetes.pod.ip}:6379",
410+
"package": "redis",
411+
"metricssssssspath": "/metrics", // on purpose we have introduced a typo
412+
"period": "42s",
418413
},
419414
}
420-
incorrecthints_results := []string{"hints/metricspath"}
415+
incorrecthints_results := []string{"hints/metricssssssspath"}
421416

422417
ann := data.mapping["annotations"]
423418
annotations, _ := ann.(mapstr.M)
424419
prefix := "co.elastic"
425-
hints, incorrecthints := utils.GenerateHints(annotations, "", prefix, true, allSupportedHints)
420+
421+
log, err := logger.New("hint-test", true)
422+
assert.NoError(t, err)
423+
424+
hints, incorrecthints := hintsCheck(annotations, "", prefix, true, allSupportedHints, log, pod)
426425

427426
assert.Equal(t, string(pod.GetUID()), data.uid)
428427
assert.Equal(t, hints, hints_result)

0 commit comments

Comments
 (0)