Skip to content

Commit

Permalink
Merge pull request containerd#10757 from cpuguy83/shim_deps
Browse files Browse the repository at this point in the history
Clean up some dependency trees for runc shim
  • Loading branch information
mxpv authored Oct 2, 2024
2 parents 06dfa0c + 2123855 commit 01ca26f
Show file tree
Hide file tree
Showing 45 changed files with 295 additions and 284 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ GOTEST ?= $(GO) test
OUTPUTDIR = $(join $(ROOTDIR), _output)
CRIDIR=$(OUTPUTDIR)/cri

SHIM_GO_TAGS := --tags no_grpc

.PHONY: clean all AUTHORS build binaries test integration generate protos check-protos coverage ci check help install uninstall vendor release static-release mandir install-man install-doc genman install-cri-deps cri-release cri-cni-release cri-integration install-deps bin/cri-integration.test remove-replace clean-vendor
.DEFAULT: default

Expand All @@ -171,7 +173,7 @@ generate: protos
@echo "$(WHALE) $@"
@PATH="${ROOTDIR}/bin:${PATH}" $(GO) generate -x ${PACKAGES}

protos: bin/protoc-gen-go-fieldpath
protos: bin/protoc-gen-go-fieldpath bin/go-buildtag
@echo "$(WHALE) $@"
@find . -path ./vendor -prune -false -o -name '*.pb.go' | xargs rm
$(eval TMPDIR := $(shell mktemp -d))
Expand All @@ -181,6 +183,7 @@ protos: bin/protoc-gen-go-fieldpath
@rm -rf ${TMPDIR} v2
go-fix-acronym -w -a '^Os' $(shell find api/ -name '*.pb.go')
go-fix-acronym -w -a '(Id|Io|Uuid|Os)$$' $(shell find api/ -name '*.pb.go')
bin/go-buildtag -w --tags '!no_grpc' $(shell find api/ -name '*_grpc.pb.go')
@test -z "$$(git status --short | grep "api/next.pb.txt" | tee /dev/stderr)" || \
$(GO) mod edit -replace=github.com/containerd/containerd/api=./api

Expand Down Expand Up @@ -230,7 +233,7 @@ cri-integration: binaries bin/cri-integration.test ## run cri integration tests
# build runc shimv2 with failpoint control, only used by integration test
bin/containerd-shim-runc-fp-v1: integration/failpoint/cmd/containerd-shim-runc-fp-v1 FORCE
@echo "$(WHALE) $@"
@CGO_ENABLED=${SHIM_CGO_ENABLED} $(GO) build ${GO_BUILD_FLAGS} -o $@ ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./integration/failpoint/cmd/containerd-shim-runc-fp-v1
@CGO_ENABLED=${SHIM_CGO_ENABLED} $(GO) build ${GO_BUILD_FLAGS} -o $@ ${SHIM_GO_LDFLAGS} ${GO_TAGS} ${SHIM_GO_TAGS} ./integration/failpoint/cmd/containerd-shim-runc-fp-v1

# build CNI bridge plugin wrapper with failpoint support, only used by integration test
bin/cni-bridge-fp: integration/failpoint/cmd/cni-bridge-fp FORCE
Expand Down Expand Up @@ -264,7 +267,7 @@ bin/gen-manpages: cmd/gen-manpages FORCE

bin/containerd-shim-runc-v2: cmd/containerd-shim-runc-v2 FORCE # set !cgo and omit pie for a static shim build: https://github.com/golang/go/issues/17789#issuecomment-258542220
@echo "$(WHALE) $@"
@CGO_ENABLED=${SHIM_CGO_ENABLED} $(GO) build ${GO_BUILD_FLAGS} -o $@ ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./cmd/containerd-shim-runc-v2
CGO_ENABLED=${SHIM_CGO_ENABLED} $(GO) build ${GO_BUILD_FLAGS} -o $@ ${SHIM_GO_LDFLAGS} ${GO_TAGS} ${SHIM_GO_TAGS} ./cmd/containerd-shim-runc-v2

binaries: $(BINARIES) ## build binaries
@echo "$(WHALE) $@"
Expand Down
2 changes: 2 additions & 0 deletions api/runtime/sandbox/v1/sandbox_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/runtime/task/v3/shim_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/containers/v1/containers_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/content/v1/content_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/diff/v1/diff_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/events/v1/events_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/images/v1/images_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/introspection/v1/introspection_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/leases/v1/leases_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/namespaces/v1/namespace_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/sandbox/v1/sandbox_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/snapshots/v1/snapshots_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/streaming/v1/streaming_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/tasks/v1/tasks_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/transfer/v1/transfer_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/services/version/v1/version_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/containerd-shim-runc-v2/manager/manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/containerd/containerd/v2/cmd/containerd-shim-runc-v2/runc"
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/containerd/v2/pkg/schedcore"
"github.com/containerd/containerd/v2/pkg/shim"
"github.com/containerd/containerd/v2/version"
Expand Down Expand Up @@ -112,7 +111,8 @@ func newCommand(ctx context.Context, id, containerdAddress, containerdTTRPCAddre
}

