Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
🐛 Ignore groups not served by the cluster (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerr authored Jun 9, 2022
1 parent 6a30cc0 commit 0ce511d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
19 changes: 14 additions & 5 deletions internal/k8sinternal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type ClientOptions struct {

type KubeClient interface {
// GetAllResources gets all supported resources from the cluster
GetAllResources(options ClientOptions) []k8s.Resource
GetAllResources(options ClientOptions) ([]k8s.Resource, error)
// GetKubernetesVersion returns the kubernetes client version
GetKubernetesVersion() (*version.Info, error)
// ServerPreferredResources returns the supported resources with the version preferred by the server.
Expand All @@ -123,11 +123,14 @@ func NewKubeClient(dynamic dynamic.Interface, discovery discovery.DiscoveryInter
}

// GetAllResources gets all supported resources from the cluster
func (kc kubeClient) GetAllResources(options ClientOptions) []k8s.Resource {
func (kc kubeClient) GetAllResources(options ClientOptions) ([]k8s.Resource, error) {
var resources []k8s.Resource

lists, err := kc.ServerPreferredResources()
if err == nil {
if err != nil {
return nil, err
}
if lists != nil {
for _, list := range lists {
if len(list.APIResources) == 0 {
continue
Expand Down Expand Up @@ -169,7 +172,7 @@ func (kc kubeClient) GetAllResources(options ClientOptions) []k8s.Resource {
if !options.IncludeGenerated {
resources = excludeGenerated(resources)
}
return resources
return resources, nil
}

// unstructuredToObject unstructured to Go typed object conversions
Expand Down Expand Up @@ -207,5 +210,11 @@ func (kc kubeClient) GetKubernetesVersion() (*version.Info, error) {

// ServerPreferredResources returns the supported resources with the version preferred by the server.
func (kc kubeClient) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
return discovery.ServerPreferredResources(kc.discoveryClient)
list, err := discovery.ServerPreferredResources(kc.discoveryClient)
// If a group is not served by the cluster the resources of this group will not be audited.
var e *discovery.ErrGroupDiscoveryFailed
if errors.As(err, &e) {
return list, nil
}
return list, err
}
22 changes: 13 additions & 9 deletions internal/k8sinternal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ func TestGetAllResources(t *testing.T) {
}

client := newFakeKubeClient(resources...)
assert.Len(t, client.GetAllResources(k8sinternal.ClientOptions{}), len(resourceTemplates)*len(namespaces))
assert.Len(
t,
client.GetAllResources(k8sinternal.ClientOptions{Namespace: namespaces[0]}),
len(resourceTemplates),
)
k8sresources, err := client.GetAllResources(k8sinternal.ClientOptions{})
require.NoError(t, err)
assert.Len(t, k8sresources, len(resourceTemplates)*len(namespaces))

k8sresources, err = client.GetAllResources(k8sinternal.ClientOptions{Namespace: namespaces[0]})
require.NoError(t, err)
assert.Len(t, k8sresources, len(resourceTemplates))
}

func setNamespace(resource k8s.Resource, namespace string) {
Expand Down Expand Up @@ -147,21 +148,24 @@ func TestIncludeGenerated(t *testing.T) {
require.NoError(t, err)

// Test IncludeGenerated = false
resources := client.GetAllResources(
resources, err := client.GetAllResources(
k8sinternal.ClientOptions{Namespace: namespace, IncludeGenerated: false},
)
require.NoError(t, err)
assert.False(t, hasPod(resources), "Expected no pods for IncludeGenerated=false")

// Test IncludeGenerated unspecified defaults to false
resources = client.GetAllResources(
resources, err = client.GetAllResources(
k8sinternal.ClientOptions{Namespace: namespace},
)
require.NoError(t, err)
assert.False(t, hasPod(resources), "Expected no pods if IncludeGenerated is unspecified (ie. default to false)")

// Test IncludeGenerated = true
resources = client.GetAllResources(
resources, err = client.GetAllResources(
k8sinternal.ClientOptions{Namespace: namespace, IncludeGenerated: true},
)
require.NoError(t, err)
assert.True(t, hasPod(resources), "Expected pods for IncludeGenerated=true")
}

Expand Down
10 changes: 8 additions & 2 deletions kubeaudit.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ func (a *Kubeaudit) AuditCluster(options AuditOptions) (*Report, error) {
return nil, err
}

resources := getResourcesFromClient(client, options)
resources, err := getResourcesFromClient(client, options)
if err != nil {
return nil, err
}
results, err := auditResources(resources, a.auditors)
if err != nil {
return nil, err
Expand All @@ -190,7 +193,10 @@ func (a *Kubeaudit) AuditLocal(configpath string, options AuditOptions) (*Report
return nil, err
}

resources := getResourcesFromClient(client, options)
resources, err := getResourcesFromClient(client, options)
if err != nil {
return nil, err
}
results, err := auditResources(resources, a.auditors)
if err != nil {
return nil, err
Expand Down
10 changes: 7 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import (
"gopkg.in/yaml.v3"
)

func getResourcesFromClient(client k8sinternal.KubeClient, options k8sinternal.ClientOptions) []KubeResource {
func getResourcesFromClient(client k8sinternal.KubeClient, options k8sinternal.ClientOptions) ([]KubeResource, error) {
var resources []KubeResource

for _, resource := range client.GetAllResources(options) {
k8sresources, err := client.GetAllResources(options)
if err != nil {
return nil, err
}
for _, resource := range k8sresources {
resources = append(resources, &kubeResource{object: resource})
}

return resources
return resources, nil
}

func getResourcesFromManifest(data []byte) ([]KubeResource, error) {
Expand Down

0 comments on commit 0ce511d

Please sign in to comment.