Skip to content

Commit

Permalink
Initial introduction of slices package (istio#44981)
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn authored May 24, 2023
1 parent 601269c commit 9a9d1c8
Show file tree
Hide file tree
Showing 38 changed files with 213 additions and 94 deletions.
3 changes: 1 addition & 2 deletions cni/pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
"path/filepath"
"sync/atomic"

"golang.org/x/exp/slices"

"istio.io/istio/cni/pkg/config"
"istio.io/istio/cni/pkg/constants"
"istio.io/istio/cni/pkg/util"
"istio.io/istio/pkg/file"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/slices"
)

var installLog = log.RegisterScope("install", "CNI install")
Expand Down
7 changes: 2 additions & 5 deletions cni/pkg/plugin/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"istio.io/istio/pkg/kube"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/maps"
)

// newKubeClient is a unit test override variable for interface create.
Expand Down Expand Up @@ -102,11 +103,7 @@ func getK8sPodInfo(client *kubernetes.Clientset, podName, podNamespace string) (

func (pi PodInfo) String() string {
var b strings.Builder
icn := make([]string, 0, len(pi.InitContainers))
for n := range pi.InitContainers {
icn = append(icn, n)
}
b.WriteString(fmt.Sprintf(" Init Containers: %v\n", icn))
b.WriteString(fmt.Sprintf(" Init Containers: %v\n", maps.Keys(pi.InitContainers)))
b.WriteString(fmt.Sprintf(" Containers: %v\n", pi.Containers))
b.WriteString(fmt.Sprintf(" Labels: %+v\n", pi.Labels))
b.WriteString(fmt.Sprintf(" Annotations: %+v\n", pi.Annotations))
Expand Down
9 changes: 3 additions & 6 deletions cni/pkg/plugin/plugin_dryrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"

Expand All @@ -35,6 +34,8 @@ import (
"istio.io/api/annotation"
"istio.io/istio/pilot/cmd/pilot-agent/options"
diff "istio.io/istio/pilot/test/util"
"istio.io/istio/pkg/maps"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/test/env"
"istio.io/istio/tools/istio-iptables/pkg/cmd"
"istio.io/istio/tools/istio-iptables/pkg/dependencies"
Expand Down Expand Up @@ -266,11 +267,7 @@ func getRules(b []byte) map[string]string {
}

func refreshGoldens(t *testing.T, goldenFileName string, generatedRules map[string]string) {
tables := make([]string, 0)
for table := range generatedRules {
tables = append(tables, table)
}
sort.Strings(tables)
tables := slices.Sort(maps.Keys(generatedRules))
goldenFileContent := ""
for _, t := range tables {
goldenFileContent += generatedRules[t] + "\n"
Expand Down
2 changes: 1 addition & 1 deletion cni/pkg/repair/repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
klabels "k8s.io/apimachinery/pkg/labels"
Expand All @@ -33,6 +32,7 @@ import (
"istio.io/istio/cni/pkg/config"
"istio.io/istio/pkg/kube"
"istio.io/istio/pkg/monitoring"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/test"
"istio.io/istio/pkg/test/util/assert"
"istio.io/istio/pkg/test/util/retry"
Expand Down
9 changes: 4 additions & 5 deletions cni/test/install_cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"istio.io/istio/cni/pkg/util"
"istio.io/istio/pkg/file"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/test/env"
"istio.io/istio/pkg/test/util/retry"
)
Expand Down Expand Up @@ -90,11 +91,9 @@ func ls(dir string, t *testing.T) []string {
if err != nil {
t.Fatalf("Failed to list files, err: %v", err)
}
fileNames := make([]string, len(files))
for i, f := range files {
fileNames[i] = f.Name()
}
return fileNames
return slices.Map(files, func(e os.DirEntry) string {
return e.Name()
})
}

func cp(src, dest string, t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion istioctl/cmd/waypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"text/tabwriter"

"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -34,6 +33,7 @@ import (
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/protocol"
"istio.io/istio/pkg/config/schema/gvk"
"istio.io/istio/pkg/slices"
)

func waypointCmd() *cobra.Command {
Expand Down
6 changes: 3 additions & 3 deletions istioctl/pkg/writer/ztunnel/configdump/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"strings"
"text/tabwriter"

"golang.org/x/exp/slices"
"sigs.k8s.io/yaml"

ztunnelDump "istio.io/istio/istioctl/pkg/util/configdump"
"istio.io/istio/pkg/slices"
)

// WorkloadFilter is used to pass filter information into workload based config writer print functions
Expand Down Expand Up @@ -149,8 +149,8 @@ func waypointName(wl *ztunnelDump.ZtunnelWorkload, prevAddresses map[string]stri
if name, ok := prevAddresses[addr]; ok {
return name
}
if idx := slices.IndexFunc(workloads, func(w *ztunnelDump.ZtunnelWorkload) bool { return w.WorkloadIP == addr }); idx > 0 {
wpName := workloads[idx].CanonicalName
if wl := slices.FindFunc(workloads, func(w *ztunnelDump.ZtunnelWorkload) bool { return w.WorkloadIP == addr }); wl != nil {
wpName := (*wl).CanonicalName
prevAddresses[addr] = wpName
return wpName
}
Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/config/kube/crdclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

"go.uber.org/atomic"
"golang.org/x/exp/slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

Expand All @@ -35,6 +34,7 @@ import (
"istio.io/istio/pkg/config/schema/gvk"
"istio.io/istio/pkg/kube"
"istio.io/istio/pkg/kube/kclient/clienttest"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/test"
"istio.io/istio/pkg/test/util/assert"
"istio.io/istio/pkg/test/util/retry"
Expand Down
16 changes: 6 additions & 10 deletions pilot/pkg/config/kube/gateway/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"sort"

"golang.org/x/exp/slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "sigs.k8s.io/gateway-api/apis/v1alpha2"
k8sbeta "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand All @@ -27,7 +26,9 @@ import (
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/schema/gvk"
"istio.io/istio/pkg/maps"
"istio.io/istio/pkg/ptr"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/util/sets"
)

Expand Down Expand Up @@ -141,11 +142,11 @@ func createRouteStatus(gateways []routeParentReference, obj config.Config, curre
}

var currentConditions []metav1.Condition
idx := slices.IndexFunc(current, func(s k8sbeta.RouteParentStatus) bool {
currentStatus := slices.FindFunc(current, func(s k8sbeta.RouteParentStatus) bool {
return parentRefString(s.ParentRef) == parentRefString(gw.OriginalReference)
})
if idx != -1 {
currentConditions = current[idx].Conditions
if currentStatus != nil {
currentConditions = currentStatus.Conditions
}
gws = append(gws, k8s.RouteParentStatus{
ParentRef: gw.OriginalReference,
Expand Down Expand Up @@ -227,12 +228,7 @@ type condition struct {
// setConditions sets the existingConditions with the new conditions
func setConditions(generation int64, existingConditions []metav1.Condition, conditions map[string]*condition) []metav1.Condition {
// Sort keys for deterministic ordering
condKeys := make([]string, 0, len(conditions))
for k := range conditions {
condKeys = append(condKeys, k)
}
sort.Strings(condKeys)
for _, k := range condKeys {
for _, k := range slices.Sort(maps.Keys(conditions)) {
cond := conditions[k]
setter := kstatus.UpdateConditionIfChanged
if cond.setOnce != "" {
Expand Down
10 changes: 4 additions & 6 deletions pilot/pkg/config/kube/gateway/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"sort"
"strings"

"golang.org/x/exp/slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
klabels "k8s.io/apimachinery/pkg/labels"
k8s "sigs.k8s.io/gateway-api/apis/v1alpha2"
Expand All @@ -40,6 +39,7 @@ import (
"istio.io/istio/pkg/config/schema/gvk"
"istio.io/istio/pkg/config/schema/kind"
"istio.io/istio/pkg/ptr"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/util/sets"
)

Expand Down Expand Up @@ -477,11 +477,9 @@ func hostnameToStringList(h []k8s.Hostname) []string {
if len(h) == 0 {
return []string{"*"}
}
res := make([]string, 0, len(h))
for _, i := range h {
res = append(res, string(i))
}
return res
return slices.Map(h, func(e k8s.Hostname) string {
return string(e)
})
}

func toInternalParentReference(p k8s.ParentReference, localNamespace string) (parentKey, error) {
Expand Down
17 changes: 8 additions & 9 deletions pilot/pkg/model/kstatus/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package kstatus
import (
"reflect"

"golang.org/x/exp/slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"istio.io/istio/pkg/config"
"istio.io/istio/pkg/slices"
)

const (
Expand Down Expand Up @@ -85,25 +85,24 @@ func GetCondition(conditions []metav1.Condition, condition string) metav1.Condit
// UpdateConditionIfChanged updates a condition if it has been changed.
func UpdateConditionIfChanged(conditions []metav1.Condition, condition metav1.Condition) []metav1.Condition {
ret := slices.Clone(conditions)
idx := slices.IndexFunc(ret, func(cond metav1.Condition) bool {
existing := slices.FindFunc(ret, func(cond metav1.Condition) bool {
return cond.Type == condition.Type
})

if idx == -1 {
if existing == nil {
ret = append(ret, condition)
return ret
}

if ret[idx].Status == condition.Status {
if ret[idx].Message == condition.Message &&
ret[idx].ObservedGeneration == condition.ObservedGeneration {
if existing.Status == condition.Status {
if existing.Message == condition.Message &&
existing.ObservedGeneration == condition.ObservedGeneration {
// Skip update, no changes
return conditions
}
// retain LastTransitionTime if status is not changed
condition.LastTransitionTime = ret[idx].LastTransitionTime
condition.LastTransitionTime = existing.LastTransitionTime
}
ret[idx] = condition
*existing = condition

return ret
}
Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/model/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (

"github.com/hashicorp/go-multierror"
"github.com/miekg/dns"
"golang.org/x/exp/slices"

"istio.io/istio/pilot/pkg/features"
"istio.io/istio/pkg/cluster"
"istio.io/istio/pkg/network"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/util/istiomultierror"
netutil "istio.io/istio/pkg/util/net"
"istio.io/istio/pkg/util/sets"
Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/model/push_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"time"

"go.uber.org/atomic"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/types"

extensions "istio.io/api/extensions/v1alpha1"
Expand All @@ -41,6 +40,7 @@ import (
"istio.io/istio/pkg/config/schema/kind"
"istio.io/istio/pkg/config/visibility"
"istio.io/istio/pkg/monitoring"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/spiffe"
"istio.io/istio/pkg/util/sets"
"istio.io/istio/pkg/workloadapi"
Expand Down
4 changes: 2 additions & 2 deletions pilot/pkg/model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (

endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
"github.com/mitchellh/copystructure"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/proto"
"k8s.io/apimachinery/pkg/types"

Expand All @@ -44,7 +42,9 @@ import (
"istio.io/istio/pkg/config/labels"
"istio.io/istio/pkg/config/protocol"
"istio.io/istio/pkg/config/visibility"
"istio.io/istio/pkg/maps"
"istio.io/istio/pkg/network"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/util/sets"
workloadapi "istio.io/istio/pkg/workloadapi"
)
Expand Down
8 changes: 3 additions & 5 deletions pilot/pkg/model/telemetry_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package model

import (
"fmt"
"sort"
"strings"

accesslog "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v3"
Expand All @@ -28,12 +27,13 @@ import (
reqwithoutquery "github.com/envoyproxy/go-control-plane/envoy/extensions/formatter/req_without_query/v3"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
otlpcommon "go.opentelemetry.io/proto/otlp/common/v1"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/types/known/structpb"

meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/istio/pilot/pkg/util/protoconv"
"istio.io/istio/pkg/config/host"
"istio.io/istio/pkg/maps"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/util/protomarshal"
)

Expand Down Expand Up @@ -477,9 +477,7 @@ func ConvertStructToAttributeKeyValues(labels map[string]*structpb.Value) []*otl
}
attrList := make([]*otlpcommon.KeyValue, 0, len(labels))
// Sort keys to ensure stable XDS generation
keys := maps.Keys(labels)
sort.Strings(keys)
for _, key := range keys {
for _, key := range slices.Sort(maps.Keys(labels)) {
value := labels[key]
kv := &otlpcommon.KeyValue{
Key: key,
Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/model/typed_xds_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/golang-lru/v2/simplelru"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/testing/protocmp"

"istio.io/istio/pilot/pkg/features"
"istio.io/istio/pkg/monitoring"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/util/sets"
)

Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/networking/core/v1alpha3/envoyfilter/rc_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (
"strings"

route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/proto"

networking "istio.io/api/networking/v1alpha3"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/util/runtime"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/proto/merge"
"istio.io/istio/pkg/slices"
"istio.io/istio/pkg/util/sets"
)

Expand Down
Loading

0 comments on commit 9a9d1c8

Please sign in to comment.