Skip to content

Commit

Permalink
NET-6663 Set gateway-kind in Workload metadata when it represents a x…
Browse files Browse the repository at this point in the history
…Gateway Pod (#3365)

Set gateway-kind in Workload metadata when it represents a xGateway Pod

By setting the gateway-kind annotation on the Pods for a MeshGateway, we indicate to the Pod controller in consul-k8s that the Pod represents a mesh gateway (or api/terminating in the future). The Pod controller then passes this along as metadata on the Workload that it creates in Consul.

The end result is that the sidecar and gateway proxy controllers can determine which Workloads they should generate ProxyStateTemplates for.
  • Loading branch information
nathancoleman authored Dec 14, 2023
1 parent 56060e3 commit 6c71611
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions control-plane/connect-inject/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const (
// ProxyDefaultHealthPort is the default HTTP health check port for the proxy.
ProxyDefaultHealthPort = 21000

// MetaGatewayKind is the meta key name for indicating which kind of gateway a Pod is for, if any.
// The value should be one of "mesh", "api", or "terminating".
MetaGatewayKind = "gateway-kind"

// MetaKeyManagedBy is the meta key name for indicating which Kubernetes controller manages a Consul resource.
MetaKeyManagedBy = "managed-by"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,16 @@ func parseLocality(node corev1.Node) *pbcatalog.Locality {

func metaFromPod(pod corev1.Pod) map[string]string {
// TODO: allow custom workload metadata
return map[string]string{
meta := map[string]string{
constants.MetaKeyKubeNS: pod.GetNamespace(),
constants.MetaKeyManagedBy: constants.ManagedByPodValue,
}

if gatewayKind := pod.Annotations[constants.AnnotationGatewayKind]; gatewayKind != "" {
meta[constants.MetaGatewayKind] = gatewayKind
}

return meta
}

// getHealthStatusFromPod checks the Pod for a "Ready" condition that is true.
Expand Down
7 changes: 5 additions & 2 deletions control-plane/gateways/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"k8s.io/utils/pointer"

meshv2beta1 "github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1"
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
)

const (
globalDefaultInstances int32 = 1
globalDefaultInstances int32 = 1
meshGatewayAnnotationKind = "mesh-gateway"
)

func (b *meshGatewayBuilder) Deployment() (*appsv1.Deployment, error) {
Expand Down Expand Up @@ -65,7 +67,8 @@ func (b *meshGatewayBuilder) deploymentSpec() (*appsv1.DeploymentSpec, error) {
ObjectMeta: metav1.ObjectMeta{
Labels: b.Labels(),
Annotations: map[string]string{
"consul.hashicorp.com/mesh-inject": "false",
constants.AnnotationMeshInject: "false",
constants.AnnotationGatewayKind: meshGatewayAnnotationKind,
},
},
Spec: corev1.PodSpec{
Expand Down
7 changes: 5 additions & 2 deletions control-plane/gateways/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"

meshv2beta1 "github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1"
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
)

func Test_meshGatewayBuilder_Deployment(t *testing.T) {
Expand Down Expand Up @@ -72,7 +73,8 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
Annotations: map[string]string{
"consul.hashicorp.com/mesh-inject": "false",
constants.AnnotationMeshInject: "false",
constants.AnnotationGatewayKind: meshGatewayAnnotationKind,
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -316,7 +318,8 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
Annotations: map[string]string{
"consul.hashicorp.com/mesh-inject": "false",
constants.AnnotationMeshInject: "false",
constants.AnnotationGatewayKind: meshGatewayAnnotationKind,
},
},
Spec: corev1.PodSpec{
Expand Down

0 comments on commit 6c71611

Please sign in to comment.