From 207ca5d6b71efaac2aacd9a73e6a9066c32152fa Mon Sep 17 00:00:00 2001 From: Ben Meier Date: Wed, 15 May 2024 08:01:17 +0100 Subject: [PATCH] fix: improved service match labels --- internal/convert/workloads.go | 15 +++++--- internal/convert/workloads_test.go | 36 ++++++++++++++++--- .../default/zz-default.provisioners.yaml | 12 +++---- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/internal/convert/workloads.go b/internal/convert/workloads.go index bf38c81..b06fdbd 100644 --- a/internal/convert/workloads.go +++ b/internal/convert/workloads.go @@ -35,6 +35,7 @@ const ( WorkloadKindDeployment = "Deployment" WorkloadKindStatefulSet = "StatefulSet" SelectorLabelWorkloadName = "score-workload" + SelectorLabelApp = "app" ) func ConvertWorkload(state *project.State, workloadName string) ([]machineryMeta.Object, error) { @@ -183,6 +184,7 @@ func ConvertWorkload(state *project.State, workloadName string) ([]machineryMeta Spec: coreV1.ServiceSpec{ Selector: map[string]string{ SelectorLabelWorkloadName: workloadName, + SelectorLabelApp: workloadName, }, Ports: portList, }, @@ -200,14 +202,16 @@ func ConvertWorkload(state *project.State, workloadName string) ([]machineryMeta Spec: v1.DeploymentSpec{ Replicas: internal.Ref(int32(1)), Selector: &machineryMeta.LabelSelector{ - MatchExpressions: []machineryMeta.LabelSelectorRequirement{ - {SelectorLabelWorkloadName, machineryMeta.LabelSelectorOpIn, []string{workloadName}}, + MatchLabels: map[string]string{ + SelectorLabelWorkloadName: workloadName, + SelectorLabelApp: workloadName, }, }, Template: coreV1.PodTemplateSpec{ ObjectMeta: machineryMeta.ObjectMeta{ Labels: map[string]string{ SelectorLabelWorkloadName: workloadName, + SelectorLabelApp: workloadName, }, Annotations: podAnnotations, }, @@ -231,6 +235,7 @@ func ConvertWorkload(state *project.State, workloadName string) ([]machineryMeta Spec: coreV1.ServiceSpec{ Selector: map[string]string{ SelectorLabelWorkloadName: workloadName, + SelectorLabelApp: workloadName, }, ClusterIP: "None", Ports: []coreV1.ServicePort{{Name: "default", Port: 99, TargetPort: intstr.FromInt32(99)}}, @@ -246,8 +251,9 @@ func ConvertWorkload(state *project.State, workloadName string) ([]machineryMeta Spec: v1.StatefulSetSpec{ Replicas: internal.Ref(int32(1)), Selector: &machineryMeta.LabelSelector{ - MatchExpressions: []machineryMeta.LabelSelectorRequirement{ - {SelectorLabelWorkloadName, machineryMeta.LabelSelectorOpIn, []string{workloadName}}, + MatchLabels: map[string]string{ + SelectorLabelWorkloadName: workloadName, + SelectorLabelApp: workloadName, }, }, ServiceName: headlessServiceName, @@ -255,6 +261,7 @@ func ConvertWorkload(state *project.State, workloadName string) ([]machineryMeta ObjectMeta: machineryMeta.ObjectMeta{ Labels: map[string]string{ SelectorLabelWorkloadName: workloadName, + SelectorLabelApp: workloadName, }, Annotations: podAnnotations, }, diff --git a/internal/convert/workloads_test.go b/internal/convert/workloads_test.go index 53a3b63..6124ace 100644 --- a/internal/convert/workloads_test.go +++ b/internal/convert/workloads_test.go @@ -83,6 +83,15 @@ func TestMassive(t *testing.T) { Image: "other-image", }, }, + Service: &scoretypes.WorkloadService{ + Ports: map[string]scoretypes.ServicePort{ + "web": { + Port: 80, + TargetPort: internal.Ref(8080), + Protocol: internal.Ref(scoretypes.ServicePortProtocolUDP), + }, + }, + }, Resources: map[string]scoretypes.Resource{ "foo": { Type: "thing", @@ -131,6 +140,24 @@ kind: ConfigMap metadata: creationTimestamp: null name: example-c1-file-0 +apiVersion: v1 +kind: Service +metadata: + annotations: + k8s.score.dev/workload-name: example + creationTimestamp: null + name: example-svc +spec: + ports: + - name: web + port: 80 + protocol: UDP + targetPort: 8080 + selector: + app: example + score-workload: example +status: + loadBalancer: {} apiVersion: apps/v1 kind: Deployment metadata: @@ -141,11 +168,9 @@ metadata: spec: replicas: 1 selector: - matchExpressions: - - key: score-workload - operator: In - values: - - example + matchLabels: + app: example + score-workload: example strategy: {} template: metadata: @@ -154,6 +179,7 @@ spec: my.custom.scope/annotation: value creationTimestamp: null labels: + app: example score-workload: example spec: containers: diff --git a/internal/provisioners/default/zz-default.provisioners.yaml b/internal/provisioners/default/zz-default.provisioners.yaml index a920510..92da390 100644 --- a/internal/provisioners/default/zz-default.provisioners.yaml +++ b/internal/provisioners/default/zz-default.provisioners.yaml @@ -160,12 +160,12 @@ serviceName: {{ .State.service }} selector: matchLabels: - scoreWorkload: {{ .SourceWorkload }} + score-workload: {{ .SourceWorkload }} app: {{ .State.service }} template: metadata: labels: - scoreWorkload: {{ .SourceWorkload }} + score-workload: {{ .SourceWorkload }} app: {{ .State.service }} annotations: k8s.score.dev/source-workload: {{ .SourceWorkload }} @@ -222,7 +222,7 @@ k8s.score.dev/resource-guid: {{ .Guid }} spec: selector: - scoreWorkload: {{ .SourceWorkload }} + score-workload: {{ .SourceWorkload }} app: {{ .State.service }} type: ClusterIP ports: @@ -268,12 +268,12 @@ serviceName: {{ .State.service }} selector: matchLabels: - scoreWorkload: {{ .SourceWorkload }} + score-workload: {{ .SourceWorkload }} app: {{ .State.service }} template: metadata: labels: - scoreWorkload: {{ .SourceWorkload }} + score-workload: {{ .SourceWorkload }} app: {{ .State.service }} annotations: k8s.score.dev/source-workload: {{ .SourceWorkload }} @@ -326,7 +326,7 @@ k8s.score.dev/resource-guid: {{ .Guid }} spec: selector: - scoreWorkload: {{ .SourceWorkload }} + score-workload: {{ .SourceWorkload }} app: {{ .State.service }} type: ClusterIP ports: