diff --git a/providers/k8s/connection/shared/resources/util.go b/providers/k8s/connection/shared/resources/util.go index 0e35db8a11..58a3f6dbee 100644 --- a/providers/k8s/connection/shared/resources/util.go +++ b/providers/k8s/connection/shared/resources/util.go @@ -16,6 +16,7 @@ import ( corev1 "k8s.io/api/core/v1" extensionsv1beta1 "k8s.io/api/extensions/v1beta1" "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" ) @@ -52,6 +53,9 @@ func GetPodSpec(obj runtime.Object) (*corev1.PodSpec, error) { podSpec = &x.Spec.Template.Spec case *appsv1.ReplicaSet: podSpec = &x.Spec.Template.Spec + case *unstructured.Unstructured: + gvk := x.GetObjectKind().GroupVersionKind() + return nil, fmt.Errorf("object %s with version %s/%s is not supported", gvk.Kind, gvk.Group, gvk.Version) default: return nil, fmt.Errorf("object type %v is not supported", x) } diff --git a/providers/k8s/resources/testdata/nginx-deployment.yaml b/providers/k8s/resources/testdata/nginx-deployment.yaml new file mode 100644 index 0000000000..cf1746d2ff --- /dev/null +++ b/providers/k8s/resources/testdata/nginx-deployment.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: nginx-deployment + naemspace: default + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/providers/k8s/resources/unsupported_test.go b/providers/k8s/resources/unsupported_test.go new file mode 100644 index 0000000000..45c603fba5 --- /dev/null +++ b/providers/k8s/resources/unsupported_test.go @@ -0,0 +1,38 @@ +// Copyright (c) Mondoo, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package resources + +import ( + "testing" + + "github.com/stretchr/testify/require" + "go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory" + "go.mondoo.com/cnquery/v9/providers/k8s/connection/manifest" + "go.mondoo.com/cnquery/v9/providers/k8s/connection/shared" + sharedres "go.mondoo.com/cnquery/v9/providers/k8s/connection/shared/resources" +) + +func TestManifestFile_OutdatedApi(t *testing.T) { + manifestFile := "./testdata/nginx-deployment.yaml" + + conn, err := manifest.NewConnection(0, &inventory.Asset{ + Connections: []*inventory.Config{ + { + Options: map[string]string{ + shared.OPTION_NAMESPACE: "default", + }, + }, + }, + }, manifest.WithManifestFile(manifestFile)) + require.NoError(t, err) + require.NotNil(t, conn) + + manifestConn := conn.(*manifest.Connection) + deployment := manifestConn.ManifestParser.Objects[0] + + parsed, err := sharedres.GetPodSpec(deployment) + require.Error(t, err) + require.Nil(t, parsed) + require.Equal(t, "object Deployment with version apps/v1beta1 is not supported", err.Error()) +}