Skip to content

Commit

Permalink
🐛 Add error message for unsupported k8s API version (#2156)
Browse files Browse the repository at this point in the history
* 🐛 Add error message for unsupported k8s API version

Fixes #2129

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Oct 10, 2023
1 parent d437b19 commit 7593191
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions providers/k8s/connection/shared/resources/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
22 changes: 22 additions & 0 deletions providers/k8s/resources/testdata/nginx-deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions providers/k8s/resources/unsupported_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 7593191

Please sign in to comment.