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

feat(kuma-cp): add pod labels on dataplane and use proxy type labels #12453

Merged
merged 12 commits into from
Jan 15, 2025
7 changes: 7 additions & 0 deletions api/mesh/v1alpha1/dataplane_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,13 @@ func (d *Dataplane) IsBuiltinGateway() bool {
d.GetNetworking().GetGateway().GetType() == Dataplane_Networking_Gateway_BUILTIN
}

func (d *Dataplane) GetProxyType() ProxyTypeLabelValues {
if d.IsBuiltinGateway() {
return GatewayLabel
}
return SidecarLabel
}

func (t MultiValueTagSet) String() string {
var tags []string
for tag := range t {
Expand Down
4 changes: 4 additions & 0 deletions api/mesh/v1alpha1/zoneegress_helpers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package v1alpha1

const ZoneEgressServiceName = "zone-egress"

func (r *ZoneEgress) GetProxyType() ProxyTypeLabelValues {
return ZoneEgressLabel
}
5 changes: 5 additions & 0 deletions api/mesh/v1alpha1/zoneingress_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package v1alpha1

func (r *ZoneIngress) GetProxyType() ProxyTypeLabelValues {
return ZoneIngressLabel
}
1 change: 1 addition & 0 deletions pkg/api-server/resource_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ var _ = Describe("Resource Endpoints on Zone, label origin", func() {
mesh_proto.ZoneTag: "default",
mesh_proto.MeshTag: mesh,
mesh_proto.EnvTag: "universal",
mesh_proto.ProxyTypeLabel: string(mesh_proto.SidecarLabel),
}))
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"kuma.io/env": "universal",
"kuma.io/mesh": "default",
"kuma.io/origin": "zone",
"kuma.io/proxy-type": "sidecar",
"kuma.io/zone": "default"
},
"networking": {
Expand Down
8 changes: 0 additions & 8 deletions pkg/core/resources/apis/mesh/dataplane_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,3 @@ func (d *DataplaneResource) AsOutbounds(resolver core_model.LabelResourceIdentif
}
return outbounds
}

func (d *DataplaneResource) GetProxyType() mesh_proto.ProxyTypeLabelValues {
spec := d.GetSpec().(*mesh_proto.Dataplane)
if spec.IsBuiltinGateway() {
return mesh_proto.GatewayLabel
}
return mesh_proto.SidecarLabel
}
5 changes: 0 additions & 5 deletions pkg/core/resources/apis/mesh/zone_egress_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"strconv"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/core/resources/model"
)

Expand Down Expand Up @@ -56,7 +55,3 @@ func (r *ZoneEgressResource) Hash() []byte {
func (r *ZoneEgressResource) IsRemoteEgress(localZone string) bool {
return r.Spec.GetZone() != "" && r.Spec.GetZone() != localZone
}

func (r *ZoneEgressResource) GetProxyType() mesh_proto.ProxyTypeLabelValues {
return mesh_proto.ZoneEgressLabel
}
5 changes: 0 additions & 5 deletions pkg/core/resources/apis/mesh/zone_ingress_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"strconv"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/core/resources/model"
)

Expand Down Expand Up @@ -52,7 +51,3 @@ func (r *ZoneIngressResource) Hash() []byte {
_, _ = hasher.Write([]byte(r.Spec.GetNetworking().GetAdvertisedAddress()))
return hasher.Sum(nil)
}

func (r *ZoneIngressResource) GetProxyType() mesh_proto.ProxyTypeLabelValues {
return mesh_proto.ZoneIngressLabel
}
7 changes: 7 additions & 0 deletions pkg/core/resources/model/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,13 @@ func ComputeLabels(
labels[mesh_proto.PolicyRoleLabel] = string(role)
}

if rd.IsProxy {
proxy, ok := spec.(ProxyResource)
if ok {
labels[mesh_proto.ProxyTypeLabel] = strings.ToLower(string(proxy.GetProxyType()))
Automaat marked this conversation as resolved.
Show resolved Hide resolved
}
}

return labels, nil
}

