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

fix(kuma-cp): change way of setting if resource is read only #5345

Merged
merged 6 commits into from
Nov 21, 2022
Merged
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
2 changes: 1 addition & 1 deletion pkg/api-server/resource_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
" You can still use 'kumactl' or the HTTP API to make read-only operations. On Universal this limitation does not apply.\n"
globalReadOnlyMessage = "On global control plane you can not modify dataplane resources with 'kumactl apply' or via the HTTP API." +
" You can still use 'kumactl' or the HTTP API to modify them on the zone control plane.\n"
zoneReadOnlyMessage = "On zone control plane you can only modify dataplane resources with 'kumactl apply' or via the HTTP API." +
zoneReadOnlyMessage = "On zone control plane you can only modify zone resources with 'kumactl apply' or via the HTTP API." +
" You can still use 'kumactl' or the HTTP API to modify the rest of the resource on the global control plane.\n"
)

Expand Down
18 changes: 17 additions & 1 deletion pkg/api-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func addResourcesEndpoints(ws *restful.WebService, defs []model.ResourceTypeDesc

for _, definition := range defs {
defType := definition.Name
if cfg.ApiServer.ReadOnly || (defType == mesh.DataplaneType && cfg.Mode == config_core.Global) || (defType != mesh.DataplaneType && cfg.Mode == config_core.Zone) {
if ShouldBeReadOnly(definition.KDSFlags, cfg) {
definition.ReadOnly = true
}
endpoints := resourceEndpoints{
Expand Down Expand Up @@ -264,6 +264,22 @@ func addResourcesEndpoints(ws *restful.WebService, defs []model.ResourceTypeDesc
}
}

func ShouldBeReadOnly(kdsFlag model.KDSFlagType, cfg *kuma_cp.Config) bool {
if cfg.ApiServer.ReadOnly {
return true
}
if kdsFlag == model.KDSDisabled {
return false
}
if cfg.Mode == config_core.Global && !kdsFlag.Has(model.ProvidedByGlobal) {
return true
}
if cfg.Mode == config_core.Zone && !kdsFlag.Has(model.ProvidedByZone) {
return true
}
return false
}

func (a *ApiServer) Start(stop <-chan struct{}) error {
errChan := make(chan error)

Expand Down
144 changes: 144 additions & 0 deletions pkg/api-server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package api_server_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

api_server "github.com/kumahq/kuma/pkg/api-server"
config_api_server "github.com/kumahq/kuma/pkg/config/api-server"
kuma_cp "github.com/kumahq/kuma/pkg/config/app/kuma-cp"
"github.com/kumahq/kuma/pkg/config/core"
"github.com/kumahq/kuma/pkg/core/resources/model"
)

var _ = Describe("Resource", func() {
type testCase struct {
kdsFlag model.KDSFlagType
mode core.CpMode
isApiReadOnly bool
expected bool
}
DescribeTable("on Global", func(given testCase) {
// given
cfg := kuma_cp.Config{
Mode: given.mode,
ApiServer: &config_api_server.ApiServerConfig{
ReadOnly: given.isApiReadOnly,
},
}

// then
Expect(api_server.ShouldBeReadOnly(given.kdsFlag, &cfg)).To(Equal(given.expected))
},
Entry("shouldn't be read only when kds from global to zone and api is not read only", testCase{
kdsFlag: model.FromGlobalToZone,
mode: core.Global,
isApiReadOnly: false,
expected: false,
}),
Entry("should be read only when kds from global to zone and api is read only", testCase{
kdsFlag: model.FromGlobalToZone,
mode: core.Global,
isApiReadOnly: true,
expected: true,
}),
Entry("should be read only when kds from zone to global and api is not read only", testCase{
kdsFlag: model.FromZoneToGlobal,
mode: core.Global,
isApiReadOnly: false,
expected: true,
}),
Entry("should be read only when kds from zone to global and api is read only", testCase{
kdsFlag: model.FromZoneToGlobal,
mode: core.Global,
isApiReadOnly: true,
expected: true,
}),
Entry("should be read only when there is no kds and api is read only", testCase{
kdsFlag: 0,
mode: core.Global,
isApiReadOnly: true,
expected: true,
}),
Entry("shouldn't be read only when there is no kds and api is not read only", testCase{
kdsFlag: 0,
mode: core.Global,
isApiReadOnly: false,
expected: false,
}),
Entry("shouldn't be read only when there are both kds types and api is not read only", testCase{
kdsFlag: model.FromZoneToGlobal | model.FromGlobalToZone,
mode: core.Global,
isApiReadOnly: false,
expected: false,
}),
Entry("should be read only when there are both kds types and api is read only", testCase{
kdsFlag: model.FromZoneToGlobal | model.FromGlobalToZone,
mode: core.Global,
isApiReadOnly: true,
expected: true,
}),
)

DescribeTable("on Zone", func(given testCase) {
// given
cfg := kuma_cp.Config{
Mode: given.mode,
ApiServer: &config_api_server.ApiServerConfig{
ReadOnly: given.isApiReadOnly,
},
}

// then
Expect(api_server.ShouldBeReadOnly(given.kdsFlag, &cfg)).To(Equal(given.expected))
},
Entry("should be read only when kds from global to zone and api is read only", testCase{
kdsFlag: model.FromGlobalToZone,
mode: core.Zone,
isApiReadOnly: true,
expected: true,
}),
Entry("should be read only when kds from global to zone and api is not read only", testCase{
kdsFlag: model.FromGlobalToZone,
mode: core.Zone,
isApiReadOnly: false,
expected: true,
}),
Entry("should be read only when kds from zone to global and api is read only", testCase{
kdsFlag: model.FromZoneToGlobal,
mode: core.Zone,
isApiReadOnly: true,
expected: true,
}),
Entry("should be read only when kds from zone to global and api is not read only", testCase{
kdsFlag: model.FromZoneToGlobal,
mode: core.Zone,
isApiReadOnly: false,
expected: false,
}),
Entry("shouldn't be read only when there is no kds and api is not read only", testCase{
kdsFlag: 0,
mode: core.Zone,
isApiReadOnly: false,
expected: false,
}),
Entry("should be read only when there is no kds and api is read only", testCase{
kdsFlag: 0,
mode: core.Zone,
isApiReadOnly: true,
expected: true,
}),
Entry("shouldn't be read only when there are both kds types and api is not read only", testCase{
kdsFlag: model.FromZoneToGlobal | model.FromGlobalToZone,
mode: core.Zone,
isApiReadOnly: false,
expected: false,
}),
Entry("should be read only when there are both kds types and api is read only", testCase{
kdsFlag: model.FromZoneToGlobal | model.FromGlobalToZone,
mode: core.Zone,
isApiReadOnly: true,
expected: true,
}),
)
})
1 change: 1 addition & 0 deletions pkg/core/resources/model/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
type KDSFlagType uint32

const (
KDSDisabled = KDSFlagType(0)
ConsumedByZone = KDSFlagType(1)
ConsumedByGlobal = KDSFlagType(1 << 2)
ProvidedByZone = KDSFlagType(1 << 3)
Expand Down