-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e0e416
commit 3afc8a9
Showing
7 changed files
with
327 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package config | ||
|
||
type CustomResourceLinks map[string]*CustomResourceLink | ||
|
||
func NewCustomResourceLinks() CustomResourceLinks { | ||
return CustomResourceLinks{} | ||
} | ||
|
||
// CustomResourceLink tracks K9s CustomResourceLink configuration. | ||
type CustomResourceLink struct { | ||
// Target represents the target GVR to open when activating a custom resource link. | ||
Target string `yaml:"target"` | ||
// LabelSelector defines keys (=target label) and values (=json path) to extract from the current resource. | ||
LabelSelector map[string]string `yaml:"labelSelector,omitempty"` | ||
// FieldSelector defines keys (=target field) and values (=json path) to extract from the current resource. | ||
FieldSelector map[string]string `yaml:"fieldSelector,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package dao | ||
|
||
import ( | ||
"fmt" | ||
"github.com/derailed/k9s/internal/config" | ||
"github.com/rs/zerolog/log" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/selection" | ||
"k8s.io/client-go/util/jsonpath" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
// extractPathFromObject extracts a json path from a given object. | ||
func extractPathFromObject(o runtime.Object, path string) ([]string, error) { | ||
var err error | ||
parser := jsonpath.New("accessor").AllowMissingKeys(true) | ||
parser.EnableJSONOutput(true) | ||
fullPath := fmt.Sprintf("{%s}", path) | ||
if err := parser.Parse(fullPath); err != nil { | ||
return nil, err | ||
} | ||
log.Debug().Msgf("Prepared JSONPath %s.", fullPath) | ||
|
||
var results [][]reflect.Value | ||
if unstructured, ok := o.(runtime.Unstructured); ok { | ||
results, err = parser.FindResults(unstructured.UnstructuredContent()) | ||
} else { | ||
results, err = parser.FindResults(reflect.ValueOf(o).Elem().Interface()) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Debug().Msgf("Results extracted %s.", results) | ||
|
||
if len(results) != 1 { | ||
return nil, nil | ||
} | ||
|
||
var values = make([]string, 0) | ||
for arrIx := range results { | ||
for valIx := range results[arrIx] { | ||
values = append(values, fmt.Sprint(results[arrIx][valIx].Interface())) | ||
} | ||
} | ||
return values, nil | ||
} | ||
|
||
// SelectorsForLink builds label and field selectors from a given object based on a custom resource link. | ||
func SelectorsForLink(link *config.CustomResourceLink, o runtime.Object) (labels.Selector, fields.Selector, error) { | ||
var labelSelector = labels.Everything() | ||
var fieldSelector fields.Selector | ||
|
||
for targetLabel, sourceField := range link.LabelSelector { | ||
values, err := extractPathFromObject(o, sourceField) | ||
switch { | ||
case err != nil: | ||
return nil, nil, err | ||
case values == nil || len(values) != 1: | ||
continue | ||
} | ||
log.Debug().Msgf("Extracted values for label selector %s: %+v.", targetLabel, values) | ||
|
||
req, err := labels.NewRequirement(targetLabel, selection.Equals, values) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
labelSelector = labelSelector.Add(*req) | ||
} | ||
|
||
for targetField, sourceField := range link.FieldSelector { | ||
values, err := extractPathFromObject(o, sourceField) | ||
switch { | ||
case err != nil: | ||
return nil, nil, err | ||
case values == nil: | ||
continue | ||
} | ||
log.Debug().Msgf("Extracted values for field selector %s: %+v.", targetField, values) | ||
|
||
sel := fields.OneTermEqualSelector(targetField, strings.Join(values, ",")) | ||
if fieldSelector == nil { | ||
fieldSelector = sel | ||
continue | ||
} | ||
fieldSelector = fields.AndSelectors( | ||
fieldSelector, | ||
sel, | ||
) | ||
} | ||
return labelSelector, fieldSelector, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package dao | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"testing" | ||
) | ||
|
||
type obj struct { | ||
Value string `json:"value,omitempty"` | ||
Child struct { | ||
Nested string `json:"nested,omitempty"` | ||
} `json:"child"` | ||
} | ||
|
||
func (o obj) GetObjectKind() schema.ObjectKind { | ||
return nil | ||
} | ||
|
||
func (o obj) DeepCopyObject() runtime.Object { | ||
return nil | ||
} | ||
|
||
func TestExtractPathFromObject(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
obj runtime.Object | ||
path string | ||
want []string | ||
}{ | ||
{ | ||
name: "empty path", | ||
obj: &obj{ | ||
Value: "", | ||
Child: struct { | ||
Nested string `json:"nested,omitempty"` | ||
}{}, | ||
}, | ||
path: ".nonexistent", | ||
want: []string{}, | ||
}, | ||
{ | ||
name: "object with path", | ||
obj: &obj{ | ||
Value: "foo", | ||
Child: struct { | ||
Nested string `json:"nested,omitempty"` | ||
}{}, | ||
}, | ||
path: ".value", | ||
want: []string{"foo"}, | ||
}, | ||
{ | ||
name: "nested with path", | ||
obj: &obj{ | ||
Value: "foo", | ||
Child: struct { | ||
Nested string `json:"nested,omitempty"` | ||
}{ | ||
Nested: "bar", | ||
}, | ||
}, | ||
path: ".child.nested", | ||
want: []string{"bar"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := extractPathFromObject(tt.obj, tt.path) | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.