Skip to content

Commit

Permalink
feat: super openim chat k8s deployment and code remould (#240)
Browse files Browse the repository at this point in the history
* Adapting to k8s environment: modify service discovery and service registration logic,modify config file reading logic

* Submit initial chart script

* change chat script

* fix bug:if env.descovery==k8s,not check zk

* fix bug:Implement option interface

* fix bug:change K8sDR.Register

* change config.yaml

* docs: add openim chat options

Signed-off-by: Xinwei Xiong(cubxxw) <[email protected]>

* docs: add openim flag

Signed-off-by: Xinwei Xiong(cubxxw) <[email protected]>

* docs: add openim version

Signed-off-by: Xinwei Xiong(cubxxw) <[email protected]>

* docs: add openim version

Signed-off-by: Xinwei Xiong(cubxxw) <[email protected]>

* docs: add openim version

Signed-off-by: Xinwei Xiong(cubxxw) <[email protected]>

* docs: add openim version

Signed-off-by: Xinwei Xiong(cubxxw) <[email protected]>

* update the config code

* update the config code

* fix:solve the conflict

* fix: fix the flag parse error

* fix:slove the script error

* fix:alter the config.yaml

* fix:alter the comfig.yaml

* fix:alter a scripts-test.yml

---------

Signed-off-by: Xinwei Xiong(cubxxw) <[email protected]>
Co-authored-by: lin.huang <[email protected]>
Co-authored-by: xuexihuang <[email protected]>
Co-authored-by: luhaoling <[email protected]>
  • Loading branch information
4 people authored Nov 7, 2023
1 parent 595d5f3 commit 9d43247
Show file tree
Hide file tree
Showing 84 changed files with 2,508 additions and 361 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/chatci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:

strategy:
matrix:
go_version: ["1.18","1.19","1.20"]
go_version: ["1.19","1.20","1.21"]
os: [ubuntu-latest]

steps:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/docker-buildx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
file: ./build/docker/api-admin/Dockerfile
file: ./build/images/api-admin/Dockerfile
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta1.outputs.tags }}
Expand All @@ -70,7 +70,7 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
file: ./build/docker/api-chat/Dockerfile
file: ./build/images/api-chat/Dockerfile
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta2.outputs.tags }}
Expand All @@ -86,7 +86,7 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
file: ./build/docker/rpc-admin/Dockerfile
file: ./build/images/rpc-admin/Dockerfile
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta3.outputs.tags }}
Expand All @@ -102,7 +102,7 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
file: ./build/docker/rpc-chat/Dockerfile
file: ./build/images/rpc-chat/Dockerfile
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta4.outputs.tags }}
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/scripts-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ jobs:

- name: deploy all services
run: |
git clone -b release-v3.1 https://github.com/OpenIMSDK/Open-IM-Server.git openim && export openim=$(pwd)/openim && cd $openim
git clone -b release-v3.3 https://github.com/OpenIMSDK/Open-IM-Server.git openim && export openim=$(pwd)/openim && cd $openim
curl -o docker-compose.yaml https://gist.githubusercontent.com/cubxxw/b1d5cbd2edfa23fee911118aa3e8249e/raw/openim-chat.sh
sudo docker compose up -d
sudo sleep 60
sudo sleep 30
sudo chmod +x ./scripts/stop_all.sh
sudo ./scripts/stop_all.sh
Expand Down Expand Up @@ -82,10 +82,8 @@ jobs:

- name: Check all services
run: |
sudo ./scripts/check_all.sh
sudo cat ../logs/openIM.log 2>/dev/null
sudo sleep 15; ports=(10008 10009 30200 30300); for port in "${ports[@]}"; do if ! lsof -i :$port > /dev/null; then echo "The service is not started on port $port."; exit 1; else echo "The service is started normally on port $port."; fi; done; echo "All port services have been started normally."
shell: bash
continue-on-error: true

- name: Print openIM.log
run: |
Expand Down
8 changes: 4 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ changelog:
# - "--platform=linux/amd64"
# goos: linux
# goarch: amd64
# dockerfile: ./build/docker/api-chat/Dockerfile
# dockerfile: ./build/images/api-chat/Dockerfile
# use: buildx

# - image_templates:
Expand All @@ -254,7 +254,7 @@ changelog:
# - "--platform=linux/arm64"
# goos: linux
# goarch: arm64
# dockerfile: ./build/docker/api-chat/Dockerfile
# dockerfile: ./build/images/api-chat/Dockerfile
# use: buildx

