Skip to content

Commit

Permalink
fix cloud provider discovery logic
Browse files Browse the repository at this point in the history
Signed-off-by: Amir Malka <[email protected]>
  • Loading branch information
amirmalka committed Nov 21, 2023
1 parent 4d923e8 commit ae53637
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/armosec/gojay v1.2.15 // indirect
github.com/aws/aws-sdk-go v1.44.312 // indirect
github.com/aws/aws-sdk-go-v2 v1.19.1 // indirect
github.com/aws/aws-sdk-go-v2/config v1.18.30 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.29 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ github.com/armosec/utils-go v0.0.40 h1:FdH8TxRG0SH3heKLrZ0Mj0eq5dNgk5go8/mteSCg2
github.com/armosec/utils-go v0.0.40/go.mod h1:pDaq3SNKQ8wliAAOq4B8re9MWmT0bX9di2Jn1jZI7lE=
github.com/armosec/utils-k8s-go v0.0.20 h1:lLgowko5JVYf6LF4osQLgL+/w/LUptARdctShAChl7I=
github.com/armosec/utils-k8s-go v0.0.20/go.mod h1:wSP3ILpyp+2bqZZD7gdZVQ/RTK3+jV17l6Dk5xCqWCg=
github.com/aws/aws-sdk-go v1.44.312 h1:llrElfzeqG/YOLFFKjg1xNpZCFJ2xraIi3PqSuP+95k=
github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go-v2 v1.17.3/go.mod h1:uzbQtefpm44goOPmdKyAlXSNcwlRgF3ePWVW6EtJvvw=
github.com/aws/aws-sdk-go-v2 v1.19.1 h1:STs0lbbpXu3byTPcnRLghs2DH0yk9qKDo27TyyJSKsM=
github.com/aws/aws-sdk-go-v2 v1.19.1/go.mod h1:uzbQtefpm44goOPmdKyAlXSNcwlRgF3ePWVW6EtJvvw=
Expand Down Expand Up @@ -350,8 +352,6 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubescape/backend v0.0.13 h1:N+fH8giGGqvy3ff2li2AwG5guVduhdiPWyvZaZxrDCU=
github.com/kubescape/backend v0.0.13/go.mod h1:ug9NFmmxT4DcQx3sgdLRzlLPWMKGHE/fpbcYUm5G5Qo=
github.com/kubescape/backend v0.0.15 h1:LOQIhwu+tgXjm4QGOLnKuPxP8zOkqd7NaiQbbnYVoCE=
github.com/kubescape/backend v0.0.15/go.mod h1:ug9NFmmxT4DcQx3sgdLRzlLPWMKGHE/fpbcYUm5G5Qo=
github.com/kubescape/go-logger v0.0.21 h1:4ZRIEw3UGUH6BG/cH3yiqFipzQSfGAoCrxlsZuk37ys=
Expand Down Expand Up @@ -642,6 +642,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -715,11 +716,13 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -731,6 +734,7 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
17 changes: 17 additions & 0 deletions watch/cloudprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package watch

import (
"github.com/kubescape/k8s-interface/cloudsupport"
"github.com/kubescape/k8s-interface/k8sinterface"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func setCloudProvider(k8sApi *k8sinterface.KubernetesApi) error {
nodeList, err := k8sApi.KubernetesClient.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil {
return err
}
cloudProvider = cloudsupport.GetCloudProvider(nodeList)
return nil
}
1 change: 1 addition & 0 deletions watch/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import (

var (
globalHTTPContext = context.Background()
cloudProvider = ""
)
2 changes: 1 addition & 1 deletion watch/jsonformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func setInstallationData(jsonReport *jsonFormat, config armometadata.ClusterConf
jsonReport.InstallationData.PostureScanEnabled = config.PostureScanEnabled
jsonReport.InstallationData.OtelCollectorEnabled = config.OtelCollectorEnabled
jsonReport.InstallationData.ClusterName = config.ClusterName
jsonReport.InstallationData.ClusterProvider = config.ClusterProvider
jsonReport.InstallationData.ClusterProvider = cloudProvider
jsonReport.InstallationData.RelevantImageVulnerabilitiesConfiguration = config.RelevantImageVulnerabilitiesConfiguration

logger.L().Debug("setting installation data", helpers.Interface("installation data", jsonReport.InstallationData))
Expand Down
5 changes: 4 additions & 1 deletion watch/jsonformat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestIsEmptyFirstReport(test *testing.T) {
}

func TestSetInstallationData(t *testing.T) {
// setting global provider value since it is taken from global variable instead of config
cloudProvider = "test"

trueBool := true
falseBool := false
testCases := []struct {
Expand Down Expand Up @@ -153,7 +156,7 @@ func TestSetInstallationData(t *testing.T) {
t.Errorf("RelevantImageVulnerabilitiesConfiguration is not equal")
}

if jsonReport.InstallationData.ClusterProvider != tc.config.ClusterProvider {
if jsonReport.InstallationData.ClusterProvider != cloudProvider {
t.Errorf("ClusterProvider is not equal")
}
}
Expand Down
8 changes: 8 additions & 0 deletions watch/watchhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"sync"

logger "github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
"github.com/kubescape/k8s-interface/k8sinterface"
"github.com/kubescape/kollector/config"
"github.com/kubescape/kollector/consts"
Expand Down Expand Up @@ -143,6 +145,12 @@ func CreateWatchHandler(config config.IConfig) (*WatchHandler, error) {
return nil, fmt.Errorf("failed to set event receiver url: %s", err.Error())
}

if err = setCloudProvider(k8sAPiObj); err != nil {
logger.L().Error("failed to set cloud provider", helpers.Error(err))
} else {
logger.L().Info("setting cloud provider", helpers.String("cloudProvider", cloudProvider))
}

result := WatchHandler{RestAPIClient: k8sAPiObj.KubernetesClient,
WebSocketHandle: createWebSocketHandler(erURL, config.AccessKey()),
extensionsClient: extensionsClientSet,
Expand Down

0 comments on commit ae53637

Please sign in to comment.