Skip to content
Draft
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
1 change: 0 additions & 1 deletion app/kumactl/cmd/export/testdata/export-no-dp.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
type: File
targetRef:
kind: Mesh
status: {}
type: MeshAccessLog
---
creationTime: "0001-01-01T00:00:00Z"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
type: File
targetRef:
kind: Mesh
status: {}
type: MeshAccessLog
---
creationTime: "0001-01-01T00:00:00Z"
Expand All @@ -59,7 +58,6 @@ spec:
type: File
targetRef:
kind: Mesh
status: {}
type: MeshAccessLog
---
creationTime: "0001-01-01T00:00:00Z"
Expand Down
2 changes: 0 additions & 2 deletions app/kumactl/cmd/export/testdata/export-uni.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ spec:
type: File
targetRef:
kind: Mesh
status: {}
type: MeshAccessLog
---
creationTime: "0001-01-01T00:00:00Z"
Expand Down Expand Up @@ -93,7 +92,6 @@ spec:
type: File
targetRef:
kind: Mesh
status: {}
type: MeshAccessLog
---
creationTime: "0001-01-01T00:00:00Z"
Expand Down
1 change: 0 additions & 1 deletion docs/generated/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19233,7 +19233,6 @@ components:
required:
- type
- name
- spec
properties:
type:
description: the type of the resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@
}
}
]
},
"status": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@
"appProtocol": "tcp"
}
]
},
"status": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@
}
}
]
},
"status": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ var DoNothingResourceResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "dnr",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var HostnameGeneratorResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: true,
ShortName: "hg",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ var MeshExternalServiceResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: true,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: true,
ShortName: "extsvc",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ var MeshIdentityResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: true,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: true,
ShortName: "mid",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ var MeshMultiZoneServiceResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: true,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mzsvc",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// +kuma:policy:has_status=true
// +kuma:policy:short_name=motb
// +kuma:policy:kds_flags=model.GlobalToZonesFlag | model.ZoneToGlobalFlag
// +kuma:policy:optional_spec=true
// +kuma:policy:singular_display_name=Mesh OpenTelemetry Backend
type MeshOpenTelemetryBackend struct {
// Endpoint optionally defines the OTel collector address and port.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ components:
MeshOpenTelemetryBackendItem:
type: object
description: "MeshOpenTelemetryBackend defines a shared OTel collector endpoint for observability policies.\nAn empty spec is valid and represents the node-local default flow\n(kuma-dp resolves the address at runtime using HOST_IP or 127.0.0.1)."
required: [type, name, spec]
required: [type, name]
properties:
type:
description: 'the type of the resource'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ var MeshOpenTelemetryBackendResourceTypeDescriptor = model.ResourceTypeDescripto
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: true,
OptionalSpec: true,
AllowedOnSystemNamespaceOnly: true,
ShortName: "motb",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ var MeshServiceResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: true,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "msvc",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ var MeshTrustResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: true,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: true,
ShortName: "mtrust",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshZoneAddressResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mza",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ var WorkloadResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: true,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "wl",
IsFromAsRules: false,
Expand Down
2 changes: 2 additions & 0 deletions pkg/core/resources/model/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ type ResourceTypeDescriptor struct {
HasRulesTargetRef bool
// HasStatus indicates that the policy has a status field
HasStatus bool
// OptionalSpec indicates that the spec field can be omitted when empty
OptionalSpec bool
// IsProxy indicates if this resource is a proxy
IsProxy bool
// IsDestination indicates if this resource is a destination
Expand Down
6 changes: 5 additions & 1 deletion pkg/core/resources/model/rest/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ func (f *from) Resource(r core_model.Resource) Resource {

meta := f.Meta(r)
if r.Descriptor().IsPluginOriginated {
spec := r.GetSpec()
if r.Descriptor().OptionalSpec && core_model.IsEmpty(spec) {
spec = nil
}
res := &v1alpha1.Resource{
ResourceMeta: meta,
Spec: r.GetSpec(),
Spec: spec,
}
if r.Descriptor().HasStatus {
res.Status = r.GetStatus()
Expand Down
7 changes: 6 additions & 1 deletion pkg/core/resources/model/rest/v1alpha1/resource.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package v1alpha1

import (
"bytes"
"encoding/json"
"time"

"github.com/kumahq/kuma/v2/pkg/core/kri"
core_model "github.com/kumahq/kuma/v2/pkg/core/resources/model"
)

var emptyJSON = []byte("{}")

type Resource struct {
ResourceMeta
Spec core_model.ResourceSpec `json:"spec,omitempty"`
Expand All @@ -32,7 +35,9 @@ func (r *Resource) MarshalJSON() ([]byte, error) {
if err != nil {
return nil, err
}
statusJSON = b
if !bytes.Equal(b, emptyJSON) {
statusJSON = b
}
}

var kriStr string
Expand Down
44 changes: 44 additions & 0 deletions pkg/core/resources/model/rest/v1alpha1/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
. "github.com/onsi/gomega"

"github.com/kumahq/kuma/v2/pkg/core/resources/model/rest/v1alpha1"
meshmetric "github.com/kumahq/kuma/v2/pkg/plugins/policies/meshmetric/api/v1alpha1"
"github.com/kumahq/kuma/v2/pkg/test/kds/samples"
)

Expand Down Expand Up @@ -99,6 +100,49 @@ var _ = Describe("Rest Resource", func() {
`
Expect(string(bytes)).To(MatchJSON(expected))
})

It("should omit spec when nil", func() {
// given - spec is nil (set by converter for optional-spec resources)
res := &v1alpha1.Resource{
ResourceMeta: v1alpha1.ResourceMeta{
Type: "MeshTrafficPermission",
Mesh: "default",
Name: "one",
CreationTime: t1,
ModificationTime: t2,
},
Spec: nil,
}

// when
bytes, err := json.Marshal(res)

// then
Expect(err).ToNot(HaveOccurred())
Expect(string(bytes)).ToNot(ContainSubstring(`"spec"`))
})

It("should omit status when set to non-nil empty struct", func() {
// given
res := &v1alpha1.Resource{
ResourceMeta: v1alpha1.ResourceMeta{
Type: "MeshTrafficPermission",
Mesh: "default",
Name: "one",
CreationTime: t1,
ModificationTime: t2,
},
Spec: samples.MeshTrafficPermission,
Status: &meshmetric.MeshMetricStatus{},
}

// when
bytes, err := json.Marshal(res)

// then
Expect(err).ToNot(HaveOccurred())
Expect(string(bytes)).ToNot(ContainSubstring(`"status"`))
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ var DoNothingPolicyResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: true,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "dnp",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ var MeshAccessLogResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: true,
HasRulesTargetRef: true,
HasStatus: true,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mal",
IsFromAsRules: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshCircuitBreakerResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: true,
HasRulesTargetRef: true,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mcb",
IsFromAsRules: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshFaultInjectionResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: true,
HasRulesTargetRef: true,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mfi",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshHealthCheckResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mhc",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshHTTPRouteResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mhttpr",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshLoadBalancingStrategyResourceTypeDescriptor = model.ResourceTypeDescript
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mlbs",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ var MeshMetricResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: true,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mm",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshPassthroughResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mp",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshProxyPatchResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mpp",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshRateLimitResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: true,
HasRulesTargetRef: true,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mrl",
IsFromAsRules: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshRetryResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mr",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshTCPRouteResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: false,
HasRulesTargetRef: false,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mtcpr",
IsFromAsRules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshTimeoutResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: true,
HasRulesTargetRef: true,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mt",
IsFromAsRules: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var MeshTLSResourceTypeDescriptor = model.ResourceTypeDescriptor{
HasFromTargetRef: true,
HasRulesTargetRef: true,
HasStatus: false,
OptionalSpec: false,
AllowedOnSystemNamespaceOnly: false,
ShortName: "mtls",
IsFromAsRules: true,
Expand Down
Loading
Loading