# - image_templates:
Expand All @@ -276,7 +276,7 @@ changelog:
# - "--platform=linux/amd64"
# goos: linux
# goarch: amd64
# dockerfile: ./build/docker/api-admin/Dockerfile
# dockerfile: ./build/images/api-admin/Dockerfile
# use: buildx

# - image_templates:
Expand All @@ -298,7 +298,7 @@ changelog:
# - "--platform=linux/arm64"
# goos: linux
# goarch: arm64
# dockerfile: ./build/docker/api-admin/Dockerfile
# dockerfile: ./build/images/api-admin/Dockerfile
# use: buildx

# docker_manifests:
Expand Down
76 changes: 68 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
# get the repo root and output path
ROOT_PACKAGE=github.com/OpenIM/chat
OUT_DIR=$(REPO_ROOT)/_output
VERSION_PACKAGE=github.com/OpenIMSDK/chat/pkg/common/version
# ==============================================================================


# define the default goal
#

Expand Down Expand Up @@ -114,16 +116,17 @@ SPACE +=
# ==============================================================================
# Build definition

GO_SUPPORTED_VERSIONS ?= 1.18|1.19|1.20|1.21
GO_LDFLAGS += -X $(VERSION_PACKAGE).GitVersion=$(VERSION) \
-X $(VERSION_PACKAGE).GitCommit=$(GIT_COMMIT) \
-X $(VERSION_PACKAGE).GitTreeState=$(GIT_TREE_STATE) \
-X $(VERSION_PACKAGE).BuildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
ifneq ($(DLV),)
GO_SUPPORTED_VERSIONS ?= 1.19|1.20|1.21|1.22
GO_LDFLAGS += -X $(VERSION_PACKAGE).gitVersion=$(VERSION) \
-X $(VERSION_PACKAGE).gitCommit=$(GIT_COMMIT) \
-X $(VERSION_PACKAGE).gitTreeState=$(GIT_TREE_STATE) \
-X $(VERSION_PACKAGE).buildDate=$(shell date +%FT%T%z) \
-s -w
ifneq ($(DEBUG),)
GO_BUILD_FLAGS += -gcflags "all=-N -l"
LDFLAGS = ""
endif
GO_BUILD_FLAGS += -ldflags "$(GO_LDFLAGS)"
GO_BUILD_FLAGS += -tags "containers_image_openpgp netgo exclude_graphdriver_devicemapper static osusergo exclude_graphdriver_btrfs" -trimpath -ldflags "$(GO_LDFLAGS)"

ifeq ($(GOOS),windows)
GO_OUT_EXT := .exe
Expand All @@ -150,6 +153,39 @@ endif

EXCLUDE_TESTS=github.com/OpenIMSDK/chat/test

# ==============================================================================
# Docker build definition
# Image and Deployment
#
DOCKER := docker

# read: https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/images.md
REGISTRY_PREFIX ?= registry.cn-hangzhou.aliyuncs.com/openimsdk #ghcr.io/openimsdk

BASE_IMAGE ?= ghcr.io/openim-sigs/openim-bash-image

IMAGE_PLAT ?= $(subst $(SPACE),$(COMMA),$(subst _,/,$(PLATFORMS)))

EXTRA_ARGS ?= --no-cache
_DOCKER_BUILD_EXTRA_ARGS :=

ifdef HTTP_PROXY
_DOCKER_BUILD_EXTRA_ARGS += --build-arg HTTP_PROXY=${HTTP_PROXY}
endif

ifneq ($(EXTRA_ARGS), )
_DOCKER_BUILD_EXTRA_ARGS += $(EXTRA_ARGS)
endif

# Determine image files by looking into build/images/*/Dockerfile
IMAGES_DIR ?= $(wildcard ${ROOT_DIR}/build/images/*)
# Determine images names by stripping out the dir names, and filter out the undesired directories
IMAGES ?= $(filter-out Dockerfile, $(foreach image,${IMAGES_DIR},$(notdir ${image})))

ifeq (${IMAGES},)
$(error Could not determine IMAGES, set ROOT_DIR or run in source dir)
endif

# ==============================================================================
# Build

Expand Down Expand Up @@ -188,7 +224,6 @@ go.build.%:
@cd $(ROOT_DIR)/cmd/*/$(COMMAND) && CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o \
$(BIN_DIR)/platforms/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) .


