Skip to content

Commit

Permalink
fix: normalize the k8s workload to fix a deep-copy int error (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed May 29, 2024
1 parent fc20b33 commit 036e44a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit 036e44a

Please sign in to comment.