Skip to content

Commit

Permalink
Merge pull request #256 from undistro/feat/POP-402-json
Browse files Browse the repository at this point in the history
Reporting POP-402 issue
  • Loading branch information
derailed authored Jun 29, 2023
2 parents 22d0830 + b66644d commit dce0a46
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 47 deletions.
2 changes: 1 addition & 1 deletion docs/codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
| ---------- | ----------------------------------------------------------- | -------- | ---------------- |
| 400 | Used? Unable to locate resource reference | 1 | |
| 401 | Key "%s" used? Unable to locate key reference | 1 | |
| 402 | No metric-server detected %v | 1 | |
| 402 | No metrics-server detected | 1 | |
| 403 | Deprecated %s API group "%s". Use "%s" instead | 2 | |
| 404 | Deprecation check failed. %v | 1 | |
| 405 | Is this a jurassic cluster? Might want to upgrade K8s a bit | 2 | |
Expand Down
2 changes: 1 addition & 1 deletion internal/issues/assets/codes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ codes:
message: Key "%s" used? Unable to locate key reference
severity: 1
402:
message: No metric-server detected %v
message: No metrics-server detected
severity: 1
403:
message: Deprecated %s API group "%s". Use "%s" instead
Expand Down
16 changes: 16 additions & 0 deletions internal/sanitize/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type (
// ClusterLister list available Clusters on a cluster.
ClusterLister interface {
ListVersion() (string, string)
HasMetrics() bool
}
)