## build-multiarch: Build binaries for multiple platforms.
.PHONY: build-multiarch
build-multiarch: go.build.verify $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS))))
Expand Down Expand Up @@ -267,6 +302,31 @@ stop:
@echo "===========> Stopping the service"
@$(ROOT_DIR)/scripts/stop_all.sh

## restart: Restart openim chat
.PHONY: restart
restart: clean stop build start check

## image.build.%: Build docker image for a specific platform
.PHONY: image.build.%
image.build.%: go.build.%
$(eval IMAGE := $(COMMAND))
$(eval IMAGE_PLAT := $(subst _,/,$(PLATFORM)))
$(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM))))
@echo "===========> Building docker image $(IMAGE) $(VERSION) for $(IMAGE_PLAT)"
@mkdir -p $(TMP_DIR)/$(IMAGE)/$(PLATFORM)
@cat $(ROOT_DIR)/build/images/Dockerfile\
| sed "s#BASE_IMAGE#$(BASE_IMAGE)#g" \
| sed "s#BINARY_NAME#$(IMAGE)#g" >$(TMP_DIR)/$(IMAGE)/Dockerfile
@cp $(BIN_DIR)/platforms/$(IMAGE_PLAT)/$(IMAGE) $(TMP_DIR)/$(IMAGE)
$(eval BUILD_SUFFIX := $(_DOCKER_BUILD_EXTRA_ARGS) --pull -t $(REGISTRY_PREFIX)/$(IMAGE)-$(ARCH):$(VERSION) $(TMP_DIR)/$(IMAGE))
@if [ $(shell $(GO) env GOARCH) != $(ARCH) ] ; then \
$(MAKE) image.daemon.verify ;\
$(DOCKER) build --platform $(IMAGE_PLAT) $(BUILD_SUFFIX) ; \
else \
$(DOCKER) build $(BUILD_SUFFIX) ; \
fi
@rm -rf $(TMP_DIR)/$(IMAGE)

## docker-build: Build docker image with the manager.
.PHONY: docker-build
docker-build:
Expand Down
65 changes: 65 additions & 0 deletions build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Building OpenIM

Building OpenIM is easy if you take advantage of the containerized build environment. This document will help guide you through understanding this build process.

## Requirements

