diff --git a/api/repositories/domain_repository.go b/api/repositories/domain_repository.go index 1f4df0299..9ada10115 100644 --- a/api/repositories/domain_repository.go +++ b/api/repositories/domain_repository.go @@ -3,6 +3,7 @@ package repositories import ( "context" "fmt" + "slices" "sort" "time" @@ -10,6 +11,7 @@ import ( 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" @@ -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) }) diff --git a/api/repositories/droplet_repository.go b/api/repositories/droplet_repository.go index 7d3d03bb2..ce2398b63 100644 --- a/api/repositories/droplet_repository.go +++ b/api/repositories/droplet_repository.go @@ -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" @@ -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 { diff --git a/api/repositories/metrics_repository.go b/api/repositories/metrics_repository.go index 325c89681..d61bc3e85 100644 --- a/api/repositories/metrics_repository.go +++ b/api/repositories/metrics_repository.go @@ -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" @@ -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 } diff --git a/api/repositories/org_repository.go b/api/repositories/org_repository.go index 1c0db8f33..49ae278b8 100644 --- a/api/repositories/org_repository.go +++ b/api/repositories/org_repository.go @@ -3,6 +3,7 @@ package repositories import ( "context" "fmt" + "slices" "time" "code.cloudfoundry.org/korifi/api/authorization" @@ -10,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" "k8s.io/apimachinery/pkg/api/meta" @@ -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) { diff --git a/api/repositories/package_repository.go b/api/repositories/package_repository.go index 7ae62d7d4..a88e14f3a 100644 --- a/api/repositories/package_repository.go +++ b/api/repositories/package_repository.go @@ -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" @@ -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) { diff --git a/api/repositories/pod_repository.go b/api/repositories/pod_repository.go index bed6c655b..a9c62b15b 100644 --- a/api/repositories/pod_repository.go +++ b/api/repositories/pod_repository.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "io" + "iter" "slices" "strings" "time" @@ -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 { diff --git a/api/repositories/process_repository.go b/api/repositories/process_repository.go index 0b64da049..c98102758 100644 --- a/api/repositories/process_repository.go +++ b/api/repositories/process_repository.go @@ -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" @@ -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) { diff --git a/api/repositories/role_repository.go b/api/repositories/role_repository.go index 78ac7b9f7..773c37108 100644 --- a/api/repositories/role_repository.go +++ b/api/repositories/role_repository.go @@ -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" @@ -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 { @@ -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 { diff --git a/api/repositories/route_repository.go b/api/repositories/route_repository.go index 20a46bfc4..64855df74 100644 --- a/api/repositories/route_repository.go +++ b/api/repositories/route_repository.go @@ -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" @@ -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 { @@ -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, @@ -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) { @@ -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, @@ -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) { diff --git a/api/repositories/service_binding_repository.go b/api/repositories/service_binding_repository.go index ffb6eb903..a2db1fcdb 100644 --- a/api/repositories/service_binding_repository.go +++ b/api/repositories/service_binding_repository.go @@ -3,6 +3,7 @@ package repositories import ( "context" "fmt" + "slices" "time" "k8s.io/apimachinery/pkg/labels" @@ -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" @@ -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 } diff --git a/api/repositories/service_broker_repository.go b/api/repositories/service_broker_repository.go index 7aacaedfa..cd34004ed 100644 --- a/api/repositories/service_broker_repository.go +++ b/api/repositories/service_broker_repository.go @@ -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" @@ -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) { diff --git a/api/repositories/service_instance_repository.go b/api/repositories/service_instance_repository.go index 20ef99afd..88739db99 100644 --- a/api/repositories/service_instance_repository.go +++ b/api/repositories/service_instance_repository.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "slices" "time" "code.cloudfoundry.org/korifi/api/authorization" @@ -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" @@ -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) { diff --git a/api/repositories/space_repository.go b/api/repositories/space_repository.go index 5fcc21ce4..2c884cdbb 100644 --- a/api/repositories/space_repository.go +++ b/api/repositories/space_repository.go @@ -3,6 +3,7 @@ package repositories import ( "context" "fmt" + "slices" "time" "code.cloudfoundry.org/korifi/api/authorization" @@ -10,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" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -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) { diff --git a/api/repositories/task_repository.go b/api/repositories/task_repository.go index 55dac4202..6c81e37f5 100644 --- a/api/repositories/task_repository.go +++ b/api/repositories/task_repository.go @@ -3,6 +3,7 @@ package repositories import ( "context" "fmt" + "slices" "time" "code.cloudfoundry.org/korifi/api/authorization" @@ -10,6 +11,7 @@ import ( 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" @@ -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) { diff --git a/go.mod b/go.mod index 3c85bf8a7..d5509769b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 969ca830c..76f85d76a 100644 --- a/go.sum +++ b/go.sum @@ -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=