From dede4749ae5bb2cadc02657273f0a406b29bb35d Mon Sep 17 00:00:00 2001 From: Nahshon Unna-Tsameret Date: Thu, 2 Jan 2025 11:57:33 +0200 Subject: [PATCH] some example of using streams Signed-off-by: Nahshon Unna-Tsameret --- controllers/operands/cdi_test.go | 2 +- controllers/operands/dashboard_test.go | 2 +- controllers/operands/imageStream.go | 10 ++++--- controllers/operands/kubevirt.go | 15 ++++++---- controllers/operands/operand.go | 13 ++------ pkg/stream/stream_test.go | 3 +- tools/csv-merger/csv-merger.go | 30 +++++++++---------- .../manifest-templator/manifest-templator.go | 7 ++--- 8 files changed, 40 insertions(+), 42 deletions(-) diff --git a/controllers/operands/cdi_test.go b/controllers/operands/cdi_test.go index c30f8c794..4255c181d 100644 --- a/controllers/operands/cdi_test.go +++ b/controllers/operands/cdi_test.go @@ -2,7 +2,6 @@ package operands import ( "context" - "github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream" "maps" "time" @@ -20,6 +19,7 @@ import ( "k8s.io/client-go/tools/reference" "k8s.io/utils/ptr" + "github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream" cdiv1beta1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" diff --git a/controllers/operands/dashboard_test.go b/controllers/operands/dashboard_test.go index 69cb6f071..380703d43 100644 --- a/controllers/operands/dashboard_test.go +++ b/controllers/operands/dashboard_test.go @@ -3,7 +3,6 @@ package operands import ( "context" "fmt" - "github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream" "maps" "os" "path" @@ -19,6 +18,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" "github.com/kubevirt/hyperconverged-cluster-operator/controllers/commontestutils" + "github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream" hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util" ) diff --git a/controllers/operands/imageStream.go b/controllers/operands/imageStream.go index 3961defd0..67e62b809 100644 --- a/controllers/operands/imageStream.go +++ b/controllers/operands/imageStream.go @@ -3,9 +3,11 @@ package operands import ( "errors" "fmt" + "maps" "os" "path/filepath" "reflect" + "slices" "strings" log "github.com/go-logr/logr" @@ -18,6 +20,7 @@ import ( hcov1beta1 "github.com/kubevirt/hyperconverged-cluster-operator/api/v1beta1" "github.com/kubevirt/hyperconverged-cluster-operator/controllers/common" + "github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream" "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util" ) @@ -138,10 +141,9 @@ type isHooks struct { } func newIsHook(required *imagev1.ImageStream, origNS string) *isHooks { - tags := make(map[string]imagev1.TagReference) - for _, tag := range required.Spec.Tags { - tags[tag.Name] = tag - } + tags := maps.Collect(stream.Transform22(slices.All(required.Spec.Tags), func(_ int, tag imagev1.TagReference) (string, imagev1.TagReference) { + return tag.Name, tag + })) return &isHooks{required: required, tags: tags, originalNS: origNS} } diff --git a/controllers/operands/kubevirt.go b/controllers/operands/kubevirt.go index 18519a542..4c99a6643 100644 --- a/controllers/operands/kubevirt.go +++ b/controllers/operands/kubevirt.go @@ -540,16 +540,19 @@ func getNetworkBindings(hcoNetworkBindings map[string]kubevirtcorev1.InterfaceBi } func getObsoleteCPUConfig(hcObsoleteCPUConf *hcov1beta1.HyperConvergedObsoleteCPUs) (map[string]bool, string) { - obsoleteCPUModels := make(map[string]bool) - for _, cpu := range hardcodedObsoleteCPUModels { - obsoleteCPUModels[cpu] = true + transformFunc := func(cpu string) (string, bool) { + return cpu, true } + + obsoleteCPUModels := maps.Collect(stream.Transform2(slices.Values(hardcodedObsoleteCPUModels), transformFunc)) + minCPUModel := "" if hcObsoleteCPUConf != nil { - for _, cpu := range hcObsoleteCPUConf.CPUModels { - obsoleteCPUModels[cpu] = true - } + maps.Insert( + obsoleteCPUModels, + stream.Transform2(slices.Values(hcObsoleteCPUConf.CPUModels), transformFunc), + ) minCPUModel = hcObsoleteCPUConf.MinCPUModel } diff --git a/controllers/operands/operand.go b/controllers/operands/operand.go index f3dcbbe13..90919b21f 100644 --- a/controllers/operands/operand.go +++ b/controllers/operands/operand.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "slices" "strings" jsonpatch "github.com/evanphx/json-patch/v5" @@ -18,6 +19,7 @@ import ( hcov1beta1 "github.com/kubevirt/hyperconverged-cluster-operator/api/v1beta1" "github.com/kubevirt/hyperconverged-cluster-operator/controllers/common" + "github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream" hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util" ) @@ -443,14 +445,5 @@ func osConditionToK8s(condition conditionsv1.Condition) metav1.Condition { } func osConditionsToK8s(conditions []conditionsv1.Condition) []metav1.Condition { - if len(conditions) == 0 { - return nil - } - - newCond := make([]metav1.Condition, len(conditions)) - for i, c := range conditions { - newCond[i] = osConditionToK8s(c) - } - - return newCond + return slices.Collect(stream.Transform(slices.Values(conditions), osConditionToK8s)) } diff --git a/pkg/stream/stream_test.go b/pkg/stream/stream_test.go index 2ad14b7df..a7f02e4b6 100644 --- a/pkg/stream/stream_test.go +++ b/pkg/stream/stream_test.go @@ -1,11 +1,12 @@ package stream import ( - "github.com/onsi/gomega" "maps" "slices" "strconv" "testing" + + "github.com/onsi/gomega" ) func TestTransform(t *testing.T) { diff --git a/tools/csv-merger/csv-merger.go b/tools/csv-merger/csv-merger.go index 0d78d4d48..9b107e4b1 100644 --- a/tools/csv-merger/csv-merger.go +++ b/tools/csv-merger/csv-merger.go @@ -42,6 +42,7 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "github.com/kubevirt/hyperconverged-cluster-operator/pkg/components" + "github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream" hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util" "github.com/kubevirt/hyperconverged-cluster-operator/tools/util" ) @@ -65,10 +66,13 @@ var ( type EnvVarFlags []corev1.EnvVar func (i *EnvVarFlags) String() string { - es := make([]string, 0) - for _, ev := range *i { - es = append(es, fmt.Sprintf("%s=%s", ev.Name, ev.Value)) - } + es := slices.Collect(stream.Transform( + slices.Values(*i), + func(ev corev1.EnvVar) string { + return fmt.Sprintf("%s=%s", ev.Name, ev.Value) + }, + )) + return strings.Join(es, ",") } @@ -136,16 +140,17 @@ var ( ) func IOReadDir(root string) ([]string, error) { - var files []string fileInfo, err := os.ReadDir(root) if err != nil { - return files, err + return nil, err } - for _, file := range fileInfo { - files = append(files, filepath.Join(root, file.Name())) - } - return files, nil + return slices.Collect(stream.Transform( + slices.Values(fileInfo), + func(file os.DirEntry) string { + return filepath.Join(root, file.Name()) + }, + )), nil } func validateNoAPIOverlap(crdDir string) error { @@ -182,11 +187,6 @@ func detectAPIOverlap(crdMap map[string][]string) map[string]sets.Set[string] { overlapsMap := make(map[string]sets.Set[string]) for operator, groups := range crdMap { for _, apiGroup := range groups { - // We work on replacement for current v2v. Remove this check when vmware import is removed - if apiGroup == "v2v.kubevirt.io" { - continue - } - compareMapWithEntry(crdMap, operator, apiGroup, overlapsMap) } } diff --git a/tools/manifest-templator/manifest-templator.go b/tools/manifest-templator/manifest-templator.go index 21c6ba92d..e53ddb350 100644 --- a/tools/manifest-templator/manifest-templator.go +++ b/tools/manifest-templator/manifest-templator.go @@ -22,8 +22,10 @@ package main import ( "flag" "log" + "maps" "os" "path" + "slices" "sort" "strconv" "strings" @@ -470,10 +472,7 @@ func writeOperatorDeploymentsAndServices(deployments []appsv1.Deployment, servic } func writeServiceAccounts(serviceAccounts map[string]v1.ServiceAccount) { - var keys []string - for saName := range serviceAccounts { - keys = append(keys, saName) - } + keys := slices.Collect(maps.Keys(serviceAccounts)) // since maps are not ordered we must enforce one before writing sort.Strings(keys)