mirror of
https://github.com/FairwindsOps/polaris.git
synced 2026-08-23 22:26:34 +00:00
Compare commits
2
Commits
9.6.4
...
rb/fix-order
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ff5a2cf94 | ||
|
|
d46a8c86be |
+12
-13
@@ -113,20 +113,19 @@ var fixCommand = &cobra.Command{
|
||||
|
||||
updatedYamlContent := ""
|
||||
if len(allMutations) > 0 {
|
||||
for _, resources := range kubeResources.Resources {
|
||||
for _, resource := range resources {
|
||||
key := fmt.Sprintf("%s/%s/%s", resource.Kind, resource.Resource.GetName(), resource.Resource.GetNamespace())
|
||||
mutations := allMutations[key]
|
||||
mutatedYamlContent, err := mutation.ApplyAllMutations(string(resource.OriginalObjectYAML), mutations)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error applying schema mutations to the resource %s: %v", key, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if updatedYamlContent != "" {
|
||||
updatedYamlContent += "\n---\n"
|
||||
}
|
||||
updatedYamlContent += mutatedYamlContent
|
||||
for _, resource := range kubeResources.Resources {
|
||||
key := fmt.Sprintf("%s/%s/%s", resource.Kind, resource.Resource.GetName(), resource.Resource.GetNamespace())
|
||||
fmt.Println("resource", key)
|
||||
mutations := allMutations[key]
|
||||
mutatedYamlContent, err := mutation.ApplyAllMutations(string(resource.OriginalObjectYAML), mutations)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error applying schema mutations to the resource %s: %v", key, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if updatedYamlContent != "" {
|
||||
updatedYamlContent += "\n---\n"
|
||||
}
|
||||
updatedYamlContent += mutatedYamlContent
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+24
-38
@@ -51,48 +51,34 @@ type ResourceProvider struct {
|
||||
SourceType string
|
||||
Nodes []corev1.Node
|
||||
Namespaces []corev1.Namespace
|
||||
Resources resourceKindMap
|
||||
Resources resourceSet
|
||||
}
|
||||
|
||||
type resourceKindMap map[string][]GenericResource
|
||||
type resourceSet []GenericResource
|
||||
|
||||
func (rkm resourceKindMap) addResource(r GenericResource) {
|
||||
gvk := r.Resource.GroupVersionKind()
|
||||
var key string
|
||||
if gvk.Group != "" {
|
||||
key = gvk.Group + "/" + gvk.Kind
|
||||
} else {
|
||||
key = gvk.Kind
|
||||
}
|
||||
rkm[key] = append(rkm[key], r)
|
||||
}
|
||||
|
||||
func (rkm resourceKindMap) addResources(rs []GenericResource) {
|
||||
func (rs resourceSet) GetNumberOfControllers() int {
|
||||
total := 0
|
||||
for _, r := range rs {
|
||||
rkm.addResource(r)
|
||||
}
|
||||
}
|
||||
|
||||
func (rkm resourceKindMap) GetLength() int {
|
||||
total := 0
|
||||
for _, rs := range rkm {
|
||||
total += len(rs)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func (rkm resourceKindMap) GetNumberOfControllers() int {
|
||||
total := 0
|
||||
for _, rs := range rkm {
|
||||
for _, r := range rs {
|
||||
if r.PodSpec != nil {
|
||||
total++
|
||||
}
|
||||
if r.PodSpec != nil {
|
||||
total++
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func (rs resourceSet) GetAllOfGroupKind(gk string) []GenericResource {
|
||||
return funk.Filter(rs, func(res GenericResource) bool {
|
||||
gvk := res.Resource.GroupVersionKind()
|
||||
var key string
|
||||
if gvk.Group != "" {
|
||||
key = gvk.Group + "/" + gvk.Kind
|
||||
} else {
|
||||
key = gvk.Kind
|
||||
}
|
||||
return key == gk
|
||||
}).([]GenericResource)
|
||||
}
|
||||
|
||||
// This is here for backward compatibility reasons
|
||||
func maybeTransformKindIntoGroupKind(k string) string {
|
||||
if k == "Ingress" {
|
||||
@@ -122,7 +108,7 @@ func newResourceProvider(version, sourceType, sourceName string) ResourceProvide
|
||||
CreationTime: time.Now(),
|
||||
Nodes: make([]corev1.Node, 0),
|
||||
Namespaces: make([]corev1.Namespace, 0),
|
||||
Resources: make(map[string][]GenericResource),
|
||||
Resources: make([]GenericResource, 0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,7 +179,7 @@ func CreateResourceProviderFromResource(ctx context.Context, workload string) (*
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resources.Resources.addResource(workloadObj)
|
||||
resources.Resources = append(resources.Resources, workloadObj)
|
||||
return &resources, nil
|
||||
}
|
||||
|
||||
@@ -377,7 +363,7 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac
|
||||
|
||||
provider.Nodes = nodes.Items
|
||||
provider.Namespaces = namespaces.Items
|
||||
provider.Resources.addResources(kubernetesResources)
|
||||
provider.Resources = append(provider.Resources, kubernetesResources...)
|
||||
logrus.Info("Done loading Kubernetes resources")
|
||||
return &provider, nil
|
||||
}
|
||||
@@ -479,13 +465,13 @@ func (resources *ResourceProvider) addResourceFromString(contents string) error
|
||||
return err
|
||||
}
|
||||
workload.OriginalObjectYAML = contentBytes
|
||||
resources.Resources.addResource(workload)
|
||||
resources.Resources = append(resources.Resources, workload)
|
||||
} else {
|
||||
newResource, err := NewGenericResourceFromBytes(contentBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resources.Resources.addResource(newResource)
|
||||
resources.Resources = append(resources.Resources, newResource)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
+18
-14
@@ -21,9 +21,11 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
conf "github.com/fairwindsops/polaris/pkg/config"
|
||||
"github.com/thoas/go-funk"
|
||||
"github.com/fairwindsops/polaris/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
conf "github.com/fairwindsops/polaris/pkg/config"
|
||||
)
|
||||
|
||||
func TestGetResourcesFromPath(t *testing.T) {
|
||||
@@ -42,12 +44,10 @@ func TestGetResourcesFromPath(t *testing.T) {
|
||||
assert.Equal(t, "two", provider.Namespaces[0].ObjectMeta.Name)
|
||||
|
||||
namespaceCount := map[string]int{}
|
||||
for _, resources := range provider.Resources {
|
||||
for _, controller := range resources {
|
||||
namespaceCount[controller.ObjectMeta.GetNamespace()]++
|
||||
}
|
||||
for _, controller := range provider.Resources {
|
||||
namespaceCount[controller.ObjectMeta.GetNamespace()]++
|
||||
}
|
||||
assert.Equal(t, 11, provider.Resources.GetLength())
|
||||
assert.Equal(t, 11, len(provider.Resources))
|
||||
assert.Equal(t, 10, namespaceCount[""])
|
||||
assert.Equal(t, 1, namespaceCount["two"])
|
||||
}
|
||||
@@ -64,8 +64,11 @@ func TestGetMultipleResourceFromSingleFile(t *testing.T) {
|
||||
|
||||
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
||||
|
||||
assert.Equal(t, 1, len(resources.Resources["extensions/Deployment"]), "Should have one controller")
|
||||
assert.Equal(t, "dashboard", resources.Resources["extensions/Deployment"][0].PodSpec.Containers[0].Name)
|
||||
assert.Equal(t, 6, len(resources.Resources), "Should have 6 resources")
|
||||
deployment := funk.Find(resources.Resources, func(res GenericResource) bool {
|
||||
return res.Resource.GroupVersionKind().Kind == "Deployment"
|
||||
}).(GenericResource)
|
||||
assert.Equal(t, "dashboard", deployment.PodSpec.Containers[0].Name)
|
||||
|
||||
assert.Equal(t, 2, len(resources.Namespaces), "Should have a namespace")
|
||||
assert.Equal(t, "polaris", resources.Namespaces[0].ObjectMeta.Name)
|
||||
@@ -87,8 +90,11 @@ func TestAddResourcesFromReader(t *testing.T) {
|
||||
|
||||
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
||||
|
||||
assert.Equal(t, 1, len(resources.Resources["extensions/Deployment"]), "Should have one controller")
|
||||
assert.Equal(t, "dashboard", resources.Resources["extensions/Deployment"][0].PodSpec.Containers[0].Name)
|
||||
assert.Equal(t, 6, len(resources.Resources), "Should have 6 resources")
|
||||
deployment := funk.Find(resources.Resources, func(res GenericResource) bool {
|
||||
return res.Resource.GroupVersionKind().Kind == "Deployment"
|
||||
}).(GenericResource)
|
||||
assert.Equal(t, "dashboard", deployment.PodSpec.Containers[0].Name)
|
||||
|
||||
assert.Equal(t, 2, len(resources.Namespaces), "Should have a namespace")
|
||||
assert.Equal(t, "polaris", resources.Namespaces[0].ObjectMeta.Name)
|
||||
@@ -158,10 +164,8 @@ func TestGetResourceFromAPI(t *testing.T) {
|
||||
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
||||
assert.Equal(t, 5, len(resources.Resources), "Should have 5 controllers")
|
||||
|
||||
for _, controllers := range resources.Resources {
|
||||
for _, ctrl := range controllers {
|
||||
expectedNames[ctrl.ObjectMeta.GetName()] = true
|
||||
}
|
||||
for _, ctrl := range resources.Resources {
|
||||
expectedNames[ctrl.ObjectMeta.GetName()] = true
|
||||
}
|
||||
for name, val := range expectedNames {
|
||||
assert.Equal(t, true, val, name)
|
||||
|
||||
@@ -72,7 +72,10 @@ func TestControllerLevelChecks(t *testing.T) {
|
||||
Severity: "danger",
|
||||
Category: "Reliability",
|
||||
}
|
||||
for _, controller := range res.Resources["Deployment"] {
|
||||
for _, controller := range res.Resources {
|
||||
if controller.Resource.GroupVersionKind().Kind != "Deployment" {
|
||||
continue
|
||||
}
|
||||
actualResult, err := applyControllerSchemaChecks(&c, nil, controller)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -96,7 +99,7 @@ func TestControllerLevelChecks(t *testing.T) {
|
||||
|
||||
res, err := kube.CreateResourceProviderFromPath("../kube/test_files/test_1")
|
||||
assert.Equal(t, nil, err, "Error should be nil")
|
||||
assert.Equal(t, 11, res.Resources.GetLength())
|
||||
assert.Equal(t, 11, len(res.Resources))
|
||||
testResources(res)
|
||||
|
||||
replicaSpec := map[string]interface{}{"replicas": 2}
|
||||
@@ -111,7 +114,7 @@ func TestControllerLevelChecks(t *testing.T) {
|
||||
k8s, dynamicClient := test.SetupTestAPI(&d1, &p1, &d2, &p2)
|
||||
res, err = kube.CreateResourceProviderFromAPI(context.Background(), k8s, "test", dynamicClient, conf.Configuration{})
|
||||
assert.Equal(t, err, nil, "error should be nil")
|
||||
assert.Equal(t, 2, res.Resources.GetLength(), "Should have two controllers")
|
||||
assert.Equal(t, 2, len(res.Resources), "Should have two controllers")
|
||||
testResources(res)
|
||||
}
|
||||
|
||||
|
||||
+2
-10
@@ -173,18 +173,10 @@ func hasExemptionAnnotation(objMeta metaV1.Object, checkID string) bool {
|
||||
|
||||
// ApplyAllSchemaChecksToResourceProvider applies all available checks to a ResourceProvider
|
||||
func ApplyAllSchemaChecksToResourceProvider(conf *config.Configuration, resourceProvider *kube.ResourceProvider) ([]Result, error) {
|
||||
results := []Result{}
|
||||
if resourceProvider == nil {
|
||||
return nil, errors.New("No resource provider set, cannot apply schema checks")
|
||||
}
|
||||
for _, resources := range resourceProvider.Resources {
|
||||
kindResults, err := ApplyAllSchemaChecksToAllResources(conf, resourceProvider, resources)
|
||||
if err != nil {
|
||||
return results, err
|
||||
}
|
||||
results = append(results, kindResults...)
|
||||
}
|
||||
return results, nil
|
||||
return ApplyAllSchemaChecksToAllResources(conf, resourceProvider, resourceProvider.Resources)
|
||||
}
|
||||
|
||||
// ApplyAllSchemaChecksToAllResources applies available checks to a list of resources
|
||||
@@ -381,7 +373,7 @@ func applySchemaCheck(conf *config.Configuration, checkID string, test schemaTes
|
||||
logrus.Warnf("No ResourceProvider available, check %s will not work in this context (e.g. admission control)", checkID)
|
||||
break
|
||||
}
|
||||
resources := test.ResourceProvider.Resources[groupkind]
|
||||
resources := test.ResourceProvider.Resources.GetAllOfGroupKind(groupkind)
|
||||
namespace := test.Resource.ObjectMeta.GetNamespace()
|
||||
if test.Resource.Kind == "Namespace" {
|
||||
namespace = test.Resource.ObjectMeta.GetName()
|
||||
|
||||
@@ -51,9 +51,8 @@ func TestMutations(t *testing.T) {
|
||||
assert.Len(t, results, 1)
|
||||
allMutations := mutation.GetMutationsFromResults(results)
|
||||
assert.Len(t, allMutations, 1)
|
||||
for _, resources := range tc.resources.Resources {
|
||||
assert.Len(t, resources, 1)
|
||||
key := fmt.Sprintf("%s/%s/%s", resources[0].Kind, resources[0].Resource.GetName(), resources[0].Resource.GetNamespace())
|
||||
for _, resource := range tc.resources.Resources {
|
||||
key := fmt.Sprintf("%s/%s/%s", resource.Kind, resource.Resource.GetName(), resource.Resource.GetNamespace())
|
||||
mutations := allMutations[key]
|
||||
yamlContent, err := mutation.ApplyAllMutations(tc.manifest, mutations)
|
||||
assert.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user