Skip to content

Commit b62753a

Browse files
authored
Merge pull request #1039 from s1061123/json-remove-dns
Add Marshal function in Result/NetConf to omit empty value
2 parents 1557aeb + d1276f1 commit b62753a

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

Diff for: libcni/api_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ var _ = Describe("Invoking plugins", func() {
486486
Expect(err).NotTo(HaveOccurred())
487487
cachedJson := `{
488488
"cniVersion": "` + version.Current() + `",
489-
"ips": [{"address": "10.1.2.3/24"}],
490-
"dns": {}
489+
"ips": [{"address": "10.1.2.3/24"}]
491490
}`
492491
err = os.WriteFile(cacheFile, []byte(cachedJson), 0o600)
493492
Expect(err).NotTo(HaveOccurred())
@@ -676,8 +675,7 @@ var _ = Describe("Invoking plugins", func() {
676675
Expect(err).NotTo(HaveOccurred())
677676
cachedJson := `{
678677
"cniVersion": "` + version.Current() + `",
679-
"ips": [{"address": "10.1.2.3/24"}],
680-
"dns": {}
678+
"ips": [{"address": "10.1.2.3/24"}]
681679
}`
682680
err = os.WriteFile(cacheFile, []byte(cachedJson), 0o600)
683681
Expect(err).NotTo(HaveOccurred())
@@ -971,7 +969,7 @@ var _ = Describe("Invoking plugins", func() {
971969
"otherCapability": capabilityArgs["otherCapability"],
972970
}
973971

974-
ipResult = fmt.Sprintf(`{"cniVersion": "%s", "dns":{},"ips":[{"address": "10.1.2.3/24"}]}`, version.Current())
972+
ipResult = fmt.Sprintf(`{"cniVersion": "%s", "ips":[{"address": "10.1.2.3/24"}]}`, version.Current())
975973
netConfigList, plugins = makePluginList(version.Current(), ipResult, rcMap)
976974

977975
ctx = context.TODO()

Diff for: pkg/types/100/types.go

+23
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ type Result struct {
9595
DNS types.DNS `json:"dns,omitempty"`
9696
}
9797

98+
// Note: DNS should be omit if DNS is empty but default Marshal function
99+
// will output empty structure hence need to write a Marshal function
100+
func (r *Result) MarshalJSON() ([]byte, error) {
101+
// use type alias to escape recursion for json.Marshal() to MarshalJSON()
102+
type fixObjType = Result
103+
104+
bytes, err := json.Marshal(fixObjType(*r)) //nolint:all
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
fixupObj := make(map[string]interface{})
110+
if err := json.Unmarshal(bytes, &fixupObj); err != nil {
111+
return nil, err
112+
}
113+
114+
if r.DNS.IsEmpty() {
115+
delete(fixupObj, "dns")
116+
}
117+
118+
return json.Marshal(fixupObj)
119+
}
120+
98121
// convertFrom100 does nothing except set the version; the types are the same
99122
func convertFrom100(from types.Result, toVersion string) (types.Result, error) {
100123
fromResult := from.(*Result)

Diff for: pkg/types/types.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,44 @@ type NetConf struct {
6464
Type string `json:"type,omitempty"`
6565
Capabilities map[string]bool `json:"capabilities,omitempty"`
6666
IPAM IPAM `json:"ipam,omitempty"`
67-
DNS DNS `json:"dns"`
67+
DNS DNS `json:"dns,omitempty"`
6868

6969
RawPrevResult map[string]interface{} `json:"prevResult,omitempty"`
7070
PrevResult Result `json:"-"`
7171
}
7272

73+
// Note: DNS should be omit if DNS is empty but default Marshal function
74+
// will output empty structure hence need to write a Marshal function
75+
func (n *NetConf) MarshalJSON() ([]byte, error) {
76+
// use type alias to escape recursion for json.Marshal() to MarshalJSON()
77+
type fixObjType = NetConf
78+
79+
bytes, err := json.Marshal(fixObjType(*n)) //nolint:all
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
fixupObj := make(map[string]interface{})
85+
if err := json.Unmarshal(bytes, &fixupObj); err != nil {
86+
return nil, err
87+
}
88+
89+
if n.DNS.IsEmpty() {
90+
delete(fixupObj, "dns")
91+
}
92+
93+
return json.Marshal(fixupObj)
94+
}
95+
7396
type IPAM struct {
7497
Type string `json:"type,omitempty"`
7598
}
7699

100+
// IsEmpty returns true if IPAM structure has no value, otherwise return false
101+
func (i *IPAM) IsEmpty() bool {
102+
return i.Type == ""
103+
}
104+
77105
// NetConfList describes an ordered list of networks.
78106
type NetConfList struct {
79107
CNIVersion string `json:"cniVersion,omitempty"`
@@ -116,6 +144,14 @@ type DNS struct {
116144
Options []string `json:"options,omitempty"`
117145
}
118146

147+
// IsEmpty returns true if DNS structure has no value, otherwise return false
148+
func (d *DNS) IsEmpty() bool {
149+
if len(d.Nameservers) == 0 && d.Domain == "" && len(d.Search) == 0 && len(d.Options) == 0 {
150+
return true
151+
}
152+
return false
153+
}
154+
119155
func (d *DNS) Copy() *DNS {
120156
if d == nil {
121157
return nil

0 commit comments

Comments
 (0)