Expand All @@ -36,6 +37,21 @@ func NewCluster(co *issues.Collector, lister ClusterLister) *Cluster {

// Sanitize cleanse the resource.
func (c *Cluster) Sanitize(ctx context.Context) error {
c.checkMetricsServer(ctx)
if err := c.checkVersion(ctx); err != nil {
return err
}
return nil
}

func (c *Cluster) checkMetricsServer(ctx context.Context) {
ctx = internal.WithFQN(ctx, "Metrics")
if !c.HasMetrics() {
c.AddCode(ctx, 402)
}
}

func (c *Cluster) checkVersion(ctx context.Context) error {
major, minor := c.ListVersion()

m, err := strconv.Atoi(major)
Expand Down
57 changes: 39 additions & 18 deletions internal/sanitize/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,53 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/derailed/popeye/internal"
"github.com/derailed/popeye/internal/client"
"github.com/derailed/popeye/internal/issues"
"github.com/derailed/popeye/pkg/config"
"github.com/stretchr/testify/assert"
)

func TestClusterSanitize(t *testing.T) {
uu := map[string]struct {
major, minor string
e issues.Issues
metrics bool
e issues.Outcome
}{
"good": {
major: "1", minor: "15",
e: issues.Issues{
{
GVR: "clusters",
Group: issues.Root,
Message: "[POP-406] K8s version OK",
Level: config.OkLevel,
metrics: true,
e: map[string]issues.Issues{
"Version": {
{
GVR: "clusters",
Group: issues.Root,
Message: "[POP-406] K8s version OK",
Level: config.OkLevel,
},
},
},
},
"guizard": {
major: "1", minor: "11",
e: issues.Issues{
{
GVR: "clusters",
Group: issues.Root,
Message: "[POP-405] Is this a jurassic cluster? Might want to upgrade K8s a bit",
Level: config.WarnLevel,
metrics: false,
e: map[string]issues.Issues{
"Version": {
{
GVR: "clusters",
Group: issues.Root,
Message: "[POP-405] Is this a jurassic cluster? Might want to upgrade K8s a bit",
Level: config.WarnLevel,
},
},
"Metrics": {
{
GVR: "clusters",
Group: issues.Root,
Message: "[POP-402] No metrics-server detected",
Level: config.InfoLevel,
},
},
},
},
Expand All @@ -44,10 +60,10 @@ func TestClusterSanitize(t *testing.T) {
for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
cl := NewCluster(issues.NewCollector(loadCodes(t), makeConfig(t)), newCluster(u.major, u.minor))
cl := NewCluster(issues.NewCollector(loadCodes(t), makeConfig(t)), newCluster(u.major, u.minor, u.metrics))

assert.Nil(t, cl.Sanitize(ctx))
assert.Equal(t, u.e, cl.Outcome()["Version"])
assert.Equal(t, u.e, cl.Outcome())
})
}
}
Expand All @@ -69,12 +85,17 @@ func makeContext(gvr, section string) context.Context {

type cluster struct {
major, minor string
metrics bool
}

func newCluster(major, minor string) cluster {
return cluster{major: major, minor: minor}
func newCluster(major, minor string, metrics bool) cluster {
return cluster{major: major, minor: minor, metrics: metrics}
}

func (c cluster) ListVersion() (string, string) {
return c.major, c.minor
}

func (c cluster) HasMetrics() bool {
return c.metrics
}
4 changes: 4 additions & 0 deletions internal/scrub/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ func NewCluster(ctx context.Context, c *Cache, codes *issues.Codes) Sanitizer {
func (d *Cluster) Sanitize(ctx context.Context) error {
return sanitize.NewCluster(d.Collector, d).Sanitize(ctx)
}

func (d *Cluster) HasMetrics() bool {
return d.client.HasMetrics()
}
16 changes: 8 additions & 8 deletions internal/scrub/cr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ type ClusterRole struct {

// NewClusterRole return a new ClusterRole scruber.
func NewClusterRole(ctx context.Context, c *Cache, codes *issues.Codes) Sanitizer {
crb := ClusterRole{
cr := ClusterRole{
client: c.factory.Client(),
Config: c.config,
Collector: issues.NewCollector(codes, c.config),
}

var err error
crb.ClusterRole, err = c.clusterroles()
cr.ClusterRole, err = c.clusterroles()
if err != nil {
crb.AddErr(ctx, err)
cr.AddErr(ctx, err)
}

crb.ClusterRoleBinding, err = c.clusterrolebindings()
cr.ClusterRoleBinding, err = c.clusterrolebindings()
if err != nil {
crb.AddCode(ctx, 402, err)
cr.AddErr(ctx, err)
}

crb.RoleBinding, err = c.rolebindings()
cr.RoleBinding, err = c.rolebindings()
if err != nil {
crb.AddErr(ctx, err)
cr.AddErr(ctx, err)
}

return &crb
return &cr
}

// Sanitize all available ClusterRoles.
Expand Down
2 changes: 1 addition & 1 deletion internal/scrub/crb.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewClusterRoleBinding(ctx context.Context, c *Cache, codes *issues.Codes) S

crb.ClusterRole, err = c.clusterroles()
if err != nil {
crb.AddCode(ctx, 402, err)
crb.AddErr(ctx, err)
}

crb.Role, err = c.roles()
Expand Down
2 changes: 1 addition & 1 deletion internal/scrub/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewHorizontalPodAutoscaler(ctx context.Context, c *Cache, codes *issues.Cod

h.Node, err = c.nodes()
if err != nil {
h.AddCode(ctx, 402, err)
h.AddErr(ctx, err)
}

h.NodesMetrics, _ = c.nodesMx()
Expand Down
2 changes: 1 addition & 1 deletion internal/scrub/np.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewNetworkPolicy(ctx context.Context, c *Cache, codes *issues.Codes) Saniti

n.Namespace, err = c.namespaces()
if err != nil {
n.AddCode(ctx, 402, err)
n.AddErr(ctx, err)
}

n.Pod, err = c.pods()
Expand Down
16 changes: 8 additions & 8 deletions internal/scrub/rb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ type RoleBinding struct {

// NewRoleBinding return a new RoleBinding scruber.
func NewRoleBinding(ctx context.Context, c *Cache, codes *issues.Codes) Sanitizer {
crb := RoleBinding{
rb := RoleBinding{
client: c.factory.Client(),
Config: c.config,
Collector: issues.NewCollector(codes, c.config),
}

var err error
crb.RoleBinding, err = c.rolebindings()
rb.RoleBinding, err = c.rolebindings()
if err != nil {
crb.AddErr(ctx, err)
rb.AddErr(ctx, err)
}

crb.ClusterRole, err = c.clusterroles()
rb.ClusterRole, err = c.clusterroles()
if err != nil {
crb.AddCode(ctx, 402, err)
rb.AddErr(ctx, err)
}

crb.Role, err = c.roles()
rb.Role, err = c.roles()
if err != nil {
crb.AddErr(ctx, err)
rb.AddErr(ctx, err)
}

return &crb
return &rb
}

// Sanitize all available RoleBindings.
Expand Down
16 changes: 8 additions & 8 deletions internal/scrub/ro.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ type Role struct {

// NewRole return a new Role scruber.
func NewRole(ctx context.Context, c *Cache, codes *issues.Codes) Sanitizer {
crb := Role{
ro := Role{
client: c.factory.Client(),
Config: c.config,
Collector: issues.NewCollector(codes, c.config),
}

var err error
crb.Role, err = c.roles()
ro.Role, err = c.roles()
if err != nil {
crb.AddErr(ctx, err)
ro.AddErr(ctx, err)
}

crb.ClusterRoleBinding, err = c.clusterrolebindings()
ro.ClusterRoleBinding, err = c.clusterrolebindings()
if err != nil {
crb.AddCode(ctx, 402, err)
ro.AddErr(ctx, err)
}

crb.RoleBinding, err = c.rolebindings()
ro.RoleBinding, err = c.rolebindings()
if err != nil {
crb.AddErr(ctx, err)
ro.AddErr(ctx, err)
}

return &crb
return &ro
}

// Sanitize all available Roles.
Expand Down

0 comments on commit dce0a46

Please sign in to comment.