Skip to content

Commit

Permalink
Feature: support customize base image (#519)
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Ma <majinjing3@gmail.com>
  • Loading branch information
jim3ma committed Aug 5, 2021
1 parent 86620ca commit 83cd262
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 61 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
- name: Get dependencies
run: |
go mod vendor
mkdir -p /tmp/artifact
- name: Setup Kind
uses: engineerd/setup-kind@v0.5.0
Expand All @@ -59,3 +60,10 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.txt
flags: e2etests

- name: Upload Logs
uses: actions/upload-artifact@v2
if: failure()
with:
name: dragonfly-logs
path: /tmp/artifact/
24 changes: 24 additions & 0 deletions build/images/base/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM quay.io/iovisor/bpftrace:v0.13.0

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update \
&& apt-get install -y binutils bpfcc-tools curl git iproute2 iputils-ping jq socat tree vim wget \
&& apt-get install -y openssh-client openssh-server \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir -p /run/sshd

ENV PATH=/go/bin:/usr/local/go/bin:$PATH

ENV GOPATH=/go
ENV GOLANG_VERSION 1.16.6
ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux-amd64.tar.gz
ENV GOLANG_DOWNLOAD_SHA256 be333ef18b3016e9d7cb7b1ff1fdb0cac800ca0be4cf2290fe613b3d069dfe0d

ARG GOPROXY

RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \
&& echo "$GOLANG_DOWNLOAD_SHA256 golang.tar.gz" | sha256sum -c - \
&& tar -C /usr/local -xzf golang.tar.gz \
&& rm golang.tar.gz \
&& go install github.com/go-delve/delve/cmd/dlv@v1.7.0
26 changes: 26 additions & 0 deletions build/images/base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Base Image for Debugging

## Build Base Image

```shell
BASE_IMAGE=dragonflyoss/base:bpftrace-v0.13.0-go-v1.16.6

docker run -ti -v /usr/src:/usr/src:ro \
-v /lib/modules/:/lib/modules:ro \
-v /sys/kernel/debug/:/sys/kernel/debug:rw \
--net=host --pid=host --privileged \
${BASE_IMAGE}
```

## Build Dragonfly Images

```shell
# tags for condition compiling
export GOTAGS="debug"
# gcflags for dlv
export GOGCFLAGS="all=-N -l"
# base image
export BASE_IMAGE=dragonflyoss/base:bpftrace-v0.13.0-go-v1.16.6

make docker-build
```
4 changes: 3 additions & 1 deletion build/images/cdn/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15.10-alpine as builder
FROM golang:1.16.6-alpine as builder

WORKDIR /go/src/d7y.io/dragonfly/v2

Expand All @@ -8,9 +8,11 @@ COPY . /go/src/d7y.io/dragonfly/v2

ARG GOPROXY
ARG GOTAGS
ARG GOGCFLAGS

RUN make build-cdn && make install-cdn

# TODO support BASE_IMAGE
FROM nginx:1.19-alpine

COPY --from=builder /go/src/d7y.io/dragonfly/v2/hack/start-cdn.sh /root/start.sh
Expand Down
9 changes: 6 additions & 3 deletions build/images/dfdaemon/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM golang:1.15.10-alpine as builder
ARG BASE_IMAGE=alpine:3.12

FROM golang:1.16.6-alpine as builder

WORKDIR /go/src/d7y.io/dragonfly/v2

Expand All @@ -8,12 +10,13 @@ COPY . /go/src/d7y.io/dragonfly/v2

ARG GOPROXY
ARG GOTAGS
ARG GOGCFLAGS

RUN make build-dfget && make install-dfget

FROM alpine:3.12
FROM ${BASE_IMAGE}

ENV PATH=/opt/dragonfly/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV PATH=/opt/dragonfly/bin:$PATH

COPY --from=builder /opt/dragonfly/bin/dfget /opt/dragonfly/bin/dfget

Expand Down
9 changes: 6 additions & 3 deletions build/images/manager/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM golang:1.15.10-alpine as builder
ARG BASE_IMAGE=alpine:3.12

FROM golang:1.16.6-alpine as builder

WORKDIR /go/src/d7y.io/dragonfly/v2

Expand All @@ -8,12 +10,13 @@ COPY . /go/src/d7y.io/dragonfly/v2

ARG GOPROXY
ARG GOTAGS
ARG GOGCFLAGS

RUN make build-manager && make install-manager

FROM alpine:3.12
FROM ${BASE_IMAGE}

ENV PATH=/opt/dragonfly/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV PATH=/opt/dragonfly/bin:$PATH

COPY --from=builder /opt/dragonfly/bin/manager /opt/dragonfly/bin/manager

Expand Down
9 changes: 6 additions & 3 deletions build/images/scheduler/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM golang:1.15.10-alpine as builder
ARG BASE_IMAGE=alpine:3.12

FROM golang:1.16.6-alpine as builder

WORKDIR /go/src/d7y.io/dragonfly/v2

Expand All @@ -8,12 +10,13 @@ COPY . /go/src/d7y.io/dragonfly/v2

ARG GOPROXY
ARG GOTAGS
ARG GOGCFLAGS

RUN make build-scheduler && make install-scheduler

FROM alpine:3.12
FROM ${BASE_IMAGE}

ENV PATH=/opt/dragonfly/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV PATH=/opt/dragonfly/bin:$PATH

COPY --from=builder /opt/dragonfly/bin/scheduler /opt/dragonfly/bin/scheduler

Expand Down
12 changes: 9 additions & 3 deletions cmd/dependency/version_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ var VersionCmd = &cobra.Command{
DisableAutoGenTag: true,
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Major: %s Minor: %s\n", version.Major, version.Minor)
fmt.Printf("GitVersion: %s\n", version.GitVersion)
fmt.Printf("Platform: %s GoVersion: %s BuildDay: %s\n", version.Platform, version.GoVersion, version.BuildDay)
fmt.Printf("MajorVersion:\t%s\n", version.Major)
fmt.Printf("MinorVersion:\t%s\n", version.Minor)
fmt.Printf("GitVersion:\t%s\n", version.GitVersion)
fmt.Printf("GitCommit:\t%s\n", version.GitCommit)
fmt.Printf("Platform:\t%s\n", version.Platform)
fmt.Printf("BuildTime:\t%s\n", version.BuildTime)
fmt.Printf("GoVersion:\t%s\n", version.GoVersion)
fmt.Printf("Gotags: \t%s\n", version.Gotags)
fmt.Printf("Gogcflags:\t%s\n", version.Gogcflags)
},
}
2 changes: 1 addition & 1 deletion deploy/charts/dragonfly/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cdn:
image: dragonflyoss/cdn
tag: v0.1.0-beta
kubectlImage: bitnami/kubectl
pullPolicy: Always
pullPolicy: IfNotPresent
resources:
requests:
cpu: "0"
Expand Down
13 changes: 11 additions & 2 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ MANAGER_BINARY_NAME=manager

