From 6e0c786b8a177aed15a3c98157f846c854a90591 Mon Sep 17 00:00:00 2001 From: Tom Godkin Date: Wed, 17 Jul 2024 20:27:34 +0100 Subject: [PATCH 1/7] Upgrade go-functional to v2 --- api/payloads/service_plan.go | 11 +++---- api/repositories/app_repository.go | 29 ++++++------------- api/repositories/domain_repository.go | 6 ++-- api/repositories/droplet_repository.go | 6 ++-- api/repositories/metrics_repository.go | 5 ++-- api/repositories/org_repository.go | 6 ++-- api/repositories/package_repository.go | 6 ++-- api/repositories/pod_repository.go | 16 +++++----- api/repositories/process_repository.go | 10 +++---- api/repositories/role_repository.go | 12 ++++---- api/repositories/route_repository.go | 20 ++++++------- .../service_binding_repository.go | 6 ++-- api/repositories/service_broker_repository.go | 7 +++-- .../service_instance_repository.go | 6 ++-- .../service_offering_repository.go | 5 ++-- api/repositories/service_plan_repository.go | 9 +++--- api/repositories/shared.go | 21 ++++++++++---- api/repositories/space_repository.go | 6 ++-- api/repositories/task_repository.go | 6 ++-- go.mod | 6 ++-- go.sum | 4 +-- tests/e2e/service_plans_test.go | 4 +-- 22 files changed, 104 insertions(+), 103 deletions(-) diff --git a/api/payloads/service_plan.go b/api/payloads/service_plan.go index bb745f396..aad8db3ae 100644 --- a/api/payloads/service_plan.go +++ b/api/payloads/service_plan.go @@ -5,6 +5,7 @@ import ( "net/url" "regexp" "strconv" + "slices" "code.cloudfoundry.org/korifi/api/payloads/params" "code.cloudfoundry.org/korifi/api/payloads/parse" @@ -13,7 +14,7 @@ import ( korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "code.cloudfoundry.org/korifi/model/services" "code.cloudfoundry.org/korifi/tools" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it" jellidation "github.com/jellydator/validation" ) @@ -109,9 +110,9 @@ func (p *ServicePlanVisibility) ToApplyMessage(planGUID string) repositories.App return repositories.ApplyServicePlanVisibilityMessage{ PlanGUID: planGUID, Type: p.Type, - Organizations: iter.Map(iter.Lift(p.Organizations), func(v services.VisibilityOrganization) string { + Organizations: slices.Collect(it.Map(slices.Values(p.Organizations), func(v services.VisibilityOrganization) string { return v.GUID - }).Collect(), + })), } } @@ -119,8 +120,8 @@ func (p *ServicePlanVisibility) ToUpdateMessage(planGUID string) repositories.Up return repositories.UpdateServicePlanVisibilityMessage{ PlanGUID: planGUID, Type: p.Type, - Organizations: iter.Map(iter.Lift(p.Organizations), func(v services.VisibilityOrganization) string { + Organizations: slices.Collect(it.Map(slices.Values(p.Organizations), func(v services.VisibilityOrganization) string { return v.GUID - }).Collect(), + })), } } diff --git a/api/repositories/app_repository.go b/api/repositories/app_repository.go index f440d0905..ce2572df1 100644 --- a/api/repositories/app_repository.go +++ b/api/repositories/app_repository.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "sort" + "slices" "strings" "time" @@ -16,7 +16,8 @@ import ( "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "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" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -191,20 +192,6 @@ func (m *ListAppsMessage) matches(cfApp korifiv1alpha1.CFApp) bool { tools.EmptyOrContains(m.Guids, cfApp.Name) } -type byName []AppRecord - -func (a byName) Len() int { - return len(a) -} - -func (a byName) Less(i, j int) bool { - return a[i].Name < a[j].Name -} - -func (a byName) Swap(i, j int) { - a[i], a[j] = a[j], a[i] -} - func (f *AppRepo) GetApp(ctx context.Context, authInfo authorization.Info, appGUID string) (AppRecord, error) { ns, err := f.namespaceRetriever.NamespaceFor(ctx, appGUID, AppResourceType) if err != nil { @@ -340,11 +327,13 @@ func (f *AppRepo) ListApps(ctx context.Context, authInfo authorization.Info, mes apps = append(apps, appList.Items...) } - filteredApps := iter.Lift(apps).Filter(message.matches) - appRecords := iter.Map(filteredApps, cfAppToAppRecord).Collect() - // By default sort it by App.DisplayName - sort.Sort(byName(appRecords)) + appRecords := slices.SortedFunc( + it.Map(itx.FromSlice(apps).Filter(message.matches), cfAppToAppRecord), + func(a, b AppRecord) int { + return strings.Compare(a.Name, b.Name) + }, + ) return appRecords, nil } diff --git a/api/repositories/domain_repository.go b/api/repositories/domain_repository.go index 5931a73e5..a762086a6 100644 --- a/api/repositories/domain_repository.go +++ b/api/repositories/domain_repository.go @@ -11,7 +11,7 @@ import ( korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -163,8 +163,8 @@ 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 := iter.Map( - iter.Lift(cfdomainList.Items).Filter(message.matches), + domainRecords := itx.Map( + itx.FromSlice(cfdomainList.Items).Filter(message.matches), cfDomainToDomainRecord, ).Collect() sort.Slice(domainRecords, func(i, j int) bool { diff --git a/api/repositories/droplet_repository.go b/api/repositories/droplet_repository.go index 46273ecde..919dd168f 100644 --- a/api/repositories/droplet_repository.go +++ b/api/repositories/droplet_repository.go @@ -7,7 +7,7 @@ import ( "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "code.cloudfoundry.org/korifi/api/authorization" apierrors "code.cloudfoundry.org/korifi/api/errors" @@ -170,8 +170,8 @@ func (r *DropletRepo) ListDroplets(ctx context.Context, authInfo authorization.I allBuilds = append(allBuilds, buildList.Items...) } - filteredBuilds := iter.Lift(allBuilds).Filter(message.matches) - return iter.Map(filteredBuilds, cfBuildToDropletRecord).Collect(), nil + filteredBuilds := itx.FromSlice(allBuilds).Filter(message.matches) + return itx.Map(filteredBuilds, cfBuildToDropletRecord).Collect(), nil } type UpdateDropletMessage struct { diff --git a/api/repositories/metrics_repository.go b/api/repositories/metrics_repository.go index 3157064f3..325c89681 100644 --- a/api/repositories/metrics_repository.go +++ b/api/repositories/metrics_repository.go @@ -3,10 +3,11 @@ package repositories import ( "context" "fmt" + "slices" "code.cloudfoundry.org/korifi/api/authorization" apierrors "code.cloudfoundry.org/korifi/api/errors" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" corev1 "k8s.io/api/core/v1" metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -44,7 +45,7 @@ func (r *MetricsRepo) GetMetrics(ctx context.Context, authInfo authorization.Inf return nil, fmt.Errorf("failed to list pods: %w", apierrors.FromK8sError(err, PodResourceType)) } - return iter.Map(iter.Lift(podList.Items), func(pod corev1.Pod) PodMetrics { + return itx.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} diff --git a/api/repositories/org_repository.go b/api/repositories/org_repository.go index 5e7001510..e810d248b 100644 --- a/api/repositories/org_repository.go +++ b/api/repositories/org_repository.go @@ -11,7 +11,7 @@ import ( "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -133,10 +133,10 @@ func (r *OrgRepo) ListOrgs(ctx context.Context, info authorization.Info, message return nil, apierrors.FromK8sError(err, OrgResourceType) } - filteredOrgs := iter.Lift(cfOrgList.Items).Filter(func(org korifiv1alpha1.CFOrg) bool { + filteredOrgs := itx.FromSlice(cfOrgList.Items).Filter(func(org korifiv1alpha1.CFOrg) bool { return authorizedNamespaces[org.Name] && message.matches(org) }) - return iter.Map(filteredOrgs, cfOrgToOrgRecord).Collect(), nil + return itx.Map(filteredOrgs, cfOrgToOrgRecord).Collect(), 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 6a92e3a07..982f09c6a 100644 --- a/api/repositories/package_repository.go +++ b/api/repositories/package_repository.go @@ -14,7 +14,7 @@ import ( "code.cloudfoundry.org/korifi/tools/dockercfg" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/go-containerregistry/pkg/name" "github.com/google/uuid" corev1 "k8s.io/api/core/v1" @@ -335,8 +335,8 @@ func (r *PackageRepo) ListPackages(ctx context.Context, authInfo authorization.I packages = append(packages, packageList.Items...) } - filteredPackages := iter.Lift(packages).Filter(message.matches) - return iter.Map(filteredPackages, r.cfPackageToPackageRecord).Collect(), nil + filteredPackages := itx.FromSlice(packages).Filter(message.matches) + return itx.Map(filteredPackages, r.cfPackageToPackageRecord).Collect(), 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 55afda7af..bed6c655b 100644 --- a/api/repositories/pod_repository.go +++ b/api/repositories/pod_repository.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "io" + "slices" "strings" "time" @@ -13,7 +14,8 @@ import ( korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" k8sclient "k8s.io/client-go/kubernetes" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" labels "k8s.io/apimachinery/pkg/labels" @@ -63,7 +65,7 @@ func (r *PodRepo) DeletePod(ctx context.Context, authInfo authorization.Info, ap return fmt.Errorf("failed to list pods: %w", apierrors.FromK8sError(err, PodResourceType)) } - podsToDelete := iter.Lift(podList.Items).Filter(func(pod corev1.Pod) bool { + podsToDelete := itx.FromSlice(podList.Items).Filter(func(pod corev1.Pod) bool { return strings.HasSuffix(pod.Name, instanceID) }).Collect() @@ -107,24 +109,24 @@ func (r *PodRepo) GetRuntimeLogsForApp(ctx context.Context, logger logr.Logger, return nil, fmt.Errorf("failed to build user client: %w", err) } - logRecords := iter.Map(iter.Lift(podList.Items), func(pod corev1.Pod) iter.Iterator[LogRecord] { + logRecords := itx.Map(slices.Values(podList.Items), func(pod corev1.Pod) func(func(LogRecord) bool) { return r.getPodLogs(ctx, logClient, logger, pod, message.Limit) }).Collect() - return iter.Chain(logRecords...).Collect(), nil + 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) iter.Iterator[LogRecord] { +func (r *PodRepo) getPodLogs(ctx context.Context, k8sClient k8sclient.Interface, logger logr.Logger, pod corev1.Pod, limit int64) itx.Iterator[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 iter.Exhausted[LogRecord]() + return itx.Exhausted[LogRecord]() } defer logReadCloser.Close() logLines := readLines(logReadCloser, logger.WithValues("pod", pod.Name)) - return iter.Map(iter.Lift(logLines), logLineToLogRecord) + return itx.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 853749778..9d2b900c2 100644 --- a/api/repositories/process_repository.go +++ b/api/repositories/process_repository.go @@ -10,7 +10,7 @@ import ( korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -18,9 +18,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -const ( - ProcessResourceType = "Process" -) +const ProcessResourceType = "Process" func NewProcessRepo(namespaceRetriever NamespaceRetriever, userClientFactory authorization.UserK8sClientFactory, namespacePermissions *authorization.NamespacePermissions) *ProcessRepo { return &ProcessRepo{ @@ -168,8 +166,8 @@ func (r *ProcessRepo) ListProcesses(ctx context.Context, authInfo authorization. processes = append(processes, processList.Items...) } - filteredProcesses := iter.Lift(processes).Filter(message.matches) - return iter.Map(filteredProcesses, cfProcessToProcessRecord).Collect(), nil + filteredProcesses := itx.FromSlice(processes).Filter(message.matches) + return itx.Map(filteredProcesses, cfProcessToProcessRecord).Collect(), 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 da5e51c50..78ac7b9f7 100644 --- a/api/repositories/role_repository.go +++ b/api/repositories/role_repository.go @@ -4,13 +4,13 @@ import ( "context" "crypto/sha256" "fmt" + "maps" "slices" "strconv" "time" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" - "golang.org/x/exp/maps" rbacv1 "k8s.io/api/rbac/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -238,7 +238,7 @@ func (r *RoleRepo) ListRoles(ctx context.Context, authInfo authorization.Info) ( return nil, fmt.Errorf("failed to list namespaces for spaces with user role bindings: %w", err) } - nsList := iter.Chain(authorisedSpaceNamespaces, authorizedOrgNamespaces).Collect() + nsList := authorisedSpaceNamespaces.Chain(authorizedOrgNamespaces).Collect() roleBindings := []rbacv1.RoleBinding{} for _, ns := range nsList { roleBindingsList := &rbacv1.RoleBindingList{} @@ -252,8 +252,8 @@ func (r *RoleRepo) ListRoles(ctx context.Context, authInfo authorization.Info) ( roleBindings = append(roleBindings, roleBindingsList.Items...) } - cfRoleBindings := iter.Lift(roleBindings).Filter(r.isCFRole) - return iter.Map(cfRoleBindings, r.toRoleRecord).Collect(), nil + cfRoleBindings := itx.FromSlice(roleBindings).Filter(r.isCFRole) + return itx.Map(cfRoleBindings, r.toRoleRecord).Collect(), nil } func (r *RoleRepo) isCFRole(rb rbacv1.RoleBinding) bool { @@ -271,7 +271,7 @@ func (r *RoleRepo) getCFRoleName(k8sRoleName string) string { } func (r *RoleRepo) getCFRoleNames() []string { - return iter.Map(iter.Lift(maps.Values(r.roleMappings)), func(r config.Role) string { + return itx.Map(maps.Values(r.roleMappings), func(r config.Role) string { return r.Name }).Collect() } diff --git a/api/repositories/route_repository.go b/api/repositories/route_repository.go index 7bce6658e..645369f8a 100644 --- a/api/repositories/route_repository.go +++ b/api/repositories/route_repository.go @@ -12,7 +12,7 @@ import ( "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" v1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -118,7 +118,7 @@ func (m *ListRoutesMessage) matchesApp(r korifiv1alpha1.CFRoute) bool { return true } - return len(iter.Lift(r.Spec.Destinations).Filter(func(d korifiv1alpha1.Destination) bool { + return len(itx.FromSlice(r.Spec.Destinations).Filter(func(d korifiv1alpha1.Destination) bool { return slices.Contains(m.AppGUIDs, d.AppRef.Name) }).Collect()) > 0 } @@ -208,8 +208,8 @@ func (r *RouteRepo) ListRoutes(ctx context.Context, authInfo authorization.Info, routes = append(routes, cfRouteList.Items...) } - filteredRoutes := iter.Lift(routes).Filter(message.matches) - return iter.Map(filteredRoutes, cfRouteToRouteRecord).Collect(), nil + filteredRoutes := itx.FromSlice(routes).Filter(message.matches) + return itx.Map(filteredRoutes, cfRouteToRouteRecord).Collect(), nil } func cfRouteToRouteRecord(cfRoute korifiv1alpha1.CFRoute) RouteRecord { @@ -232,7 +232,7 @@ func cfRouteToRouteRecord(cfRoute korifiv1alpha1.CFRoute) RouteRecord { } func cfRouteDestinationsToDestinationRecords(cfRoute korifiv1alpha1.CFRoute) []DestinationRecord { - return iter.Map(iter.Lift(cfRoute.Spec.Destinations), func(specDestination korifiv1alpha1.Destination) DestinationRecord { + return itx.Map(slices.Values(cfRoute.Spec.Destinations), func(specDestination korifiv1alpha1.Destination) DestinationRecord { record := DestinationRecord{ GUID: specDestination.GUID, AppGUID: specDestination.AppRef.Name, @@ -352,7 +352,7 @@ func (r *RouteRepo) RemoveDestinationFromRoute(ctx context.Context, authInfo aut return RouteRecord{}, fmt.Errorf("failed to get route: %w", apierrors.FromK8sError(err, RouteResourceType)) } - updatedDestinations := iter.Lift(cfRoute.Spec.Destinations).Exclude(message.matches).Collect() + updatedDestinations := itx.FromSlice(cfRoute.Spec.Destinations).Exclude(message.matches).Collect() if len(updatedDestinations) == len(cfRoute.Spec.Destinations) { return RouteRecord{}, apierrors.NewUnprocessableEntityError(nil, "Unable to unmap route from destination. Ensure the route has a destination with this guid.") } @@ -394,14 +394,14 @@ func destinationMessageToDestination(m DesiredDestination) korifiv1alpha1.Destin } func contains(existingDestinations []korifiv1alpha1.Destination, desired DesiredDestination) bool { - matches := iter.Lift(existingDestinations).Filter(func(dest korifiv1alpha1.Destination) bool { + _, ok := itx.FromSlice(existingDestinations).Find(func(dest korifiv1alpha1.Destination) bool { return desired.AppGUID == dest.AppRef.Name && desired.ProcessType == dest.ProcessType && equal(desired.Port, dest.Port) && equal(desired.Protocol, dest.Protocol) - }).Collect() + }) - return len(matches) > 0 + return ok } func equal[T comparable](v1, v2 *T) bool { @@ -435,7 +435,7 @@ func (r *RouteRepo) fetchRouteByFields(ctx context.Context, authInfo authorizati } func destinationRecordsToCFDestinations(destinationRecords []DestinationRecord) []korifiv1alpha1.Destination { - return iter.Map(iter.Lift(destinationRecords), func(destinationRecord DestinationRecord) korifiv1alpha1.Destination { + return itx.Map(itx.FromSlice(destinationRecords), func(destinationRecord DestinationRecord) korifiv1alpha1.Destination { return korifiv1alpha1.Destination{ GUID: destinationRecord.GUID, Port: destinationRecord.Port, diff --git a/api/repositories/service_binding_repository.go b/api/repositories/service_binding_repository.go index bdcc00675..ecbc166af 100644 --- a/api/repositories/service_binding_repository.go +++ b/api/repositories/service_binding_repository.go @@ -16,7 +16,7 @@ import ( "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -292,6 +292,6 @@ func (r *ServiceBindingRepo) ListServiceBindings(ctx context.Context, authInfo a serviceBindings = append(serviceBindings, serviceBindingList.Items...) } - filteredServiceBindings := iter.Lift(serviceBindings).Filter(message.matches) - return iter.Map(filteredServiceBindings, cfServiceBindingToRecord).Collect(), nil + filteredServiceBindings := itx.FromSlice(serviceBindings).Filter(message.matches) + return itx.Map(filteredServiceBindings, cfServiceBindingToRecord).Collect(), nil } diff --git a/api/repositories/service_broker_repository.go b/api/repositories/service_broker_repository.go index 9d1f0d5af..c995069fc 100644 --- a/api/repositories/service_broker_repository.go +++ b/api/repositories/service_broker_repository.go @@ -11,7 +11,7 @@ import ( "code.cloudfoundry.org/korifi/model" "code.cloudfoundry.org/korifi/model/services" "code.cloudfoundry.org/korifi/tools" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" @@ -189,8 +189,9 @@ func (r *ServiceBrokerRepo) ListServiceBrokers(ctx context.Context, authInfo aut return nil, fmt.Errorf("failed to list brokers: %w", apierrors.FromK8sError(err, ServiceBrokerResourceType)) } - brokers := iter.Lift(brokersList.Items).Filter(message.matches) - return iter.Map(brokers, toServiceBrokerRecord).Collect(), nil + brokers := itx.FromSlice(brokersList.Items).Filter(message.matches) + + return itx.Map(brokers, toServiceBrokerRecord).Collect(), 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 180511490..41f823505 100644 --- a/api/repositories/service_instance_repository.go +++ b/api/repositories/service_instance_repository.go @@ -12,7 +12,7 @@ import ( "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -267,8 +267,8 @@ func (r *ServiceInstanceRepo) ListServiceInstances(ctx context.Context, authInfo serviceInstances = append(serviceInstances, serviceInstanceList.Items...) } - filteredServiceInstances := iter.Lift(serviceInstances).Filter(message.matches) - return iter.Map(filteredServiceInstances, cfServiceInstanceToRecord).Collect(), nil + filteredServiceInstances := itx.FromSlice(serviceInstances).Filter(message.matches) + return itx.Map(filteredServiceInstances, cfServiceInstanceToRecord).Collect(), nil } func (r *ServiceInstanceRepo) GetServiceInstance(ctx context.Context, authInfo authorization.Info, guid string) (ServiceInstanceRecord, error) { diff --git a/api/repositories/service_offering_repository.go b/api/repositories/service_offering_repository.go index 848775caa..048816c00 100644 --- a/api/repositories/service_offering_repository.go +++ b/api/repositories/service_offering_repository.go @@ -11,7 +11,8 @@ import ( "code.cloudfoundry.org/korifi/model" "code.cloudfoundry.org/korifi/model/services" "code.cloudfoundry.org/korifi/tools" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it" + "github.com/BooleanCat/go-functional/v2/it/itx" k8serrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -81,7 +82,7 @@ func (r *ServiceOfferingRepo) ListOfferings(ctx context.Context, authInfo author ) } - filteredByName := iter.Map(iter.Lift(offeringsList.Items).Filter(message.matchesName), offeringToRecord).Collect() + filteredByName := slices.Collect(it.Map(itx.FromSlice(offeringsList.Items).Filter(message.matchesName), offeringToRecord)) filteredByBroker := []ServiceOfferingRecord{} for _, offering := range filteredByName { diff --git a/api/repositories/service_plan_repository.go b/api/repositories/service_plan_repository.go index 691ce2133..872de1c61 100644 --- a/api/repositories/service_plan_repository.go +++ b/api/repositories/service_plan_repository.go @@ -10,7 +10,8 @@ import ( "code.cloudfoundry.org/korifi/model" "code.cloudfoundry.org/korifi/model/services" "code.cloudfoundry.org/korifi/tools" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it" + "github.com/BooleanCat/go-functional/v2/it/itx" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -118,7 +119,7 @@ func (r *ServicePlanRepo) ListPlans(ctx context.Context, authInfo authorization. return nil, apierrors.FromK8sError(err, ServicePlanResourceType) } - filteredPlans := iter.Lift(cfServicePlans.Items).Filter(message.matches).Collect() + filteredPlans := itx.FromSlice(cfServicePlans.Items).Filter(message.matches).Collect() planRecords := []ServicePlanRecord{} for _, plan := range filteredPlans { @@ -224,10 +225,10 @@ func (r *ServicePlanRepo) toVisibilityOrganizations(ctx context.Context, authInf return nil, fmt.Errorf("failed to list orgs for plan visibility: %w", err) } - return iter.Map(iter.Lift(orgs), func(o OrgRecord) services.VisibilityOrganization { + return slices.Collect(it.Map(slices.Values(orgs), func(o OrgRecord) services.VisibilityOrganization { return services.VisibilityOrganization{ GUID: o.GUID, Name: o.Name, } - }).Collect(), nil + })), nil } diff --git a/api/repositories/shared.go b/api/repositories/shared.go index d4cc9bd59..e319f0574 100644 --- a/api/repositories/shared.go +++ b/api/repositories/shared.go @@ -3,11 +3,12 @@ package repositories import ( "context" "fmt" + "maps" + "slices" "time" "code.cloudfoundry.org/korifi/api/authorization" - "github.com/BooleanCat/go-functional/iter" - "golang.org/x/exp/maps" + "github.com/BooleanCat/go-functional/v2/it/itx" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -71,20 +72,28 @@ func getLabelOrAnnotation(mapObj map[string]string, key string) string { return mapObj[key] } -func authorizedSpaceNamespaces(ctx context.Context, authInfo authorization.Info, namespacePermissions *authorization.NamespacePermissions) (*iter.LiftIter[string], error) { +func emptyOrContains[S ~[]E, E comparable](elements S, e E) bool { + if len(elements) == 0 { + return true + } + + return slices.Contains(elements, e) +} + +func authorizedSpaceNamespaces(ctx context.Context, authInfo authorization.Info, namespacePermissions *authorization.NamespacePermissions) (itx.Iterator[string], error) { nsList, err := namespacePermissions.GetAuthorizedSpaceNamespaces(ctx, authInfo) if err != nil { return nil, fmt.Errorf("failed to list namespaces for spaces with user role bindings: %w", err) } - return iter.Lift(maps.Keys(nsList)), nil + return itx.From(maps.Keys(nsList)), nil } -func authorizedOrgNamespaces(ctx context.Context, authInfo authorization.Info, namespacePermissions *authorization.NamespacePermissions) (*iter.LiftIter[string], error) { +func authorizedOrgNamespaces(ctx context.Context, authInfo authorization.Info, namespacePermissions *authorization.NamespacePermissions) (itx.Iterator[string], error) { nsList, err := namespacePermissions.GetAuthorizedOrgNamespaces(ctx, authInfo) if err != nil { return nil, fmt.Errorf("failed to list namespaces for orgs with user role bindings: %w", err) } - return iter.Lift(maps.Keys(nsList)), nil + return itx.From(maps.Keys(nsList)), nil } diff --git a/api/repositories/space_repository.go b/api/repositories/space_repository.go index a5a615be7..1600a7366 100644 --- a/api/repositories/space_repository.go +++ b/api/repositories/space_repository.go @@ -11,7 +11,7 @@ import ( "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -155,11 +155,11 @@ func (r *SpaceRepo) ListSpaces(ctx context.Context, authInfo authorization.Info, cfSpaces = append(cfSpaces, cfSpaceList.Items...) } - filteredSpaces := iter.Lift(cfSpaces).Filter(func(s korifiv1alpha1.CFSpace) bool { + filteredSpaces := itx.FromSlice(cfSpaces).Filter(func(s korifiv1alpha1.CFSpace) bool { return authorizedSpaceNamespaces[s.Name] && message.matches(s) }) - return iter.Map(filteredSpaces, cfSpaceToSpaceRecord).Collect(), nil + return itx.Map(filteredSpaces, cfSpaceToSpaceRecord).Collect(), 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 8286e6736..1cc8dc0d1 100644 --- a/api/repositories/task_repository.go +++ b/api/repositories/task_repository.go @@ -11,7 +11,7 @@ import ( "code.cloudfoundry.org/korifi/controllers/controllers/workloads/tasks" "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/google/uuid" v1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -191,8 +191,8 @@ func (r *TaskRepo) ListTasks(ctx context.Context, authInfo authorization.Info, m tasks = append(tasks, taskList.Items...) } - filteredTasks := iter.Lift(tasks).Filter(msg.matches) - return iter.Map(filteredTasks, taskToRecord).Collect(), nil + filteredTasks := itx.FromSlice(tasks).Filter(msg.matches) + return itx.Map(filteredTasks, taskToRecord).Collect(), nil } func (r *TaskRepo) CancelTask(ctx context.Context, authInfo authorization.Info, taskGUID string) (TaskRecord, error) { diff --git a/go.mod b/go.mod index f836b48e9..2802bf224 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,11 @@ module code.cloudfoundry.org/korifi -go 1.22.4 - -toolchain go1.22.5 +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 v1.1.0 + github.com/BooleanCat/go-functional/v2 v2.0.0-beta.3 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 b24909576..a5bda0b4a 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 v1.1.0 h1:ZU8ejc2u/71I5LZbI5qiJI75Ttw1FueLNmoccPt8nDI= -github.com/BooleanCat/go-functional v1.1.0/go.mod h1:Zd1xLrGFohrDdjojLUCrzSex40yf/PVP2KB86ha9Qqg= +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/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= diff --git a/tests/e2e/service_plans_test.go b/tests/e2e/service_plans_test.go index 7f6bf0f66..981c5af79 100644 --- a/tests/e2e/service_plans_test.go +++ b/tests/e2e/service_plans_test.go @@ -5,7 +5,7 @@ import ( "net/http" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/go-resty/resty/v2" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -57,7 +57,7 @@ var _ = Describe("Service Plans", func() { Expect(err).NotTo(HaveOccurred()) Expect(listResp).To(HaveRestyStatusCode(http.StatusOK)) - brokerPlans := iter.Lift(plans.Resources).Filter(func(r resource) bool { + brokerPlans := itx.FromSlice(plans.Resources).Filter(func(r resource) bool { return r.Metadata.Labels[korifiv1alpha1.RelServiceBrokerGUIDLabel] == brokerGUID }).Collect() From b6cc4ded4668d43f7d28c9145f3c64c3ce665d79 Mon Sep 17 00:00:00 2001 From: Tom Godkin Date: Sat, 20 Jul 2024 12:42:28 +0100 Subject: [PATCH 2/7] Stop using exp maps package --- api/actions/manifest/applier.go | 5 +++-- api/actions/manifest/state_collector.go | 5 +++-- controllers/controllers/workloads/k8sns/reconciler_test.go | 5 +++-- controllers/controllers/workloads/spaces/controller_test.go | 5 +++-- go.mod | 4 ++-- scripts/helmdoc/main.go | 5 +++-- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/api/actions/manifest/applier.go b/api/actions/manifest/applier.go index c85f80654..6ddda9906 100644 --- a/api/actions/manifest/applier.go +++ b/api/actions/manifest/applier.go @@ -3,6 +3,8 @@ package manifest import ( "context" "fmt" + "maps" + "slices" "strings" "code.cloudfoundry.org/korifi/api/actions/shared" @@ -12,7 +14,6 @@ import ( "code.cloudfoundry.org/korifi/api/repositories" "code.cloudfoundry.org/korifi/api/tools/singleton" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "golang.org/x/exp/maps" ) type Applier struct { @@ -218,7 +219,7 @@ func (a *Applier) applyServices(ctx context.Context, authInfo authorization.Info } serviceInstances, err := a.serviceInstanceRepo.ListServiceInstances(ctx, authInfo, repositories.ListServiceInstanceMessage{ - Names: maps.Keys(desiredServiceNames), + Names: slices.Collect(maps.Keys(desiredServiceNames)), }) if err != nil { return err diff --git a/api/actions/manifest/state_collector.go b/api/actions/manifest/state_collector.go index 36006cd42..94ad51867 100644 --- a/api/actions/manifest/state_collector.go +++ b/api/actions/manifest/state_collector.go @@ -4,14 +4,15 @@ import ( "context" "errors" "fmt" + "maps" "path" + "slices" "code.cloudfoundry.org/korifi/api/actions/shared" "code.cloudfoundry.org/korifi/api/authorization" apierrors "code.cloudfoundry.org/korifi/api/errors" "code.cloudfoundry.org/korifi/api/repositories" "code.cloudfoundry.org/korifi/api/tools/singleton" - "golang.org/x/exp/maps" ) type StateCollector struct { @@ -131,7 +132,7 @@ func (s StateCollector) collectServiceBindings(ctx context.Context, authInfo aut serviceInstanceGUIDSet[sb.ServiceInstanceGUID] = true } - services, err := s.serviceInstanceRepo.ListServiceInstances(ctx, authInfo, repositories.ListServiceInstanceMessage{GUIDs: maps.Keys(serviceInstanceGUIDSet)}) + services, err := s.serviceInstanceRepo.ListServiceInstances(ctx, authInfo, repositories.ListServiceInstanceMessage{GUIDs: slices.Collect(maps.Keys(serviceInstanceGUIDSet))}) if err != nil { return nil, err } diff --git a/controllers/controllers/workloads/k8sns/reconciler_test.go b/controllers/controllers/workloads/k8sns/reconciler_test.go index 37d3585c1..ced4bf142 100644 --- a/controllers/controllers/workloads/k8sns/reconciler_test.go +++ b/controllers/controllers/workloads/k8sns/reconciler_test.go @@ -4,12 +4,13 @@ import ( "context" "errors" "fmt" + "maps" + "slices" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "code.cloudfoundry.org/korifi/controllers/controllers/workloads/k8sns" "code.cloudfoundry.org/korifi/tools" "code.cloudfoundry.org/korifi/tools/k8s" - "golang.org/x/exp/maps" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -165,7 +166,7 @@ var _ = Describe("K8S NS Reconciler Integration Tests", func() { Expect(createdSecret.Type).To(Equal(imageRegistrySecret.Type)) By("omitting annotations from deployment tools", func() { - Expect(maps.Keys(createdSecret.Annotations)).NotTo(ContainElements("kapp.k14s.io/foo", "meta.helm.sh/baz")) + Expect(slices.Collect(maps.Keys(createdSecret.Annotations))).NotTo(ContainElements("kapp.k14s.io/foo", "meta.helm.sh/baz")) }) }) diff --git a/controllers/controllers/workloads/spaces/controller_test.go b/controllers/controllers/workloads/spaces/controller_test.go index ea83a6f97..6af14d667 100644 --- a/controllers/controllers/workloads/spaces/controller_test.go +++ b/controllers/controllers/workloads/spaces/controller_test.go @@ -1,6 +1,8 @@ package spaces_test import ( + "maps" + "slices" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" @@ -10,7 +12,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" . "github.com/onsi/gomega/gstruct" - "golang.org/x/exp/maps" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -143,7 +144,7 @@ var _ = Describe("CFSpaceReconciler Integration Tests", func() { }).Should(Succeed()) By("omitting annotations from deployment tools", func() { - Expect(maps.Keys(createdServiceAccount.Annotations)).To(ConsistOf("cloudfoundry.org/propagate-service-account")) + Expect(slices.Collect(maps.Keys(createdServiceAccount.Annotations))).To(ConsistOf("cloudfoundry.org/propagate-service-account")) Expect(createdServiceAccount.Annotations["cloudfoundry.org/propagate-service-account"]).To(Equal("true")) }) }) diff --git a/go.mod b/go.mod index 2802bf224..c307843e2 100644 --- a/go.mod +++ b/go.mod @@ -31,8 +31,7 @@ require ( github.com/pivotal/kpack v0.15.0 github.com/satori/go.uuid v1.2.0 github.com/servicebinding/runtime v1.0.0 - golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 - golang.org/x/text v0.17.0 + golang.org/x/text v0.16.0 gopkg.in/square/go-jose.v2 v2.6.0 gopkg.in/yaml.v3 v3.0.1 k8s.io/api v0.30.3 @@ -75,6 +74,7 @@ require ( go.opentelemetry.io/otel/sdk/metric v1.28.0 // indirect go.opentelemetry.io/otel/trace v1.28.0 // indirect go.opentelemetry.io/proto/otlp v1.0.0 // indirect + golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7 // indirect reconciler.io/runtime v0.20.0 // indirect ) diff --git a/scripts/helmdoc/main.go b/scripts/helmdoc/main.go index 10f93bcd4..fc9590f4d 100644 --- a/scripts/helmdoc/main.go +++ b/scripts/helmdoc/main.go @@ -3,18 +3,19 @@ package main import ( "encoding/json" "fmt" + "maps" "os" + "slices" "sort" "strings" - "golang.org/x/exp/maps" "golang.org/x/text/cases" "golang.org/x/text/language" ) func printDocForSchema(schema map[string]any, indentLevel int) { indentStr := strings.Repeat(" ", indentLevel) - names := maps.Keys(schema) + names := slices.Collect(maps.Keys(schema)) sort.Slice(names, func(a, b int) bool { if names[a] == "global" { return true From 38b4d0bee0fbb17cc17c9c398844c1ddc1fda2e6 Mon Sep 17 00:00:00 2001 From: Tom Godkin Date: Tue, 6 Aug 2024 20:19:33 +0100 Subject: [PATCH 3/7] Upgrade go-functional to v2.0.0-beta.5 --- api/repositories/domain_repository.go | 6 ++++-- api/repositories/droplet_repository.go | 4 +++- api/repositories/metrics_repository.go | 6 +++--- api/repositories/org_repository.go | 4 +++- api/repositories/package_repository.go | 3 ++- api/repositories/pod_repository.go | 11 ++++++----- api/repositories/process_repository.go | 4 +++- api/repositories/role_repository.go | 7 ++++--- api/repositories/route_repository.go | 11 ++++++----- api/repositories/service_binding_repository.go | 4 +++- api/repositories/service_broker_repository.go | 4 +++- api/repositories/service_instance_repository.go | 4 +++- api/repositories/service_plan_repository.go | 1 + api/repositories/space_repository.go | 4 +++- api/repositories/task_repository.go | 4 +++- go.mod | 2 +- go.sum | 4 ++-- 17 files changed, 53 insertions(+), 30 deletions(-) diff --git a/api/repositories/domain_repository.go b/api/repositories/domain_repository.go index a762086a6..a28857913 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" @@ -11,6 +12,7 @@ import ( korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "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" @@ -163,10 +165,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 919dd168f..ff7a847b0 100644 --- a/api/repositories/droplet_repository.go +++ b/api/repositories/droplet_repository.go @@ -3,10 +3,12 @@ package repositories import ( "context" "fmt" + "slices" "time" "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" "code.cloudfoundry.org/korifi/api/authorization" @@ -171,7 +173,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 e810d248b..d8a79c314 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" @@ -11,6 +12,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" "k8s.io/apimachinery/pkg/api/meta" @@ -136,7 +138,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 982f09c6a..c8cf48f3d 100644 --- a/api/repositories/package_repository.go +++ b/api/repositories/package_repository.go @@ -14,6 +14,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" @@ -336,7 +337,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 9d2b900c2..7dc6d207f 100644 --- a/api/repositories/process_repository.go +++ b/api/repositories/process_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" "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" @@ -167,7 +169,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 645369f8a..483d31cd4 100644 --- a/api/repositories/route_repository.go +++ b/api/repositories/route_repository.go @@ -12,6 +12,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" v1 "k8s.io/api/core/v1" @@ -209,7 +210,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 { @@ -232,7 +233,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, @@ -250,7 +251,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) { @@ -435,7 +436,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, @@ -445,7 +446,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 ecbc166af..fbdfe8f29 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" @@ -16,6 +17,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" @@ -293,5 +295,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 c995069fc..640a6b00b 100644 --- a/api/repositories/service_broker_repository.go +++ b/api/repositories/service_broker_repository.go @@ -3,6 +3,7 @@ package repositories import ( "context" "fmt" + "slices" "time" "code.cloudfoundry.org/korifi/api/authorization" @@ -11,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" @@ -191,7 +193,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 41f823505..ef82673e7 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/service_plan_repository.go b/api/repositories/service_plan_repository.go index 872de1c61..9b78bfa2e 100644 --- a/api/repositories/service_plan_repository.go +++ b/api/repositories/service_plan_repository.go @@ -3,6 +3,7 @@ package repositories import ( "context" "fmt" + "slices" "code.cloudfoundry.org/korifi/api/authorization" apierrors "code.cloudfoundry.org/korifi/api/errors" diff --git a/api/repositories/space_repository.go b/api/repositories/space_repository.go index 1600a7366..7bd3032a2 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" @@ -11,6 +12,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" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -159,7 +161,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 1cc8dc0d1..00c50827c 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" @@ -11,6 +12,7 @@ import ( "code.cloudfoundry.org/korifi/controllers/controllers/workloads/tasks" "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" v1 "k8s.io/api/core/v1" @@ -192,7 +194,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 c307843e2..582ffbf35 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 a5bda0b4a..3a95cd15c 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= From b046484eca2042ad40818f3a48e0e6607169b32e Mon Sep 17 00:00:00 2001 From: Danail Branekov Date: Wed, 14 Aug 2024 11:04:20 +0000 Subject: [PATCH 4/7] Bump golang to 1.23 --- go.mod | 5 +++-- go.sum | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 582ffbf35..3bec33be9 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,11 @@ module code.cloudfoundry.org/korifi -go 1.23 +go 1.23.0 require ( code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5 code.cloudfoundry.org/go-loggregator/v8 v8.0.5 + github.com/BooleanCat/go-functional v1.1.0 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 @@ -31,7 +32,7 @@ require ( github.com/pivotal/kpack v0.15.0 github.com/satori/go.uuid v1.2.0 github.com/servicebinding/runtime v1.0.0 - golang.org/x/text v0.16.0 + golang.org/x/text v0.17.0 gopkg.in/square/go-jose.v2 v2.6.0 gopkg.in/yaml.v3 v3.0.1 k8s.io/api v0.30.3 diff --git a/go.sum b/go.sum index 3a95cd15c..d08912b08 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +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 v1.1.0 h1:ZU8ejc2u/71I5LZbI5qiJI75Ttw1FueLNmoccPt8nDI= +github.com/BooleanCat/go-functional v1.1.0/go.mod h1:Zd1xLrGFohrDdjojLUCrzSex40yf/PVP2KB86ha9Qqg= 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= From d09672c2b323963801faa012b4ffe95060cb3624 Mon Sep 17 00:00:00 2001 From: Danail Branekov Date: Thu, 15 Aug 2024 09:58:23 +0000 Subject: [PATCH 5/7] Replace remaining go-functional v1 usages with v2 --- api/handlers/include/resolver.go | 12 +++++++----- api/handlers/service_offering.go | 12 +++++++----- api/payloads/service_plan.go | 2 +- api/presenter/service_binding.go | 8 +++++--- api/repositories/relationships/repository.go | 12 +++++++----- api/repositories/shared.go | 9 --------- go.mod | 1 - go.sum | 2 -- 8 files changed, 27 insertions(+), 31 deletions(-) diff --git a/api/handlers/include/resolver.go b/api/handlers/include/resolver.go index a1b067858..3470aefeb 100644 --- a/api/handlers/include/resolver.go +++ b/api/handlers/include/resolver.go @@ -2,12 +2,14 @@ package include import ( "context" + "slices" "code.cloudfoundry.org/korifi/api/authorization" "code.cloudfoundry.org/korifi/api/payloads/params" "code.cloudfoundry.org/korifi/api/repositories/relationships" "code.cloudfoundry.org/korifi/model" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it" + "github.com/BooleanCat/go-functional/v2/it/itx" ) //counterfeiter:generate -o fake -fake-name ResourceRelationshipRepository . ResourceRelationshipRepository @@ -35,9 +37,9 @@ func (h *IncludeResolver[S, E]) ResolveIncludes( ) ([]model.IncludedResource, error) { includes := []model.IncludedResource{} - repoResources := iter.Map(iter.Lift(resources), func(e E) relationships.Resource { + repoResources := slices.Collect(it.Map(itx.FromSlice(resources), func(e E) relationships.Resource { return e - }).Collect() + })) for _, includeResourceRule := range includeResourceRules { includedResources, err := h.resolveInclude(ctx, authInfo, repoResources, includeResourceRule.RelationshipPath) @@ -71,12 +73,12 @@ func (h *IncludeResolver[S, E]) resolveInclude( return nil, err } - includedResources = iter.Map(iter.Lift(resources), func(r relationships.Resource) model.IncludedResource { + includedResources = slices.Collect(it.Map(itx.FromSlice(resources), func(r relationships.Resource) model.IncludedResource { return model.IncludedResource{ Type: plural(relatedResourceType), Resource: r, } - }).Collect() + })) } return includedResources, nil diff --git a/api/handlers/service_offering.go b/api/handlers/service_offering.go index 1b94646c1..4e5af124d 100644 --- a/api/handlers/service_offering.go +++ b/api/handlers/service_offering.go @@ -5,6 +5,7 @@ import ( "context" "net/http" "net/url" + "slices" "code.cloudfoundry.org/korifi/api/authorization" apierrors "code.cloudfoundry.org/korifi/api/errors" @@ -14,7 +15,8 @@ import ( "code.cloudfoundry.org/korifi/api/routing" "code.cloudfoundry.org/korifi/model" "code.cloudfoundry.org/korifi/tools" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it" + "github.com/BooleanCat/go-functional/v2/it/itx" "github.com/go-logr/logr" ) @@ -75,9 +77,9 @@ func (h *ServiceOffering) listBrokersForOfferings( authInfo authorization.Info, serviceOfferings []repositories.ServiceOfferingRecord, ) ([]repositories.ServiceBrokerRecord, error) { - brokerGUIDs := iter.Map(iter.Lift(serviceOfferings), func(o repositories.ServiceOfferingRecord) string { + brokerGUIDs := slices.Collect(it.Map(itx.FromSlice(serviceOfferings), func(o repositories.ServiceOfferingRecord) string { return o.ServiceBrokerGUID - }).Collect() + })) return h.serviceBrokerRepo.ListServiceBrokers(ctx, authInfo, repositories.ListServiceBrokerMessage{ GUIDs: tools.Uniq(brokerGUIDs), @@ -100,12 +102,12 @@ func (h *ServiceOffering) getBrokerIncludes( return nil, err } - brokerIncludes := iter.Map(iter.Lift(brokers), func(b repositories.ServiceBrokerRecord) model.IncludedResource { + brokerIncludes := slices.Collect(it.Map(itx.FromSlice(brokers), func(b repositories.ServiceBrokerRecord) model.IncludedResource { return model.IncludedResource{ Type: "service_brokers", Resource: presenter.ForServiceBroker(b, baseURL), } - }).Collect() + })) brokerIncludesFielded := []model.IncludedResource{} for _, brokerInclude := range brokerIncludes { diff --git a/api/payloads/service_plan.go b/api/payloads/service_plan.go index aad8db3ae..8d2594108 100644 --- a/api/payloads/service_plan.go +++ b/api/payloads/service_plan.go @@ -4,8 +4,8 @@ import ( "fmt" "net/url" "regexp" - "strconv" "slices" + "strconv" "code.cloudfoundry.org/korifi/api/payloads/params" "code.cloudfoundry.org/korifi/api/payloads/parse" diff --git a/api/presenter/service_binding.go b/api/presenter/service_binding.go index 46ad4a328..fb8cd15e2 100644 --- a/api/presenter/service_binding.go +++ b/api/presenter/service_binding.go @@ -2,10 +2,12 @@ package presenter import ( "net/url" + "slices" "code.cloudfoundry.org/korifi/api/repositories" "code.cloudfoundry.org/korifi/model" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it" + "github.com/BooleanCat/go-functional/v2/it/itx" ) const ( @@ -79,12 +81,12 @@ func ForServiceBinding(record repositories.ServiceBindingRecord, baseURL url.URL } func ForServiceBindingList(serviceBindingRecords []repositories.ServiceBindingRecord, appRecords []repositories.AppRecord, baseURL, requestURL url.URL) ListResponse[ServiceBindingResponse] { - includedApps := iter.Map(iter.Lift(appRecords), func(app repositories.AppRecord) model.IncludedResource { + includedApps := slices.Collect(it.Map(itx.FromSlice(appRecords), func(app repositories.AppRecord) model.IncludedResource { return model.IncludedResource{ Type: "apps", Resource: ForApp(app, baseURL), } - }).Collect() + })) return ForList(ForServiceBinding, serviceBindingRecords, baseURL, requestURL, includedApps...) } diff --git a/api/repositories/relationships/repository.go b/api/repositories/relationships/repository.go index 6820be41a..e5ac1146d 100644 --- a/api/repositories/relationships/repository.go +++ b/api/repositories/relationships/repository.go @@ -3,11 +3,13 @@ package relationships import ( "context" "fmt" + "slices" "code.cloudfoundry.org/korifi/api/authorization" "code.cloudfoundry.org/korifi/api/repositories" "code.cloudfoundry.org/korifi/model" - "github.com/BooleanCat/go-functional/iter" + "github.com/BooleanCat/go-functional/v2/it" + "github.com/BooleanCat/go-functional/v2/it/itx" ) //counterfeiter:generate -o fake -fake-name ServiceOfferingRepository . ServiceOfferingRepository @@ -41,9 +43,9 @@ func NewResourseRelationshipsRepo( } func (r *ResourceRelationshipsRepo) ListRelatedResources(ctx context.Context, authInfo authorization.Info, relatedResourceType string, resources []Resource) ([]Resource, error) { - relatedResourceGUIDs := iter.Map(iter.Lift(resources), func(r Resource) string { + relatedResourceGUIDs := slices.Collect(it.Map(itx.FromSlice(resources), func(r Resource) string { return r.Relationships()[relatedResourceType].Data.GUID - }).Collect() + })) switch relatedResourceType { case "service_offering": @@ -65,7 +67,7 @@ func (r *ResourceRelationshipsRepo) ListRelatedResources(ctx context.Context, au } func asResources[S ~[]E, E Resource](resources S, err error) ([]Resource, error) { - return iter.Map(iter.Lift(resources), func(o E) Resource { + return slices.Collect(it.Map(itx.FromSlice(resources), func(o E) Resource { return o - }).Collect(), err + })), err } diff --git a/api/repositories/shared.go b/api/repositories/shared.go index e319f0574..679284985 100644 --- a/api/repositories/shared.go +++ b/api/repositories/shared.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "maps" - "slices" "time" "code.cloudfoundry.org/korifi/api/authorization" @@ -72,14 +71,6 @@ func getLabelOrAnnotation(mapObj map[string]string, key string) string { return mapObj[key] } -func emptyOrContains[S ~[]E, E comparable](elements S, e E) bool { - if len(elements) == 0 { - return true - } - - return slices.Contains(elements, e) -} - func authorizedSpaceNamespaces(ctx context.Context, authInfo authorization.Info, namespacePermissions *authorization.NamespacePermissions) (itx.Iterator[string], error) { nsList, err := namespacePermissions.GetAuthorizedSpaceNamespaces(ctx, authInfo) if err != nil { diff --git a/go.mod b/go.mod index 3bec33be9..71bcb41ea 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.23.0 require ( code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5 code.cloudfoundry.org/go-loggregator/v8 v8.0.5 - github.com/BooleanCat/go-functional v1.1.0 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 diff --git a/go.sum b/go.sum index d08912b08..3a95cd15c 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,6 @@ 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 v1.1.0 h1:ZU8ejc2u/71I5LZbI5qiJI75Ttw1FueLNmoccPt8nDI= -github.com/BooleanCat/go-functional v1.1.0/go.mod h1:Zd1xLrGFohrDdjojLUCrzSex40yf/PVP2KB86ha9Qqg= 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= From d3ac8fbe7e579b8b9fe2dfb87d3157af3928fc99 Mon Sep 17 00:00:00 2001 From: Danail Branekov Date: Thu, 15 Aug 2024 10:28:49 +0000 Subject: [PATCH 6/7] Remove unnecessary assertions on empty result By switching to go-functional v2 and adopting golang 1.23 iterators, collecting an empty iterator results into `nil` rather than en empty slice. This makes no difference to Korifi, therefore this commit removes explicit checks for empty non-nil slices in tests --- api/handlers/service_plan_test.go | 10 ++++------ api/repositories/process_repository_test.go | 1 - 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/api/handlers/service_plan_test.go b/api/handlers/service_plan_test.go index 7de1a1dcf..849034fc1 100644 --- a/api/handlers/service_plan_test.go +++ b/api/handlers/service_plan_test.go @@ -253,9 +253,8 @@ var _ = Describe("ServicePlan", func() { _, actualAuthInfo, actualMessage := servicePlanRepo.ApplyPlanVisibilityArgsForCall(0) Expect(actualAuthInfo).To(Equal(authInfo)) Expect(actualMessage).To(Equal(repositories.ApplyServicePlanVisibilityMessage{ - PlanGUID: "my-service-plan", - Type: korifiv1alpha1.PublicServicePlanVisibilityType, - Organizations: []string{}, + PlanGUID: "my-service-plan", + Type: korifiv1alpha1.PublicServicePlanVisibilityType, })) Expect(rr).To(HaveHTTPStatus(http.StatusOK)) @@ -318,9 +317,8 @@ var _ = Describe("ServicePlan", func() { _, actualAuthInfo, actualMessage := servicePlanRepo.UpdatePlanVisibilityArgsForCall(0) Expect(actualAuthInfo).To(Equal(authInfo)) Expect(actualMessage).To(Equal(repositories.UpdateServicePlanVisibilityMessage{ - PlanGUID: "my-service-plan", - Type: korifiv1alpha1.PublicServicePlanVisibilityType, - Organizations: []string{}, + PlanGUID: "my-service-plan", + Type: korifiv1alpha1.PublicServicePlanVisibilityType, })) Expect(rr).To(HaveHTTPStatus(http.StatusOK)) diff --git a/api/repositories/process_repository_test.go b/api/repositories/process_repository_test.go index bc9cc8336..68579af9a 100644 --- a/api/repositories/process_repository_test.go +++ b/api/repositories/process_repository_test.go @@ -195,7 +195,6 @@ var _ = Describe("ProcessRepo", func() { It("returns an empty list", func() { Expect(processes).To(BeEmpty()) - Expect(processes).ToNot(BeNil()) }) }) From a4a20011773dc8150e5db4e304a1053ed5b49585 Mon Sep 17 00:00:00 2001 From: Tom Godkin Date: Thu, 15 Aug 2024 11:43:05 +0100 Subject: [PATCH 7/7] Upgrade go-functional to v2.0.0-beta.6 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 71bcb41ea..4cd2d5c1b 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.0 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.5 + github.com/BooleanCat/go-functional/v2 v2.0.0-beta.6 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 3a95cd15c..fb2a76c12 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.5 h1:/AbWbCabFMF3sDWK+YrUBs2W2g8LcjvmzB7f7an4E6U= -github.com/BooleanCat/go-functional/v2 v2.0.0-beta.5/go.mod h1:IpUUAXAc9CiWDb+YDXkJyyUhtOVqDtyICDRg/de1IaQ= +github.com/BooleanCat/go-functional/v2 v2.0.0-beta.6 h1:oWriTGder5N8yraSGLrsSwjoulEWeKG0XR9RGZklQsw= +github.com/BooleanCat/go-functional/v2 v2.0.0-beta.6/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=