Skip to content

Commit

Permalink
Merge branch 'main' into NET-6744-fix-mesh-gw-creation-for-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jm96441n authored Dec 19, 2023
2 parents 9349dde + 8716b2f commit e679b35
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 99 deletions.
29 changes: 0 additions & 29 deletions control-plane/gateways/annotations.go

This file was deleted.

14 changes: 7 additions & 7 deletions control-plane/gateways/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func (b *meshGatewayBuilder) Deployment() (*appsv1.Deployment, error) {
spec, err := b.deploymentSpec()
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: b.gateway.Name,
Namespace: b.gateway.Namespace,
Labels: b.Labels(),
Name: b.gateway.Name,
Namespace: b.gateway.Namespace,
Labels: b.labelsForDeployment(),
Annotations: b.annotationsForDeployment(),
},
Spec: *spec,
}, err
Expand Down Expand Up @@ -54,14 +55,13 @@ func (b *meshGatewayBuilder) deploymentSpec() (*appsv1.DeploymentSpec, error) {
}

return &appsv1.DeploymentSpec{
// TODO NET-6721
Replicas: deploymentReplicaCount(deploymentConfig.Replicas, nil),
Selector: &metav1.LabelSelector{
MatchLabels: b.Labels(),
MatchLabels: b.labelsForDeployment(),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: b.Labels(),
Labels: b.labelsForDeployment(),
Annotations: map[string]string{
// Indicate that this pod is a mesh gateway pod so that the Pod controller,
// consul-k8s CLI, etc. can key off of it
Expand Down Expand Up @@ -94,7 +94,7 @@ func (b *meshGatewayBuilder) deploymentSpec() (*appsv1.DeploymentSpec, error) {
Weight: 1,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: b.Labels(),
MatchLabels: b.labelsForDeployment(),
},
TopologyKey: "kubernetes.io/hostname",
},
Expand Down
34 changes: 10 additions & 24 deletions control-plane/gateways/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,17 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
},
want: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
Labels: defaultLabels,
Annotations: map[string]string{},
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(1),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
MatchLabels: defaultLabels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
Labels: defaultLabels,
Annotations: map[string]string{
constants.AnnotationGatewayKind: meshGatewayAnnotationKind,
constants.AnnotationMeshInject: "false",
Expand Down Expand Up @@ -286,9 +281,7 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
Weight: 1,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
MatchLabels: defaultLabels,
},
TopologyKey: "kubernetes.io/hostname",
},
Expand Down Expand Up @@ -321,22 +314,17 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
},
want: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
Labels: defaultLabels,
Annotations: map[string]string{},
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(1),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
MatchLabels: defaultLabels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
Labels: defaultLabels,
Annotations: map[string]string{
constants.AnnotationGatewayKind: meshGatewayAnnotationKind,
constants.AnnotationMeshInject: "false",
Expand Down Expand Up @@ -530,9 +518,7 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
Weight: 1,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
MatchLabels: defaultLabels,
},
TopologyKey: "kubernetes.io/hostname",
},
Expand Down
10 changes: 0 additions & 10 deletions control-plane/gateways/labels.go

This file was deleted.

142 changes: 142 additions & 0 deletions control-plane/gateways/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package gateways

import (
"golang.org/x/exp/slices"

"github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1"
)

const labelManagedBy = "mesh.consul.hashicorp.com/managed-by"

var defaultLabels = map[string]string{labelManagedBy: "consul-k8s"}

func (b *meshGatewayBuilder) annotationsForDeployment() map[string]string {
if b.gcc == nil {
return map[string]string{}
}
return computeAnnotationsOrLabels(b.gateway.Annotations, b.gcc.Spec.Deployment.Annotations, b.gcc.Spec.Annotations)
}

func (b *meshGatewayBuilder) annotationsForRole() map[string]string {
if b.gcc == nil {
return map[string]string{}
}
return computeAnnotationsOrLabels(b.gateway.Annotations, b.gcc.Spec.Role.Annotations, b.gcc.Spec.Annotations)
}

