Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Commit

Permalink
Handle malformed input metrics
Browse files Browse the repository at this point in the history
Use the snap plugin utilities namespace
lib to check namespace parts for
invalid chars. Also checks the length
of a namespace and rejects it if is
too short.
  • Loading branch information
daniellee committed Dec 10, 2016
1 parent fb0fc94 commit 192a1ee
Show file tree
Hide file tree
Showing 16 changed files with 1,406 additions and 0 deletions.
11 changes: 11 additions & 0 deletions kubestate/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ import (
type deploymentCollector struct {
}

const (
minDeploymentNamespaceSize = 7
)

func (*deploymentCollector) Collect(mts []plugin.Metric, deployment v1beta1.Deployment) ([]plugin.Metric, error) {
metrics := make([]plugin.Metric, 0)

for _, mt := range mts {
ns := mt.Namespace.Strings()

if len(ns) < minDeploymentNamespaceSize {
continue
}
if !isValidNamespace(ns) {
continue
}

if ns[5] == "metadata" && ns[6] == "generation" {
metric := createDeploymentMetric(mt, ns, deployment, deployment.Generation)
metrics = append(metrics, metric)
Expand Down
5 changes: 5 additions & 0 deletions kubestate/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ var deploymentCases = []struct {
"grafanalabs.kubestate.deployment.default.PausedDeploy.spec.paused 1",
},
},
{
deployment: mockDeployments[0],
metrics: malformedMetricTypes,
expected: []string{},
},
}

func TestDeploymentCollector(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions kubestate/kubestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
. "github.com/intelsdi-x/snap-plugin-utilities/logger"
. "github.com/intelsdi-x/snap-plugin-utilities/ns"
// "k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -74,6 +75,17 @@ func slugify(name string) string {
return strings.Replace(name, ".", "_", -1)
}

func isValidNamespace(ns []string) bool {
for i := range ns {
err := ValidateMetricNamespacePart(ns[i])
if err != nil {
// Logger.
return false
}
}
return true
}

func getPodMetricTypes() []plugin.Metric {
mts := []plugin.Metric{}
// Pod metrics
Expand Down
11 changes: 11 additions & 0 deletions kubestate/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ import (
type nodeCollector struct {
}

const (
minNodeNamespaceSize = 6
)

func (*nodeCollector) Collect(mts []plugin.Metric, node v1.Node) ([]plugin.Metric, error) {
metrics := make([]plugin.Metric, 0)

for _, mt := range mts {
ns := mt.Namespace.Strings()

if len(ns) < minNodeNamespaceSize {
continue
}
if !isValidNamespace(ns) {
continue
}

if ns[4] == "spec" && ns[5] == "unschedulable" {
metric := createNodeMetric(mt, ns, node, boolInt(node.Spec.Unschedulable))
metrics = append(metrics, metric)
Expand Down
5 changes: 5 additions & 0 deletions kubestate/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ var nodeCases = []struct {
"grafanalabs.kubestate.node.127_0_0_1.status.allocatable.pods 555",
},
},
{
node: mockNodes[0],
metrics: malformedMetricTypes,
expected: []string{},
},
}

func TestNodeCollector(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions kubestate/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ import (
type podCollector struct {
}

const (
minPodNamespaceSize = 8
)

func (*podCollector) Collect(mts []plugin.Metric, pod v1.Pod) ([]plugin.Metric, error) {
metrics := make([]plugin.Metric, 0)

for _, mt := range mts {
ns := mt.Namespace.Strings()

if len(ns) < minPodNamespaceSize {
continue
}
if !isValidNamespace(ns) {
continue
}

if ns[2] == "pod" && ns[5] == "status" {
if ns[6] == "phase" {
ns[3] = pod.Namespace
Expand Down
16 changes: 16 additions & 0 deletions kubestate/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ var mockPods = []v1.Pod{
},
}

var malformedMetricTypes = []plugin.Metric{
{
Namespace: plugin.NewNamespace("grafanalabs", "kubestate"),
Version: 1,
},
{
Namespace: plugin.NewNamespace("grafanalabs", "kubestate", "pod", "container", "test", "test", "status", "metric^1"),
Version: 1,
},
}

var cases = []struct {
pod v1.Pod
metrics []plugin.Metric
Expand Down Expand Up @@ -161,6 +172,11 @@ var cases = []struct {
"grafanalabs.kubestate.pod.container.kube-system.node1.pod2.container2.limits.memory.bytes 2e+08",
},
},
{
pod: mockPods[1],
metrics: malformedMetricTypes,
expected: []string{},
},
}

func TestPodCollector(t *testing.T) {
Expand Down
Loading

0 comments on commit 192a1ee

Please sign in to comment.