feat: upstream enterprise preview (#1841)

feat: upstream enterprise preview

---------

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
Co-authored-by: CorentinPtrl <pitrel.corentin@gmail.com>
This commit is contained in:
Oliver Bähler
2026-05-28 00:58:58 +02:00
committed by GitHub
co-authored by CorentinPtrl
parent 7a65ab7afc
commit cc4fb45d70
462 changed files with 48456 additions and 10529 deletions
+52
View File
@@ -0,0 +1,52 @@
// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package jsonpath
import (
"bytes"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/util/jsonpath"
)
const maxJSONPathLength = 1024
// CompiledJSONPath wraps a parsed JSONPath expression for repeated use.
type CompiledJSONPath struct {
jp *jsonpath.JSONPath
}
// CompileJSONPath parses and validates a JSONPath source path once.
// Example sourcePath: ".spec.resources.requests.cpu".
func CompileJSONPath(sourcePath string) (*CompiledJSONPath, error) {
sourcePath = strings.TrimSpace(sourcePath)
if err := validateSourcePath(sourcePath); err != nil {
return nil, err
}
j := jsonpath.New("usagePath")
j.AllowMissingKeys(true)
if err := j.Parse(wrapJSONPath(sourcePath)); err != nil {
return nil, fmt.Errorf("parse usage jsonpath %q: %w", sourcePath, err)
}
return &CompiledJSONPath{jp: j}, nil
}
// Execute applies a precompiled JSONPath to the given object and returns the extracted value.
func (c *CompiledJSONPath) Execute(u unstructured.Unstructured) (string, error) {
if c == nil || c.jp == nil {
return "", fmt.Errorf("compiled jsonpath is nil")
}
var buf bytes.Buffer
if err := c.jp.Execute(&buf, u.Object); err != nil {
return "", fmt.Errorf("execute usage jsonpath: %w", err)
}
return strings.TrimSpace(buf.String()), nil
}
+258
View File
@@ -0,0 +1,258 @@
// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package jsonpath
import (
"strings"
"testing"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func TestCompileJSONPath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
sourcePath string
wantErr bool
errMsg string
}{
{
name: "valid simple path",
sourcePath: ".spec.resources.requests.cpu",
},
{
name: "valid path with surrounding whitespace",
sourcePath: " .spec.resources.requests.memory ",
},
{
name: "empty path",
sourcePath: "",
wantErr: true,
errMsg: "sourcePath must not be empty",
},
{
name: "missing leading dot",
sourcePath: "spec.resources.requests.cpu",
wantErr: true,
errMsg: "sourcePath must start with '.'",
},
{
name: "contains newline",
sourcePath: ".spec.\nresources.requests.cpu",
wantErr: true,
errMsg: "sourcePath must not contain control whitespace",
},
{
name: "contains tab",
sourcePath: ".spec.\tresources.requests.cpu",
wantErr: true,
errMsg: "sourcePath must not contain control whitespace",
},
{
name: "too long",
sourcePath: "." + strings.Repeat("a", maxJSONPathLength),
wantErr: true,
errMsg: "sourcePath exceeds max length",
},
{
name: "invalid jsonpath syntax",
sourcePath: ".spec[",
wantErr: true,
errMsg: "parse usage jsonpath",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := CompileJSONPath(tt.sourcePath)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.errMsg) {
t.Fatalf("expected error to contain %q, got %q", tt.errMsg, err.Error())
}
if got != nil {
t.Fatalf("expected compiled path to be nil on error, got %#v", got)
}
return
}
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if got == nil {
t.Fatal("expected compiled path, got nil")
}
if got.jp == nil {
t.Fatal("expected compiled jsonpath to be initialized, got nil jp")
}
})
}
}
func TestCompiledJSONPathExecute(t *testing.T) {
t.Parallel()
tests := []struct {
name string
compiled *CompiledJSONPath
object unstructured.Unstructured
want string
wantErr bool
errMsg string
prepareJP string
}{
{
name: "nil receiver",
compiled: nil,
object: unstructured.Unstructured{},
wantErr: true,
errMsg: "compiled jsonpath is nil",
},
{
name: "nil jsonpath",
compiled: &CompiledJSONPath{},
object: unstructured.Unstructured{},
wantErr: true,
errMsg: "compiled jsonpath is nil",
},
{
name: "extract string value",
prepareJP: ".spec.resources.requests.cpu",
object: unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"resources": map[string]interface{}{
"requests": map[string]interface{}{
"cpu": "250m",
},
},
},
},
},
want: "250m",
},
{
name: "trim surrounding whitespace",
prepareJP: ".spec.value",
object: unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"value": " hello world ",
},
},
},
want: "hello world",
},
{
name: "extract numeric value",
prepareJP: ".spec.replicas",
object: unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"replicas": int64(3),
},
},
},
want: "3",
},
{
name: "missing path returns empty string",
prepareJP: ".spec.resources.requests.memory",
object: unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"resources": map[string]interface{}{
"requests": map[string]interface{}{
"cpu": "250m",
},
},
},
},
},
want: "",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
compiled := tt.compiled
if tt.prepareJP != "" {
var err error
compiled, err = CompileJSONPath(tt.prepareJP)
if err != nil {
t.Fatalf("failed to compile jsonpath for test: %v", err)
}
}
got, err := compiled.Execute(tt.object)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil (value=%q)", got)
}
if !strings.Contains(err.Error(), tt.errMsg) {
t.Fatalf("expected error to contain %q, got %q", tt.errMsg, err.Error())
}
return
}
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if got != tt.want {
t.Fatalf("expected %q, got %q", tt.want, got)
}
})
}
}
func TestCompileUsageJSONPath_Execute_Success(t *testing.T) {
compiled, err := CompileJSONPath(".spec.resources.requests.memory")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
u := unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"resources": map[string]interface{}{
"requests": map[string]interface{}{
"memory": "1Gi",
},
},
},
},
}
got, err := compiled.Execute(u)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if got != "1Gi" {
t.Fatalf("expected %q, got %q", "1Gi", got)
}
}
func TestCompiledJSONPath_Execute_NilReceiver(t *testing.T) {
var compiled *CompiledJSONPath
u := unstructured.Unstructured{
Object: map[string]interface{}{},
}
_, err := compiled.Execute(u)
if err == nil {
t.Fatal("expected error, got nil")
}
}
+41
View File
@@ -0,0 +1,41 @@
// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package jsonpath
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// EvaluateTruthyFromCompiled evaluates a compiled JSONPath expression and interprets the result
// using "truthy" semantics:
//
// - empty result => false
// - "false" (case-insensitive) => false
// - "0" => false
// - anything else non-empty => true
func EvaluateTruthyFromCompiled(u unstructured.Unstructured, compiled *CompiledJSONPath) (bool, error) {
if compiled == nil {
return false, fmt.Errorf("compiled jsonpath is nil")
}
value, err := compiled.Execute(u)
if err != nil {
return false, err
}
value = strings.TrimSpace(value)
if value == "" {
return false, nil
}
switch strings.ToLower(value) {
case "false", "0":
return false, nil
default:
return true, nil
}
}
+33
View File
@@ -0,0 +1,33 @@
// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package jsonpath
import (
"fmt"
"strings"
)
func wrapJSONPath(sourcePath string) string {
return fmt.Sprintf("{%s}", sourcePath)
}
func validateSourcePath(sourcePath string) error {
if sourcePath == "" {
return fmt.Errorf("sourcePath must not be empty")
}
if len(sourcePath) > maxJSONPathLength {
return fmt.Errorf("sourcePath exceeds max length of %d", maxJSONPathLength)
}
if !strings.HasPrefix(sourcePath, ".") {
return fmt.Errorf("sourcePath must start with '.'")
}
if strings.ContainsAny(sourcePath, "\r\n\t") {
return fmt.Errorf("sourcePath must not contain control whitespace")
}
return nil
}
+120
View File
@@ -0,0 +1,120 @@
// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package jsonpath
import (
"strings"
"testing"
)
func TestWrapJSONPath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
sourcePath string
want string
}{
{
name: "simple path",
sourcePath: ".spec.resources.requests.cpu",
want: "{.spec.resources.requests.cpu}",
},
{
name: "empty path",
sourcePath: "",
want: "{}",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := wrapJSONPath(tt.sourcePath)
if got != tt.want {
t.Fatalf("expected %q, got %q", tt.want, got)
}
})
}
}
func TestValidateSourcePath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
sourcePath string
wantErr bool
errMsg string
}{
{
name: "valid path",
sourcePath: ".spec.resources.requests.cpu",
},
{
name: "valid minimal path",
sourcePath: ".a",
},
{
name: "empty path",
sourcePath: "",
wantErr: true,
errMsg: "sourcePath must not be empty",
},
{
name: "too long",
sourcePath: "." + strings.Repeat("a", maxJSONPathLength),
wantErr: true,
errMsg: "sourcePath exceeds max length",
},
{
name: "missing dot prefix",
sourcePath: "spec.value",
wantErr: true,
errMsg: "sourcePath must start with '.'",
},
{
name: "contains carriage return",
sourcePath: ".spec\r.value",
wantErr: true,
errMsg: "sourcePath must not contain control whitespace",
},
{
name: "contains newline",
sourcePath: ".spec\nvalue",
wantErr: true,
errMsg: "sourcePath must not contain control whitespace",
},
{
name: "contains tab",
sourcePath: ".spec\tvalue",
wantErr: true,
errMsg: "sourcePath must not contain control whitespace",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := validateSourcePath(tt.sourcePath)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.errMsg) {
t.Fatalf("expected error to contain %q, got %q", tt.errMsg, err.Error())
}
return
}
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
})
}
}