From c7d1292c7dfb5abba04b30b5543cdc8825ca5364 Mon Sep 17 00:00:00 2001 From: Alessio Greggi Date: Tue, 14 Feb 2023 13:45:23 +0100 Subject: [PATCH 1/2] fix(hostsensorutils): improve cloud provider detection Signed-off-by: Alessio Greggi --- .../hostsensorutils/hostsensorgetfrompod.go | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/core/pkg/hostsensorutils/hostsensorgetfrompod.go b/core/pkg/hostsensorutils/hostsensorgetfrompod.go index 874d7db3..18732892 100644 --- a/core/pkg/hostsensorutils/hostsensorgetfrompod.go +++ b/core/pkg/hostsensorutils/hostsensorgetfrompod.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "reflect" "strings" "sync" @@ -221,6 +222,17 @@ func (hsh *HostSensorHandler) GetKubeletConfigurations(ctx context.Context) ([]h return res, err } +// hasCloudProviderInfo iterate over the []hostsensor.HostSensorDataEnvelope list to find info about cloud provider. +// If information are found, ther return true. Return false otherwise. +func hasCloudProviderInfo(cpi []hostsensor.HostSensorDataEnvelope) bool { + for index, _ := range cpi { + if !reflect.DeepEqual(cpi[index].GetData(), json.RawMessage("{}\n")) { + return true + } + } + return false +} + func (hsh *HostSensorHandler) CollectResources(ctx context.Context) ([]hostsensor.HostSensorDataEnvelope, map[string]apis.StatusInfo, error) { res := make([]hostsensor.HostSensorDataEnvelope, 0) infoMap := make(map[string]apis.StatusInfo) @@ -323,18 +335,9 @@ func (hsh *HostSensorHandler) CollectResources(ctx context.Context) ([]hostsenso res = append(res, kcData...) } - // GetControlPlaneInfo - kcData, err = hsh.GetControlPlaneInfo(ctx) - if err != nil { - addInfoToMap(ControlPlaneInfo, infoMap, err) - logger.L().Ctx(ctx).Warning(err.Error()) - } - if len(kcData) > 0 { - res = append(res, kcData...) - } - // GetCloudProviderInfo kcData, err = hsh.GetCloudProviderInfo(ctx) + isCloudProvider := hasCloudProviderInfo(kcData) if err != nil { addInfoToMap(CloudProviderInfo, infoMap, err) logger.L().Ctx(ctx).Warning(err.Error()) @@ -343,6 +346,18 @@ func (hsh *HostSensorHandler) CollectResources(ctx context.Context) ([]hostsenso res = append(res, kcData...) } + // GetControlPlaneInfo + if !isCloudProvider { // we retrieve control plane info only if we are not using a cloud provider + kcData, err = hsh.GetControlPlaneInfo(ctx) + if err != nil { + addInfoToMap(ControlPlaneInfo, infoMap, err) + logger.L().Ctx(ctx).Warning(err.Error()) + } + if len(kcData) > 0 { + res = append(res, kcData...) + } + } + // GetCNIInfo kcData, err = hsh.GetCNIInfo(ctx) if err != nil { From 159d3907b506377468140edfa97fe882a7544e2a Mon Sep 17 00:00:00 2001 From: Alessio Greggi Date: Thu, 16 Feb 2023 11:37:12 +0100 Subject: [PATCH 2/2] style(hostsensorutils): simplify code with gofmt Signed-off-by: Alessio Greggi --- core/pkg/hostsensorutils/hostsensorgetfrompod.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pkg/hostsensorutils/hostsensorgetfrompod.go b/core/pkg/hostsensorutils/hostsensorgetfrompod.go index 18732892..74e7da07 100644 --- a/core/pkg/hostsensorutils/hostsensorgetfrompod.go +++ b/core/pkg/hostsensorutils/hostsensorgetfrompod.go @@ -225,7 +225,7 @@ func (hsh *HostSensorHandler) GetKubeletConfigurations(ctx context.Context) ([]h // hasCloudProviderInfo iterate over the []hostsensor.HostSensorDataEnvelope list to find info about cloud provider. // If information are found, ther return true. Return false otherwise. func hasCloudProviderInfo(cpi []hostsensor.HostSensorDataEnvelope) bool { - for index, _ := range cpi { + for index := range cpi { if !reflect.DeepEqual(cpi[index].GetData(), json.RawMessage("{}\n")) { return true }