Skip to content

Commit

Permalink
Merge pull request #5030 from tonistiigi/slices-maps
Browse files Browse the repository at this point in the history
refactor with slices and maps pkg
  • Loading branch information
AkihiroSuda authored Jun 13, 2024
2 parents f529bae + dfc3527 commit ec2f4e2
Show file tree
Hide file tree
Showing 26 changed files with 81 additions and 147 deletions.
5 changes: 2 additions & 3 deletions cache/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"fmt"
"maps"
"os"
"strconv"

Expand Down Expand Up @@ -232,9 +233,7 @@ func computeBlobChain(ctx context.Context, sr *immutableRef, createIfNeeded bool
if err != nil {
return nil, errors.Wrapf(err, "failed to finalize compression")
}
for k, v := range a {
desc.Annotations[k] = v
}
maps.Copy(desc.Annotations, a)
}
info, err := sr.cm.ContentStore.Info(ctx, desc.Digest)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions cache/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"fmt"
"maps"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -727,9 +728,7 @@ func (sr *immutableRef) ociDesc(ctx context.Context, dhs DescHandlers, preferNon
}
} else if dh, ok := dhs[desc.Digest]; ok {
// No blob metadtata is stored in the content store. Try to get annotations from desc handlers.
for k, v := range filterAnnotationsForSave(dh.Annotations) {
desc.Annotations[k] = v
}
maps.Copy(desc.Annotations, filterAnnotationsForSave(dh.Annotations))
}

diffID := sr.getDiffID()
Expand Down
5 changes: 2 additions & 3 deletions cache/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"fmt"
"maps"
"net/url"
"strings"

Expand Down Expand Up @@ -235,9 +236,7 @@ func (sr *immutableRef) getRemote(ctx context.Context, createIfNeeded bool, refC
for _, k := range addAnnotations {
newDesc.Annotations[k] = desc.Annotations[k]
}
for k, v := range blobDesc.Annotations {
newDesc.Annotations[k] = v
}
maps.Copy(newDesc.Annotations, blobDesc.Annotations)
desc = newDesc
}
}
Expand Down
5 changes: 2 additions & 3 deletions cache/remotecache/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package inline
import (
"context"
"encoding/json"
"slices"

"github.com/containerd/containerd/labels"
"github.com/moby/buildkit/cache/remotecache"
Expand Down Expand Up @@ -167,8 +168,6 @@ func layerToBlobs(idx int, layers []v1.CacheLayer) []digest.Digest {
idx = layer.ParentIndex
}
// reverse so they go lowest to highest
for i, j := 0, len(ds)-1; i < j; i, j = i+1, j-1 {
ds[i], ds[j] = ds[j], ds[i]
}
slices.Reverse(ds)
return ds
}
5 changes: 2 additions & 3 deletions cache/remotecache/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"context"
"maps"
"strconv"

"github.com/containerd/containerd/content"
Expand Down Expand Up @@ -163,9 +164,7 @@ func (dsl *withDistributionSourceLabel) SnapshotLabels(descs []ocispecs.Descript
if labels == nil {
labels = make(map[string]string)
}
for k, v := range estargz.SnapshotLabels(dsl.ref, descs, index) {
labels[k] = v
}
maps.Copy(labels, estargz.SnapshotLabels(dsl.ref, descs, index))
return labels
}

Expand Down
9 changes: 4 additions & 5 deletions client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"maps"

"github.com/moby/buildkit/client/buildid"
gateway "github.com/moby/buildkit/frontend/gateway/client"
Expand Down Expand Up @@ -42,12 +43,10 @@ func (c *Client) Build(ctx context.Context, opt SolveOpt, product string, buildF
}

