Skip to content

Commit 34eed68

Browse files
committed
fix: string and k8s function return types
1 parent 39543d6 commit 34eed68

File tree

5 files changed

+45
-56
lines changed

5 files changed

+45
-56
lines changed

funcs/strings_gen.go

Lines changed: 15 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kubernetes/cel_export.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ func Library() []cel.EnvOption {
1111
k8sGetHealth(),
1212
k8sGetStatus(),
1313
k8sIsHealthy2(),
14-
k8sHealth(),
1514
k8sCPUAsMillicores(),
1615
k8sMemoryAsBytes(),
1716
}

kubernetes/health.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,9 @@ func GetHealth(in interface{}) HealthStatus {
104104
}
105105
}
106106

107-
func k8sHealth() cel.EnvOption {
108-
return cel.Function("k8s.health",
109-
cel.Overload("k8s.health_any",
110-
[]*cel.Type{cel.AnyType},
111-
cel.AnyType,
112-
cel.UnaryBinding(func(obj ref.Val) ref.Val {
113-
jsonObj, _ := conv.AnyToMapStringAny(GetHealth(obj.Value()))
114-
return types.NewDynamicMap(types.DefaultTypeAdapter, jsonObj)
115-
}),
116-
),
117-
)
118-
}
119-
120107
func k8sGetHealth() cel.EnvOption {
121-
return cel.Function("GetHealth",
122-
cel.Overload("GetHealth_any",
108+
return cel.Function("k8s.getHealth",
109+
cel.Overload("k8s.getHealth",
123110
[]*cel.Type{cel.AnyType},
124111
cel.AnyType,
125112
cel.UnaryBinding(func(obj ref.Val) ref.Val {
@@ -131,8 +118,8 @@ func k8sGetHealth() cel.EnvOption {
131118
}
132119

133120
func k8sGetStatus() cel.EnvOption {
134-
return cel.Function("GetStatus",
135-
cel.Overload("GetStatus",
121+
return cel.Function("k8s.getStatus",
122+
cel.Overload("k8s.getStatus",
136123
[]*cel.Type{cel.AnyType},
137124
cel.AnyType,
138125
cel.UnaryBinding(func(obj ref.Val) ref.Val {
@@ -143,10 +130,10 @@ func k8sGetStatus() cel.EnvOption {
143130
}
144131

145132
func k8sIsHealthy() cel.EnvOption {
146-
return cel.Function("k8s.is_healthy",
147-
cel.Overload("k8s.is_healthy_any",
133+
return cel.Function("k8s.isHealthy",
134+
cel.Overload("k8s.isHealthy_any",
148135
[]*cel.Type{cel.AnyType},
149-
cel.StringType,
136+
cel.BoolType,
150137
cel.UnaryBinding(func(obj ref.Val) ref.Val {
151138
return types.Bool(GetHealth(obj.Value()).OK)
152139
}),
@@ -158,7 +145,7 @@ func k8sIsHealthy2() cel.EnvOption {
158145
return cel.Function("IsHealthy",
159146
cel.Overload("IsHealthy_interface{}",
160147
[]*cel.Type{cel.AnyType},
161-
cel.StringType,
148+
cel.BoolType,
162149
cel.UnaryBinding(func(obj ref.Val) ref.Val {
163150
return types.Bool(GetHealth(obj.Value()).OK)
164151
}),

kubernetes/testdata.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package kubernetes
22

3+
import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
4+
35
var TestHealthy = `
46
apiVersion: v1
57
kind: Service
@@ -29,6 +31,7 @@ status:
2931
- hostname: abc123.us-west-2.elb.amazonaws.com
3032
`
3133

34+
var TesthealthyUnstructured = GetUnstructured(TestHealthy)
3235
var TestProgressing = `
3336
apiVersion: v1
3437
kind: Service
@@ -52,6 +55,8 @@ status:
5255
loadBalancer: {}
5356
`
5457

58+
var TestProgressingUnstructured = GetUnstructured(TestProgressing)
59+
5560
var TestUnhealthy = `
5661
apiVersion: v1
5762
kind: Pod
@@ -141,6 +146,8 @@ status:
141146
startTime: 2018-12-02T09:19:36Z
142147
`
143148

149+
var TestUnhealthyUnstructured = GetUnstructured(TestUnhealthy)
150+
144151
var TestLuaStatus = `
145152
apiVersion: argoproj.io/v1alpha1
146153
kind: ApplicationSet
@@ -173,3 +180,9 @@ status:
173180
status: "True"
174181
type: ErrorOccurred
175182
`
183+
184+
var TestLuaStatusUnstructured = GetUnstructured(TestLuaStatus)
185+
186+
var TestUnstructuredList = []unstructured.Unstructured{
187+
*TesthealthyUnstructured, *TestProgressingUnstructured, *TestUnhealthyUnstructured, *TestLuaStatusUnstructured,
188+
}

tests/cel_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func TestExtensions(t *testing.T) {
207207
{nil, `"hello, world".replace("world", "team")`, "hello, team"}, // strings lib
208208
{nil, `sets.contains([1, 2, 3, 4], [2, 3])`, "true"}, // sets lib
209209
{nil, `[1,2,3,4].slice(1, 3)`, "[2 3]"}, // lists lib
210+
{nil, `"this is a string: %s\nand an integer: %d".format(["str", 42])`, "this is a string: str\nand an integer: 42"},
210211
})
211212
}
212213
func TestStrings(t *testing.T) {
@@ -226,14 +227,15 @@ func TestStrings(t *testing.T) {
226227

227228
// {map[string]interface{}{"s": []string{"a", "b", "b", "a"}}, "Uniq(s)", "[a b]"},
228229
{map[string]interface{}{"s": "hello world"}, "s.title()", "Hello World"},
230+
{map[string]interface{}{"s": "hello world"}, "s.abbrev(4)", "h..."},
229231
{map[string]interface{}{"s": "hello world"}, "s.camelCase()", "helloWorld"},
230232
{map[string]interface{}{"s": "hello world"}, "s.kebabCase()", "hello-world"},
231233
{map[string]interface{}{"s": "hello world"}, "s.snakeCase()", "hello_world"},
232234
{map[string]interface{}{"s": "hel\"lo world"}, "s.quote()", "\"hel\\\"lo world\""},
233235
{map[string]interface{}{"s": "hello world"}, "s.squote()", "'hello world'"},
234236
{map[string]interface{}{"s": "hello world"}, "s.shellQuote()", "'hello world'"},
235237
{map[string]interface{}{"s": "hello world"}, "s.slug()", "hello-world"},
236-
// {map[string]interface{}{"s": "hello world"}, "s.runeCount()", "Hello World"},
238+
{map[string]interface{}{"s": "hello world"}, "s.indent(4, '-')", "----hello world"},
237239
{nil, "uuid.IsValid(uuid.V1())", "true"},
238240
{nil, "uuid.IsValid(uuid.V4())", "true"},
239241
}
@@ -343,6 +345,8 @@ func TestCelK8s(t *testing.T) {
343345
}{
344346

345347
{Input: `k8s.is_healthy(healthy_obj)`, Output: true},
348+
{Input: `dyn(obj_list).all(i, k8s.is_healthy(i))`, Output: false},
349+
{Input: `dyn(unstructured_list).all(i, k8s.is_healthy(i))`, Output: false},
346350
{Input: `k8s.is_healthy(unhealthy_obj)`, Output: false},
347351
{Input: `k8s.health(healthy_obj).status`, Output: "Healthy"},
348352
{Input: `k8s.health(unhealthy_obj).message`, Output: "Back-off 40s restarting failed container=main pod=my-pod_argocd(63674389-f613-11e8-a057-fe5f49266390)"},
@@ -351,8 +355,10 @@ func TestCelK8s(t *testing.T) {
351355
}
352356

353357
environment := map[string]any{
354-
"healthy_obj": kubernetes.TestHealthy,
355-
"unhealthy_obj": kubernetes.TestUnhealthy,
358+
"healthy_obj": kubernetes.TestHealthy,
359+
"unhealthy_obj": kubernetes.TestUnhealthy,
360+
"obj_list": []string{kubernetes.TestHealthy, kubernetes.TestUnhealthy},
361+
"unstructured_list": kubernetes.TestUnstructuredList,
356362
}
357363
for i, td := range testData {
358364
executeTemplate(t, i, td.Input, td.Output, environment)

0 commit comments

Comments
 (0)