Repository: kubean-io/kubean
Pull request #195 (2022-10-12 11:28:51)

struct-omitempty-pointer
<!--  

Thanks for sending a pull request!  Here are some tips for you:

* make sure your commit is signed off

-->

**What type of PR is this?**
<!-- 
Uncomment only one ` /kind <>` line, hit enter to put that in a new line, and remove leading whitespaces from that line:

/kind api-change
/kind bug
/kind cleanup
/kind design
/kind documentation
/kind failing-test
/kind feature
/kind flake
-->
/kind bug
**What this PR does / why we need it**:
it will panic when HostsMap len equal zero and k8s version less than 1.20.11, because the HostsMap's type is struct.
if type is struct but not Pointer, omitempty invalidate
**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

**Special notes for your reviewer**:




Patch:
diff --git a/api/apis/kubeaninfomanifest/v1alpha1/types.go b/api/apis/kubeaninfomanifest/v1alpha1/types.go
index 49f61f733..587c91b1b 100644
--- a/api/apis/kubeaninfomanifest/v1alpha1/types.go
+++ b/api/apis/kubeaninfomanifest/v1alpha1/types.go
@@ -49,7 +49,7 @@ type LocalService struct {
 	// +optional
 	YumRepo []string `json:"yumRepo,omitempty" yaml:"yumRepo,omitempty"`
 	// +optional
-	HostsMap []HostsMap `json:"hostsMap,omitempty" yaml:"hostsMap,omitempty"`
+	HostsMap []*HostsMap `json:"hostsMap,omitempty" yaml:"hostsMap,omitempty"`
 }
 
 type HostsMap struct {
diff --git a/api/apis/kubeaninfomanifest/v1alpha1/zz_generated.deepcopy.go b/api/apis/kubeaninfomanifest/v1alpha1/zz_generated.deepcopy.go
index f45fd241f..4c198eefa 100644
--- a/api/apis/kubeaninfomanifest/v1alpha1/zz_generated.deepcopy.go
+++ b/api/apis/kubeaninfomanifest/v1alpha1/zz_generated.deepcopy.go
@@ -176,8 +176,14 @@ func (in *LocalService) DeepCopyInto(out *LocalService) {
 	}
 	if in.HostsMap != nil {
 		in, out := &in.HostsMap, &out.HostsMap
-		*out = make([]HostsMap, len(*in))
-		copy(*out, *in)
+		*out = make([]*HostsMap, len(*in))
+		for i := range *in {
+			if (*in)[i] != nil {
+				in, out := &(*in)[i], &(*out)[i]
+				*out = new(HostsMap)
+				**out = **in
+			}
+		}
 	}
 	return
 }
diff --git a/vendor/kubean.io/api/apis/kubeaninfomanifest/v1alpha1/types.go b/vendor/kubean.io/api/apis/kubeaninfomanifest/v1alpha1/types.go
index 49f61f733..587c91b1b 100644
--- a/vendor/kubean.io/api/apis/kubeaninfomanifest/v1alpha1/types.go
+++ b/vendor/kubean.io/api/apis/kubeaninfomanifest/v1alpha1/types.go
@@ -49,7 +49,7 @@ type LocalService struct {
 	// +optional
 	YumRepo []string `json:"yumRepo,omitempty" yaml:"yumRepo,omitempty"`
 	// +optional
-	HostsMap []HostsMap `json:"hostsMap,omitempty" yaml:"hostsMap,omitempty"`
+	HostsMap []*HostsMap `json:"hostsMap,omitempty" yaml:"hostsMap,omitempty"`
 }
 
 type HostsMap struct {
diff --git a/vendor/kubean.io/api/apis/kubeaninfomanifest/v1alpha1/zz_generated.deepcopy.go b/vendor/kubean.io/api/apis/kubeaninfomanifest/v1alpha1/zz_generated.deepcopy.go
index f45fd241f..4c198eefa 100644
--- a/vendor/kubean.io/api/apis/kubeaninfomanifest/v1alpha1/zz_generated.deepcopy.go
+++ b/vendor/kubean.io/api/apis/kubeaninfomanifest/v1alpha1/zz_generated.deepcopy.go
@@ -176,8 +176,14 @@ func (in *LocalService) DeepCopyInto(out *LocalService) {
 	}
 	if in.HostsMap != nil {
 		in, out := &in.HostsMap, &out.HostsMap
-		*out = make([]HostsMap, len(*in))
-		copy(*out, *in)
+		*out = make([]*HostsMap, len(*in))
+		for i := range *in {
+			if (*in)[i] != nil {
+				in, out := &(*in)[i], &(*out)[i]
+				*out = new(HostsMap)
+				**out = **in
+			}
+		}
 	}
 	return
 }


Test patch:
diff --git a/pkg/controllers/infomanifest/controller_test.go b/pkg/controllers/infomanifest/controller_test.go
index a409a1db1..70b7296ab 100644
--- a/pkg/controllers/infomanifest/controller_test.go
+++ b/pkg/controllers/infomanifest/controller_test.go
@@ -173,7 +173,7 @@ func Test_ParseConfigMapToLocalService(t *testing.T) {
 				RegistryRepo: "temp-registry.daocloud.io:5000",
 				FilesRepo:    "http://temp-registry.daocloud.io:9000",
 				YumRepo:      []string{"http://temp-registry.daocloud.io:9000/kubean/centos-iso/\\$releasever/os/\\$basearch", "http://temp-registry.daocloud.io:9000/centos/\\$releasever/os/\\$basearch"},
-				HostsMap: []kubeaninfomanifestv1alpha1.HostsMap{
+				HostsMap: []*kubeaninfomanifestv1alpha1.HostsMap{
 					{Domain: "temp-registry.daocloud.io", Address: "a.b.c.d"},
 				},
 			},
@@ -319,7 +319,7 @@ func Test_UpdateGlobalLocalService1(t *testing.T) {
 				RegistryRepo: "temp-registry.daocloud.io:5000",
 				FilesRepo:    "http://temp-registry.daocloud.io:9000",
 				YumRepo:      []string{"http://temp-registry.daocloud.io:9000/kubean/centos-iso/\\$releasever/os/\\$basearch", "http://temp-registry.daocloud.io:9000/centos/\\$releasever/os/\\$basearch"},
-				HostsMap: []kubeaninfomanifestv1alpha1.HostsMap{
+				HostsMap: []*kubeaninfomanifestv1alpha1.HostsMap{
 					{
 						Domain:  "temp-registry.daocloud.io",
 						Address: "a.b.c.d",
@@ -359,7 +359,7 @@ func Test_UpdateGlobalLocalService1(t *testing.T) {
 				RegistryRepo: "temp-registry.daocloud.io:5000",
 				FilesRepo:    "http://temp-registry.daocloud.io:9000",
 				YumRepo:      []string{"http://temp-registry.daocloud.io:9000/kubean/centos-iso/\\$releasever/os/\\$basearch", "http://temp-registry.daocloud.io:9000/centos/\\$releasever/os/\\$basearch"},
-				HostsMap: []kubeaninfomanifestv1alpha1.HostsMap{
+				HostsMap: []*kubeaninfomanifestv1alpha1.HostsMap{
 					{
 						Domain:  "temp-registry.daocloud.io",
 						Address: "a.b.c.d1",