cb := func(ref string, s *session.Session, opts map[string]string) error {
for k, v := range opts {
if feOpts == nil {
feOpts = map[string]string{}
}
feOpts[k] = v
if feOpts == nil {
feOpts = map[string]string{}
}
maps.Copy(feOpts, opts)
gwClient := c.gatewayClientForBuild(ref)
g, err := grpcclient.New(ctx, feOpts, s.ID(), product, gwClient, gworkers)
if err != nil {
Expand Down
12 changes: 3 additions & 9 deletions client/llb/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package llb

import (
"io"
"maps"

"github.com/containerd/containerd/platforms"
"github.com/moby/buildkit/solver/pb"
Expand All @@ -18,24 +19,17 @@ type Definition struct {
}

func (def *Definition) ToPB() *pb.Definition {
md := make(map[digest.Digest]pb.OpMetadata, len(def.Metadata))
for k, v := range def.Metadata {
md[k] = v
}
return &pb.Definition{
Def: def.Def,
Source: def.Source,
Metadata: md,
Metadata: maps.Clone(def.Metadata),
}
}

func (def *Definition) FromPB(x *pb.Definition) {
def.Def = x.Def
def.Source = x.Source
def.Metadata = make(map[digest.Digest]pb.OpMetadata)
for k, v := range x.Metadata {
def.Metadata[k] = v
}
def.Metadata = maps.Clone(x.Metadata)
}

func (def *Definition) Head() (digest.Digest, error) {
Expand Down
9 changes: 3 additions & 6 deletions client/llb/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"net"
"strings"

Expand Down Expand Up @@ -566,9 +567,7 @@ func mergeMetadata(m1, m2 pb.OpMetadata) pb.OpMetadata {
if m1.Description == nil {
m1.Description = make(map[string]string)
}
for k, v := range m2.Description {
m1.Description[k] = v
}
maps.Copy(m1.Description, m2.Description)
}
if m2.ExportCache != nil {
m1.ExportCache = m2.ExportCache
Expand Down Expand Up @@ -597,9 +596,7 @@ func WithDescription(m map[string]string) ConstraintsOpt {
if c.Metadata.Description == nil {
c.Metadata.Description = map[string]string{}
}
for k, v := range m {
c.Metadata.Description[k] = v
}
maps.Copy(c.Metadata.Description, m)
})
}

Expand Down
10 changes: 3 additions & 7 deletions client/solve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"io"
"maps"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -219,13 +220,8 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
})
}

frontendAttrs := map[string]string{}
for k, v := range opt.FrontendAttrs {
frontendAttrs[k] = v
}
for k, v := range cacheOpt.frontendAttrs {
frontendAttrs[k] = v
}
frontendAttrs := maps.Clone(opt.FrontendAttrs)
maps.Copy(frontendAttrs, cacheOpt.frontendAttrs)

solveCtx, cancelSolve := context.WithCancelCause(ctx)
var res *SolveResponse
Expand Down
6 changes: 3 additions & 3 deletions cmd/buildctl/build/opt.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package build

import "maps"

func ParseOpt(opts []string) (map[string]string, error) {
m := loadOptEnv()
m2, err := attrMap(opts)
if err != nil {
return nil, err
}
for k, v := range m2 {
m[k] = v
}
maps.Copy(m, m2)
return m, nil
}
6 changes: 3 additions & 3 deletions cmd/buildkitd/main_containerd_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"context"
"maps"
"os"
"runtime"
"strconv"
Expand Down Expand Up @@ -212,9 +213,8 @@ func applyContainerdFlags(c *cli.Context, cfg *config.Config) error {
if cfg.Workers.Containerd.Labels == nil {
cfg.Workers.Containerd.Labels = make(map[string]string)
}
for k, v := range labels {
cfg.Workers.Containerd.Labels[k] = v
}
maps.Copy(cfg.Workers.Containerd.Labels, labels)

if c.GlobalIsSet("containerd-worker-addr") {
cfg.Workers.Containerd.Address = c.GlobalString("containerd-worker-addr")
}
Expand Down
35 changes: 10 additions & 25 deletions exporter/containerimage/annotations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package containerimage

import (
"maps"

ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"

Expand Down Expand Up @@ -73,24 +75,15 @@ func (ag AnnotationsGroup) Platform(p *ocispecs.Platform) *Annotations {
}

for _, a := range ag {
for k, v := range a.Index {
res.Index[k] = v
}
for k, v := range a.IndexDescriptor {
res.IndexDescriptor[k] = v
}
maps.Copy(res.Index, a.Index)
maps.Copy(res.IndexDescriptor, a.IndexDescriptor)
}
for _, pk := range ps {
if _, ok := ag[pk]; !ok {
continue
}

for k, v := range ag[pk].Manifest {
res.Manifest[k] = v
}
for k, v := range ag[pk].ManifestDescriptor {
res.ManifestDescriptor[k] = v
}
maps.Copy(res.Manifest, ag[pk].Manifest)
maps.Copy(res.ManifestDescriptor, ag[pk].ManifestDescriptor)
}
return res
}
Expand Down Expand Up @@ -122,18 +115,10 @@ func (a *Annotations) merge(other *Annotations) *Annotations {
}
}

for k, v := range other.Index {
a.Index[k] = v
}
for k, v := range other.IndexDescriptor {
a.IndexDescriptor[k] = v
}
for k, v := range other.Manifest {
a.Manifest[k] = v
}
for k, v := range other.ManifestDescriptor {
a.ManifestDescriptor[k] = v
}
maps.Copy(a.Index, other.Index)
maps.Copy(a.IndexDescriptor, other.IndexDescriptor)
maps.Copy(a.Manifest, other.Manifest)
maps.Copy(a.ManifestDescriptor, other.ManifestDescriptor)

return a
}
10 changes: 5 additions & 5 deletions exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"maps"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -210,9 +211,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
if src.Metadata == nil {
src.Metadata = make(map[string][]byte)
}
for k, v := range e.meta {
src.Metadata[k] = v
}
maps.Copy(src.Metadata, e.meta)

opts := e.opts
as, _, err := ParseAnnotations(src.Metadata)
Expand Down Expand Up @@ -514,9 +513,10 @@ func addAnnotations(m map[digest.Digest]map[string]string, desc ocispecs.Descrip
m[desc.Digest] = desc.Annotations
return
}
for k, v := range desc.Annotations {
a[k] = v
if a == nil {
a = make(map[string]string)
}
maps.Copy(a, desc.Annotations)
}

func NewDescriptorReference(desc ocispecs.Descriptor, release func(context.Context) error) exporter.DescriptorReference {
Expand Down
5 changes: 2 additions & 3 deletions exporter/oci/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"maps"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -139,9 +140,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
if src.Metadata == nil {
src.Metadata = make(map[string][]byte)
}
for k, v := range e.meta {
src.Metadata[k] = v
}
maps.Copy(src.Metadata, e.meta)

opts := e.opts
as, _, err := containerimage.ParseAnnotations(src.Metadata)
Expand Down
5 changes: 2 additions & 3 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"math"
"net/url"
"os"
Expand Down Expand Up @@ -726,9 +727,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
if len(opt.Labels) != 0 && target.image.Config.Labels == nil {
target.image.Config.Labels = make(map[string]string, len(opt.Labels))
}
for k, v := range opt.Labels {
target.image.Config.Labels[k] = v
}
maps.Copy(target.image.Config.Labels, opt.Labels)

// If lint.Error() returns an error, it means that
// there were warnings, and that our linter has been
Expand Down
Loading

0 comments on commit ec2f4e2

Please sign in to comment.