Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize the k8s workload to fix a deep-copy int error #1138

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pkg/modules/generators/app_configurations_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
k8sjson "k8s.io/apimachinery/pkg/util/json"
"kcl-lang.io/kpm/pkg/package"

v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
Expand Down Expand Up @@ -214,7 +215,17 @@ func PatchWorkload(workload *v1.Resource, patcher *v1.Patcher) error {
}

un := &unstructured.Unstructured{}
un.SetUnstructuredContent(workload.Attributes)
attributes := workload.Attributes

// normalize attributes with K8s json util. Especially numbers are converted to int64 or float64
out, err := k8sjson.Marshal(attributes)
if err != nil {
return err
}
if err = k8sjson.Unmarshal(out, &attributes); err != nil {
return err
}
un.SetUnstructuredContent(attributes)

// patch labels
if patcher.Labels != nil {
Expand Down
17 changes: 16 additions & 1 deletion pkg/modules/generators/app_configurations_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -292,6 +293,14 @@ func Test_patchWorkload(t *testing.T) {
{
Name: "my-app",
Image: "my-app-image",
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
TCPSocket: &corev1.TCPSocketAction{
Host: "localhost:8080",
},
},
InitialDelaySeconds: 2,
},
Env: []corev1.EnvVar{
{
Name: "MY_ENV",
Expand All @@ -304,8 +313,14 @@ func Test_patchWorkload(t *testing.T) {
},
},
}
// convert deploy to unstructured

// convert deploy to map with yamlv3 to simulate what we did in the module framework
deploymentUnstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(deployment)
assert.NoError(t, err)
out, err := yaml.Marshal(deploymentUnstructured)
assert.NoError(t, err)
err = yaml.Unmarshal(out, deploymentUnstructured)

res := &v1.Resource{
ID: "apps/v1:Deployment:default:default-dev-foo",
Type: "Kubernetes",
Expand Down
Loading