Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add column selector_query to kubernetes_* tables and add optional key column selector_search to kubernetes_pod table. Closes #63 #65

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions kubernetes/table_kubernetes_daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func tableKubernetesDaemonset(ctx context.Context) *plugin.Table {
Description: "The number of old history to retain to allow rollback. This is a pointer to distinguish between explicit zero and not specified. Defaults to 10.",
Transform: transform.FromField("Spec.RevisionHistoryLimit"),
},
{
Name: "selector_query",
Type: proto.ColumnType_STRING,
Description: "A query string representation of the selector.",
Transform: transform.FromField("Spec.Selector").Transform(labelSelectorToString),
},
{
Name: "selector",
Type: proto.ColumnType_JSON,
Expand Down
6 changes: 6 additions & 0 deletions kubernetes/table_kubernetes_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func tableKubernetesDeployment(ctx context.Context) *plugin.Table {
Description: "Number of desired pods. Defaults to 1.",
Transform: transform.FromField("Spec.Replicas"),
},
{
Name: "selector_query",
Type: proto.ColumnType_STRING,
Description: "A query string representation of the selector.",
Transform: transform.FromField("Spec.Selector").Transform(labelSelectorToString),
},
{
Name: "selector",
Type: proto.ColumnType_JSON,
Expand Down
18 changes: 12 additions & 6 deletions kubernetes/table_kubernetes_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ func tableKubernetesJob(ctx context.Context) *plugin.Table {
Description: "The number of retries before marking this job failed. Defaults to 6.",
Transform: transform.FromField("Spec.BackoffLimit"),
},
{
Name: "selector",
Type: proto.ColumnType_JSON,
Description: "A label query over pods that should match the pod count.",
Transform: transform.FromField("Spec.Selector"),
},
{
Name: "manual_selector",
Type: proto.ColumnType_BOOL,
Expand All @@ -66,6 +60,18 @@ func tableKubernetesJob(ctx context.Context) *plugin.Table {
Description: "limits the lifetime of a Job that has finished execution (either Complete or Failed). If this field is set, ttlSecondsAfterFinished after the Job finishes, it is eligible to be automatically deleted.",
Transform: transform.FromField("Spec.TTLSecondsAfterFinished"),
},
{
Name: "selector_query",
Type: proto.ColumnType_STRING,
Description: "A query string representation of the selector.",
Transform: transform.FromField("Spec.Selector").Transform(labelSelectorToString),
},
{
Name: "selector",
Type: proto.ColumnType_JSON,
Description: "A label query over pods that should match the pod count.",
Transform: transform.FromField("Spec.Selector"),
},
{
Name: "template",
Type: proto.ColumnType_JSON,
Expand Down
15 changes: 6 additions & 9 deletions kubernetes/table_kubernetes_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ func tableKubernetesPod(ctx context.Context) *plugin.Table {
List: &plugin.ListConfig{
Hydrate: listK8sPods,
KeyColumns: []*plugin.KeyColumn{
{Name: "label_selector", Require: plugin.Optional},
{Name: "selector_search", Require: plugin.Optional, CacheMatch: "exact"},
},
},
Columns: k8sCommonColumns([]*plugin.Column{
//// PodSpec Columns
{
Name: "label_selector",
Name: "selector_search",
Type: proto.ColumnType_STRING,
Description: "A selector to restrict the list of returned objects by their labels.",
Transform: transform.FromQual("label_selector"),
Description: "A label selector string to restrict the list of returned objects by their labels.",
Transform: transform.FromQual("selector_search"),
},
{
Name: "volumes",
Expand Down Expand Up @@ -421,11 +421,8 @@ func listK8sPods(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData
}

option := metav1.ListOptions{}

labelSelector := d.KeyColumnQuals["label_selector"].GetStringValue()

if labelSelector != "" {
option.LabelSelector = labelSelector
if d.KeyColumnQuals["selector_search"] != nil {
option.LabelSelector = d.KeyColumnQuals["selector_search"].GetStringValue()
}

pods, err := clientset.CoreV1().Pods("").List(ctx, option)
Expand Down
6 changes: 6 additions & 0 deletions kubernetes/table_kubernetes_replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func tableKubernetesReplicaSet(ctx context.Context) *plugin.Table {
Description: "Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0",
Transform: transform.FromField("Spec.MinReadySeconds"),
},
{
Name: "selector_query",
Type: proto.ColumnType_STRING,
Description: "A query string representation of the selector.",
Transform: transform.FromField("Spec.Selector").Transform(labelSelectorToString),
},
{
Name: "selector",
Type: proto.ColumnType_JSON,
Expand Down
6 changes: 6 additions & 0 deletions kubernetes/table_kubernetes_replication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func tableKubernetesReplicaController(ctx context.Context) *plugin.Table {
Description: "Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0",
Transform: transform.FromField("Spec.MinReadySeconds"),
},
{
Name: "selector_query",
Type: proto.ColumnType_STRING,
Description: "A query string representation of the selector.",
Transform: transform.FromField("Spec.Selector").Transform(labelSelectorToString),
},
{
Name: "selector",
Type: proto.ColumnType_JSON,
Expand Down
6 changes: 6 additions & 0 deletions kubernetes/table_kubernetes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func tableKubernetesService(ctx context.Context) *plugin.Table {
Description: "A list of ports that are exposed by this service.",
Transform: transform.FromField("Spec.Ports"),
},
{
Name: "selector_query",
Type: proto.ColumnType_STRING,
Description: "Route service traffic to pods with label keys and values matching this selector. String format representation.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description should probably be consistent with the other selector_query descrptions

Transform: transform.FromField("Spec.Selector").Transform(selectorMapToString),
},
{
Name: "selector",
Type: proto.ColumnType_JSON,
Expand Down
34 changes: 33 additions & 1 deletion kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"strings"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

Expand Down Expand Up @@ -207,6 +208,37 @@ func v1TimeToRFC3339(_ context.Context, d *transform.TransformData) (interface{}
}
}

func labelSelectorToString(_ context.Context, d *transform.TransformData) (interface{}, error) {
if d.Value == nil {
return nil, nil
}

selector := d.Value.(*v1.LabelSelector)

ss, err := v1.LabelSelectorAsSelector(selector)
if err != nil {
return nil, err
}

return ss.String(), nil
}

func selectorMapToString(ctx context.Context, d *transform.TransformData) (interface{}, error) {
logger := plugin.Logger(ctx)
logger.Trace("selectorMapToString")

selector_map := d.Value.(map[string]string)

if len(selector_map) == 0 {
logger.Warn("selectorMapToString", "!!! selector is empty")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this warning was for debugging and should be removed

return nil, nil
}

selector_string := labels.SelectorFromSet(selector_map).String()

return selector_string, nil
}

//// Other Utility functions

func isNotFoundError(err error) bool {
Expand Down