-
Notifications
You must be signed in to change notification settings - Fork 16
/
deployments.go
63 lines (56 loc) · 1.73 KB
/
deployments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package tables
import (
"context"
"log"
"github.com/aquasecurity/kube-query/utils"
"github.com/kolide/osquery-go/plugin/table"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// DeploymentsTable implements the Table interface,
// Uses kubeclient to extract information about pods
type DeploymentsTable struct {
columns []table.ColumnDefinition
name string
client kubernetes.Interface
}
// NewDeploymentsTable creates a new DeploymentsTable
// saves given initialized kubernetes client
func NewDeploymentsTable(kubeclient kubernetes.Interface) *DeploymentsTable {
columns := []table.ColumnDefinition{
table.TextColumn("name"),
table.TextColumn("namespace"),
table.TextColumn("selector"),
}
return &DeploymentsTable{
name: "kubernetes_deployments",
columns: columns,
client: kubeclient,
}
}
// Name Returns name of table
func (t *DeploymentsTable) Name() string {
return t.name
}
// Columns Retrieves the initialized columns
func (t *DeploymentsTable) Columns() []table.ColumnDefinition {
return t.columns
}
// Generate uses the api to retrieve information on all pods
func (t *DeploymentsTable) Generate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
depls, err := t.client.ExtensionsV1beta1().Deployments("").List(metav1.ListOptions{})
if err != nil {
log.Println("could not get deployments from k8s api")
return nil, err
}
rows := make([]map[string]string, len(depls.Items))
for i, depl := range depls.Items {
rows[i] = map[string]string{
"name": depl.Name,
"namespace": depl.Namespace,
// TODO: check whether Selector is always using LabelSelector
"selector": utils.Map2Str(depl.Spec.Selector.MatchLabels),
}
}
return rows, nil
}