1. Docker, using one of the following configurations:
* **macOS** Install Docker for Mac. See installation instructions [here](https://docs.docker.com/docker-for-mac/).
**Note**: You will want to set the Docker VM to have at least 4GB of initial memory or building will likely fail.
* **Linux with local Docker** Install Docker according to the [instructions](https://docs.docker.com/installation/#installation) for your OS.
* **Windows with Docker Desktop WSL2 backend** Install Docker according to the [instructions](https://docs.docker.com/docker-for-windows/wsl-tech-preview/). Be sure to store your sources in the local Linux file system, not the Windows remote mount at `/mnt/c`.

**Note**: You will need to check if Docker CLI plugin buildx is properly installed (`docker-buildx` file should be present in `~/.docker/cli-plugins`). You can install buildx according to the [instructions](https://github.com/docker/buildx/blob/master/README.md#installing).

2. **Optional** [Google Cloud SDK](https://developers.google.com/cloud/sdk/)

You must install and configure Google Cloud SDK if you want to upload your release to Google Cloud Storage and may safely omit this otherwise.

## Actions

About [Images packages](https://github.com/orgs/OpenIMSDK/packages?repo_name=Open-IM-Server)

All files in the `build/images` directory are not templated and are instead rendered by Github Actions, which is an automated process.

Trigger condition:
1. create a new tag with the format `vX.Y.Z` (e.g. `v1.0.0`)
2. push the tag to the remote repository
3. wait for the build to finish
4. download the artifacts from the release page

## Make images

**help info:**

```bash
$ make image.help
```

**build images:**

```bash
$ make image
```

## Overview

While it is possible to build OpenIM using a local golang installation, we have a build process that runs in a Docker container. This simplifies initial set up and provides for a very consistent build and test environment.


## Basic Flow

The scripts directly under [`build/`](.) are used to build and test. They will ensure that the `openim-build` Docker image is built (based on [`build/build-image/Dockerfile`](../Dockerfile) and after base image's `OPENIM_BUILD_IMAGE_CROSS_TAG` from Dockerfile is replaced with one of those actual tags of the base image, like `v1.13.9-2`) and then execute the appropriate command in that container. These scripts will both ensure that the right data is cached from run to run for incremental builds and will copy the results back out of the container. You can specify a different registry/name and version for `openim-cross` by setting `OPENIM_CROSS_IMAGE` and `OPENIM_CROSS_VERSION`, see [`common.sh`](common.sh) for more details.

The `openim-build` container image is built by first creating a "context" directory in `_output/images/build-image`. It is done there instead of at the root of the OpenIM repo to minimize the amount of data we need to package up when building the image.

There are 3 different containers instances that are run from this image. The first is a "data" container to store all data that needs to persist across to support incremental builds. Next there is an "rsync" container that is used to transfer data in and out to the data container. Lastly there is a "build" container that is used for actually doing build actions. The data container persists across runs while the rsync and build containers are deleted after each use.

`rsync` is used transparently behind the scenes to efficiently move data in and out of the container. This will use an ephemeral port picked by Docker. You can modify this by setting the `OPENIM_RSYNC_PORT` env variable.

All Docker names are suffixed with a hash derived from the file path (to allow concurrent usage on things like CI machines) and a version number. When the version number changes all state is cleared and clean build is started. This allows the build infrastructure to be changed and signal to CI systems that old artifacts need to be deleted.

## Build artifacts
The build system output all its products to a top level directory in the source repository named `_output`.
These include the binary compiled packages (e.g. imctl, openim-api etc.) and archived Docker images.
If you intend to run a component with a docker image you will need to import it from this directory with
10 changes: 10 additions & 0 deletions build/images/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM BASE_IMAGE

WORKDIR ${SERVER_WORKDIR}

# Set HTTP proxy
ARG BINARY_NAME

COPY BINARY_NAME ./bin/BINARY_NAME

ENTRYPOINT ["./bin/BINARY_NAME"]
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ RUN go mod download

COPY . .

RUN make clean
RUN make build BINS=admin-api

FROM ghcr.io/openim-sigs/openim-bash-image:latest
RUN cp /openim/openim-chat/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/admin-api /usr/bin/admin-api

WORKDIR ${CHAT_WORKDIR}

COPY --from=builder ${OPENIM_CHAT_BINDIR}/platforms /openim/openim-chat/_output/bin/platforms
COPY --from=builder ${OPENIM_CHAT_CONFIG_NAME} /openim/openim-chat/config/config.yaml
FROM ghcr.io/openim-sigs/openim-bash-image:latest

ENV PORT=10009
EXPOSE 10009
WORKDIR ${CHAT_WORKDIR}

RUN mv ${OPENIM_CHAT_BINDIR}/platforms/$(get_os)/$(get_arch)/admin-api /usr/bin/admin-api
COPY --from=builder /usr/bin/admin-api ./bin/admin-api

ENTRYPOINT ["bash", "-c", "admin-api --port $PORT -c $OPENIM_CHAT_CONFIG_NAME"]
ENTRYPOINT ["./bin/admin-api"]
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# OpenIM base image: https://github.com/openim-sigs/openim-base-image

# Set go mod installation source and proxy

ARG GOARCH
ARG GOOS

Expand All @@ -31,19 +34,15 @@ RUN go mod download

COPY . .

RUN make clean
RUN make build BINS=chat-api

FROM ghcr.io/openim-sigs/openim-bash-image:latest
RUN cp /openim/openim-chat/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/chat-api /usr/bin/chat-api

WORKDIR ${CHAT_WORKDIR}

COPY --from=builder ${OPENIM_CHAT_BINDIR}/platforms /openim/openim-chat/_output/bin/platforms
COPY --from=builder ${OPENIM_CHAT_CONFIG_NAME} /openim/openim-chat/config/config.yaml
FROM ghcr.io/openim-sigs/openim-bash-image:latest

ENV PORT=10008
EXPOSE 10008
WORKDIR ${CHAT_WORKDIR}

RUN mv ${OPENIM_CHAT_BINDIR}/platforms/$(get_os)/$(get_arch)/chat-api /usr/bin/chat-api
COPY --from=builder /usr/bin/chat-api ./bin/chat-api

ENTRYPOINT ["bash", "-c", "chat-api --port $PORT -c $OPENIM_CHAT_CONFIG_NAME"]
ENTRYPOINT ["./bin/chat-api"]
Loading

0 comments on commit 9d43247

Please sign in to comment.