Skip to content

Commit

Permalink
🐛 Add back k8s node feature flag check (#2878)
Browse files Browse the repository at this point in the history
We had this check in v8 and lost it during migration.

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Dec 19, 2023
1 parent a6c5fa4 commit 685dc34
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
15 changes: 8 additions & 7 deletions providers/k8s/connection/manifest/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v9"
"go.mondoo.com/cnquery/v9/providers"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/plugin"
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestManifestDiscovery(t *testing.T) {
HasRecording: false,
CreateResource: resources.CreateResource,
}
inv, err := resources.Discover(pluginRuntime)
inv, err := resources.Discover(pluginRuntime, cnquery.Features{})
require.NoError(t, err)
require.Len(t, inv.Spec.Assets, 2)

Expand All @@ -89,7 +90,7 @@ func TestManifestDiscovery(t *testing.T) {
HasRecording: false,
CreateResource: resources.CreateResource,
}
inv, err = resources.Discover(pluginRuntime)
inv, err = resources.Discover(pluginRuntime, cnquery.Features{})
require.NoError(t, err)
require.Len(t, inv.Spec.Assets, 2)

Expand All @@ -99,7 +100,7 @@ func TestManifestDiscovery(t *testing.T) {
HasRecording: false,
CreateResource: resources.CreateResource,
}
inv, err = resources.Discover(pluginRuntime)
inv, err = resources.Discover(pluginRuntime, cnquery.Features{})
require.NoError(t, err)
require.Len(t, inv.Spec.Assets, 1)
}
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestOperatorManifest(t *testing.T) {
HasRecording: false,
CreateResource: resources.CreateResource,
}
inv, err := resources.Discover(pluginRuntime)
inv, err := resources.Discover(pluginRuntime, cnquery.Features{})
require.NoError(t, err)
require.Len(t, inv.Spec.Assets, 2)

Expand Down Expand Up @@ -180,7 +181,7 @@ func TestOperatorManifestWithNamespaceFilter(t *testing.T) {
HasRecording: false,
CreateResource: resources.CreateResource,
}
inv, err := resources.Discover(pluginRuntime)
inv, err := resources.Discover(pluginRuntime, cnquery.Features{})
require.NoError(t, err)
require.Len(t, inv.Spec.Assets, 2)

Expand Down Expand Up @@ -227,7 +228,7 @@ func TestManifestNoObjects(t *testing.T) {
HasRecording: false,
CreateResource: resources.CreateResource,
}
inv, err := resources.Discover(pluginRuntime)
inv, err := resources.Discover(pluginRuntime, cnquery.Features{})
require.NoError(t, err)
require.Len(t, inv.Spec.Assets, 1)

Expand Down Expand Up @@ -272,7 +273,7 @@ func TestManifestDir(t *testing.T) {
HasRecording: false,
CreateResource: resources.CreateResource,
}
inv, err := resources.Discover(pluginRuntime)
inv, err := resources.Discover(pluginRuntime, cnquery.Features{})
require.NoError(t, err)
require.Len(t, inv.Spec.Assets, 3)

Expand Down
7 changes: 4 additions & 3 deletions providers/k8s/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

"go.mondoo.com/cnquery/v9"
"go.mondoo.com/cnquery/v9/llx"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/plugin"
Expand Down Expand Up @@ -126,7 +127,7 @@ func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
}
}

inventory, err := s.discover(conn)
inventory, err := s.discover(conn, req.Features)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,7 +196,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
return conn, err
}

func (s *Service) discover(conn shared.Connection) (*inventory.Inventory, error) {
func (s *Service) discover(conn shared.Connection, features cnquery.Features) (*inventory.Inventory, error) {
if conn.InventoryConfig().Discover == nil {
return nil, nil
}
Expand All @@ -206,7 +207,7 @@ func (s *Service) discover(conn shared.Connection) (*inventory.Inventory, error)
return nil, errors.New("connection " + strconv.FormatUint(uint64(conn.ID()), 10) + " not found")
}

return resources.Discover(runtime)
return resources.Discover(runtime, features)
}

func (s *Service) GetData(req *plugin.DataReq) (*plugin.DataRes, error) {
Expand Down
11 changes: 6 additions & 5 deletions providers/k8s/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v9"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v9/providers/k8s/connection/shared"
Expand Down Expand Up @@ -83,7 +84,7 @@ func (f *NamespaceFilterOpts) skipNamespace(namespace string) bool {
return false
}

func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
func Discover(runtime *plugin.Runtime, features cnquery.Features) (*inventory.Inventory, error) {
conn := runtime.Connection.(shared.Connection)

in := &inventory.Inventory{Spec: &inventory.InventorySpec{
Expand Down Expand Up @@ -136,7 +137,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
if err != nil {
return nil, err
}
setRelatedAssets(conn, root, assets, od)
setRelatedAssets(conn, root, assets, od, features)
in.Spec.Assets = append(in.Spec.Assets, assets...)
} else {
nss, err := discoverNamespaces(conn, invConfig, "", nil, nsFilter)
Expand All @@ -159,7 +160,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
if err != nil {
return nil, err
}
setRelatedAssets(conn, ns, assets, od)
setRelatedAssets(conn, ns, assets, od, features)
in.Spec.Assets = append(in.Spec.Assets, assets...)
}
}
Expand Down Expand Up @@ -978,7 +979,7 @@ func createPlatformData(objectKind, runtime string) (*inventory.Platform, error)
return platformData, nil
}

func setRelatedAssets(conn shared.Connection, root *inventory.Asset, assets []*inventory.Asset, od *PlatformIdOwnershipIndex) {
func setRelatedAssets(conn shared.Connection, root *inventory.Asset, assets []*inventory.Asset, od *PlatformIdOwnershipIndex, features cnquery.Features) {
// everything is connected to the root asset
root.RelatedAssets = append(root.RelatedAssets, assets...)

Expand All @@ -1000,7 +1001,7 @@ func setRelatedAssets(conn shared.Connection, root *inventory.Asset, assets []*i
// from the ownerReference field
if platformEntry, ok := od.GetKubernetesObjectData(ownerPlatformId); ok {
platformData, err := createPlatformData(platformEntry.Kind, conn.Runtime())
if err != nil {
if err != nil || (!features.IsActive(cnquery.K8sNodeDiscovery) && platformData.Name == "k8s-node") {
continue
}
a.RelatedAssets = append(a.RelatedAssets, &inventory.Asset{
Expand Down

0 comments on commit 685dc34

Please sign in to comment.