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

policy/api: Rework Rule.MarshalJSON() to ease maintainability #11651

Merged
merged 1 commit into from
May 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 28 additions & 52 deletions pkg/policy/api/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package api

import (
"bytes"
"context"
"encoding/json"

Expand Down Expand Up @@ -80,65 +79,42 @@ type Rule struct {
// MarshalJSON returns the JSON encoding of Rule r. We need to overwrite it to
// enforce omitempty on the EndpointSelector nested structures.
func (r *Rule) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte('{')

if r.EndpointSelector.LabelSelector != nil {
jsonValue, err := json.Marshal(r.EndpointSelector)
if err != nil {
return nil, err
}
buf.Write([]byte(`"endpointSelector":`))
buf.Write(jsonValue)
}

if r.NodeSelector.LabelSelector != nil {
jsonValue, err := json.Marshal(r.NodeSelector)
if err != nil {
return nil, err
}
buf.Write([]byte(`"nodeSelector":`))
buf.Write(jsonValue)
}

if r.Ingress != nil {
jsonValue, err := json.Marshal(r.Ingress)
if err != nil {
return nil, err
}
buf.Write([]byte(`,"ingress":`))
buf.Write(jsonValue)
type common struct {
Ingress []IngressRule `json:"ingress,omitempty"`
Egress []EgressRule `json:"egress,omitempty"`
Labels labels.LabelArray `json:"labels,omitempty"`
Description string `json:"description,omitempty"`
}

if r.Egress != nil {
jsonValue, err := json.Marshal(r.Egress)
if err != nil {
return nil, err
}
buf.Write([]byte(`,"egress":`))
buf.Write(jsonValue)
var a interface{}
ruleCommon := common{
Ingress: r.Ingress,
Egress: r.Egress,
Labels: r.Labels,
Description: r.Description,
}

if r.Labels != nil {
jsonValue, err := json.Marshal(r.Labels)
if err != nil {
return nil, err
// Only one of endpointSelector or nodeSelector is permitted.
switch {
case r.EndpointSelector.LabelSelector != nil:
a = struct {
EndpointSelector EndpointSelector `json:"endpointSelector,omitempty"`
common
}{
EndpointSelector: r.EndpointSelector,
common: ruleCommon,
}
buf.Write([]byte(`,"labels":`))
buf.Write(jsonValue)
}

if r.Description != "" {
jsonValue, err := json.Marshal(r.Description)
if err != nil {
return nil, err
case r.NodeSelector.LabelSelector != nil:
a = struct {
NodeSelector EndpointSelector `json:"nodeSelector,omitempty"`
common
}{
NodeSelector: r.NodeSelector,
common: ruleCommon,
}
buf.Write([]byte(`,"description":`))
buf.Write(jsonValue)
}

buf.WriteByte('}')
return buf.Bytes(), nil
return json.Marshal(a)
}

func (r *Rule) DeepEqual(o *Rule) bool {
Expand Down