Skip to content

Commit

Permalink
[Maint] cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Mar 9, 2024
1 parent da01220 commit 908ea9a
Show file tree
Hide file tree
Showing 37 changed files with 182 additions and 110 deletions.
11 changes: 2 additions & 9 deletions internal/dao/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/derailed/popeye/internal"
"github.com/derailed/popeye/internal/client"
"github.com/rs/zerolog/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -22,14 +21,8 @@ type Generic struct {

// List returns a collection of resources.
func (g *Generic) List(ctx context.Context) ([]runtime.Object, error) {
labelSel, ok := ctx.Value(internal.KeyLabels).(string)
if !ok {
log.Debug().Msgf("No label selector found in context. Listing all resources")
}
ns, ok := ctx.Value(internal.KeyNamespace).(string)
if !ok {
panic("BOOM no ns in context")
}
labelSel, _ := ctx.Value(internal.KeyLabels).(string)
ns, _ := ctx.Value(internal.KeyNamespace).(string)
if client.IsAllNamespace(ns) {
ns = client.AllNamespaces
}
Expand Down
11 changes: 4 additions & 7 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,14 @@ func matchSel(labels map[string]string, e metav1.LabelSelectorRequirement) bool

// MatchLabels check if pod labels match a selector.
func MatchLabels(labels, sel map[string]string) bool {
if len(sel) == 0 {
return false
}

var count int
for k, v := range sel {
if v1, ok := labels[k]; !ok || v1 != v {
return false
if v1, ok := labels[k]; ok && v == v1 {
count++
}
}

return true
return count > 0
}

func (db *DB) Exists(kind types.GVR, fqn string) bool {
Expand Down
26 changes: 21 additions & 5 deletions internal/db/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package db

import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
Expand Down Expand Up @@ -58,7 +59,6 @@ func LoadResource[T metav1.ObjectMetaAccessor](ctx context.Context, l *Loader, g
if l.isLoaded(gvr) || gvr == types.BlankGVR {
return nil
}

oo, err := loadResource(ctx, gvr)
if err != nil {
return err
Expand All @@ -83,11 +83,25 @@ func Cast[T any](o runtime.Object) (T, error) {
func Save[T metav1.ObjectMetaAccessor](ctx context.Context, dba *DB, gvr types.GVR, oo []runtime.Object) error {
txn := dba.Txn(true)
defer txn.Commit()

for _, o := range oo {
u, err := Cast[T](o)
if err != nil {
return err
var (
u T
err error
)
// !!BOZO!! Dud. Can't hydrate cnp/ccnp from unstructured??
if gvr.R() == "ciliumnetworkpolicies" || gvr.R() == "ciliumclusterwidenetworkpolicies" {
bb, err := json.Marshal(o.(*unstructured.Unstructured))
if err != nil {
return err
}
if err = json.Unmarshal(bb, &u); err != nil {
return err
}
} else {
u, err = Cast[T](o)
if err != nil {
return err
}
}
if err := txn.Insert(gvr.String(), u); err != nil {
return err
Expand Down Expand Up @@ -156,6 +170,7 @@ func (l *Loader) fetchPodsMetrics(c types.Connection) (*mv1beta1.PodMetricsList,
}
ctx, cancel := context.WithTimeout(context.Background(), client.CallTimeout)
defer cancel()

return vc.MetricsV1beta1().PodMetricses(c.ActiveNamespace()).List(ctx, metav1.ListOptions{})
}

Expand All @@ -167,6 +182,7 @@ func (l *Loader) fetchNodesMetrics(c types.Connection) (*mv1beta1.NodeMetricsLis

ctx, cancel := context.WithTimeout(context.Background(), client.CallTimeout)
defer cancel()

return vc.MetricsV1beta1().NodeMetricses().List(ctx, metav1.ListOptions{})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lint/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *Cluster) checkVersion(ctx context.Context) error {
return err
}

ctx = internal.WithSpec(ctx, specFor("Version", nil))
ctx = internal.WithSpec(ctx, SpecFor("Version", nil))
if rev.Major != tolerableMajor || rev.Minor < tolerableMinor {
c.AddCode(ctx, 405)
} else {
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *ConfigMap) checkStale(ctx context.Context, refs *sync.Map) error {
cm := o.(*v1.ConfigMap)
fqn := client.FQN(cm.Namespace, cm.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, specFor(fqn, cm))
ctx = internal.WithSpec(ctx, SpecFor(fqn, cm))
if s.system.skip(fqn) {
continue
}
Expand Down
42 changes: 0 additions & 42 deletions internal/lint/cm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,3 @@ func TestConfigMapLint(t *testing.T) {
assert.Equal(t, `[POP-400] Used? Unable to locate resource reference`, ii[0].Message)
assert.Equal(t, rules.InfoLevel, ii[0].Level)
}

// ----------------------------------------------------------------------------
// Helpers...

// type mockConfigMap struct{}

// func newMockConfigMap() mockConfigMap {
// return mockConfigMap{}
// }

// func (c mockConfigMap) PodRefs(refs *sync.Map) {
// refs.Store("cm:default/cm1", internal.StringSet{
// "k1": internal.Blank,
// "k2": internal.Blank,
// })
// refs.Store("cm:default/cm2", internal.AllKeys)
// refs.Store("cm:default/cm4", internal.StringSet{
// "k1": internal.Blank,
// })
// }

// func (c mockConfigMap) ListConfigMaps() map[string]*v1.ConfigMap {
// return map[string]*v1.ConfigMap{
// "default/cm1": makeMockConfigMap("cm1"),
// "default/cm2": makeMockConfigMap("cm2"),
// "default/cm3": makeMockConfigMap("cm3"),
// "default/cm4": makeMockConfigMap("cm4"),
// }
// }

// func makeMockConfigMap(n string) *v1.ConfigMap {
// return &v1.ConfigMap{
// ObjectMeta: metav1.ObjectMeta{
// Name: n,
// Namespace: "default",
// },
// Data: map[string]string{
// "k1": "",
// "k2": "",
// },
// }
// }
10 changes: 5 additions & 5 deletions internal/lint/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestContainerCheckUtilization(t *testing.T) {
}

ctx := test.MakeContext("containers", "container")
ctx = internal.WithSpec(ctx, specFor("default/p1", nil))
ctx = internal.WithSpec(ctx, SpecFor("default/p1", nil))
for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestContainerCheckResources(t *testing.T) {
l := NewContainer("default/p1", newRangeCollector(t))

t.Run(k, func(t *testing.T) {
ctx = internal.WithSpec(ctx, specFor("default/p1", nil))
ctx = internal.WithSpec(ctx, SpecFor("default/p1", nil))
ctx = internal.WithGroup(ctx, types.NewGVR("containers"), co.Name)
l.checkResources(ctx, co)

Expand Down Expand Up @@ -184,7 +184,7 @@ func TestContainerCheckImageTags(t *testing.T) {
}

ctx := test.MakeContext("containers", "container")
ctx = internal.WithSpec(ctx, specFor("default/p1", nil))
ctx = internal.WithSpec(ctx, SpecFor("default/p1", nil))
ctx = internal.WithGroup(ctx, types.NewGVR("containers"), "c1")
for k := range uu {
u := uu[k]
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestContainerCheckImageRegistry(t *testing.T) {
}

ctx := test.MakeContext("containers", "container")
ctx = internal.WithSpec(ctx, specFor("default/p1", nil))
ctx = internal.WithSpec(ctx, SpecFor("default/p1", nil))
ctx = internal.WithGroup(ctx, types.NewGVR("containers"), "c1")
for k := range uu {
u := uu[k]
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestContainerCheckNamedPorts(t *testing.T) {
}

ctx := test.MakeContext("containers", "container")
ctx = internal.WithSpec(ctx, specFor("p1", nil))
ctx = internal.WithSpec(ctx, SpecFor("p1", nil))
ctx = internal.WithGroup(ctx, types.NewGVR("v1/pods"), "p1")
for k := range uu {
u := uu[k]
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/cr.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *ClusterRole) checkStale(ctx context.Context, refs *sync.Map) {
cr := o.(*rbacv1.ClusterRole)
fqn := client.FQN(cr.Namespace, cr.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, specFor(fqn, cr))
ctx = internal.WithSpec(ctx, SpecFor(fqn, cr))
if s.system.skip(fqn) {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/crb.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *ClusterRoleBinding) checkInUse(ctx context.Context) {
fqn := client.FQN(crb.Namespace, crb.Name)

c.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, specFor(fqn, crb))
ctx = internal.WithSpec(ctx, SpecFor(fqn, crb))

switch crb.RoleRef.Kind {
case "ClusterRole":
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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, SpecFor(fqn, cj))
s.checkCronJob(ctx, fqn, cj)
s.checkContainers(ctx, fqn, cj.Spec.JobTemplate.Spec.Template.Spec)
s.checkUtilization(ctx, over, fqn)
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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, SpecFor(fqn, dp))
s.checkDeployment(ctx, dp)
s.checkContainers(ctx, fqn, dp.Spec.Template.Spec)
s.checkUtilization(ctx, over, dp)
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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, SpecFor(fqn, ds))

s.checkDaemonSet(ctx, ds)
s.checkContainers(ctx, fqn, ds.Spec.Template.Spec)
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *Gateway) Lint(ctx context.Context) error {
gw := o.(*gwv1.Gateway)
fqn := client.FQN(gw.Namespace, gw.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, specFor(fqn, gw))
ctx = internal.WithSpec(ctx, SpecFor(fqn, gw))
s.checkRefs(ctx, gw)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lint/gwc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *GatewayClass) Lint(ctx context.Context) error {
gwc := o.(*gwv1.GatewayClass)
fqn := client.FQN(gwc.Namespace, gwc.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, specFor(fqn, gwc))
ctx = internal.WithSpec(ctx, SpecFor(fqn, gwc))
s.checkRefs(ctx, gwc.Name)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lint/gwr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *HTTPRoute) Lint(ctx context.Context) error {
gwr := o.(*gwv1.HTTPRoute)
fqn := client.FQN(gwr.Namespace, gwr.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, specFor(fqn, gwr))
ctx = internal.WithSpec(ctx, SpecFor(fqn, gwr))
s.checkRoute(ctx, fqn, gwr)
}

Expand Down
Loading

0 comments on commit 908ea9a

Please sign in to comment.