-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add KCL based customized health check
- Loading branch information
1 parent
f2ee452
commit 7d3e180
Showing
7 changed files
with
201 additions
and
5 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
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,45 @@ | ||
package kcl | ||
|
||
import ( | ||
"fmt" | ||
|
||
"kcl-lang.io/kcl-go/pkg/kcl" | ||
"kcl-lang.io/kcl-go/pkg/tools/format" | ||
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" | ||
) | ||
|
||
const ref = "res" | ||
|
||
// Assemble and format the whole KCL code with yaml integration. | ||
func assembleKCLHealthCheck(healthPolicyCode string, resource []byte) (string, error) { | ||
yamlStr := fmt.Sprintf(` | ||
import yaml | ||
%s = yaml.decode(%q) | ||
`, ref, resource) | ||
kclCode := yamlStr + healthPolicyCode | ||
kclFormatted, err := format.FormatCode(kclCode) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(kclFormatted), nil | ||
} | ||
|
||
// Run health check with KCL health policy during apply. | ||
func RunKCLHealthCheck(healthPolicyCode string, resource []byte) error { | ||
kclCode, err := assembleKCLHealthCheck(healthPolicyCode, resource) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = kcl.Run("", kcl.WithCode(kclCode)) | ||
return err | ||
} | ||
|
||
// Get KCL code from extensions of the resource in the Spec. | ||
func ConvertKCLCode(healthPolicy any) (string, bool) { | ||
if hp, ok := healthPolicy.(map[string]any); ok { | ||
if code, ok := hp[v1.FieldKCLHealthCheckKCL].(string); ok { | ||
return code, true | ||
} | ||
} | ||
return "", false | ||
} |
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,102 @@ | ||
package kcl | ||
|
||
import ( | ||
"testing" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" | ||
) | ||
|
||
func TestAssembleKCLHealthCheck(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
hpCode string | ||
resource []byte | ||
want string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "Valid input", | ||
hpCode: "assert res.a == res.b", | ||
resource: []byte("this is resource"), | ||
want: "import yaml\n\nres = yaml.decode(\"this is resource\")\nassert res.a == res.b\n", | ||
expectError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := assembleKCLHealthCheck(tt.hpCode, tt.resource) | ||
if (err != nil) != tt.expectError { | ||
t.Errorf("assembleKCLHealthCheck() error = %v, expectError %v", err, tt.expectError) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("assembleKCLHealthCheck() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRunKCLHealthCheck(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
hpCode string | ||
resource []byte | ||
expectError bool | ||
}{ | ||
{ | ||
name: "Valid input", | ||
hpCode: "a = \"this is health policy\"", | ||
resource: []byte("this is resource"), | ||
expectError: false, | ||
}, | ||
// Add more test cases if needed | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := RunKCLHealthCheck(tt.hpCode, tt.resource) | ||
if (err != nil) != tt.expectError { | ||
t.Errorf("RunKCLHealthCheck() error = %v, expectError %v", err, tt.expectError) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestConvertKCLCode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
healthPolicy any | ||
want string | ||
expectOk bool | ||
}{ | ||
{ | ||
name: "Valid KCL code in health policy", | ||
healthPolicy: map[string]any{ | ||
v1.FieldKCLHealthCheckKCL: "assert res.a == res.b", | ||
}, | ||
want: "assert res.a == res.b", | ||
expectOk: true, | ||
}, | ||
{ | ||
name: "No KCL code in health policy", | ||
healthPolicy: map[string]any{ | ||
"other_field": "other_value", | ||
}, | ||
want: "", | ||
expectOk: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, ok := ConvertKCLCode(tt.healthPolicy) | ||
if got != tt.want { | ||
t.Errorf("ConvertKCLCode() got = %v, want %v", got, tt.want) | ||
} | ||
if ok != tt.expectOk { | ||
t.Errorf("ConvertKCLCode() ok = %v, expectOk %v", ok, tt.expectOk) | ||
} | ||
}) | ||
} | ||
} |