func (b *meshGatewayBuilder) annotationsForRoleBinding() map[string]string {
if b.gcc == nil {
return map[string]string{}
}
return computeAnnotationsOrLabels(b.gateway.Annotations, b.gcc.Spec.RoleBinding.Annotations, b.gcc.Spec.Annotations)
}

func (b *meshGatewayBuilder) annotationsForService() map[string]string {
if b.gcc == nil {
return map[string]string{}
}
return computeAnnotationsOrLabels(b.gateway.Annotations, b.gcc.Spec.Service.Annotations, b.gcc.Spec.Annotations)
}

func (b *meshGatewayBuilder) annotationsForServiceAccount() map[string]string {
if b.gcc == nil {
return map[string]string{}
}
return computeAnnotationsOrLabels(b.gateway.Annotations, b.gcc.Spec.ServiceAccount.Annotations, b.gcc.Spec.Annotations)
}

func (b *meshGatewayBuilder) labelsForDeployment() map[string]string {
if b.gcc == nil {
return defaultLabels
}

labels := computeAnnotationsOrLabels(b.gateway.Labels, b.gcc.Spec.Deployment.Labels, b.gcc.Spec.Labels)
for k, v := range defaultLabels {
labels[k] = v
}
return labels
}

func (b *meshGatewayBuilder) labelsForRole() map[string]string {
if b.gcc == nil {
return defaultLabels
}

labels := computeAnnotationsOrLabels(b.gateway.Labels, b.gcc.Spec.Role.Labels, b.gcc.Spec.Labels)
for k, v := range defaultLabels {
labels[k] = v
}
return labels
}

func (b *meshGatewayBuilder) labelsForRoleBinding() map[string]string {
if b.gcc == nil {
return defaultLabels
}

labels := computeAnnotationsOrLabels(b.gateway.Labels, b.gcc.Spec.RoleBinding.Labels, b.gcc.Spec.Labels)
for k, v := range defaultLabels {
labels[k] = v
}
return labels
}

func (b *meshGatewayBuilder) labelsForService() map[string]string {
if b.gcc == nil {
return defaultLabels
}

labels := computeAnnotationsOrLabels(b.gateway.Labels, b.gcc.Spec.Service.Labels, b.gcc.Spec.Labels)
for k, v := range defaultLabels {
labels[k] = v
}
return labels
}

func (b *meshGatewayBuilder) labelsForServiceAccount() map[string]string {
if b.gcc == nil {
return defaultLabels
}

labels := computeAnnotationsOrLabels(b.gateway.Labels, b.gcc.Spec.ServiceAccount.Labels, b.gcc.Spec.Labels)
for k, v := range defaultLabels {
labels[k] = v
}
return labels
}

// computeAnnotationsOrLabels compiles a set of annotations or labels
// using the following priority, highest to lowest:
// 1. inherited keys specified on the primary
// 2. added key-values specified on the primary
// 3. inherited keys specified on the secondary
// 4. added key-values specified on the secondary
func computeAnnotationsOrLabels(inheritFrom map[string]string, primary, secondary v2beta1.GatewayClassAnnotationsLabelsConfig) map[string]string {
out := map[string]string{}

// Add key-values specified on the secondary
for k, v := range secondary.Set {
out[k] = v
}

// Inherit keys specified on the secondary
for k, v := range inheritFrom {
if slices.Contains(secondary.InheritFromGateway, k) {
out[k] = v
}
}

// Add key-values specified on the primary
for k, v := range primary.Set {
out[k] = v
}

// Inherit keys specified on the primary
for k, v := range inheritFrom {
if slices.Contains(primary.InheritFromGateway, k) {
out[k] = v
}
}

return out
}
Loading

0 comments on commit e679b35

Please sign in to comment.