diff --git a/.dockerignore b/.dockerignore index 5c11ea14736114f37ad170f9e2c4c2eed0097c74..08cfd041886030d69ffd073a7e7cc937f6a5ea37 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,12 @@ -.idea -ygot +.git +.gitlab +.cobra.yaml +.dockerignore +.gitlab-ci.yaml +CONTRIBUTING.md +README.md +ci +doc +artifacts +build-tools +models diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9837b0f9ef4baf4fd913aebc17cf912636280c69..5ad82946c0e48a1fed2b733cb4814722c5fff26f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,7 +3,7 @@ stages: variables: IMAGE_PATH: "${CI_REGISTRY_IMAGE}" - + .build: &build stage: build @@ -18,7 +18,7 @@ build-gnmi-target-ubuntu: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - TAG=${CI_COMMIT_BRANCH//\//-} - IMAGE_NAME="$IMAGE_PATH/ubuntu" - - docker buildx build -t "$IMAGE_NAME:$TAG" -f Dockerfile --target "ubuntu" . + - docker buildx build -t "$IMAGE_NAME:$TAG" -f target.Dockerfile --target "ubuntu" . - docker push "$IMAGE_NAME:$TAG" <<: *build @@ -27,6 +27,6 @@ build-gnmi-target-debian: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - TAG=${CI_COMMIT_BRANCH//\//-} - IMAGE_NAME="$IMAGE_PATH/debian" - - docker buildx build -t "$IMAGE_NAME:$TAG" -f Dockerfile --target "debian" . + - docker buildx build -t "$IMAGE_NAME:$TAG" -f target.Dockerfile --target "debian" . - docker push "$IMAGE_NAME:$TAG" <<: *build diff --git a/Dockerfile.debug b/Dockerfile.debug index edcf701e5893616262ffdceaa88bae6eb2df42bd..4b322e91938bd3b86d6dbd2600c9b536f0ec6dc7 100644 --- a/Dockerfile.debug +++ b/Dockerfile.debug @@ -1,7 +1,20 @@ -ARG GOLANG_VERSION=1.18 +ARG GOLANG_VERSION=1.20.3 +ARG BUILDARGS -FROM ubuntu:22.04 -RUN apt-get update && apt-get -y install golang-go -EXPOSE 7030 +FROM golang:$GOLANG_VERSION-buster as builder +WORKDIR /gnmi-target/ COPY . . -ENTRYPOINT ["go", "run", "main.go"] +RUN --mount=type=cache,target=/root/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build +RUN go install github.com/go-delve/delve/cmd/dlv@v1.20.2 +RUN make build-debug + +FROM ubuntu:22.04 as ubuntu +EXPOSE 7030 +WORKDIR /debug/ +RUN apt-get update && apt-get upgrade -y +RUN apt-get install -y iproute2 +RUN apt-get install -y iputils-ping +COPY --from=builder /go/bin/dlv /debug/ +COPY --from=builder /gnmi-target/artifacts/gnmi-target /debug/ +CMD [ "/debug/dlv", "--listen=:4000", "--headless=true", "--log=true", "--accept-multiclient", "--api-version=2", "exec", "/debug/gnmi-target", "start" ] diff --git a/Makefile b/Makefile index c4013d1418da4ee7cfc9d26d0e8bce331e59ff9d..1a9be4cd9ddb8fa114bf41d3269143f7e10146ec 100644 --- a/Makefile +++ b/Makefile @@ -21,20 +21,23 @@ pre: install-tools: @echo Install development tooling mkdir -p $(GOSDN_PRG) - export GOBIN=$(GOSDN_PRG) && go install github.com/openconfig/ygot/generator@v0.18.1 &&\ + export GOBIN=$(GOSDN_PRG) && go install github.com/openconfig/ygot/generator@v0.27.0 &&\ go install github.com/andresterba/go-ygot-generator-generator@v0.0.4 @echo Finished installing development tooling build: pre $(GOBUILD) -o $(BUILD_ARTIFACTS_PATH)/gnmi-target ./main.go +build-debug: pre + $(GOBUILD) -gcflags="all=-N -l" -o $(BUILD_ARTIFACTS_PATH)/gnmi-target ./main.go + generate-yang-models: install-tools cd modeldata/gnmitargetygot &&\ ../../$(TOOLS_DIR)/go-ygot-generator-generator config.yaml gostructs.go &&\ go generate container: build - docker buildx build --rm -t gnmi-target --load -f ./Dockerfile . + docker buildx build --rm -t gnmi-target --load -f ./target.Dockerfile . container-debug: docker buildx build --rm -t gnmi-target-debug --load -f ./Dockerfile.debug . diff --git a/ci/.deploy-container-ci.yml b/ci/.deploy-container-ci.yml index 5766eefe28db4926ff95a5178d319ee2ca096ad6..3ef29df643d4e1c995bb6fe781cbfc8519c53d31 100644 --- a/ci/.deploy-container-ci.yml +++ b/ci/.deploy-container-ci.yml @@ -41,7 +41,7 @@ build:image: extends: .build_template stage: build script: - - docker buildx build --platform linux/amd64,linux/arm64 -t $TAG -f Dockerfile --push . + - docker buildx build --platform linux/amd64,linux/arm64 -t $TAG -f target.Dockerfile --push . rules: - !reference [.default_rules_dev, rules] - !reference [.default_rules_prod, rules] diff --git a/doc/debug.md b/doc/debug.md new file mode 100644 index 0000000000000000000000000000000000000000..906727e85c459173711de71092ffe999182a165f --- /dev/null +++ b/doc/debug.md @@ -0,0 +1,34 @@ +# Debug + +Running the target in an encapsulated environment is recommended. + +## Debugging using Dockerfile.debug + +A Dockerfile for debugging (`Dockerfile.debug`) is provided within the +repositories root. + +### Usage: + +The following assumes that VSCode is used. + +1. Extend `launch.json`: + +``` +{ + "version": "0.2.0", + "configurations": [ + ... + { + "name": "Go Containerized Debug", + "type": "go", + "request": "attach", + "mode": "remote", + "port": 4000, + "host": "127.0.0.1" + } + ] +} +``` +2. Build the debug container: `make container-debug` +3. Run the container: `docker run --rm --privileged -d -p 7030:7030 -p 4000:4000 gnmi-target-debug:latest` +4. Set breakpoints and attach to the running container by using the configuration added in step 1 diff --git a/Dockerfile b/target.Dockerfile similarity index 75% rename from Dockerfile rename to target.Dockerfile index 56d5a8dded5c0337ce6e14174bcc2f74291a7528..d7b949efe8823aa2b1322360abc7d7cd3ce07f22 100644 --- a/Dockerfile +++ b/target.Dockerfile @@ -1,13 +1,12 @@ -ARG GOLANG_VERSION=1.18 +ARG GOLANG_VERSION=1.20.3 +ARG BUILDARGS -FROM golang:$GOLANG_VERSION-buster AS installer +FROM golang:$GOLANG_VERSION-buster as builder WORKDIR /gnmi-target/ -COPY go.* ./ -RUN go mod download - -FROM installer as builder COPY . . -RUN make build +RUN --mount=type=cache,target=/root/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + make build FROM ubuntu:22.04 as ubuntu RUN apt-get update && apt-get upgrade -y diff --git a/target.Dockerfile.dockerignore b/target.Dockerfile.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..08cfd041886030d69ffd073a7e7cc937f6a5ea37 --- /dev/null +++ b/target.Dockerfile.dockerignore @@ -0,0 +1,12 @@ +.git +.gitlab +.cobra.yaml +.dockerignore +.gitlab-ci.yaml +CONTRIBUTING.md +README.md +ci +doc +artifacts +build-tools +models