Skip to content

Commit

Permalink
feat(meshservice): sync mesh service to other zones (#10380)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Dyszkiewicz <jakub.dyszkiewicz@gmail.com>
  • Loading branch information
jakubdyszkiewicz committed Jun 7, 2024
1 parent f086ded commit 6e73eeb
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 16 deletions.
6 changes: 3 additions & 3 deletions docs/generated/raw/kuma-cp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,9 @@ ipam:
# MeshService address management
meshService:
# CIDR for MeshService IPs
cidr: 241.0.0.0/8 # ENV: IPAM_MESH_SERVICE_CIDR
cidr: 241.0.0.0/8 # ENV: KUMA_IPAM_MESH_SERVICE_CIDR
meshExternalService:
# CIDR for MeshExternalService IPs
cidr: 242.0.0.0/8 # ENV: IPAM_MESH_EXTERNAL_SERVICE_CIDR
cidr: 242.0.0.0/8 # ENV: KUMA_IPAM_MESH_EXTERNAL_SERVICE_CIDR
# Interval on which Kuma will allocate new IPs for MeshServices and MeshExternalServices
allocationInterval: 5s # ENV: IPAM_ALLOCATION_INTERVAL
allocationInterval: 5s # ENV: KUMA_IPAM_ALLOCATION_INTERVAL
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
"singularDisplayName": "Mesh Retry"
},
{
"includeInFederation": true,
"includeInFederation": false,
"name": "MeshService",
"path": "meshservices",
"pluralDisplayName": "Mesh Services",
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/app/kuma-cp/kuma-cp.defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,9 @@ ipam:
# MeshService address management
meshService:
# CIDR for MeshService IPs
cidr: 241.0.0.0/8 # ENV: IPAM_MESH_SERVICE_CIDR
cidr: 241.0.0.0/8 # ENV: KUMA_IPAM_MESH_SERVICE_CIDR
meshExternalService:
# CIDR for MeshExternalService IPs
cidr: 242.0.0.0/8 # ENV: IPAM_MESH_EXTERNAL_SERVICE_CIDR
cidr: 242.0.0.0/8 # ENV: KUMA_IPAM_MESH_EXTERNAL_SERVICE_CIDR
# Interval on which Kuma will allocate new IPs for MeshServices and MeshExternalServices
allocationInterval: 5s # ENV: IPAM_ALLOCATION_INTERVAL
allocationInterval: 5s # ENV: KUMA_IPAM_ALLOCATION_INTERVAL
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Port struct {
// MeshService
// +kuma:policy:is_policy=false
// +kuma:policy:has_status=true
// +kuma:policy:kds_flags=model.ZoneToGlobalFlag | model.GlobalToAllButOriginalZoneFlag
type MeshService struct {
Selector Selector `json:"selector,omitempty"`
// +patchMergeKey=port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ var MeshServiceResourceTypeDescriptor = model.ResourceTypeDescriptor{
Resource: NewMeshServiceResource(),
ResourceList: &MeshServiceResourceList{},
Scope: model.ScopeMesh,
KDSFlags: model.GlobalToAllZonesFlag | model.ZoneToGlobalFlag,
KDSFlags: model.ZoneToGlobalFlag | model.GlobalToAllButOriginalZoneFlag,
WsPath: "meshservices",
KumactlArg: "meshservice",
KumactlListArg: "meshservices",
Expand Down
14 changes: 12 additions & 2 deletions pkg/kds/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/kumahq/kuma/pkg/core"
config_manager "github.com/kumahq/kuma/pkg/core/config/manager"
core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
meshservice_api "github.com/kumahq/kuma/pkg/core/resources/apis/meshservice/api/v1alpha1"
"github.com/kumahq/kuma/pkg/core/resources/apis/system"
"github.com/kumahq/kuma/pkg/core/resources/manager"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
Expand Down Expand Up @@ -77,6 +78,9 @@ func DefaultContext(
reconcile.If(
reconcile.IsKubernetes(cfg.Store.Type),
RemoveK8sSystemNamespaceSuffixMapper(cfg.Store.Kubernetes.SystemNamespace)),
reconcile.If(
reconcile.TypeIs(meshservice_api.MeshServiceType),
RemoveStatus()),
reconcile.If(
reconcile.And(
reconcile.ScopeIs(core_model.ScopeMesh),
Expand Down Expand Up @@ -192,7 +196,13 @@ func RemoveK8sSystemNamespaceSuffixMapper(k8sSystemNamespace string) reconcile.R
return func(_ kds.Features, r core_model.Resource) (core_model.Resource, error) {
dotSuffix := fmt.Sprintf(".%s", k8sSystemNamespace)
newName := strings.TrimSuffix(r.GetMeta().GetName(), dotSuffix)
return util.CloneResource(r, util.WithName(newName)), nil
return util.CloneResource(r, util.WithResourceName(newName)), nil
}
}

func RemoveStatus() reconcile.ResourceMapper {
return func(_ kds.Features, r core_model.Resource) (core_model.Resource, error) {
return util.CloneResource(r, util.WithoutStatus()), nil
}
}

Expand All @@ -209,7 +219,7 @@ func HashSuffixMapper(checkKDSFeature bool, labelsToUse ...string) reconcile.Res
values = append(values, r.GetMeta().GetLabels()[lbl])
}

return util.CloneResource(r, util.WithName(hash.HashedName(r.GetMeta().GetMesh(), name, values...))), nil
return util.CloneResource(r, util.WithResourceName(hash.HashedName(r.GetMeta().GetMesh(), name, values...))), nil
}
}

Expand Down
33 changes: 29 additions & 4 deletions pkg/kds/util/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ func ZoneTag(r model.Resource) string {
case *mesh_proto.ZoneEgress:
return res.GetZone()
default:
return ""
// todo(jakubdyszkiewicz): consider replacing this whole function with just model.ZoneOfResource(r)
return model.ZoneOfResource(r)
}
}

Expand Down Expand Up @@ -168,12 +169,36 @@ func StatsOf(status *system_proto.KDSSubscriptionStatus, resourceType model.Reso
return stat
}

func CloneResource(res core_model.Resource, fs ...CloneResourceMetaOpt) core_model.Resource {
type cloneResource struct {
name string
withoutStatus bool
}

func WithResourceName(name string) CloneResourceOpt {
return func(m *cloneResource) {
m.name = name
}
}

func WithoutStatus() CloneResourceOpt {
return func(m *cloneResource) {
m.withoutStatus = true
}
}

type CloneResourceOpt func(*cloneResource)

func CloneResource(res core_model.Resource, fs ...CloneResourceOpt) core_model.Resource {
opts := &cloneResource{}
for _, f := range fs {
f(opts)
}

newObj := res.Descriptor().NewObject()
newMeta := CloneResourceMeta(res.GetMeta(), fs...)
newMeta := CloneResourceMeta(res.GetMeta(), WithName(opts.name))
newObj.SetMeta(newMeta)
_ = newObj.SetSpec(res.GetSpec())
if newObj.Descriptor().HasStatus {
if newObj.Descriptor().HasStatus && !opts.withoutStatus {
_ = newObj.SetStatus(res.GetStatus())
}
return newObj
Expand Down
10 changes: 9 additions & 1 deletion test/e2e_env/multizone/meshservice/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ spec:
g.Expect(out).To(ContainSubstring("ip: 241.0.0."))
}, "30s", "1s").Should(Succeed())

// and MeshService is synced to global with status
// and MeshService is synced to global with the original status
Eventually(func(g Gomega) {
out, err := multizone.Global.GetKumactlOptions().RunKumactlAndGetOutput("get", "meshservices", "-m", meshName, "-oyaml")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(out).To(ContainSubstring("kuma.io/display-name: backend"))
g.Expect(out).To(ContainSubstring("ip: 241.0.0."))
}, "30s", "1s").Should(Succeed())

// and MeshService is synced to other zone but status is generated by other zone
Eventually(func(g Gomega) {
out, err := multizone.UniZone2.GetKumactlOptions().RunKumactlAndGetOutput("get", "meshservices", "-m", meshName, "-oyaml")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(out).To(ContainSubstring("kuma.io/display-name: backend"))
g.Expect(out).To(ContainSubstring("ip: 251.0.0."))
}, "30s", "1s").Should(Succeed())
})
}
1 change: 1 addition & 0 deletions test/framework/envs/multizone/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func SetupAndGetState() []byte {
WithIngressEnvoyAdminTunnel(),
WithEnv("KUMA_XDS_DATAPLANE_DEREGISTRATION_DELAY", "0s"), // we have only 1 Kuma CP instance so there is no risk setting this to 0
WithEnv("KUMA_MULTIZONE_ZONE_KDS_NACK_BACKOFF", "1s"),
WithEnv("KUMA_IPAM_MESH_SERVICE_CIDR", "251.0.0.0/8"), // just to see that the status is not synced around
},
framework.KumaDeploymentOptionsFromConfig(framework.Config.KumaCpConfig.Multizone.UniZone2)...,
)
Expand Down
2 changes: 1 addition & 1 deletion tools/policy-gen/generator/cmd/core_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ var {{.Name}}ResourceTypeDescriptor = model.ResourceTypeDescriptor{
Resource: New{{.Name}}Resource(),
ResourceList: &{{.Name}}ResourceList{},
Scope: model.ScopeMesh,
KDSFlags: model.GlobalToAllZonesFlag | model.ZoneToGlobalFlag,
KDSFlags: {{.KDSFlags}},
WsPath: "{{.Path}}",
KumactlArg: "{{index .AlternativeNames 0}}",
KumactlListArg: "{{.Path}}",
Expand Down
6 changes: 6 additions & 0 deletions tools/policy-gen/generator/pkg/parse/policyconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type PolicyConfig struct {
GoModule string
ResourceDir string
IsPolicy bool
KDSFlags string
}

func Policy(path string) (PolicyConfig, error) {
Expand Down Expand Up @@ -135,6 +136,11 @@ func newPolicyConfig(pkg, name string, markers map[string]string, fields map[str
if v, ok := parseBool(markers, "kuma:policy:has_status"); ok {
res.HasStatus = v
}
if v, ok := markers["kuma:policy:kds_flags"]; ok {
res.KDSFlags = v
} else {
res.KDSFlags = "model.GlobalToAllZonesFlag | model.ZoneToGlobalFlag"
}

if v, ok := markers["kuma:policy:singular_display_name"]; ok {
res.SingularDisplayName = v
Expand Down

0 comments on commit 6e73eeb

Please sign in to comment.