PKG=d7y.io/dragonfly/v2
BUILD_IMAGE=golang:1.15.8

VERSION=$(git rev-parse --short HEAD)
BUILD_TIME=$(date -u '+%Y-%m-%dT%H:%M:%SZ')

GOPROXY=${GOPROXY:-}
DATE=$(date "+%Y%m%d-%H:%M:%S")
GOTAGS=${GOTAGS:-}
GOGCFLAGS=${GOGCFLAGS:-}
GOLDFLAGS="-X d7y.io/dragonfly/v2/version.GitCommit=${VERSION}"
GOLDFLAGS="${GOLDFLAGS} -X d7y.io/dragonfly/v2/version.BuildTime=${BUILD_TIME}"
GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.Gotags=${GOTAGS:-none}\""
GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.GoVersion=$(go version | grep -o 'go[^ ].*')\""
GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.Gogcflags=${GOGCFLAGS:-none}\""

curDir=$(cd "$(dirname "$0")" && pwd)
cd "${curDir}" || return
Expand All @@ -33,7 +42,7 @@ create-dirs() {
build-local() {
test -f "${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1" && rm -f "${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
cd "${BUILD_SOURCE_HOME}/cmd/$2" || return
go build -tags="${GOTAGS}" -o="${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
go build -tags="${GOTAGS}" -ldflags="${GOLDFLAGS}" -gcflags="${GOGCFLAGS}" -o="${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
chmod a+x "${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
echo "BUILD: $2 in ${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
}
Expand Down
49 changes: 27 additions & 22 deletions hack/docker-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,49 @@ set -o nounset
set -o errexit
set -o pipefail

curDir=$(cd "$(dirname "$0")" && pwd)
cd "${curDir}/../" || return

D7Y_VERSION=${D7Y_VERSION:-"latest"}
D7Y_REGISTRY=${D7Y_REGISTRY:-d7yio}
GOPROXY=${GOPROXY:-}
curDir=$(cd "$(dirname "$0")" && pwd)
IMAGES_DIR="build/images"
cd "${curDir}/../" || return
BASE_IMAGE=${BASE_IMAGE:-alpine:3.12}

GOPROXY=${GOPROXY:-`go env GOPROXY`}
GOTAGS=${GOTAGS:-}

docker-build::build-cdn() {
docker build --build-arg GOPROXY="${GOPROXY}" -t "${D7Y_REGISTRY}"/cdn:"${D7Y_VERSION}" -f "${IMAGES_DIR}/cdn/Dockerfile" .
}

docker-build::build-dfdaemon() {
docker build --build-arg GOPROXY="${GOPROXY}" --build-arg GOTAGS="${GOTAGS}" -t "${D7Y_REGISTRY}"/dfdaemon:"${D7Y_VERSION}" -f "${IMAGES_DIR}/dfdaemon/Dockerfile" .
}

docker-build::build-scheduler() {
docker build --build-arg GOPROXY="${GOPROXY}" -t "${D7Y_REGISTRY}"/scheduler:"${D7Y_VERSION}" -f "${IMAGES_DIR}/scheduler/Dockerfile" .
}

docker-build::build-manager() {
docker build --build-arg GOPROXY="${GOPROXY}" -t "${D7Y_REGISTRY}"/manager:"${D7Y_VERSION}" -f "${IMAGES_DIR}/manager/Dockerfile" .
GOGCFLAGS=${GOGCFLAGS:-}

# enable bash debug output
DEBUG=${DEBUG:-}

if [[ -n "$DEBUG" ]]; then
set -x
fi

docker-build() {
name=$1
docker build \
--build-arg GOPROXY="${GOPROXY}" \
--build-arg GOTAGS="${GOTAGS}" \
--build-arg GOGCFLAGS="${GOGCFLAGS}" \
--build-arg BASE_IMAGE="${BASE_IMAGE}" \
-t "${D7Y_REGISTRY}/${name}:${D7Y_VERSION}" \
-f "${IMAGES_DIR}/${name}/Dockerfile" .
}

main() {
case "${1-}" in
cdn)
docker-build::build-cdn
docker-build cdn
;;
dfdaemon)
docker-build::build-dfdaemon
docker-build dfdaemon
;;
scheduler)
docker-build::build-scheduler
docker-build scheduler
;;
manager)
docker-build::build-manager
docker-build manager
esac
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (conn *Connection) GetClientConn(hashKey string, stick bool) (*grpc.ClientC
if stick && !ok {
conn.rwMutex.RUnlock()
// if request is stateful, hash key must exist in key2NodeMap
return nil, fmt.Errorf("it is a stateful request but cannot find hash key(%s) in key2NodeMap", hashKey)
return nil, fmt.Errorf("it is a stateful request, but cannot find hash key(%s) in key2NodeMap", hashKey)
}
if ok {
// if exist
Expand Down
10 changes: 5 additions & 5 deletions scheduler/core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *state) start() {
}
parent, candidates, hashParent := s.sched.ScheduleParent(peer)
if !hashParent && !peer.Host.CDN {
logger.WithTaskAndPeerID(peer.Task.TaskID, peer.PeerID).Warnf("waitScheduleParentPeerQueue: there is no available parentreschedule it in one second")
logger.WithTaskAndPeerID(peer.Task.TaskID, peer.PeerID).Warnf("waitScheduleParentPeerQueue: there is no available parent, reschedule it in one second")
s.waitScheduleParentPeerQueue.Done(v)
s.waitScheduleParentPeerQueue.AddAfter(peer, time.Second)
continue
Expand All @@ -96,7 +96,7 @@ var _ event = startReportPieceResultEvent{}
func (e startReportPieceResultEvent) apply(s *state) {
parent, candidates, hasParent := s.sched.ScheduleParent(e.peer)
if !hasParent {
logger.WithTaskAndPeerID(e.peer.Task.TaskID, e.peer.PeerID).Warnf("peerScheduleParentEvent: there is no available parentreschedule it in one second")
logger.WithTaskAndPeerID(e.peer.Task.TaskID, e.peer.PeerID).Warnf("peerScheduleParentEvent: there is no available parent, reschedule it in one second")
s.waitScheduleParentPeerQueue.AddAfter(e.peer, time.Second)
return
}
Expand All @@ -123,7 +123,7 @@ func (e peerDownloadPieceSuccessEvent) apply(s *state) {
var hasParent bool
parentPeer, candidates, hasParent = s.sched.ScheduleParent(e.peer)
if !hasParent {
logger.WithTaskAndPeerID(e.peer.Task.TaskID, e.peer.PeerID).Warnf("peerDownloadPieceSuccessEvent: there is no available parentreschedule it in one second")
logger.WithTaskAndPeerID(e.peer.Task.TaskID, e.peer.PeerID).Warnf("peerDownloadPieceSuccessEvent: there is no available parent, reschedule it in one second")
s.waitScheduleParentPeerQueue.AddAfter(e.peer, time.Second)
return
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func (e peerDownloadFailEvent) apply(s *state) {
child := (value).(*types.Peer)
parent, candidates, hasParent := s.sched.ScheduleParent(child)
if !hasParent {
logger.WithTaskAndPeerID(child.Task.TaskID, child.PeerID).Warnf("peerDownloadFailEvent: there is no available parentreschedule it in one second")
logger.WithTaskAndPeerID(child.Task.TaskID, child.PeerID).Warnf("peerDownloadFailEvent: there is no available parent, reschedule it in one second")
s.waitScheduleParentPeerQueue.AddAfter(e.peer, time.Second)
return true
}
Expand Down Expand Up @@ -306,7 +306,7 @@ func handlePeerLeave(peer *types.Peer, s *state) {
child := value.(*types.Peer)
parent, candidates, hasParent := s.sched.ScheduleParent(child)
if !hasParent {
logger.WithTaskAndPeerID(child.Task.TaskID, child.PeerID).Warnf("handlePeerLeave: there is no available parentreschedule it in one second")
logger.WithTaskAndPeerID(child.Task.TaskID, child.PeerID).Warnf("handlePeerLeave: there is no available parent, reschedule it in one second")
s.waitScheduleParentPeerQueue.AddAfter(child, time.Second)
return true
}
Expand Down
2 changes: 0 additions & 2 deletions scheduler/types/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ func (task *Task) GetLastAccessTime() time.Time {
}

func (task *Task) ListPeers() *sortedlist.SortedList {
task.lock.Lock()
defer task.lock.Unlock()
return task.peers
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/dfget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = Describe("Download with dfget", func() {
Expect(err).NotTo(HaveOccurred())
fmt.Println(podName)
Expect(strings.HasPrefix(podName, "dragonfly-dfdaemon-")).Should(BeTrue())
pod := e2eutil.NewPodExec(dragonflyNamespace, podName)
pod := e2eutil.NewPodExec(dragonflyNamespace, podName, "dfdaemon")

for _, v := range e2eutil.GetFileList() {
url := e2eutil.GetFileURL(v)
Expand Down
24 changes: 21 additions & 3 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@
package e2e

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"strings"
"testing"

"d7y.io/dragonfly/v2/test/e2e/e2eutil"
. "github.com/onsi/ginkgo" //nolint
. "github.com/onsi/gomega" //nolint
)

var _ = AfterSuite(func() {
out, err := e2eutil.KubeCtlCommand("-n", dragonflyNamespace, "get", "pod", "-l", "component=dfdaemon",
"-o", "jsonpath='{range .items[*]}{.metadata.name}{end}'").CombinedOutput()
podName := strings.Trim(string(out), "'")
Expect(err).NotTo(HaveOccurred())
Expect(strings.HasPrefix(podName, "dragonfly-dfdaemon-")).Should(BeTrue())

e2eutil.KubeCtlCopyCommand(dragonflyNamespace, podName, "/var/log/dragonfly/daemon/core.log", "/tmp/artifact/daemon.log").CombinedOutput()
e2eutil.KubeCtlCopyCommand(dragonflyNamespace, "dragonfly-cdn-0", "/var/log/dragonfly/cdn/core.log", "/tmp/artifact/cdn-0.log").CombinedOutput()
e2eutil.KubeCtlCopyCommand(dragonflyNamespace, "dragonfly-cdn-1", "/var/log/dragonfly/cdn/core.log", "/tmp/artifact/cdn-1.log").CombinedOutput()
e2eutil.KubeCtlCopyCommand(dragonflyNamespace, "dragonfly-cdn-2", "/var/log/dragonfly/cdn/core.log", "/tmp/artifact/cdn-2.log").CombinedOutput()
e2eutil.KubeCtlCopyCommand(dragonflyNamespace, "dragonfly-scheduler-0", "/var/log/dragonfly/scheduler/core.log", "/tmp/artifact/scheduler-0.log").CombinedOutput()
e2eutil.KubeCtlCopyCommand(dragonflyNamespace, "dragonfly-scheduler-1", "/var/log/dragonfly/scheduler/core.log", "/tmp/artifact/scheduler-1.log").CombinedOutput()
e2eutil.KubeCtlCopyCommand(dragonflyNamespace, "dragonfly-scheduler-2", "/var/log/dragonfly/scheduler/core.log", "/tmp/artifact/scheduler-2.log").CombinedOutput()
})

// TestE2E is the root of e2e test function
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down

0 comments on commit 83cd262

Please sign in to comment.