Skip to content

Commit

Permalink
some example of using streams
Browse files Browse the repository at this point in the history
Signed-off-by: Nahshon Unna-Tsameret <[email protected]>
  • Loading branch information
nunnatsa committed Jan 2, 2025
1 parent 0c0ce3b commit dede474
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion controllers/operands/cdi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package operands

import (
"context"
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream"
"maps"
"time"

Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion controllers/operands/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package operands
import (
"context"
"fmt"
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/stream"
"maps"
"os"
"path"
Expand All @@ -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"
)

Expand Down
10 changes: 6 additions & 4 deletions controllers/operands/imageStream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package operands
import (
"errors"
"fmt"
"maps"
"os"
"path/filepath"
"reflect"
"slices"
"strings"

log "github.com/go-logr/logr"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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}
}

Expand Down
15 changes: 9 additions & 6 deletions controllers/operands/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 3 additions & 10 deletions controllers/operands/operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strings"

jsonpatch "github.com/evanphx/json-patch/v5"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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))
}
3 changes: 2 additions & 1 deletion pkg/stream/stream_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package stream

import (
"github.com/onsi/gomega"
"maps"
"slices"
"strconv"
"testing"

"github.com/onsi/gomega"
)

func TestTransform(t *testing.T) {
Expand Down
30 changes: 15 additions & 15 deletions tools/csv-merger/csv-merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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, ",")
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down
7 changes: 3 additions & 4 deletions tools/manifest-templator/manifest-templator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ package main
import (
"flag"
"log"
"maps"
"os"
"path"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit dede474

Please sign in to comment.