Skip to content

Commit

Permalink
fix: improved service match labels
Browse files Browse the repository at this point in the history
  • Loading branch information
astromechza committed May 15, 2024
1 parent 507bcc0 commit 207ca5d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
15 changes: 11 additions & 4 deletions internal/convert/workloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
WorkloadKindDeployment = "Deployment"
WorkloadKindStatefulSet = "StatefulSet"
SelectorLabelWorkloadName = "score-workload"
SelectorLabelApp = "app"
)

func ConvertWorkload(state *project.State, workloadName string) ([]machineryMeta.Object, error) {
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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)}},
Expand All @@ -246,15 +251,17 @@ 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,
Template: coreV1.PodTemplateSpec{
ObjectMeta: machineryMeta.ObjectMeta{
Labels: map[string]string{
SelectorLabelWorkloadName: workloadName,
SelectorLabelApp: workloadName,
},
Annotations: podAnnotations,
},
Expand Down
36 changes: 31 additions & 5 deletions internal/convert/workloads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -154,6 +179,7 @@ spec:
my.custom.scope/annotation: value
creationTimestamp: null
labels:
app: example
score-workload: example
spec:
containers:
Expand Down
12 changes: 6 additions & 6 deletions internal/provisioners/default/zz-default.provisioners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -222,7 +222,7 @@
k8s.score.dev/resource-guid: {{ .Guid }}
spec:
selector:
scoreWorkload: {{ .SourceWorkload }}
score-workload: {{ .SourceWorkload }}
app: {{ .State.service }}
type: ClusterIP
ports:
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -326,7 +326,7 @@
k8s.score.dev/resource-guid: {{ .Guid }}
spec:
selector:
scoreWorkload: {{ .SourceWorkload }}
score-workload: {{ .SourceWorkload }}
app: {{ .State.service }}
type: ClusterIP
ports:
Expand Down

0 comments on commit 207ca5d

Please sign in to comment.