Skip to content

Commit

Permalink
feat: add k8s health support for cel
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Aug 24, 2023
1 parent 4f5ca5d commit 796f684
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 27 deletions.
43 changes: 32 additions & 11 deletions cel/cel_test.go → celext/cel_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cel
package celext

import (
"fmt"
"testing"

"github.com/flanksource/gomplate/v3/funcs"
"github.com/flanksource/gomplate/v3/k8s"
"github.com/google/cel-go/cel"
"github.com/stretchr/testify/assert"
)
Expand All @@ -14,22 +15,22 @@ func panIf(err error) {
}
}

func executeTemplate(t *testing.T, i int, input string, output any) {
env, err := cel.NewEnv(funcs.CelEnvOption...)
func executeTemplate(t *testing.T, i int, input string, output any, environment map[string]any) {
env, err := cel.NewEnv(GetCelEnv(environment)...)
panIf(err)

ast, issues := env.Compile(input)
if issues != nil && issues.Err() != nil {
panIf(err)
}

prg, err := env.Program(ast)
prg, err := env.Program(ast, cel.Globals(environment))
panIf(err)

out, _, err := prg.Eval(map[string]any{})
out, _, err := prg.Eval(environment)
panIf(err)

assert.EqualValues(t, out.Value(), output)
assert.EqualValues(t, output, out.Value(), fmt.Sprintf("Test:%d failed", i+1))
}

func TestCelNamespace(t *testing.T) {
Expand All @@ -44,7 +45,7 @@ func TestCelNamespace(t *testing.T) {
}

for i, td := range testData {
executeTemplate(t, i, td.Input, td.Output)
executeTemplate(t, i, td.Input, td.Output, nil)
}
}

Expand All @@ -59,7 +60,7 @@ func TestCelMultipleReturns(t *testing.T) {
}

for i, td := range testData {
executeTemplate(t, i, td.Input, td.Outputs)
executeTemplate(t, i, td.Input, td.Outputs, nil)
}
}

Expand All @@ -74,7 +75,7 @@ func TestCelVariadic(t *testing.T) {
}

for i, td := range testData {
executeTemplate(t, i, td.Input, td.Output)
executeTemplate(t, i, td.Input, td.Output, nil)
}
}

Expand All @@ -87,6 +88,26 @@ func TestCelSliceReturn(t *testing.T) {
}

for i, td := range testData {
executeTemplate(t, i, td.Input, td.Output)
executeTemplate(t, i, td.Input, td.Output, nil)
}
}

func TestCelK8s(t *testing.T) {
testData := []struct {
Input string
Output any
}{
{Input: `k8s.is_healthy(healthy_obj)`, Output: true},
{Input: `k8s.is_healthy(unhealthy_obj)`, Output: false},
{Input: `k8s.health(healthy_obj).status`, Output: "Healthy"},
{Input: `k8s.health(unhealthy_obj).message`, Output: "Back-off 40s restarting failed container=main pod=my-pod_argocd(63674389-f613-11e8-a057-fe5f49266390)"},
}

for i, td := range testData {
environment := map[string]any{
"healthy_obj": k8s.TestHealthy,
"unhealthy_obj": k8s.TestUnhealthy,
}
executeTemplate(t, i, td.Input, td.Output, environment)
}
}
69 changes: 69 additions & 0 deletions celext/celfuncs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package celext

import (
"encoding/json"

"github.com/flanksource/gomplate/v3/funcs"
"github.com/flanksource/gomplate/v3/k8s"
pkgStrings "github.com/flanksource/gomplate/v3/strings"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/ext"
)

func GetCelEnv(environment map[string]any) []cel.EnvOption {
var opts []cel.EnvOption

// Generated functions
opts = append(opts, funcs.CelEnvOption...)

opts = append(opts, pkgStrings.CelEnvOption...)

// load other cel-go extensions that aren't available by default
extensions := []cel.EnvOption{ext.Math(), ext.Encoders(), ext.Strings(), ext.Sets(), ext.Lists()}
opts = append(opts, extensions...)

// Load input as variables
for k := range environment {
opts = append(opts, cel.Variable(k, cel.AnyType))
}

opts = append(opts, []cel.EnvOption{k8sHealth(), k8sIsHealthy()}...)
return opts
}

func k8sHealth() cel.EnvOption {
return cel.Function("k8s.health",
cel.Overload("k8s.health_any",
[]*cel.Type{cel.AnyType},
cel.AnyType,
cel.UnaryBinding(func(obj ref.Val) ref.Val {
jsonObj, _ := toJSON(k8s.GetHealth(obj.Value()))
return types.NewDynamicMap(types.DefaultTypeAdapter, jsonObj)
}),
),
)
}

func k8sIsHealthy() cel.EnvOption {
return cel.Function("k8s.is_healthy",
cel.Overload("k8s.is_healthy_any",
[]*cel.Type{cel.AnyType},
cel.StringType,
cel.UnaryBinding(func(obj ref.Val) ref.Val {
return types.Bool(k8s.GetHealth(obj.Value()).OK)
}),
),
)
}

func toJSON(v any) (map[string]any, error) {
var jsonObj map[string]any
b, err := json.Marshal(v)
if err != nil {
return jsonObj, err
}
err = json.Unmarshal(b, &jsonObj)
return jsonObj, err
}
18 changes: 2 additions & 16 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
"strings"
gotemplate "text/template"

"github.com/flanksource/gomplate/v3/funcs"
"github.com/flanksource/gomplate/v3/celext"
_ "github.com/flanksource/gomplate/v3/js"
pkgStrings "github.com/flanksource/gomplate/v3/strings"
"github.com/flanksource/mapstructure"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/ext"
"github.com/robertkrimen/otto"
"github.com/robertkrimen/otto/registry"
_ "github.com/robertkrimen/otto/underscore"
Expand Down Expand Up @@ -66,7 +64,6 @@ func RunTemplate(environment map[string]any, template Template) (string, error)
if err != nil {
return "", err
}

data, err := serialize(environment)
if err != nil {
return "", err
Expand All @@ -81,18 +78,7 @@ func RunTemplate(environment map[string]any, template Template) (string, error)

// cel-go
if template.Expression != "" {
var opts = funcs.CelEnvOption
opts = append(opts, pkgStrings.CelEnvOption...)

// load other cel-go extensions that aren't available by default
extensions := []cel.EnvOption{ext.Math(), ext.Encoders(), ext.Strings(), ext.Sets(), ext.Lists()}
opts = append(opts, extensions...)

for k := range environment {
opts = append(opts, cel.Variable(k, cel.AnyType))
}

env, err := cel.NewEnv(opts...)
env, err := cel.NewEnv(celext.GetCelEnv(environment)...)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 796f684

Please sign in to comment.