Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate container list to allow filtering #380

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/lint/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/derailed/popeye/internal/dao"
"github.com/derailed/popeye/internal/db"
"github.com/derailed/popeye/internal/issues"
"github.com/derailed/popeye/internal/rules"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
)
Expand All @@ -31,6 +32,12 @@ func NewCronJob(co *issues.Collector, db *db.DB) *CronJob {
}
}

func CjSpecFor(fqn string, cj *batchv1.CronJob) rules.Spec {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be dry-ed up a bit i.e coSpecFor(string, metav1.ObjectMetaAccessor, v1.PodSpec).
Thus we would not need the same impl for any pod resources.

spec := SpecFor(fqn, cj)
spec.Containers = containerList(cj.Spec.JobTemplate.Spec.Template.Spec)
return spec
}

// Lint cleanse the resource.
func (s *CronJob) Lint(ctx context.Context) error {
over := pullOverAllocs(ctx)
Expand All @@ -40,7 +47,7 @@ func (s *CronJob) Lint(ctx context.Context) error {
cj := o.(*batchv1.CronJob)
fqn := client.FQN(cj.Namespace, cj.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, SpecFor(fqn, cj))
ctx = internal.WithSpec(ctx, CjSpecFor(fqn, cj))
s.checkCronJob(ctx, fqn, cj)
s.checkContainers(ctx, fqn, cj.Spec.JobTemplate.Spec.Template.Spec)
s.checkUtilization(ctx, over, fqn)
Expand Down
52 changes: 52 additions & 0 deletions internal/lint/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
mv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
)

Expand Down Expand Up @@ -59,3 +60,54 @@ func TestCronJobLint(t *testing.T) {
assert.Equal(t, `[POP-106] No resources requests/limits defined`, ii[5].Message)
assert.Equal(t, rules.WarnLevel, ii[5].Level)
}

func TestCjSpecFor(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests really test the same call. Better would be to have tests that actually show the resolved fix i.e no spinach vs with spinach and show container codes exclusions.

tests := map[string]struct {
fqn string
cronJob *batchv1.CronJob
want rules.Spec
}{
"full": {
fqn: "default/p1",
cronJob: &batchv1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"p1": "blee"},
Annotations: map[string]string{"default": "fred"},
},
Spec: batchv1.CronJobSpec{
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{Name: "ic1"},
},
Containers: []v1.Container{
{Name: "c1"},
{Name: "c2"},
},
},
},
},
},
},
},
want: rules.Spec{
FQN: "default/p1",
Labels: rules.Labels{"p1": "blee"},
Annotations: rules.Labels{"default": "fred"},
Containers: []string{"ic1", "c1", "c2"},
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := CjSpecFor(tc.fqn, tc.cronJob)
assert.Equal(t, tc.want.FQN, got.FQN)
assert.Equal(t, tc.want.Labels, got.Labels)
assert.Equal(t, tc.want.Annotations, got.Annotations)
assert.ElementsMatch(t, tc.want.Containers, got.Containers)
})
}
}
9 changes: 8 additions & 1 deletion internal/lint/dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/derailed/popeye/internal/client"
"github.com/derailed/popeye/internal/db"
"github.com/derailed/popeye/internal/issues"
"github.com/derailed/popeye/internal/rules"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -30,6 +31,12 @@ func NewDeployment(co *issues.Collector, db *db.DB) *Deployment {
}
}

func DpSpecFor(fqn string, dp *appsv1.Deployment) rules.Spec {
spec := SpecFor(fqn, dp)
spec.Containers = containerList(dp.Spec.Template.Spec)
return spec
}

// Lint cleanse the resource.
func (s *Deployment) Lint(ctx context.Context) error {
over := pullOverAllocs(ctx)
Expand All @@ -39,7 +46,7 @@ func (s *Deployment) Lint(ctx context.Context) error {
dp := o.(*appsv1.Deployment)
fqn := client.FQN(dp.Namespace, dp.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, SpecFor(fqn, dp))
ctx = internal.WithSpec(ctx, DpSpecFor(fqn, dp))
s.checkDeployment(ctx, dp)
s.checkContainers(ctx, fqn, dp.Spec.Template.Spec)
s.checkUtilization(ctx, over, dp)
Expand Down
48 changes: 48 additions & 0 deletions internal/lint/dp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
mv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
)

Expand Down Expand Up @@ -56,3 +57,50 @@ func TestDPLint(t *testing.T) {
assert.Equal(t, `[POP-666] Lint internal error: no pod selector given`, ii[1].Message)
assert.Equal(t, rules.ErrorLevel, ii[1].Level)
}

func TestDpSpecFor(t *testing.T) {
tests := map[string]struct {
fqn string
deployment *appsv1.Deployment
want rules.Spec
}{
"full": {
fqn: "default/p1",
deployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"p1": "blee"},
Annotations: map[string]string{"default": "fred"},
},
Spec: appsv1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{Name: "ic1"},
},
Containers: []v1.Container{
{Name: "c1"},
{Name: "c2"},
},
},
},
},
},
want: rules.Spec{
FQN: "default/p1",
Labels: rules.Labels{"p1": "blee"},
Annotations: rules.Labels{"default": "fred"},
Containers: []string{"ic1", "c1", "c2"},
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := DpSpecFor(tc.fqn, tc.deployment)
assert.Equal(t, tc.want.FQN, got.FQN)
assert.Equal(t, tc.want.Labels, got.Labels)
assert.Equal(t, tc.want.Annotations, got.Annotations)
assert.ElementsMatch(t, tc.want.Containers, got.Containers)
})
}
}
9 changes: 8 additions & 1 deletion internal/lint/ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/derailed/popeye/internal/client"
"github.com/derailed/popeye/internal/db"
"github.com/derailed/popeye/internal/issues"
"github.com/derailed/popeye/internal/rules"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
)
Expand All @@ -29,6 +30,12 @@ func NewDaemonSet(co *issues.Collector, db *db.DB) *DaemonSet {
}
}

