Skip to content

Commit

Permalink
Carthatic refactor of some non-functional code
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Godkin <tomgodkin@pm.me>
  • Loading branch information
BooleanCat authored and georgethebeatle committed Sep 2, 2024
1 parent 6e3716e commit 3a08223
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions statefulset-runner/controllers/appworkload_to_stset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"maps"
"regexp"
"slices"
"sort"
"strings"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools"
"github.com/BooleanCat/go-functional/v2/it"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -93,20 +96,16 @@ func (r *AppWorkloadToStatefulsetConverter) Convert(appWorkload *korifiv1alpha1.
return envs[i].Name < envs[j].Name
})

ports := []corev1.ContainerPort{}

for _, port := range appWorkload.Spec.Ports {
ports = append(ports, corev1.ContainerPort{ContainerPort: port})
}

containers := []corev1.Container{
{
Name: ApplicationContainerName,
Image: appWorkload.Spec.Image,
ImagePullPolicy: corev1.PullAlways,
Command: appWorkload.Spec.Command,
Env: envs,
Ports: ports,
Ports: slices.Collect(it.Map(slices.Values(appWorkload.Spec.Ports), func(port int32) corev1.ContainerPort {
return corev1.ContainerPort{ContainerPort: port}
})),
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: tools.PtrTo(false),
Capabilities: &corev1.Capabilities{
Expand Down Expand Up @@ -227,22 +226,15 @@ func truncateString(str string, num int) string {
}

func toLabelSelectorRequirements(selector *metav1.LabelSelector) []metav1.LabelSelectorRequirement {
labels := make([]string, 0, len(selector.MatchLabels))
for k := range selector.MatchLabels {
labels = append(labels, k)
}
sort.Strings(labels)
labels := slices.Values(slices.Sorted(maps.Keys(selector.MatchLabels)))

reqs := make([]metav1.LabelSelectorRequirement, 0, len(labels))
for _, label := range labels {
reqs = append(reqs, metav1.LabelSelectorRequirement{
return slices.Collect(it.Map(labels, func(label string) metav1.LabelSelectorRequirement {
return metav1.LabelSelectorRequirement{
Key: label,
Operator: metav1.LabelSelectorOpIn,
Values: []string{selector.MatchLabels[label]},
})
}

return reqs
}
}))
}

func statefulSetLabelSelector(appWorkload *korifiv1alpha1.AppWorkload) *metav1.LabelSelector {
Expand Down

0 comments on commit 3a08223

Please sign in to comment.