Skip to content

Commit 908ea9a

Browse files
committed
[Maint] cleanup
1 parent da01220 commit 908ea9a

37 files changed

+182
-110
lines changed

internal/dao/generic.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/derailed/popeye/internal"
1010
"github.com/derailed/popeye/internal/client"
11-
"github.com/rs/zerolog/log"
1211
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1413
"k8s.io/apimachinery/pkg/runtime"
@@ -22,14 +21,8 @@ type Generic struct {
2221

2322
// List returns a collection of resources.
2423
func (g *Generic) List(ctx context.Context) ([]runtime.Object, error) {
25-
labelSel, ok := ctx.Value(internal.KeyLabels).(string)
26-
if !ok {
27-
log.Debug().Msgf("No label selector found in context. Listing all resources")
28-
}
29-
ns, ok := ctx.Value(internal.KeyNamespace).(string)
30-
if !ok {
31-
panic("BOOM no ns in context")
32-
}
24+
labelSel, _ := ctx.Value(internal.KeyLabels).(string)
25+
ns, _ := ctx.Value(internal.KeyNamespace).(string)
3326
if client.IsAllNamespace(ns) {
3427
ns = client.AllNamespaces
3528
}

internal/db/db.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -382,17 +382,14 @@ func matchSel(labels map[string]string, e metav1.LabelSelectorRequirement) bool
382382

383383
// MatchLabels check if pod labels match a selector.
384384
func MatchLabels(labels, sel map[string]string) bool {
385-
if len(sel) == 0 {
386-
return false
387-
}
388-
385+
var count int
389386
for k, v := range sel {
390-
if v1, ok := labels[k]; !ok || v1 != v {
391-
return false
387+
if v1, ok := labels[k]; ok && v == v1 {
388+
count++
392389
}
393390
}
394391

395-
return true
392+
return count > 0
396393
}
397394

398395
func (db *DB) Exists(kind types.GVR, fqn string) bool {

internal/db/loader.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package db
55

66
import (
77
"context"
8+
"encoding/json"
89
"fmt"
910
"strings"
1011
"sync"
@@ -58,7 +59,6 @@ func LoadResource[T metav1.ObjectMetaAccessor](ctx context.Context, l *Loader, g
5859
if l.isLoaded(gvr) || gvr == types.BlankGVR {
5960
return nil
6061
}
61-
6262
oo, err := loadResource(ctx, gvr)
6363
if err != nil {
6464
return err
@@ -83,11 +83,25 @@ func Cast[T any](o runtime.Object) (T, error) {
8383
func Save[T metav1.ObjectMetaAccessor](ctx context.Context, dba *DB, gvr types.GVR, oo []runtime.Object) error {
8484
txn := dba.Txn(true)
8585
defer txn.Commit()
86-
8786
for _, o := range oo {
88-
u, err := Cast[T](o)
89-
if err != nil {
90-
return err
87+
var (
88+
u T
89+
err error
90+
)
91+
// !!BOZO!! Dud. Can't hydrate cnp/ccnp from unstructured??
92+
if gvr.R() == "ciliumnetworkpolicies" || gvr.R() == "ciliumclusterwidenetworkpolicies" {
93+
bb, err := json.Marshal(o.(*unstructured.Unstructured))
94+
if err != nil {
95+
return err
96+
}
97+
if err = json.Unmarshal(bb, &u); err != nil {
98+
return err
99+
}
100+
} else {
101+
u, err = Cast[T](o)
102+
if err != nil {
103+
return err
104+
}
91105
}
92106
if err := txn.Insert(gvr.String(), u); err != nil {
93107
return err
@@ -156,6 +170,7 @@ func (l *Loader) fetchPodsMetrics(c types.Connection) (*mv1beta1.PodMetricsList,
156170
}
157171
ctx, cancel := context.WithTimeout(context.Background(), client.CallTimeout)
158172
defer cancel()
173+
159174
return vc.MetricsV1beta1().PodMetricses(c.ActiveNamespace()).List(ctx, metav1.ListOptions{})
160175
}
161176

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

168183
ctx, cancel := context.WithTimeout(context.Background(), client.CallTimeout)
169184
defer cancel()
185+
170186
return vc.MetricsV1beta1().NodeMetricses().List(ctx, metav1.ListOptions{})
171187
}
172188

internal/lint/cluster.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c *Cluster) checkVersion(ctx context.Context) error {
4949
return err
5050
}
5151

52-
ctx = internal.WithSpec(ctx, specFor("Version", nil))
52+
ctx = internal.WithSpec(ctx, SpecFor("Version", nil))
5353
if rev.Major != tolerableMajor || rev.Minor < tolerableMinor {
5454
c.AddCode(ctx, 405)
5555
} else {

internal/lint/cm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (s *ConfigMap) checkStale(ctx context.Context, refs *sync.Map) error {
5151
cm := o.(*v1.ConfigMap)
5252
fqn := client.FQN(cm.Namespace, cm.Name)
5353
s.InitOutcome(fqn)
54-
ctx = internal.WithSpec(ctx, specFor(fqn, cm))
54+
ctx = internal.WithSpec(ctx, SpecFor(fqn, cm))
5555
if s.system.skip(fqn) {
5656
continue
5757
}

internal/lint/cm_test.go

-42
Original file line numberDiff line numberDiff line change
@@ -45,45 +45,3 @@ func TestConfigMapLint(t *testing.T) {
4545
assert.Equal(t, `[POP-400] Used? Unable to locate resource reference`, ii[0].Message)
4646
assert.Equal(t, rules.InfoLevel, ii[0].Level)
4747
}
48-
49-
// ----------------------------------------------------------------------------
50-
// Helpers...
51-
52-
// type mockConfigMap struct{}
53-
54-
// func newMockConfigMap() mockConfigMap {
55-
// return mockConfigMap{}
56-
// }
57-
58-
// func (c mockConfigMap) PodRefs(refs *sync.Map) {
59-
// refs.Store("cm:default/cm1", internal.StringSet{
60-
// "k1": internal.Blank,
61-
// "k2": internal.Blank,
62-
// })
63-
// refs.Store("cm:default/cm2", internal.AllKeys)
64-
// refs.Store("cm:default/cm4", internal.StringSet{
65-
// "k1": internal.Blank,
66-
// })
67-
// }
68-
69-
// func (c mockConfigMap) ListConfigMaps() map[string]*v1.ConfigMap {
70-
// return map[string]*v1.ConfigMap{
71-
// "default/cm1": makeMockConfigMap("cm1"),
72-
// "default/cm2": makeMockConfigMap("cm2"),
73-
// "default/cm3": makeMockConfigMap("cm3"),
74-
// "default/cm4": makeMockConfigMap("cm4"),
75-
// }
76-
// }
77-
78-
// func makeMockConfigMap(n string) *v1.ConfigMap {
79-
// return &v1.ConfigMap{
80-
// ObjectMeta: metav1.ObjectMeta{
81-
// Name: n,
82-
// Namespace: "default",
83-
// },
84-
// Data: map[string]string{
85-
// "k1": "",
86-
// "k2": "",
87-
// },
88-
// }
89-
// }

internal/lint/container_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestContainerCheckUtilization(t *testing.T) {
7575
}
7676

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

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

@@ -184,7 +184,7 @@ func TestContainerCheckImageTags(t *testing.T) {
184184
}
185185

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

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

250250
ctx := test.MakeContext("containers", "container")
251-
ctx = internal.WithSpec(ctx, specFor("p1", nil))
251+
ctx = internal.WithSpec(ctx, SpecFor("p1", nil))
252252
ctx = internal.WithGroup(ctx, types.NewGVR("v1/pods"), "p1")
253253
for k := range uu {
254254
u := uu[k]

internal/lint/cr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (s *ClusterRole) checkStale(ctx context.Context, refs *sync.Map) {
7272
cr := o.(*rbacv1.ClusterRole)
7373
fqn := client.FQN(cr.Namespace, cr.Name)
7474
s.InitOutcome(fqn)
75-
ctx = internal.WithSpec(ctx, specFor(fqn, cr))
75+
ctx = internal.WithSpec(ctx, SpecFor(fqn, cr))
7676
if s.system.skip(fqn) {
7777
continue
7878
}

internal/lint/crb.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *ClusterRoleBinding) checkInUse(ctx context.Context) {
4646
fqn := client.FQN(crb.Namespace, crb.Name)
4747

4848
c.InitOutcome(fqn)
49-
ctx = internal.WithSpec(ctx, specFor(fqn, crb))
49+
ctx = internal.WithSpec(ctx, SpecFor(fqn, crb))
5050

5151
switch crb.RoleRef.Kind {
5252
case "ClusterRole":

internal/lint/cronjob.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (s *CronJob) Lint(ctx context.Context) error {
4141
cj := o.(*batchv1.CronJob)
4242
fqn := client.FQN(cj.Namespace, cj.Name)
4343
s.InitOutcome(fqn)
44-
ctx = internal.WithSpec(ctx, specFor(fqn, cj))
44+
ctx = internal.WithSpec(ctx, SpecFor(fqn, cj))
4545
s.checkCronJob(ctx, fqn, cj)
4646
s.checkContainers(ctx, fqn, cj.Spec.JobTemplate.Spec.Template.Spec)
4747
s.checkUtilization(ctx, over, fqn)

internal/lint/dp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *Deployment) Lint(ctx context.Context) error {
3939
dp := o.(*appsv1.Deployment)
4040
fqn := client.FQN(dp.Namespace, dp.Name)
4141
s.InitOutcome(fqn)
42-
ctx = internal.WithSpec(ctx, specFor(fqn, dp))
42+
ctx = internal.WithSpec(ctx, SpecFor(fqn, dp))
4343
s.checkDeployment(ctx, dp)
4444
s.checkContainers(ctx, fqn, dp.Spec.Template.Spec)
4545
s.checkUtilization(ctx, over, dp)

internal/lint/ds.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (s *DaemonSet) Lint(ctx context.Context) error {
3838
ds := o.(*appsv1.DaemonSet)
3939
fqn := client.FQN(ds.Namespace, ds.Name)
4040
s.InitOutcome(fqn)
41-
ctx = internal.WithSpec(ctx, specFor(fqn, ds))
41+
ctx = internal.WithSpec(ctx, SpecFor(fqn, ds))
4242

4343
s.checkDaemonSet(ctx, ds)
4444
s.checkContainers(ctx, fqn, ds.Spec.Template.Spec)

internal/lint/gw.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (s *Gateway) Lint(ctx context.Context) error {
4040
gw := o.(*gwv1.Gateway)
4141
fqn := client.FQN(gw.Namespace, gw.Name)
4242
s.InitOutcome(fqn)
43-
ctx = internal.WithSpec(ctx, specFor(fqn, gw))
43+
ctx = internal.WithSpec(ctx, SpecFor(fqn, gw))
4444
s.checkRefs(ctx, gw)
4545
}
4646

internal/lint/gwc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *GatewayClass) Lint(ctx context.Context) error {
3939
gwc := o.(*gwv1.GatewayClass)
4040
fqn := client.FQN(gwc.Namespace, gwc.Name)
4141
s.InitOutcome(fqn)
42-
ctx = internal.WithSpec(ctx, specFor(fqn, gwc))
42+
ctx = internal.WithSpec(ctx, SpecFor(fqn, gwc))
4343
s.checkRefs(ctx, gwc.Name)
4444
}
4545

internal/lint/gwr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (s *HTTPRoute) Lint(ctx context.Context) error {
4141
gwr := o.(*gwv1.HTTPRoute)
4242
fqn := client.FQN(gwr.Namespace, gwr.Name)
4343
s.InitOutcome(fqn)
44-
ctx = internal.WithSpec(ctx, specFor(fqn, gwr))
44+
ctx = internal.WithSpec(ctx, SpecFor(fqn, gwr))
4545
s.checkRoute(ctx, fqn, gwr)
4646
}
4747

0 commit comments

Comments
 (0)