func DsSpecFor(fqn string, ds *appsv1.DaemonSet) rules.Spec {
spec := SpecFor(fqn, ds)
spec.Containers = containerList(ds.Spec.Template.Spec)
return spec
}

// Lint cleanse the resource.
func (s *DaemonSet) Lint(ctx context.Context) error {
over := pullOverAllocs(ctx)
Expand All @@ -38,7 +45,7 @@ func (s *DaemonSet) Lint(ctx context.Context) error {
ds := o.(*appsv1.DaemonSet)
fqn := client.FQN(ds.Namespace, ds.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, SpecFor(fqn, ds))
ctx = internal.WithSpec(ctx, DsSpecFor(fqn, ds))

s.checkDaemonSet(ctx, ds)
s.checkContainers(ctx, fqn, ds.Spec.Template.Spec)
Expand Down
48 changes: 48 additions & 0 deletions internal/lint/ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
mv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
)

Expand Down Expand Up @@ -51,3 +52,50 @@ func TestDSLint(t *testing.T) {
assert.Equal(t, `[POP-508] No pods match controller selector: app=p10`, ii[5].Message)
assert.Equal(t, rules.ErrorLevel, ii[5].Level)
}

func TestDsSpecFor(t *testing.T) {
tests := map[string]struct {
fqn string
daemonSet *appsv1.DaemonSet
want rules.Spec
}{
"full": {
fqn: "default/p1",
daemonSet: &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"p1": "blee"},
Annotations: map[string]string{"default": "fred"},
},
Spec: appsv1.DaemonSetSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{Name: "ic1"},
},
Containers: []v1.Container{
{Name: "c1"},
{Name: "c2"},
},
},
},
},
},
want: rules.Spec{
FQN: "default/p1",
Labels: rules.Labels{"p1": "blee"},
Annotations: rules.Labels{"default": "fred"},
Containers: []string{"ic1", "c1", "c2"},
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := DsSpecFor(tc.fqn, tc.daemonSet)
assert.Equal(t, tc.want.FQN, got.FQN)
assert.Equal(t, tc.want.Labels, got.Labels)
assert.Equal(t, tc.want.Annotations, got.Annotations)
assert.ElementsMatch(t, tc.want.Containers, got.Containers)
})
}
}
12 changes: 12 additions & 0 deletions internal/lint/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func SpecFor(fqn string, o metav1.ObjectMetaAccessor) rules.Spec {
return spec
}

func containerList(podTemplate v1.PodSpec) []string {
var containers []string
for _, co := range podTemplate.InitContainers {
containers = append(containers, co.Name)
}
for _, co := range podTemplate.Containers {
containers = append(containers, co.Name)
}

return containers
}

func resourceUsage(ctx context.Context, dba *db.DB, c Collector, ns string, sel *metav1.LabelSelector) ConsumptionMetrics {
var mx ConsumptionMetrics

Expand Down
9 changes: 8 additions & 1 deletion internal/lint/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/derailed/popeye/internal/dao"
"github.com/derailed/popeye/internal/db"
"github.com/derailed/popeye/internal/issues"
"github.com/derailed/popeye/internal/rules"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
)
Expand All @@ -30,6 +31,12 @@ func NewJob(co *issues.Collector, db *db.DB) *Job {
}
}

func JSpecFor(fqn string, j *batchv1.Job) rules.Spec {
spec := SpecFor(fqn, j)
spec.Containers = containerList(j.Spec.Template.Spec)
return spec
}

// Lint cleanse the resource.
func (s *Job) Lint(ctx context.Context) error {
over := pullOverAllocs(ctx)
Expand All @@ -39,7 +46,7 @@ func (s *Job) Lint(ctx context.Context) error {
j := o.(*batchv1.Job)
fqn := client.FQN(j.Namespace, j.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, SpecFor(fqn, j))
ctx = internal.WithSpec(ctx, JSpecFor(fqn, j))
s.checkJob(ctx, fqn, j)
s.checkContainers(ctx, fqn, j.Spec.Template.Spec)
s.checkUtilization(ctx, over, fqn)
Expand Down
48 changes: 48 additions & 0 deletions internal/lint/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
mv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
)

Expand Down Expand Up @@ -45,3 +46,50 @@ func TestJobLint(t *testing.T) {
assert.Equal(t, `[POP-106] No resources requests/limits defined`, ii[1].Message)
assert.Equal(t, rules.WarnLevel, ii[1].Level)
}

func TestJSpecFor(t *testing.T) {
tests := map[string]struct {
fqn string
job *batchv1.Job
want rules.Spec
}{
"full": {
fqn: "default/p1",
job: &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"p1": "blee"},
Annotations: map[string]string{"default": "fred"},
},
Spec: batchv1.JobSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{Name: "ic1"},
},
Containers: []v1.Container{
{Name: "c1"},
{Name: "c2"},
},
},
},
},
},
want: rules.Spec{
FQN: "default/p1",
Labels: rules.Labels{"p1": "blee"},
Annotations: rules.Labels{"default": "fred"},
Containers: []string{"ic1", "c1", "c2"},
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := JSpecFor(tc.fqn, tc.job)
assert.Equal(t, tc.want.FQN, got.FQN)
assert.Equal(t, tc.want.Labels, got.Labels)
assert.Equal(t, tc.want.Annotations, got.Annotations)
assert.ElementsMatch(t, tc.want.Containers, got.Containers)
})
}
}
Loading