func readSpec() (*spec, error) {
f, err := os.Open(oci.ConfigFilename)
const configFileName = "config.json"
f, err := os.Open(configFileName)
if err != nil {
return nil, err
}
Expand Down
19 changes: 17 additions & 2 deletions cmd/containerd-shim-runc-v2/runc/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ package runc

import (
"context"
"encoding/json"
"os"
"path/filepath"

"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/log"
"github.com/opencontainers/runtime-spec/specs-go"
)

// ShouldKillAllOnExit reads the bundle's OCI spec and returns true if
// there is an error reading the spec or if the container has a private PID namespace
func ShouldKillAllOnExit(ctx context.Context, bundlePath string) bool {
spec, err := oci.ReadSpec(filepath.Join(bundlePath, oci.ConfigFilename))
spec, err := readSpec(bundlePath)
if err != nil {
log.G(ctx).WithError(err).Error("shouldKillAllOnExit: failed to read config.json")
return true
Expand All @@ -45,3 +46,17 @@ func ShouldKillAllOnExit(ctx context.Context, bundlePath string) bool {
}
return true
}

func readSpec(p string) (*specs.Spec, error) {
const configFileName = "config.json"
f, err := os.Open(filepath.Join(p, configFileName))
if err != nil {
return nil, err
}
defer f.Close()
var s specs.Spec
if err := json.NewDecoder(f).Decode(&s); err != nil {
return nil, err
}
return &s, nil
}
96 changes: 96 additions & 0 deletions cmd/go-buildtag/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"flag"
"fmt"
"io"
"os"
)

func main() {
wrFl := flag.Bool("w", false, "Write to file")
tagsFl := flag.String("tags", "", "Tags to add")

flag.Parse()

if flag.NArg() < 1 {
fmt.Fprintln(os.Stderr, "Usage: go-buildtag <options> <file1> [<file2> <file3> ...]")
os.Exit(1)
}

wr := *wrFl
tags := *tagsFl
for i := 0; i < flag.NArg(); i++ {
p := flag.Arg(i)
if err := handle(wr, tags, p); err != nil {
fmt.Fprintln(os.Stderr, p+":", err)
os.Exit(2)
}
}
}

func handle(wr bool, tags, p string) (retErr error) {
out := os.Stdout

rdr, err := os.Open(p)
if err != nil {
return fmt.Errorf("could not open source file: %w", err)
}
defer rdr.Close()

if wr {
f, err := os.Create(p + ".tmp")
if err != nil {
return fmt.Errorf("error creatign temp file")
}
defer func() {
if retErr != nil {
os.Remove(f.Name())
}
}()

defer f.Close()
out = f
}

_, err = fmt.Fprintf(out, "//go:build %s\n\n", tags)
if err != nil {
return fmt.Errorf("error writing build tags: %w", err)
}

_, err = io.Copy(out, rdr)
if err != nil {
return fmt.Errorf("error copying original content to temp file")
}

rdr.Close()
if wr {
out.Close()

if err := os.Remove(p); err != nil {
return fmt.Errorf("error removing original source file: %w", err)
}

if err := os.Rename(p+".tmp", p); err != nil {
return fmt.Errorf("error replacing original source: %w", err)
}
}

return nil
}
9 changes: 5 additions & 4 deletions contrib/snapshotservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/core/snapshots"
"github.com/containerd/containerd/v2/core/snapshots/proxy"
ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/errdefs"
)
Expand Down Expand Up @@ -103,16 +104,16 @@ func (s service) Stat(ctx context.Context, sr *snapshotsapi.StatSnapshotRequest)
return nil, errdefs.ToGRPC(err)
}

return &snapshotsapi.StatSnapshotResponse{Info: snapshots.InfoToProto(info)}, nil
return &snapshotsapi.StatSnapshotResponse{Info: proxy.InfoToProto(info)}, nil
}

func (s service) Update(ctx context.Context, sr *snapshotsapi.UpdateSnapshotRequest) (*snapshotsapi.UpdateSnapshotResponse, error) {
info, err := s.sn.Update(ctx, snapshots.InfoFromProto(sr.Info), sr.UpdateMask.GetPaths()...)
info, err := s.sn.Update(ctx, proxy.InfoFromProto(sr.Info), sr.UpdateMask.GetPaths()...)
if err != nil {
return nil, errdefs.ToGRPC(err)
}

return &snapshotsapi.UpdateSnapshotResponse{Info: snapshots.InfoToProto(info)}, nil
return &snapshotsapi.UpdateSnapshotResponse{Info: proxy.InfoToProto(info)}, nil
}

func (s service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Snapshots_ListServer) error {
Expand All @@ -125,7 +126,7 @@ func (s service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Sna
}
)
err := s.sn.Walk(ss.Context(), func(ctx context.Context, info snapshots.Info) error {
buffer = append(buffer, snapshots.InfoToProto(info))
buffer = append(buffer, proxy.InfoToProto(info))

if len(buffer) >= 100 {
if err := sendBlock(buffer); err != nil {
Expand Down
Loading

0 comments on commit 01ca26f

Please sign in to comment.