Skip to content

Commit

Permalink
Upgrade go-functional to v2.0.0-beta.5
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Aug 6, 2024
1 parent 1d04566 commit 2334a71
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 30 deletions.
6 changes: 4 additions & 2 deletions api/repositories/domain_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package repositories
import (
"context"
"fmt"
"slices"
"sort"
"time"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools/k8s"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"

Expand Down Expand Up @@ -162,10 +164,10 @@ func (r *DomainRepo) ListDomains(ctx context.Context, authInfo authorization.Inf
return []DomainRecord{}, fmt.Errorf("failed to list domains in namespace %s: %w", r.rootNamespace, apierrors.FromK8sError(err, DomainResourceType))
}

domainRecords := itx.Map(
domainRecords := slices.Collect(it.Map(
itx.FromSlice(cfdomainList.Items).Filter(message.matches),
cfDomainToDomainRecord,
).Collect()
))
sort.Slice(domainRecords, func(i, j int) bool {
return domainRecords[i].CreatedAt.Before(domainRecords[j].CreatedAt)
})
Expand Down
4 changes: 3 additions & 1 deletion api/repositories/droplet_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package repositories
import (
"context"
"fmt"
"slices"
"time"

"code.cloudfoundry.org/korifi/tools/k8s"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"

"code.cloudfoundry.org/korifi/api/authorization"
Expand Down Expand Up @@ -170,7 +172,7 @@ func (r *DropletRepo) ListDroplets(ctx context.Context, authInfo authorization.I
}

filteredBuilds := itx.FromSlice(allBuilds).Filter(message.matches)
return itx.Map(filteredBuilds, cfBuildToDropletRecord).Collect(), nil
return slices.Collect(it.Map(filteredBuilds, cfBuildToDropletRecord)), nil
}

type UpdateDropletMessage struct {
Expand Down
6 changes: 3 additions & 3 deletions api/repositories/metrics_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/BooleanCat/go-functional/v2/it"
corev1 "k8s.io/api/core/v1"
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -45,9 +45,9 @@ func (r *MetricsRepo) GetMetrics(ctx context.Context, authInfo authorization.Inf
return nil, fmt.Errorf("failed to list pods: %w", apierrors.FromK8sError(err, PodResourceType))
}

return itx.Map(slices.Values(podList.Items), func(pod corev1.Pod) PodMetrics {
return slices.Collect(it.Map(slices.Values(podList.Items), func(pod corev1.Pod) PodMetrics {
metrics := metricsv1beta1.PodMetrics{}
_ = userClient.Get(ctx, client.ObjectKeyFromObject(&pod), &metrics)
return PodMetrics{Pod: pod, Metrics: metrics}
}).Collect(), nil
})), nil
}
4 changes: 3 additions & 1 deletion api/repositories/org_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package repositories
import (
"context"
"fmt"
"slices"
"time"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -135,7 +137,7 @@ func (r *OrgRepo) ListOrgs(ctx context.Context, info authorization.Info, message
filteredOrgs := itx.FromSlice(cfOrgList.Items).Filter(func(org korifiv1alpha1.CFOrg) bool {
return authorizedNamespaces[org.Name] && message.matches(org)
})
return itx.Map(filteredOrgs, cfOrgToOrgRecord).Collect(), nil
return slices.Collect(it.Map(filteredOrgs, cfOrgToOrgRecord)), nil
}

func (r *OrgRepo) GetOrg(ctx context.Context, info authorization.Info, orgGUID string) (OrgRecord, error) {
Expand Down
3 changes: 2 additions & 1 deletion api/repositories/package_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.cloudfoundry.org/korifi/tools/dockercfg"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/uuid"
Expand Down Expand Up @@ -335,7 +336,7 @@ func (r *PackageRepo) ListPackages(ctx context.Context, authInfo authorization.I
}

filteredPackages := itx.FromSlice(packages).Filter(message.matches)
return itx.Map(filteredPackages, r.cfPackageToPackageRecord).Collect(), nil
return slices.Collect(it.Map(filteredPackages, r.cfPackageToPackageRecord)), nil
}

func (r *PackageRepo) UpdatePackageSource(ctx context.Context, authInfo authorization.Info, message UpdatePackageSourceMessage) (PackageRecord, error) {
Expand Down
11 changes: 6 additions & 5 deletions api/repositories/pod_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"iter"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -109,24 +110,24 @@ func (r *PodRepo) GetRuntimeLogsForApp(ctx context.Context, logger logr.Logger,
return nil, fmt.Errorf("failed to build user client: %w", err)
}

logRecords := itx.Map(slices.Values(podList.Items), func(pod corev1.Pod) func(func(LogRecord) bool) {
logRecords := slices.Collect(it.Map(slices.Values(podList.Items), func(pod corev1.Pod) func(func(LogRecord) bool) {
return r.getPodLogs(ctx, logClient, logger, pod, message.Limit)
}).Collect()
}))

return slices.Collect(it.Chain(logRecords...)), nil
}

func (r *PodRepo) getPodLogs(ctx context.Context, k8sClient k8sclient.Interface, logger logr.Logger, pod corev1.Pod, limit int64) itx.Iterator[LogRecord] {
func (r *PodRepo) getPodLogs(ctx context.Context, k8sClient k8sclient.Interface, logger logr.Logger, pod corev1.Pod, limit int64) iter.Seq[LogRecord] {
logReadCloser, err := k8sClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{Timestamps: true, TailLines: &limit}).Stream(ctx)
if err != nil {
logger.Info("failed to fetch logs", "pod", pod.Name, "reason", err)
return itx.Exhausted[LogRecord]()
return it.Exhausted[LogRecord]()
}

defer logReadCloser.Close()

logLines := readLines(logReadCloser, logger.WithValues("pod", pod.Name))
return itx.Map(slices.Values(logLines), logLineToLogRecord)
return it.Map(slices.Values(logLines), logLineToLogRecord)
}

func readLines(r io.Reader, logger logr.Logger) []string {
Expand Down
4 changes: 3 additions & 1 deletion api/repositories/process_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package repositories
import (
"context"
"fmt"
"slices"
"time"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools/k8s"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -166,7 +168,7 @@ func (r *ProcessRepo) ListProcesses(ctx context.Context, authInfo authorization.
}

filteredProcesses := itx.FromSlice(processes).Filter(message.matches)
return itx.Map(filteredProcesses, cfProcessToProcessRecord).Collect(), nil
return slices.Collect(it.Map(filteredProcesses, cfProcessToProcessRecord)), nil
}

func (r *ProcessRepo) ScaleProcess(ctx context.Context, authInfo authorization.Info, scaleProcessMessage ScaleProcessMessage) (ProcessRecord, error) {
Expand Down
7 changes: 4 additions & 3 deletions api/repositories/role_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"time"

"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -253,7 +254,7 @@ func (r *RoleRepo) ListRoles(ctx context.Context, authInfo authorization.Info) (
}

cfRoleBindings := itx.FromSlice(roleBindings).Filter(r.isCFRole)
return itx.Map(cfRoleBindings, r.toRoleRecord).Collect(), nil
return slices.Collect(it.Map(cfRoleBindings, r.toRoleRecord)), nil
}

func (r *RoleRepo) isCFRole(rb rbacv1.RoleBinding) bool {
Expand All @@ -271,9 +272,9 @@ func (r *RoleRepo) getCFRoleName(k8sRoleName string) string {
}

func (r *RoleRepo) getCFRoleNames() []string {
return itx.Map(maps.Values(r.roleMappings), func(r config.Role) string {
return slices.Collect(it.Map(maps.Values(r.roleMappings), func(r config.Role) string {
return r.Name
}).Collect()
}))
}

func (r *RoleRepo) toRoleRecord(roleBinding rbacv1.RoleBinding) RoleRecord {
Expand Down
11 changes: 6 additions & 5 deletions api/repositories/route_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -208,7 +209,7 @@ func (r *RouteRepo) ListRoutes(ctx context.Context, authInfo authorization.Info,
}

filteredRoutes := itx.FromSlice(routes).Filter(message.matches)
return itx.Map(filteredRoutes, cfRouteToRouteRecord).Collect(), nil
return slices.Collect(it.Map(filteredRoutes, cfRouteToRouteRecord)), nil
}

func cfRouteToRouteRecord(cfRoute korifiv1alpha1.CFRoute) RouteRecord {
Expand All @@ -231,7 +232,7 @@ func cfRouteToRouteRecord(cfRoute korifiv1alpha1.CFRoute) RouteRecord {
}

func cfRouteDestinationsToDestinationRecords(cfRoute korifiv1alpha1.CFRoute) []DestinationRecord {
return itx.Map(slices.Values(cfRoute.Spec.Destinations), func(specDestination korifiv1alpha1.Destination) DestinationRecord {
return slices.Collect(it.Map(slices.Values(cfRoute.Spec.Destinations), func(specDestination korifiv1alpha1.Destination) DestinationRecord {
record := DestinationRecord{
GUID: specDestination.GUID,
AppGUID: specDestination.AppRef.Name,
Expand All @@ -249,7 +250,7 @@ func cfRouteDestinationsToDestinationRecords(cfRoute korifiv1alpha1.CFRoute) []D
}

return record
}).Collect()
}))
}

func (r *RouteRepo) ListRoutesForApp(ctx context.Context, authInfo authorization.Info, appGUID string, spaceGUID string) ([]RouteRecord, error) {
Expand Down Expand Up @@ -434,7 +435,7 @@ func (r *RouteRepo) fetchRouteByFields(ctx context.Context, authInfo authorizati
}

func destinationRecordsToCFDestinations(destinationRecords []DestinationRecord) []korifiv1alpha1.Destination {
return itx.Map(itx.FromSlice(destinationRecords), func(destinationRecord DestinationRecord) korifiv1alpha1.Destination {
return slices.Collect(it.Map(itx.FromSlice(destinationRecords), func(destinationRecord DestinationRecord) korifiv1alpha1.Destination {
return korifiv1alpha1.Destination{
GUID: destinationRecord.GUID,
Port: destinationRecord.Port,
Expand All @@ -444,7 +445,7 @@ func destinationRecordsToCFDestinations(destinationRecords []DestinationRecord)
ProcessType: destinationRecord.ProcessType,
Protocol: destinationRecord.Protocol,
}
}).Collect()
}))
}

func (r *RouteRepo) PatchRouteMetadata(ctx context.Context, authInfo authorization.Info, message PatchRouteMetadataMessage) (RouteRecord, error) {
Expand Down
4 changes: 3 additions & 1 deletion api/repositories/service_binding_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repositories
import (
"context"
"fmt"
"slices"
"time"

"k8s.io/apimachinery/pkg/labels"
Expand All @@ -15,6 +16,7 @@ import (
"code.cloudfoundry.org/korifi/controllers/webhooks/validation"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -292,5 +294,5 @@ func (r *ServiceBindingRepo) ListServiceBindings(ctx context.Context, authInfo a
}

filteredServiceBindings := itx.FromSlice(serviceBindings).Filter(message.matches)
return itx.Map(filteredServiceBindings, cfServiceBindingToRecord).Collect(), nil
return slices.Collect(it.Map(filteredServiceBindings, cfServiceBindingToRecord)), nil
}
3 changes: 2 additions & 1 deletion api/repositories/service_broker_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"code.cloudfoundry.org/korifi/model"
"code.cloudfoundry.org/korifi/model/services"
"code.cloudfoundry.org/korifi/tools"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -190,7 +191,7 @@ func (r *ServiceBrokerRepo) ListServiceBrokers(ctx context.Context, authInfo aut

brokers := itx.FromSlice(brokersList.Items).Filter(message.matches)

return itx.Map(brokers, toServiceBrokerRecord).Collect(), nil
return slices.Collect(it.Map(brokers, toServiceBrokerRecord)), nil
}

func (r *ServiceBrokerRepo) GetServiceBroker(ctx context.Context, authInfo authorization.Info, guid string) (ServiceBrokerRecord, error) {
Expand Down
4 changes: 3 additions & 1 deletion api/repositories/service_instance_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"time"

"code.cloudfoundry.org/korifi/api/authorization"
Expand All @@ -12,6 +13,7 @@ import (
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -268,7 +270,7 @@ func (r *ServiceInstanceRepo) ListServiceInstances(ctx context.Context, authInfo
}

filteredServiceInstances := itx.FromSlice(serviceInstances).Filter(message.matches)
return itx.Map(filteredServiceInstances, cfServiceInstanceToRecord).Collect(), nil
return slices.Collect(it.Map(filteredServiceInstances, cfServiceInstanceToRecord)), nil
}

func (r *ServiceInstanceRepo) GetServiceInstance(ctx context.Context, authInfo authorization.Info, guid string) (ServiceInstanceRecord, error) {
Expand Down
4 changes: 3 additions & 1 deletion api/repositories/space_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package repositories
import (
"context"
"fmt"
"slices"
"time"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -158,7 +160,7 @@ func (r *SpaceRepo) ListSpaces(ctx context.Context, authInfo authorization.Info,
return authorizedSpaceNamespaces[s.Name] && message.matches(s)
})

return itx.Map(filteredSpaces, cfSpaceToSpaceRecord).Collect(), nil
return slices.Collect(it.Map(filteredSpaces, cfSpaceToSpaceRecord)), nil
}

func (r *SpaceRepo) GetSpace(ctx context.Context, info authorization.Info, spaceGUID string) (SpaceRecord, error) {
Expand Down
4 changes: 3 additions & 1 deletion api/repositories/task_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package repositories
import (
"context"
"fmt"
"slices"
"time"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/workloads/tasks"
"code.cloudfoundry.org/korifi/tools/k8s"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -191,7 +193,7 @@ func (r *TaskRepo) ListTasks(ctx context.Context, authInfo authorization.Info, m
}

filteredTasks := itx.FromSlice(tasks).Filter(msg.matches)
return itx.Map(filteredTasks, taskToRecord).Collect(), nil
return slices.Collect(it.Map(filteredTasks, taskToRecord)), nil
}

func (r *TaskRepo) CancelTask(ctx context.Context, authInfo authorization.Info, taskGUID string) (TaskRecord, error) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23
require (
code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5
code.cloudfoundry.org/go-loggregator/v8 v8.0.5
github.com/BooleanCat/go-functional/v2 v2.0.0-beta.3
github.com/BooleanCat/go-functional/v2 v2.0.0-beta.5
github.com/Masterminds/semver v1.5.0
github.com/PaesslerAG/jsonpath v0.1.1
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+Z
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/BooleanCat/go-functional/v2 v2.0.0-beta.3 h1:7lCZO6m4H2dUF5mTsWgkaaNjwvmdLX+YgL4z5V06wXY=
github.com/BooleanCat/go-functional/v2 v2.0.0-beta.3/go.mod h1:IpUUAXAc9CiWDb+YDXkJyyUhtOVqDtyICDRg/de1IaQ=
github.com/BooleanCat/go-functional/v2 v2.0.0-beta.5 h1:/AbWbCabFMF3sDWK+YrUBs2W2g8LcjvmzB7f7an4E6U=
github.com/BooleanCat/go-functional/v2 v2.0.0-beta.5/go.mod h1:IpUUAXAc9CiWDb+YDXkJyyUhtOVqDtyICDRg/de1IaQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
Expand Down

0 comments on commit 2334a71

Please sign in to comment.