Expand Down
47 changes: 47 additions & 0 deletions pkg/core/resources/model/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,5 +393,52 @@ var _ = Describe("ComputeLabels", func() {
"kuma.io/env": "kubernetes",
},
}),
Entry("gateway dataplane proxy", testCase{
mode: core.Zone,
isK8s: true,
localZone: "zone-1",
r: builders.Dataplane().
WithMesh("mesh-1").
WithBuiltInGateway("test-gateway").
Build(),
expectedLabels: map[string]string{
"kuma.io/mesh": "mesh-1",
"kuma.io/origin": "zone",
"kuma.io/zone": "zone-1",
"kuma.io/env": "kubernetes",
"kuma.io/proxy-type": "gateway",
},
}),
Entry("dataplane proxy", testCase{
mode: core.Zone,
isK8s: true,
localZone: "zone-1",
r: builders.Dataplane().
WithName("backend-1").
WithServices("backend").
WithMesh("mesh-1").
Build(),
expectedLabels: map[string]string{
"kuma.io/mesh": "mesh-1",
"kuma.io/origin": "zone",
"kuma.io/zone": "zone-1",
"kuma.io/env": "kubernetes",
"kuma.io/proxy-type": "sidecar",
},
}),
Entry("zone egress proxy", testCase{
mode: core.Zone,
isK8s: true,
localZone: "zone-1",
r: builders.ZoneEgress().
WithPort(1001).
Build(),
expectedLabels: map[string]string{
"kuma.io/origin": "zone",
"kuma.io/zone": "zone-1",
"kuma.io/env": "kubernetes",
"kuma.io/proxy-type": "zoneegress",
},
}),
)
})
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
Rules:
127.0.0.1:80:
- BackendRefOriginIndex: {}
Conf:
action: Allow
Origin:
- creationTime: "0001-01-01T00:00:00Z"
mesh: default
modificationTime: "0001-01-01T00:00:00Z"
name: mtp-allow-kuma-other-ns-and-tag
type: MeshTrafficPermission
Subset:
- Key: abcd
Not: false
Value: abcd
- Key: k8s.kuma.io/namespace
Not: false
Value: kuma-other
- BackendRefOriginIndex: {}
Conf:
action: Allow
Origin:
- creationTime: "0001-01-01T00:00:00Z"
mesh: default
modificationTime: "0001-01-01T00:00:00Z"
name: mtp-allow-kuma-one
type: MeshTrafficPermission
Subset:
- Key: k8s.kuma.io/namespace
Not: false
Value: kuma-one
- BackendRefOriginIndex: {}
Conf:
action: Allow
Origin:
- creationTime: "0001-01-01T00:00:00Z"
mesh: default
modificationTime: "0001-01-01T00:00:00Z"
name: mtp-allow-kuma-other-ns-and-tag
type: MeshTrafficPermission
Subset:
- Key: abcd
Not: false
Value: abcd
- Key: k8s.kuma.io/namespace
Not: false
Value: kuma-other
- BackendRefOriginIndex: {}
Conf:
action: Allow
Origin:
- creationTime: "0001-01-01T00:00:00Z"
mesh: default
modificationTime: "0001-01-01T00:00:00Z"
name: mtp-allow-kuma-one
type: MeshTrafficPermission
Subset:
- Key: k8s.kuma.io/namespace
Not: false
Value: kuma-one
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
Rules:
127.0.0.1:80:
- BackendRefOriginIndex: {}
Conf:
action: Deny
Origin:
- creationTime: "0001-01-01T00:00:00Z"
mesh: default
modificationTime: "0001-01-01T00:00:00Z"
name: mtp-allow-kuma-one
type: MeshTrafficPermission
Subset:
- Key: app
Not: false
Value: demo
- Key: k8s.kuma.io/namespace
Not: false
Value: kuma-one
- BackendRefOriginIndex: {}
Conf:
action: Allow
Origin:
- creationTime: "0001-01-01T00:00:00Z"
mesh: default
modificationTime: "0001-01-01T00:00:00Z"
name: mtp-allow-kuma-one
type: MeshTrafficPermission
Subset:
- Key: app
Not: true
Value: demo
- Key: k8s.kuma.io/namespace
Not: false
Value: kuma-one
- BackendRefOriginIndex: {}
Conf:
action: Deny
Origin:
- creationTime: "0001-01-01T00:00:00Z"
mesh: default
modificationTime: "0001-01-01T00:00:00Z"
name: mtp-allow-kuma-one
type: MeshTrafficPermission
Subset:
- Key: app
Not: false
Value: demo
- Key: k8s.kuma.io/namespace
Not: false
Value: kuma-one
- BackendRefOriginIndex: {}
Conf:
action: Allow
Origin:
- creationTime: "0001-01-01T00:00:00Z"
mesh: default
modificationTime: "0001-01-01T00:00:00Z"
name: mtp-allow-kuma-one
type: MeshTrafficPermission
Subset:
- Key: app
Not: true
Value: demo
- Key: k8s.kuma.io/namespace
Not: false
Value: kuma-one
33 changes: 29 additions & 4 deletions pkg/plugins/runtime/k8s/controllers/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,14 @@ var _ = Describe("PodReconciler", func() {
mesh: poc
metadata:
creationTimestamp: null
labels:
app: sample
k8s.kuma.io/namespace: demo
kuma.io/env: kubernetes
kuma.io/mesh: poc
kuma.io/origin: zone
kuma.io/proxy-type: sidecar
kuma.io/zone: zone-1
name: pod-with-kuma-sidecar-and-ip
namespace: demo
ownerReferences:
Expand Down Expand Up @@ -731,6 +739,14 @@ var _ = Describe("PodReconciler", func() {
mesh: poc
metadata:
creationTimestamp: null
labels:
app: sample
k8s.kuma.io/namespace: demo
kuma.io/env: kubernetes
kuma.io/mesh: poc
kuma.io/origin: zone
kuma.io/proxy-type: sidecar
kuma.io/zone: zone-1
name: pod-with-kuma-sidecar-and-ip
namespace: demo
ownerReferences:
Expand Down Expand Up @@ -874,23 +890,24 @@ var _ = Describe("PodReconciler", func() {
mesh: poc
metadata:
creationTimestamp: null
name: pod-with-custom-admin-port
namespace: demo
labels:
app: sample
k8s.kuma.io/namespace: demo
kuma.io/display-name: pod-with-custom-admin-port
kuma.io/env: kubernetes
kuma.io/mesh: poc
kuma.io/origin: zone
kuma.io/proxy-type: sidecar
kuma.io/zone: zone-1
name: pod-with-custom-admin-port
namespace: demo
ownerReferences:
- apiVersion: v1
blockOwnerDeletion: true
controller: true
kind: Pod
name: pod-with-custom-admin-port
uid: pod-with-custom-admin-port-demo
resourceVersion: "1"
resourceVersion: "2"
spec:
networking:
address: 192.168.0.1
Expand Down Expand Up @@ -997,6 +1014,14 @@ var _ = Describe("PodReconciler", func() {
mesh: poc
metadata:
creationTimestamp: null
labels:
app: sample
k8s.kuma.io/namespace: demo
kuma.io/env: kubernetes
kuma.io/mesh: poc
kuma.io/origin: zone
kuma.io/proxy-type: sidecar
kuma.io/zone: zone-1
name: pod-with-custom-admin-port
namespace: demo
ownerReferences:
Expand Down
7 changes: 4 additions & 3 deletions pkg/plugins/runtime/k8s/controllers/pod_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p *PodConverter) PodToDataplane(
labels, err := model.ComputeLabels(
core_mesh.DataplaneResourceTypeDescriptor,
currentSpec,
map[string]string{},
pod.Labels,
model.NewNamespace(pod.Namespace, pod.Namespace == p.SystemNamespace),
dataplane.Mesh,
p.Mode,
Expand All @@ -78,6 +78,7 @@ func (p *PodConverter) PodToDataplane(
return nil
}
dataplane.SetSpec(dataplaneProto)
dataplane.SetLabels(labels)
return nil
}

Expand All @@ -102,7 +103,7 @@ func (p *PodConverter) PodToIngress(ctx context.Context, zoneIngress *mesh_k8s.Z
labels, err := model.ComputeLabels(
core_mesh.ZoneIngressResourceTypeDescriptor,
currentSpec,
map[string]string{},
pod.Labels,
model.NewNamespace(pod.Namespace, pod.Namespace == p.SystemNamespace),
model.NoMesh,
p.Mode,
Expand Down Expand Up @@ -141,7 +142,7 @@ func (p *PodConverter) PodToEgress(ctx context.Context, zoneEgress *mesh_k8s.Zon
labels, err := model.ComputeLabels(
core_mesh.ZoneEgressResourceTypeDescriptor,
currentSpec,
map[string]string{},
pod.Labels,
model.NewNamespace(pod.Namespace, pod.Namespace == p.SystemNamespace),
model.NoMesh,
p.Mode,
Expand Down
Loading
Loading