diff --git a/go.mod b/go.mod index 0d955d6a..a116b305 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/spf13/cobra v1.1.3 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.7.0 + github.com/thoas/go-funk v0.7.0 go.uber.org/zap v1.16.0 // indirect golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect diff --git a/go.sum b/go.sum index 25633e3a..595706e3 100644 --- a/go.sum +++ b/go.sum @@ -617,6 +617,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/thoas/go-funk v0.7.0 h1:GmirKrs6j6zJbhJIficOsz2aAI7700KsU/5YrdHRM1Y= +github.com/thoas/go-funk v0.7.0/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= diff --git a/pkg/kube/resources.go b/pkg/kube/resources.go index be3edcf0..580843b8 100644 --- a/pkg/kube/resources.go +++ b/pkg/kube/resources.go @@ -15,11 +15,13 @@ import ( conf "github.com/fairwindsops/polaris/pkg/config" "github.com/sirupsen/logrus" + "github.com/thoas/go-funk" corev1 "k8s.io/api/core/v1" v1beta1 "k8s.io/api/extensions/v1beta1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" k8sYaml "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -208,7 +210,6 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac logrus.Errorf("Error fetching Ingresses: %v", err) return nil, err } - // Get arbitrary kinds here resources, err := restmapper.GetAPIGroupResources(kube.Discovery()) if err != nil { @@ -217,6 +218,40 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac } restMapper := restmapper.NewDiscoveryRESTMapper(resources) + handledKinds := []conf.TargetKind{ + conf.TargetContainer, + conf.TargetPod, + conf.TargetController, + conf.TargetIngress, + } + var additionalKinds []conf.TargetKind + for _, check := range c.CustomChecks { + if !funk.Contains(handledKinds, check.Target) { + additionalKinds = append(additionalKinds, check.Target) + handledKinds = append(handledKinds, check.Target) + } + } + + var arbitraryObjects []*unstructured.Unstructured + for _, kind := range additionalKinds { + apiVersion := "Not sure where to get this" + fqKind := schema.FromAPIVersionAndKind(apiVersion, string(kind)) + mapping, err := (restMapper).RESTMapping(fqKind.GroupKind(), fqKind.Version) + if err != nil { + logrus.Warnf("Error retrieving mapping of API %s and Kind %s because of error: %v", apiVersion, kind, err) + return nil, err + } + + objects, err := (*dynamic).Resource(mapping.Resource).Namespace("").List(ctx, metav1.ListOptions{}) + if err != nil { + logrus.Warnf("Error retrieving parent object API %s and Kind %s because of error: %v", mapping.Resource.Version, mapping.Resource.Resource, err) + return nil, err + } + for _, obj := range objects.Items { + arbitraryObjects = append(arbitraryObjects, &obj) + } + } + objectCache := map[string]unstructured.Unstructured{} controllers, err := LoadControllers(ctx, pods.Items, dynamic, &restMapper, objectCache) @@ -234,7 +269,7 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac Namespaces: namespaces.Items, Controllers: controllers, Ingresses: ingressList.Items, - ArbitraryKinds: []*unstructured.Unstructured{}, + ArbitraryKinds: arbitraryObjects, } return &api, nil } diff --git a/pkg/kube/resources_test.go b/pkg/kube/resources_test.go index 0ade609c..d68b59e4 100644 --- a/pkg/kube/resources_test.go +++ b/pkg/kube/resources_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + conf "github.com/fairwindsops/polaris/pkg/config" "github.com/fairwindsops/polaris/test" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" @@ -88,7 +89,7 @@ func TestAddResourcesFromReader(t *testing.T) { func TestGetResourceFromAPI(t *testing.T) { k8s, dynamicInterface := test.SetupTestAPI(test.GetMockControllers("test")...) - resources, err := CreateResourceProviderFromAPI(context.Background(), k8s, "test", &dynamicInterface) + resources, err := CreateResourceProviderFromAPI(context.Background(), k8s, "test", &dynamicInterface, conf.Configuration{}) assert.Equal(t, nil, err, "Error should be nil") assert.Equal(t, "Cluster", resources.SourceType, "Should have type Path")