Skip to content

Commit

Permalink
chore: use range-over-func iterators for resource iteration
Browse files Browse the repository at this point in the history
Bump to Go 1.23 and use new iterator mechanism. Also fix new linter issues.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed Aug 21, 2024
1 parent dc349c1 commit 5d48547
Show file tree
Hide file tree
Showing 56 changed files with 173 additions and 298 deletions.
2 changes: 1 addition & 1 deletion client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ replace (
require (
github.com/adrg/xdg v0.5.0
github.com/blang/semver v3.5.1+incompatible
github.com/cosi-project/runtime v0.5.5
github.com/cosi-project/runtime v0.6.0
github.com/fatih/color v1.17.0
github.com/gertd/go-pluralize v0.2.1
github.com/google/uuid v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ github.com/containerd/go-cni v1.1.10 h1:c2U73nld7spSWfiJwSh/8W9DK+/qQwYM2rngIhCy
github.com/containerd/go-cni v1.1.10/go.mod h1:/Y/sL8yqYQn1ZG1om1OncJB1W4zN3YmjfP/ShCzG/OY=
github.com/containernetworking/cni v1.2.2 h1:9IbP6KJQQxVKo4hhnm8r50YcVKrJbJu3Dqw+Rbt1vYk=
github.com/containernetworking/cni v1.2.2/go.mod h1:DuLgF+aPd3DzcTQTtp/Nvl1Kim23oFKdm2okJzBQA5M=
github.com/cosi-project/runtime v0.5.5 h1:GFoHnngpg4QVZluAUDwUbCe/sYOYBXKULxL/6DD99pU=
github.com/cosi-project/runtime v0.5.5/go.mod h1:m+bkfUzKYeUyoqYAQBxdce3bfgncG8BsqcbfKRbvJKs=
github.com/cosi-project/runtime v0.6.0 h1:ufybTVO1TUVx3YbuazVGKjYd0XoBBlaTvT3W30t09oc=
github.com/cosi-project/runtime v0.6.0/go.mod h1:14kfPrnGOG9pwc/AeRV1KquA5JYP7DUic/aGE8js9VE=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
4 changes: 1 addition & 3 deletions client/pkg/client/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ func Example() {
machine *omni.MachineStatus
)

for iter := machines.Iterator(); iter.Next(); {
item := iter.Value()

for item := range machines.All() {
log.Printf("machine %s, connected: %t", item.Metadata(), item.TypedSpec().Value.GetConnected())

// Check cluster assignment for a machine.
Expand Down
10 changes: 5 additions & 5 deletions client/pkg/omnictl/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ func resolveResourceType(ctx context.Context, st state.State, resourceType strin

var matched []*meta.ResourceDefinition

for it := rds.Iterator(); it.Next(); {
if strings.EqualFold(it.Value().Metadata().ID(), resourceType) {
matched = append(matched, it.Value())
for val := range rds.All() {
if strings.EqualFold(val.Metadata().ID(), resourceType) {
matched = append(matched, val)

continue
}

spec := it.Value().TypedSpec()
spec := val.TypedSpec()

for _, alias := range spec.AllAliases {
if strings.EqualFold(alias, resourceType) {
matched = append(matched, it.Value())
matched = append(matched, val)

break
}
Expand Down
6 changes: 3 additions & 3 deletions client/pkg/omnictl/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ func filterMedia[T any](ctx context.Context, client *client.Client, check func(v
return nil, err
}

var result []T
result := make([]T, 0, media.Len())

for it := media.Iterator(); it.Next(); {
if val, ok := check(it.Value()); ok {
for item := range media.All() {
if val, ok := check(item); ok {
result = append(result, val)
}
}
Expand Down
20 changes: 3 additions & 17 deletions client/pkg/template/operations/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,7 @@ func collectClusterResources(ctx context.Context, st state.State, clusterID stri

machineSetNodes := make(map[string][]*omni.MachineSetNode, machineSetNodeList.Len())

for iter := machineSetNodeList.Iterator(); iter.Next(); {
machineSetNode := iter.Value()

for machineSetNode := range machineSetNodeList.All() {
machineSetLabel, ok := machineSetNode.Metadata().Labels().Get(omni.LabelMachineSet)
if !ok {
return clusterResources{}, fmt.Errorf("machine set node %q has no machine set label", machineSetNode.Metadata().ID())
Expand Down Expand Up @@ -437,7 +435,7 @@ func collectClusterResources(ctx context.Context, st state.State, clusterID stri

return clusterResources{
cluster: cluster,
machineSets: listToSlice(machineSetList),
machineSets: slices.AppendSeq(make([]*omni.MachineSet, 0, machineSetList.Len()), machineSetList.All()),
machineSetNodes: machineSetNodes,
patches: patches,
extensions: extensions,
Expand Down Expand Up @@ -538,22 +536,10 @@ func getInstallDiskFromConfigPatch(configPatch *omni.ConfigPatch) string {
return disk
}

func listToSlice[T resource.Resource](list safe.List[T]) []T {
result := make([]T, 0, list.Len())

for iter := list.Iterator(); iter.Next(); {
result = append(result, iter.Value())
}

return result
}

func listToMap[K comparable, T resource.Resource](list safe.List[T], keyFunc func(T) K) map[K]T {
result := make(map[K]T, list.Len())

for iter := list.Iterator(); iter.Next(); {
value := iter.Value()

for value := range list.All() {
result[keyFunc(value)] = value
}

Expand Down
4 changes: 1 addition & 3 deletions client/pkg/template/operations/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ func buildState(ctx context.Context, t *testing.T) state.State {
func listToMap[T resource.Resource](list safe.List[T]) map[resource.ID]T {
result := make(map[resource.ID]T, list.Len())

for iter := list.Iterator(); iter.Next(); {
value := iter.Value()

for value := range list.All() {
result[value.Metadata().ID()] = value
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/integration-test/pkg/tests/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ func AssertDownloadUsingCLI(testCtx context.Context, client *client.Client, omni

var images []*omni.InstallationMedia

for it := media.Iterator(); it.Next(); {
spec := it.Value().TypedSpec().Value
for val := range media.All() {
spec := val.TypedSpec().Value

switch {
case spec.Profile == constants.BoardRPiGeneric:
fallthrough
case spec.Profile == "aws":
fallthrough
case spec.Profile == "iso":
images = append(images, it.Value())
images = append(images, val)
}
}

Expand Down
8 changes: 2 additions & 6 deletions cmd/integration-test/pkg/tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ func nodes(ctx context.Context, cli *client.Client, clusterName string, labels .

nodeList := make([]node, 0, clusterMachineList.Len())

for iter := clusterMachineList.Iterator(); iter.Next(); {
clusterMachine := iter.Value()

for clusterMachine := range clusterMachineList.All() {
machine, err := safe.StateGet[*omni.Machine](ctx, st, omni.NewMachine(resources.DefaultNamespace, clusterMachine.Metadata().ID()).Metadata())
if err != nil && !state.IsNotFoundError(err) {
return nil, err
Expand Down Expand Up @@ -169,9 +167,7 @@ func talosNodeIPs(ctx context.Context, talosState state.State) ([]string, error)

nodeIPs := make([]string, 0, list.Len())

for iter := list.Iterator(); iter.Next(); {
member := iter.Value()

for member := range list.All() {
if len(member.TypedSpec().Addresses) == 0 {
return nil, fmt.Errorf("no addresses for member %q", member.Metadata().ID())
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/integration-test/pkg/tests/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func checkExtension(ctx context.Context, cli *client.Client, machineID resource.
return err
}

for iter := extensionStatusList.Iterator(); iter.Next(); {
extensionStatus := iter.Value()

for extensionStatus := range extensionStatusList.All() {
if extensionStatus.TypedSpec().Metadata.Name == extension {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/integration-test/pkg/tests/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ func AssertSomeImagesAreDownloadable(testCtx context.Context, client *client.Cli

var images []*omni.InstallationMedia

for it := media.Iterator(); it.Next(); {
spec := it.Value().TypedSpec().Value
for val := range media.All() {
spec := val.TypedSpec().Value

switch {
case spec.Profile == constants.BoardRPiGeneric:
fallthrough
case spec.Profile == "aws":
fallthrough
case spec.Profile == "iso":
images = append(images, it.Value())
images = append(images, val)
}
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/integration-test/pkg/tests/talos.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,7 @@ func AssertTalosServiceIsRestarted(testCtx context.Context, cli *client.Client,
clusterMachineList, err := safe.StateListAll[*omni.ClusterMachine](ctx, cli.Omni().State(), state.WithLabelQuery(labelQueryOpts...))
require.NoError(t, err)

for it := clusterMachineList.Iterator(); it.Next(); {
clusterMachine := it.Value()
for clusterMachine := range clusterMachineList.All() {
nodeID := clusterMachine.Metadata().ID()

t.Logf("Restarting service %q on node %q", service, nodeID)
Expand Down
2 changes: 1 addition & 1 deletion cmd/integration-test/pkg/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ func (f matchStringOnly) ResetCoverage() {}
func (f matchStringOnly) SnapshotCoverage() {}

func (f matchStringOnly) InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) {
return "", nil, nil
return "", func(string, string) (string, error) { return "", nil }, func() float64 { return 0 }
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/blang/semver/v4 v4.0.0
github.com/containers/image/v5 v5.32.1
github.com/cosi-project/runtime v0.5.5
github.com/cosi-project/runtime v0.6.0
github.com/cosi-project/state-etcd v0.3.0
github.com/crewjam/saml v0.4.14
github.com/dustin/go-humanize v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cosi-project/runtime v0.5.5 h1:GFoHnngpg4QVZluAUDwUbCe/sYOYBXKULxL/6DD99pU=
github.com/cosi-project/runtime v0.5.5/go.mod h1:m+bkfUzKYeUyoqYAQBxdce3bfgncG8BsqcbfKRbvJKs=
github.com/cosi-project/runtime v0.6.0 h1:ufybTVO1TUVx3YbuazVGKjYd0XoBBlaTvT3W30t09oc=
github.com/cosi-project/runtime v0.6.0/go.mod h1:14kfPrnGOG9pwc/AeRV1KquA5JYP7DUic/aGE8js9VE=
github.com/cosi-project/state-etcd v0.3.0 h1:mRqsowUb6Px6ZC9Qt61WOrATyMLJEahQ9T9rLriTDN4=
github.com/cosi-project/state-etcd v0.3.0/go.mod h1:9PAv45buB5kWEGB+TuAk8FZxyRYxcB3198W/JndzcbA=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
Expand Down
6 changes: 3 additions & 3 deletions internal/backend/grpc/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ func (s *managementServer) KubernetesUpgradePreChecks(ctx context.Context, req *
return nil, err
}

for iter := cmis.Iterator(); iter.Next(); {
if len(iter.Value().TypedSpec().Value.NodeIps) > 0 {
controlplaneNodes = append(controlplaneNodes, iter.Value().TypedSpec().Value.NodeIps[0])
for cmi := range cmis.All() {
if len(cmi.TypedSpec().Value.NodeIps) > 0 {
controlplaneNodes = append(controlplaneNodes, cmi.TypedSpec().Value.NodeIps[0])
}
}

Expand Down
8 changes: 2 additions & 6 deletions internal/backend/grpc/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ func (s *managementServer) ListServiceAccounts(ctx context.Context, _ *emptypb.E

serviceAccounts := make([]*management.ListServiceAccountsResponse_ServiceAccount, 0, identityList.Len())

for iter := identityList.Iterator(); iter.Next(); {
identity := iter.Value()

for identity := range identityList.All() {
user, err := safe.StateGet[*authres.User](ctx, s.omniState, authres.NewUser(resources.DefaultNamespace, identity.TypedSpec().Value.UserId).Metadata())
if err != nil {
return nil, err
Expand All @@ -232,9 +230,7 @@ func (s *managementServer) ListServiceAccounts(ctx context.Context, _ *emptypb.E

publicKeys := make([]*management.ListServiceAccountsResponse_ServiceAccount_PgpPublicKey, 0, publicKeyList.Len())

for keyIter := publicKeyList.Iterator(); keyIter.Next(); {
key := keyIter.Value()

for key := range publicKeyList.All() {
publicKeys = append(publicKeys, &management.ListServiceAccountsResponse_ServiceAccount_PgpPublicKey{
Id: key.Metadata().ID(),
Armored: string(key.TypedSpec().Value.GetPublicKey()),
Expand Down
4 changes: 1 addition & 3 deletions internal/backend/oidc/internal/storage/keys/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ func (s *Storage) cleanupOldKeys(ctx context.Context) error {

newKeySet := make([]op.Key, 0, keys.Len())

for iter := keys.Iterator(); iter.Next(); {
key := iter.Value()

for key := range keys.All() {
if s.clock.Now().After(key.TypedSpec().Value.Expiration.AsTime()) {
s.logger.Info("destroying expired OIDC key",
zap.String("key_id", key.Metadata().ID()),
Expand Down
4 changes: 1 addition & 3 deletions internal/backend/resourcelogger/resourcelogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ func resourceNamesToDefinitions(ctx context.Context, st state.State) (map[string
nameToRDs[name][rd.TypedSpec().Type] = rd
}

for it := rds.Iterator(); it.Next(); {
rd := it.Value()

for rd := range rds.All() {
add(rd.Metadata().ID(), rd)

for _, alias := range rd.TypedSpec().AllAliases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ func (ctrl *ClusterDestroyStatusController) Run(ctx context.Context, r controlle
return fmt.Errorf("error listing Cluster resources: %w", err)
}

for iter := clusters.Iterator(); iter.Next(); {
cluster := iter.Value()

for cluster := range clusters.All() {
if cluster.Metadata().Phase() != resource.PhaseTearingDown {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func NewClusterEndpointController() *ClusterEndpointController {

clusterEndpoint.TypedSpec().Value.ManagementAddresses = nil

for iter := items.Iterator(); iter.Next(); {
if iter.Value().TypedSpec().Value.ManagementAddress == "" {
for val := range items.All() {
if val.TypedSpec().Value.ManagementAddress == "" {
continue
}

clusterEndpoint.TypedSpec().Value.ManagementAddresses = append(
clusterEndpoint.TypedSpec().Value.ManagementAddresses,
iter.Value().TypedSpec().Value.ManagementAddress,
val.TypedSpec().Value.ManagementAddress,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ func NewClusterKubernetesNodesController() *ClusterKubernetesNodesController {

nodes := make([]string, 0, identityList.Len())

for iter := identityList.Iterator(); iter.Next(); {
identity := iter.Value()

for identity := range identityList.All() {
if identity.Metadata().Phase() == resource.PhaseTearingDown {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,13 @@ func (ctrl *ClusterLoadBalancerController) Run(ctx context.Context, r controller
}

allocatedPorts := map[string]struct{}{}
for iter := configs.Iterator(); iter.Next(); {
allocatedPorts[iter.Value().TypedSpec().Value.BindPort] = struct{}{}
for val := range configs.All() {
allocatedPorts[val.TypedSpec().Value.BindPort] = struct{}{}
}

tracker := trackResource(r, resources.DefaultNamespace, omni.LoadBalancerConfigType)

for iter := list.Iterator(); iter.Next(); {
cluster := iter.Value()

for cluster := range list.All() {
tracker.keep(cluster)

var endpoints []string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ func (ctrl *ClusterMachineIdentityController) reconcileCollectors(ctx context.Co

expectedCollectors := map[string]clustermachine.IdentityCollectorTaskSpec{}

for iter := list.Iterator(); iter.Next(); {
clusterMachine := iter.Value()

for clusterMachine := range list.All() {
tracker.keep(clusterMachine)

if clusterMachine.Metadata().Phase() == resource.PhaseTearingDown {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (ctrl *ClusterMachineStatusMetricsController) Run(ctx context.Context, r co
specs.ClusterMachineStatusSpec_DESTROYING: 0,
}

for iter := clusterMachineStatusList.Iterator(); iter.Next(); {
clusterMachinesByStatus[iter.Value().TypedSpec().Value.Stage]++
for val := range clusterMachineStatusList.All() {
clusterMachinesByStatus[val.TypedSpec().Value.Stage]++
}

for status, num := range clusterMachinesByStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ func NewClusterStatusController(embeddedDiscoveryServiceEnabled bool) *ClusterSt

phases := map[specs.MachineSetPhase]int{}

for iter := list.Iterator(); iter.Next(); {
machineSetStatus := iter.Value().TypedSpec().Value
for mss := range list.All() {
machineSetStatus := mss.TypedSpec().Value

machines.Total += machineSetStatus.GetMachines().GetTotal()
machines.Healthy += machineSetStatus.GetMachines().GetHealthy()

_, isControlPlane := iter.Value().Metadata().Labels().Get(omni.LabelControlPlaneRole)
_, isControlPlane := mss.Metadata().Labels().Get(omni.LabelControlPlaneRole)
if isControlPlane {
var cpStatus *omni.ControlPlaneStatus

cpStatus, err = safe.ReaderGet[*omni.ControlPlaneStatus](
ctx, r,
resource.NewMetadata(resources.DefaultNamespace, omni.ControlPlaneStatusType, iter.Value().Metadata().ID(), resource.VersionUndefined),
resource.NewMetadata(resources.DefaultNamespace, omni.ControlPlaneStatusType, mss.Metadata().ID(), resource.VersionUndefined),
)
if err != nil && !state.IsNotFoundError(err) {
return err
Expand Down
Loading

0 comments on commit 5d48547

Please sign in to comment.