Skip to content

Commit

Permalink
use set labels for setting labels on deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
jm96441n committed Dec 18, 2023
1 parent 2e5c86b commit bb90713
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
26 changes: 26 additions & 0 deletions control-plane/gateways/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
config: GatewayConfig{},
gcc: &meshv2beta1.GatewayClassConfig{
Spec: meshv2beta1.GatewayClassConfigSpec{
GatewayClassAnnotationsAndLabels: meshv2beta1.GatewayClassAnnotationsAndLabels{
Labels: meshv2beta1.GatewayClassAnnotationsLabelsConfig{
Set: map[string]string{
"app": "consul",
"chart": "consul-helm",
"heritage": "Helm",
"release": "consul",
},
},
},
Deployment: meshv2beta1.GatewayClassDeploymentConfig{
Container: &meshv2beta1.GatewayClassContainerConfig{
HostPort: 8080,
Expand Down Expand Up @@ -69,19 +79,31 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
"mesh.consul.hashicorp.com/app": "consul",
"mesh.consul.hashicorp.com/chart": "consul-helm",
"mesh.consul.hashicorp.com/heritage": "Helm",
"mesh.consul.hashicorp.com/release": "consul",
},
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(1),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
"mesh.consul.hashicorp.com/app": "consul",
"mesh.consul.hashicorp.com/chart": "consul-helm",
"mesh.consul.hashicorp.com/heritage": "Helm",
"mesh.consul.hashicorp.com/release": "consul",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
"mesh.consul.hashicorp.com/app": "consul",
"mesh.consul.hashicorp.com/chart": "consul-helm",
"mesh.consul.hashicorp.com/heritage": "Helm",
"mesh.consul.hashicorp.com/release": "consul",
},
Annotations: map[string]string{
constants.AnnotationGatewayKind: meshGatewayAnnotationKind,
Expand Down Expand Up @@ -288,6 +310,10 @@ func Test_meshGatewayBuilder_Deployment(t *testing.T) {
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
"mesh.consul.hashicorp.com/app": "consul",
"mesh.consul.hashicorp.com/chart": "consul-helm",
"mesh.consul.hashicorp.com/heritage": "Helm",
"mesh.consul.hashicorp.com/release": "consul",
},
},
TopologyKey: "kubernetes.io/hostname",
Expand Down
11 changes: 10 additions & 1 deletion control-plane/gateways/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
package gateways

func (b *meshGatewayBuilder) Labels() map[string]string {
return map[string]string{
labels := map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
}

if b.gcc != nil {
labels["mesh.consul.hashicorp.com/app"] = b.gcc.Spec.Labels.Set["app"]
labels["mesh.consul.hashicorp.com/chart"] = b.gcc.Spec.Labels.Set["chart"]
labels["mesh.consul.hashicorp.com/heritage"] = b.gcc.Spec.Labels.Set["heritage"]
labels["mesh.consul.hashicorp.com/release"] = b.gcc.Spec.Labels.Set["release"]
}

return labels
}
18 changes: 18 additions & 0 deletions control-plane/gateways/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func Test_meshGatewayBuilder_Service(t *testing.T) {
config: GatewayConfig{},
gcc: &meshv2beta1.GatewayClassConfig{
Spec: meshv2beta1.GatewayClassConfigSpec{
GatewayClassAnnotationsAndLabels: meshv2beta1.GatewayClassAnnotationsAndLabels{
Labels: meshv2beta1.GatewayClassAnnotationsLabelsConfig{
Set: map[string]string{
"app": "consul",
"chart": "consul-helm",
"heritage": "Helm",
"release": "consul",
},
},
},
Deployment: meshv2beta1.GatewayClassDeploymentConfig{
Container: &meshv2beta1.GatewayClassContainerConfig{
PortModifier: 8000,
Expand All @@ -53,13 +63,21 @@ func Test_meshGatewayBuilder_Service(t *testing.T) {
want: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"mesh.consul.hashicorp.com/app": "consul",
"mesh.consul.hashicorp.com/chart": "consul-helm",
"mesh.consul.hashicorp.com/heritage": "Helm",
"mesh.consul.hashicorp.com/release": "consul",
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
},
Annotations: map[string]string{},
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"mesh.consul.hashicorp.com/managed-by": "consul-k8s",
"mesh.consul.hashicorp.com/app": "consul",
"mesh.consul.hashicorp.com/chart": "consul-helm",
"mesh.consul.hashicorp.com/heritage": "Helm",
"mesh.consul.hashicorp.com/release": "consul",
},
Type: corev1.ServiceTypeLoadBalancer,
Ports: []corev1.ServicePort{
Expand Down

0 comments on commit bb90713

Please sign in to comment.