diff --git a/.github/workflows/chatci.yml b/.github/workflows/chatci.yml index 0d124f695..7f707246e 100644 --- a/.github/workflows/chatci.yml +++ b/.github/workflows/chatci.yml @@ -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: diff --git a/.github/workflows/docker-buildx.yml b/.github/workflows/docker-buildx.yml index d2543f302..224df06b2 100644 --- a/.github/workflows/docker-buildx.yml +++ b/.github/workflows/docker-buildx.yml @@ -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 }} @@ -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 }} @@ -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 }} @@ -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 }} diff --git a/.github/workflows/scripts-test.yml b/.github/workflows/scripts-test.yml index edde4240f..8ac23e69d 100644 --- a/.github/workflows/scripts-test.yml +++ b/.github/workflows/scripts-test.yml @@ -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 @@ -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: | diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 745582659..010f71405 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/Makefile b/Makefile index 3e1a0fa07..a4924343a 100644 --- a/Makefile +++ b/Makefile @@ -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 # @@ -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 @@ -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 @@ -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)))) @@ -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: diff --git a/build/README.md b/build/README.md new file mode 100644 index 000000000..edd419ae5 --- /dev/null +++ b/build/README.md @@ -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 diff --git a/build/images/Dockerfile b/build/images/Dockerfile new file mode 100644 index 000000000..518de78b1 --- /dev/null +++ b/build/images/Dockerfile @@ -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"] \ No newline at end of file diff --git a/build/docker/api-admin/Dockerfile b/build/images/api-admin/Dockerfile similarity index 73% rename from build/docker/api-admin/Dockerfile rename to build/images/api-admin/Dockerfile index a1aeaf1f1..6d7b713d2 100644 --- a/build/docker/api-admin/Dockerfile +++ b/build/images/api-admin/Dockerfile @@ -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"] \ No newline at end of file diff --git a/build/docker/api-chat/Dockerfile b/build/images/api-chat/Dockerfile similarity index 72% rename from build/docker/api-chat/Dockerfile rename to build/images/api-chat/Dockerfile index 1b5225fd3..3a06d51e8 100644 --- a/build/docker/api-chat/Dockerfile +++ b/build/images/api-chat/Dockerfile @@ -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 @@ -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"] \ No newline at end of file diff --git a/build/docker/rpc-admin/Dockerfile b/build/images/rpc-admin/Dockerfile similarity index 65% rename from build/docker/rpc-admin/Dockerfile rename to build/images/rpc-admin/Dockerfile index b77cf6110..736e21227 100644 --- a/build/docker/rpc-admin/Dockerfile +++ b/build/images/rpc-admin/Dockerfile @@ -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 @@ -31,22 +34,15 @@ RUN go mod download COPY . . -RUN make clean RUN make build BINS=admin-rpc -FROM ghcr.io/openim-sigs/openim-bash-image:latest - -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 +RUN cp /openim/openim-chat/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/admin-rpc /usr/bin/admin-rpc -CMD ["bash", "-c", "${OPENIM_CHAT_BINDIR}/platforms/$(get_os)/$(get_arch)/admin-rpc --port ${PORT} --config_folder_path ${OPENIM_CHAT_CONFIG_NAME}"] +FROM ghcr.io/openim-sigs/openim-bash-image:latest -ENV PORT=30200 -EXPOSE 30200 +WORKDIR ${CHAT_WORKDIR} -RUN mv ${OPENIM_CHAT_BINDIR}/platforms/$(get_os)/$(get_arch)/admin-rpc /usr/bin/admin-rpc +COPY --from=builder /usr/bin/admin-rpc ./bin/admin-rpc -ENTRYPOINT ["bash", "-c", "admin-rpc --port $PORT -c $OPENIM_CHAT_CONFIG_NAME"] +ENTRYPOINT ["./bin/admin-rpc"] \ No newline at end of file diff --git a/build/docker/rpc-chat/Dockerfile b/build/images/rpc-chat/Dockerfile similarity index 68% rename from build/docker/rpc-chat/Dockerfile rename to build/images/rpc-chat/Dockerfile index 436254ba7..e9ffa383f 100644 --- a/build/docker/rpc-chat/Dockerfile +++ b/build/images/rpc-chat/Dockerfile @@ -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 @@ -31,21 +34,15 @@ RUN go mod download COPY . . -RUN make clean RUN make build BINS=chat-rpc -RUN rm -rf ./_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/admin-rpc +RUN cp /openim/openim-chat/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/chat-rpc /usr/bin/chat-rpc + FROM ghcr.io/openim-sigs/openim-bash-image:latest 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 - -ENV PORT=30300 -EXPOSE 30300 - -RUN mv ${OPENIM_CHAT_BINDIR}/platforms/$(get_os)/$(get_arch)/chat-rpc /usr/bin/chat-rpc +COPY --from=builder /usr/bin/chat-rpc ./bin/chat-rpc -ENTRYPOINT ["bash", "-c", "chat-rpc --port $PORT -c $OPENIM_CHAT_CONFIG_NAME"] +ENTRYPOINT ["./bin/chat-rpc"] \ No newline at end of file diff --git a/cmd/api/admin-api/Makefile b/cmd/api/admin-api/Makefile deleted file mode 100644 index 3b8b11dd4..000000000 --- a/cmd/api/admin-api/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -.PHONY: all build run gotool install clean help - -BINARY_NAME=admin-api -BIN_DIR=../../../bin/ - - -all: gotool build - -build: - CGO_ENABLED=0 GOOS=linux GOARCH=amd64; go build -ldflags="-w -s" -o ${BINARY_NAME} - -run: - @go run ./ - -gotool: - go fmt ./ - go vet ./ - -install: - make build - mv ${BINARY_NAME} ${BIN_DIR} - -clean: - @if [ -f ${BINARY_NAME} ] ; then rm ${BINARY_NAME} ; fi diff --git a/cmd/api/admin-api/main.go b/cmd/api/admin-api/main.go index e9cb6baba..07c66de6f 100644 --- a/cmd/api/admin-api/main.go +++ b/cmd/api/admin-api/main.go @@ -15,18 +15,21 @@ package main import ( - "flag" - "github.com/OpenIMSDK/chat/tools/component" + "fmt" + "github.com/OpenIMSDK/chat/pkg/discovery_register" "math/rand" "net" "strconv" "time" + "github.com/OpenIMSDK/chat/tools/component" + "github.com/OpenIMSDK/tools/discoveryregistry" + mw2 "github.com/OpenIMSDK/chat/pkg/common/mw" + "github.com/OpenIMSDK/chat/pkg/common/version" "github.com/OpenIMSDK/chat/internal/api" "github.com/OpenIMSDK/chat/pkg/common/config" - openKeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mw" "google.golang.org/grpc" @@ -37,18 +40,24 @@ import ( func main() { rand.Seed(time.Now().UnixNano()) - var configFile string - flag.StringVar(&configFile, "config_folder_path", "../config/config.yaml", "Config full path") - - var ginPort int - - flag.IntVar(&ginPort, "port", 10009, "get ginServerPort from cmd") + configFile, ginPort, hide, showVersion, err := config.FlagParse() + if err != nil { + panic(err) + } - var hide bool - flag.BoolVar(&hide, "hide", false, "hide the ComponentCheck result") + // Check if the version flag was set + if showVersion { + ver := version.Get() + fmt.Println("Version:", ver.GitVersion) + fmt.Println("Git Commit:", ver.GitCommit) + fmt.Println("Build Date:", ver.BuildDate) + fmt.Println("Go Version:", ver.GoVersion) + fmt.Println("Compiler:", ver.Compiler) + fmt.Println("Platform:", ver.Platform) + return + } - flag.Parse() - err := component.ComponentCheck(configFile, hide) + err = component.ComponentCheck(configFile, hide) if err != nil { return } @@ -58,9 +67,13 @@ func main() { if err := log.InitFromConfig("chat.log", "admin-api", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil { panic(err) } - zk, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger())) + if config.Config.Envs.Discovery == "k8s" { + ginPort = 80 + } + var zk discoveryregistry.SvcDiscoveryRegistry + zk, err = discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) + //zk, err = openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + // openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger())) if err != nil { panic(err) } diff --git a/cmd/api/chat-api/Makefile b/cmd/api/chat-api/Makefile deleted file mode 100644 index 7252c8b4f..000000000 --- a/cmd/api/chat-api/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -.PHONY: all build run gotool install clean help - -BINARY_NAME=chat-api -BIN_DIR=../../../bin/ - - -all: gotool build - -build: - CGO_ENABLED=0 GOOS=linux GOARCH=amd64; go build -ldflags="-w -s" -o ${BINARY_NAME} - -run: - @go run ./ - -gotool: - go fmt ./ - go vet ./ - -install: - make build - mv ${BINARY_NAME} ${BIN_DIR} - -clean: - @if [ -f ${BINARY_NAME} ] ; then rm ${BINARY_NAME} ; fi diff --git a/cmd/api/chat-api/main.go b/cmd/api/chat-api/main.go index ef3632c78..0c0a907fa 100644 --- a/cmd/api/chat-api/main.go +++ b/cmd/api/chat-api/main.go @@ -17,19 +17,21 @@ package main import ( "flag" "fmt" - "math/rand" "net" "strconv" "time" + "github.com/OpenIMSDK/chat/pkg/discovery_register" + "github.com/OpenIMSDK/tools/discoveryregistry" + "github.com/OpenIMSDK/chat/tools/component" mw2 "github.com/OpenIMSDK/chat/pkg/common/mw" + "github.com/OpenIMSDK/chat/pkg/common/version" "github.com/OpenIMSDK/chat/internal/api" "github.com/OpenIMSDK/chat/pkg/common/config" - openKeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mw" "google.golang.org/grpc" @@ -38,21 +40,34 @@ import ( "github.com/gin-gonic/gin" ) +var rng *rand.Rand + +func init() { + rng = rand.New(rand.NewSource(time.Now().UnixNano())) +} + func main() { - rand.Seed(time.Now().UnixNano()) - var configFile string - flag.StringVar(&configFile, "config_folder_path", "../config/config.yaml", "Config full path:") - // defaultPorts := config.Config.ChatApi.GinPort - var ginPort int - flag.IntVar(&ginPort, "port", 10008, "get ginServerPort from cmd") + configFile, ginPort, hide, showVersion, err := config.FlagParse() + if err != nil { + panic(err) + } - var hide bool - flag.BoolVar(&hide, "hide", true, "hide the ComponentCheck result") + // Check if the version flag was set + if showVersion { + ver := version.Get() + fmt.Println("Version:", ver.GitVersion) + fmt.Println("Git Commit:", ver.GitCommit) + fmt.Println("Build Date:", ver.BuildDate) + fmt.Println("Go Version:", ver.GoVersion) + fmt.Println("Compiler:", ver.Compiler) + fmt.Println("Platform:", ver.Platform) + return + } flag.Parse() - err := component.ComponentCheck(configFile, hide) + err = component.ComponentCheck(configFile, hide) if err != nil { return } @@ -64,9 +79,14 @@ func main() { if err := log.InitFromConfig("chat.log", "chat-api", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil { panic(err) } - zk, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger())) + if config.Config.Envs.Discovery == "k8s" { + ginPort = 80 + } + var zk discoveryregistry.SvcDiscoveryRegistry + zk, err = discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) + /*zk, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger()))*/ if err != nil { panic(err) } diff --git a/cmd/rpc/admin-rpc/Makefile b/cmd/rpc/admin-rpc/Makefile deleted file mode 100644 index f4ff0f59e..000000000 --- a/cmd/rpc/admin-rpc/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -.PHONY: all build run gotool install clean help - -BINARY_NAME=admin-rpc -BIN_DIR=../../../bin/ - -all: gotool build - -build: - CGO_ENABLED=0 GOOS=linux GOARCH=amd64; go build -ldflags="-w -s" -o ${BINARY_NAME} - -run: - @go run ./ - -gotool: - go fmt ./ - go vet ./ - -install: - make build - mv ${BINARY_NAME} ${BIN_DIR} - -clean: - @if [ -f ${BINARY_NAME} ] ; then rm ${BINARY_NAME} ; fi diff --git a/cmd/rpc/admin-rpc/main.go b/cmd/rpc/admin-rpc/main.go index 32391f3ed..872f8c5bb 100644 --- a/cmd/rpc/admin-rpc/main.go +++ b/cmd/rpc/admin-rpc/main.go @@ -16,11 +16,14 @@ package main import ( "flag" + "fmt" + "math/rand" + "time" + "github.com/OpenIMSDK/chat/pkg/common/chatrpcstart" + "github.com/OpenIMSDK/chat/pkg/common/version" "github.com/OpenIMSDK/chat/tools/component" "github.com/OpenIMSDK/tools/log" - "math/rand" - "time" "github.com/OpenIMSDK/chat/internal/rpc/admin" "github.com/OpenIMSDK/chat/pkg/common/config" @@ -28,24 +31,35 @@ import ( func main() { rand.Seed(time.Now().UnixNano()) - var configFile string - flag.StringVar(&configFile, "config_folder_path", "../config/config.yaml", "Config full path") - - var rpcPort int - flag.IntVar(&rpcPort, "port", 30200, "get rpc ServerPort from cmd") + configFile, rpcPort, hide, showVersion, err := config.FlagParse() + if err != nil { + panic(err) + } - var hide bool - flag.BoolVar(&hide, "hide", true, "hide the ComponentCheck result") + // Check if the version flag was set + if showVersion { + ver := version.Get() + fmt.Println("Version:", ver.GitVersion) + fmt.Println("Git Commit:", ver.GitCommit) + fmt.Println("Build Date:", ver.BuildDate) + fmt.Println("Go Version:", ver.GoVersion) + fmt.Println("Compiler:", ver.Compiler) + fmt.Println("Platform:", ver.Platform) + return + } flag.Parse() - err := component.ComponentCheck(configFile, hide) + err = component.ComponentCheck(configFile, hide) if err != nil { return } if err := config.InitConfig(configFile); err != nil { panic(err) } + if config.Config.Envs.Discovery == "k8s" { + rpcPort = 80 + } if err := log.InitFromConfig("chat.log", "admin-rpc", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil { panic(err) } diff --git a/cmd/rpc/chat-rpc/Makefile b/cmd/rpc/chat-rpc/Makefile deleted file mode 100644 index 9368c60a3..000000000 --- a/cmd/rpc/chat-rpc/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -.PHONY: all build run gotool install clean help - -BINARY_NAME=chat-rpc -BIN_DIR=../../../bin/ - -all: gotool build - -build: - CGO_ENABLED=0 GOOS=linux GOARCH=amd64; go build -ldflags="-w -s" -o ${BINARY_NAME} - -run: - @go run ./ - -gotool: - go fmt ./ - go vet ./ - -install: - make build - mv ${BINARY_NAME} ${BIN_DIR} - -clean: - @if [ -f ${BINARY_NAME} ] ; then rm ${BINARY_NAME} ; fi diff --git a/cmd/rpc/chat-rpc/main.go b/cmd/rpc/chat-rpc/main.go index 50de20a67..5b00c9f86 100644 --- a/cmd/rpc/chat-rpc/main.go +++ b/cmd/rpc/chat-rpc/main.go @@ -1,51 +1,48 @@ -// Copyright © 2023 OpenIM open source community. All rights reserved. -// -// 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" + "math/rand" + "time" + "github.com/OpenIMSDK/chat/pkg/common/chatrpcstart" "github.com/OpenIMSDK/chat/tools/component" "github.com/OpenIMSDK/tools/log" - "math/rand" - "time" "github.com/OpenIMSDK/chat/internal/rpc/chat" "github.com/OpenIMSDK/chat/pkg/common/config" + "github.com/OpenIMSDK/chat/pkg/common/version" ) func main() { rand.Seed(time.Now().UnixNano()) - var configFile string - flag.StringVar(&configFile, "config_folder_path", "../config/config.yaml", "Config full path") - - var rpcPort int - - flag.IntVar(&rpcPort, "port", 30300, "get rpc ServerPort from cmd") + configFile, rpcPort, hide, showVersion, err := config.FlagParse() + if err != nil { + panic(err) + } - var hide bool - flag.BoolVar(&hide, "hide", true, "hide the ComponentCheck result") + // Check if the version flag was set + if showVersion { + ver := version.Get() + fmt.Println("Version:", ver.GitVersion) + fmt.Println("Git Commit:", ver.GitCommit) + fmt.Println("Build Date:", ver.BuildDate) + fmt.Println("Go Version:", ver.GoVersion) + fmt.Println("Compiler:", ver.Compiler) + fmt.Println("Platform:", ver.Platform) + return + } - flag.Parse() - err := component.ComponentCheck(configFile, hide) + err = component.ComponentCheck(configFile, hide) if err != nil { return } if err := config.InitConfig(configFile); err != nil { panic(err) } + if config.Config.Envs.Discovery == "k8s" { + rpcPort = 80 + } if err := log.InitFromConfig("chat.log", "chat-rpc", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil { panic(err) } diff --git a/config/config.yaml b/config/config.yaml index 729504517..5c6a52704 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -13,6 +13,9 @@ # limitations under the License. # 需要先启动OpenIMServer +envs: + discovery: "zookeeper" + zookeeper: schema: openim zkAddr: @@ -50,7 +53,7 @@ mysql: # maxLifeTime: 60 #连接可以重复使用的最长时间(秒) # logLevel: 4 #日志级别 1=slient 2=error 3=warn 4=info # slowThreshold: 500 #慢语句阈值 (毫秒) - database: openim_enterprise + # database: openim_enterprise # 没有配置表示和OpenIM一致 log: diff --git a/deployment/helm-charts/.helmignore b/deployment/helm-charts/.helmignore new file mode 100644 index 000000000..0e8a0eb36 --- /dev/null +++ b/deployment/helm-charts/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deployment/helm-charts/Chart.yaml b/deployment/helm-charts/Chart.yaml new file mode 100644 index 000000000..141970797 --- /dev/null +++ b/deployment/helm-charts/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: admin-api +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/deployment/helm-charts/charts/admin-rpc/.helmignore b/deployment/helm-charts/charts/admin-rpc/.helmignore new file mode 100644 index 000000000..0e8a0eb36 --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deployment/helm-charts/charts/admin-rpc/Chart.yaml b/deployment/helm-charts/charts/admin-rpc/Chart.yaml new file mode 100644 index 000000000..81a2aadb5 --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: admin-rpc +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/deployment/helm-charts/charts/admin-rpc/templates/NOTES.txt b/deployment/helm-charts/charts/admin-rpc/templates/NOTES.txt new file mode 100644 index 000000000..cb5db3d55 --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "admin-rpc.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "admin-rpc.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "admin-rpc.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "admin-rpc.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/deployment/helm-charts/charts/admin-rpc/templates/_helpers.tpl b/deployment/helm-charts/charts/admin-rpc/templates/_helpers.tpl new file mode 100644 index 000000000..b6da95769 --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "admin-rpc.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "admin-rpc.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "admin-rpc.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "admin-rpc.labels" -}} +helm.sh/chart: {{ include "admin-rpc.chart" . }} +{{ include "admin-rpc.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "admin-rpc.selectorLabels" -}} +app.kubernetes.io/name: {{ include "admin-rpc.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "admin-rpc.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "admin-rpc.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/admin-rpc/templates/deployment.yaml b/deployment/helm-charts/charts/admin-rpc/templates/deployment.yaml new file mode 100644 index 000000000..b8c92167b --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/templates/deployment.yaml @@ -0,0 +1,69 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "admin-rpc.fullname" . }} + labels: + {{- include "admin-rpc.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "admin-rpc.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "admin-rpc.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "admin-rpc.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + #livenessProbe: + # httpGet: + # path: / + # port: http + #readinessProbe: + # httpGet: + # path: / + # port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - mountPath: /app/config/config.yaml + name: config + subPath: config.yaml + volumes: + - name: config + configMap: + name: imchat-cm + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/deployment/helm-charts/charts/admin-rpc/templates/hpa.yaml b/deployment/helm-charts/charts/admin-rpc/templates/hpa.yaml new file mode 100644 index 000000000..ea6966c60 --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "admin-rpc.fullname" . }} + labels: + {{- include "admin-rpc.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "admin-rpc.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/admin-rpc/templates/ingress.yaml b/deployment/helm-charts/charts/admin-rpc/templates/ingress.yaml new file mode 100644 index 000000000..1c1ba0e9d --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "admin-rpc.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "admin-rpc.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/admin-rpc/templates/service.yaml b/deployment/helm-charts/charts/admin-rpc/templates/service.yaml new file mode 100644 index 000000000..5104079a9 --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "admin-rpc.fullname" . }} + labels: + {{- include "admin-rpc.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "admin-rpc.selectorLabels" . | nindent 4 }} diff --git a/deployment/helm-charts/charts/admin-rpc/templates/serviceaccount.yaml b/deployment/helm-charts/charts/admin-rpc/templates/serviceaccount.yaml new file mode 100644 index 000000000..d79d94a66 --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "admin-rpc.serviceAccountName" . }} + labels: + {{- include "admin-rpc.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/admin-rpc/values.yaml b/deployment/helm-charts/charts/admin-rpc/values.yaml new file mode 100644 index 000000000..c4a4ae9b9 --- /dev/null +++ b/deployment/helm-charts/charts/admin-rpc/values.yaml @@ -0,0 +1,82 @@ +# Default values for admin-rpc. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: false + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/deployment/helm-charts/charts/chat-api/.helmignore b/deployment/helm-charts/charts/chat-api/.helmignore new file mode 100644 index 000000000..0e8a0eb36 --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deployment/helm-charts/charts/chat-api/Chart.yaml b/deployment/helm-charts/charts/chat-api/Chart.yaml new file mode 100644 index 000000000..f885ada69 --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: chat-api +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/deployment/helm-charts/charts/chat-api/templates/NOTES.txt b/deployment/helm-charts/charts/chat-api/templates/NOTES.txt new file mode 100644 index 000000000..012270869 --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "chat-api.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "chat-api.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "chat-api.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "chat-api.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/deployment/helm-charts/charts/chat-api/templates/_helpers.tpl b/deployment/helm-charts/charts/chat-api/templates/_helpers.tpl new file mode 100644 index 000000000..a7a4c2c28 --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "chat-api.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "chat-api.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "chat-api.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "chat-api.labels" -}} +helm.sh/chart: {{ include "chat-api.chart" . }} +{{ include "chat-api.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "chat-api.selectorLabels" -}} +app.kubernetes.io/name: {{ include "chat-api.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "chat-api.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "chat-api.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/chat-api/templates/deployment.yaml b/deployment/helm-charts/charts/chat-api/templates/deployment.yaml new file mode 100644 index 000000000..f5518d00d --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/templates/deployment.yaml @@ -0,0 +1,69 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "chat-api.fullname" . }} + labels: + {{- include "chat-api.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "chat-api.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "chat-api.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "chat-api.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + #livenessProbe: + # httpGet: + # path: / + # port: http + #readinessProbe: + # httpGet: + # path: / + # port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - mountPath: /app/config/config.yaml + name: config + subPath: config.yaml + volumes: + - name: config + configMap: + name: imchat-cm + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/deployment/helm-charts/charts/chat-api/templates/hpa.yaml b/deployment/helm-charts/charts/chat-api/templates/hpa.yaml new file mode 100644 index 000000000..454cd0ed6 --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "chat-api.fullname" . }} + labels: + {{- include "chat-api.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "chat-api.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/chat-api/templates/ingress.yaml b/deployment/helm-charts/charts/chat-api/templates/ingress.yaml new file mode 100644 index 000000000..f9a7ab0ed --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "chat-api.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "chat-api.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/chat-api/templates/service.yaml b/deployment/helm-charts/charts/chat-api/templates/service.yaml new file mode 100644 index 000000000..7761dccc2 --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "chat-api.fullname" . }} + labels: + {{- include "chat-api.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "chat-api.selectorLabels" . | nindent 4 }} diff --git a/deployment/helm-charts/charts/chat-api/templates/serviceaccount.yaml b/deployment/helm-charts/charts/chat-api/templates/serviceaccount.yaml new file mode 100644 index 000000000..ab59c1aff --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "chat-api.serviceAccountName" . }} + labels: + {{- include "chat-api.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/chat-api/values.yaml b/deployment/helm-charts/charts/chat-api/values.yaml new file mode 100644 index 000000000..bd8b8b396 --- /dev/null +++ b/deployment/helm-charts/charts/chat-api/values.yaml @@ -0,0 +1,84 @@ +# Default values for chat-api. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: false + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: true + className: "nginx" + annotations: + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + nginx.ingress.kubernetes.io/use-regex: "true" + nginx.ingress.kubernetes.io/rewrite-target: /$2 + hosts: + - host: openim1.nsddd.top + paths: + - path: /chat(/|$)(.*) + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/deployment/helm-charts/charts/chat-rpc/.helmignore b/deployment/helm-charts/charts/chat-rpc/.helmignore new file mode 100644 index 000000000..0e8a0eb36 --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deployment/helm-charts/charts/chat-rpc/Chart.yaml b/deployment/helm-charts/charts/chat-rpc/Chart.yaml new file mode 100644 index 000000000..32af6b4e0 --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: chat-rpc +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/deployment/helm-charts/charts/chat-rpc/templates/NOTES.txt b/deployment/helm-charts/charts/chat-rpc/templates/NOTES.txt new file mode 100644 index 000000000..2f1beaee5 --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "chat-rpc.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "chat-rpc.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "chat-rpc.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "chat-rpc.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/deployment/helm-charts/charts/chat-rpc/templates/_helpers.tpl b/deployment/helm-charts/charts/chat-rpc/templates/_helpers.tpl new file mode 100644 index 000000000..9edc1981c --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "chat-rpc.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "chat-rpc.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "chat-rpc.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "chat-rpc.labels" -}} +helm.sh/chart: {{ include "chat-rpc.chart" . }} +{{ include "chat-rpc.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "chat-rpc.selectorLabels" -}} +app.kubernetes.io/name: {{ include "chat-rpc.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "chat-rpc.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "chat-rpc.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/chat-rpc/templates/deployment.yaml b/deployment/helm-charts/charts/chat-rpc/templates/deployment.yaml new file mode 100644 index 000000000..d73eb9455 --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/templates/deployment.yaml @@ -0,0 +1,69 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "chat-rpc.fullname" . }} + labels: + {{- include "chat-rpc.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "chat-rpc.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "chat-rpc.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "chat-rpc.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + #livenessProbe: + # httpGet: + # path: / + # port: http + #readinessProbe: + # httpGet: + # path: / + # port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - mountPath: /app/config/config.yaml + name: config + subPath: config.yaml + volumes: + - name: config + configMap: + name: imchat-cm + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/deployment/helm-charts/charts/chat-rpc/templates/hpa.yaml b/deployment/helm-charts/charts/chat-rpc/templates/hpa.yaml new file mode 100644 index 000000000..4ff778486 --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "chat-rpc.fullname" . }} + labels: + {{- include "chat-rpc.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "chat-rpc.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/chat-rpc/templates/ingress.yaml b/deployment/helm-charts/charts/chat-rpc/templates/ingress.yaml new file mode 100644 index 000000000..8b742fe7b --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "chat-rpc.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "chat-rpc.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/chat-rpc/templates/service.yaml b/deployment/helm-charts/charts/chat-rpc/templates/service.yaml new file mode 100644 index 000000000..5056fc7f7 --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "chat-rpc.fullname" . }} + labels: + {{- include "chat-rpc.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "chat-rpc.selectorLabels" . | nindent 4 }} diff --git a/deployment/helm-charts/charts/chat-rpc/templates/serviceaccount.yaml b/deployment/helm-charts/charts/chat-rpc/templates/serviceaccount.yaml new file mode 100644 index 000000000..085101f90 --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "chat-rpc.serviceAccountName" . }} + labels: + {{- include "chat-rpc.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/charts/chat-rpc/values.yaml b/deployment/helm-charts/charts/chat-rpc/values.yaml new file mode 100644 index 000000000..1da19de48 --- /dev/null +++ b/deployment/helm-charts/charts/chat-rpc/values.yaml @@ -0,0 +1,82 @@ +# Default values for chat-rpc. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: false + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/deployment/helm-charts/config.yaml b/deployment/helm-charts/config.yaml new file mode 100644 index 000000000..b6dc0e3ed --- /dev/null +++ b/deployment/helm-charts/config.yaml @@ -0,0 +1,112 @@ +image: + repository: registry.cn-shenzhen.aliyuncs.com/huanglin_hub/admin-api + pullPolicy: Always + tag: "dev" +admin-rpc: + image: + repository: registry.cn-shenzhen.aliyuncs.com/huanglin_hub/admin-rpc + pullPolicy: Always + tag: "dev" +chat-api: + image: + repository: registry.cn-shenzhen.aliyuncs.com/huanglin_hub/chat-api + pullPolicy: Always + tag: "dev" +chat-rpc: + image: + repository: registry.cn-shenzhen.aliyuncs.com/huanglin_hub/chat-rpc + pullPolicy: Always + tag: "dev" + +config: + envs: + discovery: k8s + zookeeper: + schema: openim + zkAddr: + - 127.0.0.1:12181 + username: "" + password: "" + + chatApi: + openImChatApiPort: [ 80 ] + listenIP: + + adminApi: + openImAdminApiPort: [ 80 ] + listenIP: + + rpc: + registerIP: #作为rpc启动时,注册到zookeeper的IP,api/gateway能访问到此ip和对应的rpcPort中的端口 + listenIP: #默认为0.0.0.0 + + rpcPort: + openImAdminPort: [ 80 ] + openImChatPort: [ 80 ] + rpcRegisterName: + openImAdminName: openimchat-admin-rpc:80 + openImChatName: openimchat-chat-rpc:80 + + # 没有配置表示和OpenIM一致 + mysql: + address: [ im-mysql:3306 ] #目前仅支持单机 + username: root #用户名 + password: openIM123 #密码 + #database: openIM_v2 #不建议修改 + maxOpenConn: 1000 #最大连接数 + maxIdleConn: 100 #最大空闲连接数 + maxLifeTime: 60 #连接可以重复使用的最长时间(秒) + logLevel: 4 #日志级别 1=slient 2=error 3=warn 4=info + slowThreshold: 500 #慢语句阈值 (毫秒) + database: openim_enterprise + + # 没有配置表示和OpenIM一致 + log: + storageLocation: ../logs/ #存放目录 + rotationTime: 24 #日志旋转时间 + remainRotationCount: 2 #日志数量 + remainLogLevel: 6 #日志级别 6表示全都打印, + isStdout: true + isJson: false + withStack: false + + secret: openIM123 + tokenPolicy: + expire: 86400 + + verifyCode: + validTime: 300 # 验证码有效时间 + validCount: 5 # 验证码有效次数 + uintTime: 86400 # 单位时间间隔 + maxCount: 10 # 单位时间内最大获取次数 + superCode: "666666" # 超级验证码(只有use为空时使用) + len: 6 # 验证码长度 + use: "" # 使用的验证码服务(use: "ali") + ali: + endpoint: "dysmsapi.aliyuncs.com" + accessKeyId: "" + accessKeySecret: "" + signName: "" + verificationCodeTemplateCode: "" + + # 获取ip的header,没有配置直接获取远程地址 + #proxyHeader: "X-Forwarded-For" + + adminList: + - adminID: admin1 + nickname: chat1 + imAdmin: openIM123456 + - adminID: admin2 + nickname: chat2 + imAdmin: openIM654321 + - adminID: admin3 + nickname: chat3 + imAdmin: openIMAdmin + + + openIMUrl: "http://openimserver-openim-api" + + redis: + address: [ im-redis-master:6379 ] + username: '' + password: openIM123 diff --git a/deployment/helm-charts/templates/NOTES.txt b/deployment/helm-charts/templates/NOTES.txt new file mode 100644 index 000000000..9cbe57754 --- /dev/null +++ b/deployment/helm-charts/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "admin-api.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "admin-api.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "admin-api.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "admin-api.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/deployment/helm-charts/templates/_helpers.tpl b/deployment/helm-charts/templates/_helpers.tpl new file mode 100644 index 000000000..10ad4b5e4 --- /dev/null +++ b/deployment/helm-charts/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "admin-api.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "admin-api.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "admin-api.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "admin-api.labels" -}} +helm.sh/chart: {{ include "admin-api.chart" . }} +{{ include "admin-api.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "admin-api.selectorLabels" -}} +app.kubernetes.io/name: {{ include "admin-api.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "admin-api.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "admin-api.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/deployment/helm-charts/templates/app-cm.yaml b/deployment/helm-charts/templates/app-cm.yaml new file mode 100644 index 000000000..0d03ce71e --- /dev/null +++ b/deployment/helm-charts/templates/app-cm.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: imchat-cm +data: + config.yaml: |+ + {{- with .Values.config }} + {{- toYaml . | nindent 4 }} + {{- end }} diff --git a/deployment/helm-charts/templates/deployment.yaml b/deployment/helm-charts/templates/deployment.yaml new file mode 100644 index 000000000..361ac1653 --- /dev/null +++ b/deployment/helm-charts/templates/deployment.yaml @@ -0,0 +1,69 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "admin-api.fullname" . }} + labels: + {{- include "admin-api.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "admin-api.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "admin-api.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "admin-api.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + #livenessProbe: + # httpGet: + # path: / + # port: http + #readinessProbe: + # httpGet: + # path: / + # port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - mountPath: /app/config/config.yaml + name: config + subPath: config.yaml + volumes: + - name: config + configMap: + name: imchat-cm + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/deployment/helm-charts/templates/hpa.yaml b/deployment/helm-charts/templates/hpa.yaml new file mode 100644 index 000000000..60d7bd200 --- /dev/null +++ b/deployment/helm-charts/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "admin-api.fullname" . }} + labels: + {{- include "admin-api.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "admin-api.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/templates/ingress.yaml b/deployment/helm-charts/templates/ingress.yaml new file mode 100644 index 000000000..2ea7af213 --- /dev/null +++ b/deployment/helm-charts/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "admin-api.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "admin-api.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/templates/service.yaml b/deployment/helm-charts/templates/service.yaml new file mode 100644 index 000000000..f4e3506b2 --- /dev/null +++ b/deployment/helm-charts/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "admin-api.fullname" . }} + labels: + {{- include "admin-api.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "admin-api.selectorLabels" . | nindent 4 }} diff --git a/deployment/helm-charts/templates/serviceaccount.yaml b/deployment/helm-charts/templates/serviceaccount.yaml new file mode 100644 index 000000000..04801cd74 --- /dev/null +++ b/deployment/helm-charts/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "admin-api.serviceAccountName" . }} + labels: + {{- include "admin-api.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/deployment/helm-charts/values.yaml b/deployment/helm-charts/values.yaml new file mode 100644 index 000000000..e483907b6 --- /dev/null +++ b/deployment/helm-charts/values.yaml @@ -0,0 +1,84 @@ +# Default values for admin-api. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: false + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: true + className: "nginx" + annotations: + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + nginx.ingress.kubernetes.io/use-regex: "true" + nginx.ingress.kubernetes.io/rewrite-target: /$2 + hosts: + - host: openim1.nsddd.top + paths: + - path: /complete_admin(/|$)(.*) + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/go.mod b/go.mod index 828403eff..b8422f130 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/OpenIMSDK/chat -go 1.18 +go 1.19 require ( github.com/OpenIMSDK/open_utils v1.0.8 @@ -30,7 +30,9 @@ require ( github.com/OpenIMSDK/protocol v0.0.21 github.com/OpenIMSDK/tools v0.0.15 github.com/go-zookeeper/zk v1.0.3 + github.com/gosuri/uitable v0.0.4 github.com/redis/go-redis/v9 v9.1.0 + github.com/spf13/pflag v1.0.5 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df ) @@ -48,6 +50,7 @@ require ( github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/clbanning/mxj/v2 v2.5.6 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/fatih/color v1.15.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -63,12 +66,15 @@ require ( github.com/kr/pretty v0.3.1 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/lestrrat-go/strftime v1.0.6 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/rivo/uniseg v0.2.0 // indirect github.com/tjfoc/gmsm v1.3.2 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect diff --git a/go.sum b/go.sum index 5a4bc1f83..ca8eba71f 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -94,6 +96,8 @@ github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY= +github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo= github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg= github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= @@ -128,8 +132,13 @@ github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkL github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA= github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ= github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -151,11 +160,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redis/go-redis/v9 v9.1.0 h1:137FnGdk+EQdCbye1FW+qOEcY5S+SpY9T0NiuqvtfMY= github.com/redis/go-redis/v9 v9.1.0/go.mod h1:urWj3He21Dj5k4TK1y59xH8Uj6ATueP8AH1cY3lZl4c= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/assertions v1.1.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= @@ -249,6 +262,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/rpc/admin/register_add_group.go b/internal/rpc/admin/register_add_group.go index ce998d997..32dc3c542 100644 --- a/internal/rpc/admin/register_add_group.go +++ b/internal/rpc/admin/register_add_group.go @@ -19,10 +19,8 @@ import ( "strings" "time" - "github.com/OpenIMSDK/tools/log" - - "github.com/OpenIMSDK/tools/errs" + "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/utils" admin2 "github.com/OpenIMSDK/chat/pkg/common/db/table/admin" diff --git a/internal/rpc/chat/log.go b/internal/rpc/chat/log.go index e48135d7c..34d476cd6 100644 --- a/internal/rpc/chat/log.go +++ b/internal/rpc/chat/log.go @@ -3,6 +3,9 @@ package chat import ( "context" "fmt" + "math/rand" + "time" + "github.com/OpenIMSDK/chat/pkg/common/constant" table "github.com/OpenIMSDK/chat/pkg/common/db/table/chat" "github.com/OpenIMSDK/chat/pkg/common/mctx" @@ -11,8 +14,6 @@ import ( "github.com/OpenIMSDK/tools/mw/specialerror" utils2 "github.com/OpenIMSDK/tools/utils" "gorm.io/gorm/utils" - "math/rand" - "time" ) func (o *chatSvr) genLogID() string { diff --git a/internal/rpc/chat/statistic.go b/internal/rpc/chat/statistic.go index 74b979bbb..9651ef041 100644 --- a/internal/rpc/chat/statistic.go +++ b/internal/rpc/chat/statistic.go @@ -16,9 +16,10 @@ package chat import ( "context" + "time" + "github.com/OpenIMSDK/chat/pkg/proto/chat" "github.com/OpenIMSDK/tools/errs" - "time" ) func (o *chatSvr) UserLoginCount(ctx context.Context, req *chat.UserLoginCountReq) (*chat.UserLoginCountResp, error) { diff --git a/pkg/common/chatrpcstart/start.go b/pkg/common/chatrpcstart/start.go index 10785bc53..f06cf1208 100644 --- a/pkg/common/chatrpcstart/start.go +++ b/pkg/common/chatrpcstart/start.go @@ -18,14 +18,12 @@ import ( "fmt" "net" "strconv" - "time" "github.com/OpenIMSDK/chat/pkg/common/config" chatMw "github.com/OpenIMSDK/chat/pkg/common/mw" + "github.com/OpenIMSDK/chat/pkg/discovery_register" "github.com/OpenIMSDK/tools/discoveryregistry" - openKeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/errs" - "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mw" "github.com/OpenIMSDK/tools/network" "github.com/OpenIMSDK/tools/utils" @@ -36,12 +34,15 @@ import ( func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error, options ...grpc.ServerOption) error { fmt.Println("start", rpcRegisterName, "server, port: ", rpcPort, "prometheusPort:", prometheusPort, ", OpenIM version: ", config.Version) - zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger())) - if err != nil { + var zkClient discoveryregistry.SvcDiscoveryRegistry + zkClient, err := discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) + /* + zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger()))*/if err != nil { return errs.Wrap(err) } + // defer zkClient.CloseZK() defer zkClient.Close() zkClient.AddOption(chatMw.AddUserType(), mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) registerIP, err := network.GetRpcRegisterIP(config.Config.Rpc.RegisterIP) diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 5a4b60cc8..8e450d616 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -20,6 +20,9 @@ import _ "embed" var Version string var Config struct { + Envs struct { + Discovery string `yaml:"discovery"` + } `yaml:"envs"` Zookeeper struct { Schema string `yaml:"schema"` ZkAddr []string `yaml:"zkAddr"` diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index f6bd7fcd8..561fe28e7 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -17,14 +17,17 @@ package config import ( "bytes" "errors" + "flag" "fmt" "os" "path/filepath" "runtime" "time" + Constant "github.com/OpenIMSDK/chat/pkg/common/constant" "github.com/OpenIMSDK/protocol/constant" openKeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" + "github.com/OpenIMSDK/tools/utils" "gopkg.in/yaml.v3" ) @@ -32,30 +35,42 @@ import ( var ( _, b, _, _ = runtime.Caller(0) // Root folder of this project. - Root = filepath.Join(filepath.Dir(b), "../../..") + Root = filepath.Join(filepath.Dir(b), "../..") ) func readConfig(configFile string) ([]byte, error) { b, err := os.ReadFile(configFile) - if err != nil { + if err != nil { // File exists and was read successfully return nil, utils.Wrap(err, configFile) } return b, nil - // cfgName := os.Getenv("CONFIG_NAME") - // if len(cfgName) != 0 { - // data, err := os.ReadFile(filepath.Join(cfgName, "config", "config.yaml")) - // if err != nil { - // data, err = os.ReadFile(filepath.Join(Root, "config", "config.yaml")) - // if err != nil { - // return nil, err - // } - // } else { - // Root = cfgName - // } - // return data, nil - // } else { - // return os.ReadFile(fmt.Sprintf("../config/%s", "config.yaml")) - // } + + // //First, check the configFile argument + // if configFile != "" { + // b, err := os.ReadFile(configFile) + // if err == nil { // File exists and was read successfully + // fmt.Println("这里aaaaaaaa") + // return b, nil + // } + // } + // + // // Second, check for OPENIMCHATCONFIG environment variable + // envConfigPath := os.Getenv("OPENIMCHATCONFIG") + // if envConfigPath != "" { + // b, err := os.ReadFile(envConfigPath) + // if err == nil { // File exists and was read successfully + // return b, nil + // } + // // Again, if there was an error, you can either log it or ignore. + // } + // + // // If neither configFile nor environment variable provided a valid path, use default path + // defaultConfigPath := filepath.Join(Root, "config", "config.yaml") + // b, err := os.ReadFile(defaultConfigPath) + // if err != nil { + // return nil, utils.Wrap(err, defaultConfigPath) + // } + // return b, nil } func InitConfig(configFile string) error { @@ -66,60 +81,63 @@ func InitConfig(configFile string) error { if err := yaml.NewDecoder(bytes.NewReader(data)).Decode(&Config); err != nil { return fmt.Errorf("parse loacl openIMConfig file error: %w", err) } - zk, err := openKeeper.NewClient(Config.Zookeeper.ZkAddr, Config.Zookeeper.Schema, - openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(Config.Zookeeper.Username, - Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(&zkLogger{})) - if err != nil { - return utils.Wrap(err, "conn zk error ") - } - defer zk.Close() - var openIMConfigData []byte - for i := 0; i < 100; i++ { - var err error - configData, err := zk.GetConfFromRegistry(constant.OpenIMCommonConfigKey) + if Config.Envs.Discovery != "k8s" { + zk, err := openKeeper.NewClient(Config.Zookeeper.ZkAddr, Config.Zookeeper.Schema, + openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(Config.Zookeeper.Username, + Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(&zkLogger{})) if err != nil { - fmt.Printf("get zk config [%d] error: %v\n", i, err) - time.Sleep(time.Second) - continue + return utils.Wrap(err, "conn zk error ") } - if len(configData) == 0 { - fmt.Printf("get zk config [%d] data is empty\n", i) - time.Sleep(time.Second) - continue + defer zk.Close() + var openIMConfigData []byte + for i := 0; i < 100; i++ { + var err error + configData, err := zk.GetConfFromRegistry(constant.OpenIMCommonConfigKey) + if err != nil { + fmt.Printf("get zk config [%d] error: %v\n;envs.descoery=%s", i, err, Config.Envs.Discovery) + time.Sleep(time.Second) + continue + } + if len(configData) == 0 { + fmt.Printf("get zk config [%d] data is empty\n", i) + time.Sleep(time.Second) + continue + } + openIMConfigData = configData } - openIMConfigData = configData - } - if len(openIMConfigData) == 0 { - return errors.New("get zk config data failed") - } - if err := yaml.NewDecoder(bytes.NewReader(openIMConfigData)).Decode(&imConfig); err != nil { - return fmt.Errorf("parse zk openIMConfig: %w", err) + if len(openIMConfigData) == 0 { + return errors.New("get zk config data failed") + } + if err := yaml.NewDecoder(bytes.NewReader(openIMConfigData)).Decode(&imConfig); err != nil { + return fmt.Errorf("parse zk openIMConfig: %w", err) + } + // 这里可以优化,可将其优化为结构体层面的赋值 + configFieldCopy(&Config.Mysql.Address, imConfig.Mysql.Address) + configFieldCopy(&Config.Mysql.Username, imConfig.Mysql.Username) + configFieldCopy(&Config.Mysql.Password, imConfig.Mysql.Password) + configFieldCopy(&Config.Mysql.Database, imConfig.Mysql.Database) + configFieldCopy(&Config.Mysql.MaxOpenConn, imConfig.Mysql.MaxOpenConn) + configFieldCopy(&Config.Mysql.MaxIdleConn, imConfig.Mysql.MaxIdleConn) + configFieldCopy(&Config.Mysql.MaxLifeTime, imConfig.Mysql.MaxLifeTime) + configFieldCopy(&Config.Mysql.LogLevel, imConfig.Mysql.LogLevel) + configFieldCopy(&Config.Mysql.SlowThreshold, imConfig.Mysql.SlowThreshold) + + configFieldCopy(&Config.Log.StorageLocation, imConfig.Log.StorageLocation) + configFieldCopy(&Config.Log.RotationTime, imConfig.Log.RotationTime) + configFieldCopy(&Config.Log.RemainRotationCount, imConfig.Log.RemainRotationCount) + configFieldCopy(&Config.Log.RemainLogLevel, imConfig.Log.RemainLogLevel) + configFieldCopy(&Config.Log.IsStdout, imConfig.Log.IsStdout) + configFieldCopy(&Config.Log.WithStack, imConfig.Log.WithStack) + configFieldCopy(&Config.Log.IsJson, imConfig.Log.IsJson) + + configFieldCopy(&Config.Secret, imConfig.Secret) + configFieldCopy(&Config.TokenPolicy.Expire, imConfig.TokenPolicy.Expire) + + // Redis + configFieldCopy(&Config.Redis.Address, imConfig.Redis.Address) + configFieldCopy(&Config.Redis.Password, imConfig.Redis.Password) + configFieldCopy(&Config.Redis.Username, imConfig.Redis.Username) } - configFieldCopy(&Config.Mysql.Address, imConfig.Mysql.Address) - configFieldCopy(&Config.Mysql.Username, imConfig.Mysql.Username) - configFieldCopy(&Config.Mysql.Password, imConfig.Mysql.Password) - configFieldCopy(&Config.Mysql.Database, imConfig.Mysql.Database) - configFieldCopy(&Config.Mysql.MaxOpenConn, imConfig.Mysql.MaxOpenConn) - configFieldCopy(&Config.Mysql.MaxIdleConn, imConfig.Mysql.MaxIdleConn) - configFieldCopy(&Config.Mysql.MaxLifeTime, imConfig.Mysql.MaxLifeTime) - configFieldCopy(&Config.Mysql.LogLevel, imConfig.Mysql.LogLevel) - configFieldCopy(&Config.Mysql.SlowThreshold, imConfig.Mysql.SlowThreshold) - - configFieldCopy(&Config.Log.StorageLocation, imConfig.Log.StorageLocation) - configFieldCopy(&Config.Log.RotationTime, imConfig.Log.RotationTime) - configFieldCopy(&Config.Log.RemainRotationCount, imConfig.Log.RemainRotationCount) - configFieldCopy(&Config.Log.RemainLogLevel, imConfig.Log.RemainLogLevel) - configFieldCopy(&Config.Log.IsStdout, imConfig.Log.IsStdout) - configFieldCopy(&Config.Log.WithStack, imConfig.Log.WithStack) - configFieldCopy(&Config.Log.IsJson, imConfig.Log.IsJson) - - configFieldCopy(&Config.Secret, imConfig.Secret) - configFieldCopy(&Config.TokenPolicy.Expire, imConfig.TokenPolicy.Expire) - - // Redis - configFieldCopy(&Config.Redis.Address, imConfig.Redis.Address) - configFieldCopy(&Config.Redis.Password, imConfig.Redis.Password) - configFieldCopy(&Config.Redis.Username, imConfig.Redis.Username) configData, err := yaml.Marshal(&Config) fmt.Printf("debug: %s\nconfig:\n%s\n", time.Now(), string(configData)) @@ -160,3 +178,79 @@ type zkLogger struct{} func (l *zkLogger) Printf(format string, a ...interface{}) { fmt.Printf("zk get config %s\n", fmt.Sprintf(format, a...)) } + +func checkFileExists(filePath string) bool { + info, err := os.Stat(filePath) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + +func findConfigFile(paths []string) (string, error) { + for _, path := range paths { + if checkFileExists(path) { + return path, nil + } + } + return "", fmt.Errorf("configPath not found") +} + +func CreateCatalogPath() []string { + + return []string{Constant.ConfigPath1, Constant.ConfigPath2} +} + +func findConfigPath(configFile string) (string, error) { + path := make([]string, 10) + + // First, check the configFile argument + if configFile != "" { + if _, err := findConfigFile([]string{configFile}); err != nil { + return "", errors.New("the configFile argument path is error") + } + return configFile, nil + } + + // Second, check for OPENIMCONFIG environment variable + envConfigPath := os.Getenv(Constant.OpenIMConfig) + if envConfigPath != "" { + if _, err := findConfigFile([]string{envConfigPath}); err != nil { + return "", errors.New("the environment path config path is error") + } + return envConfigPath, nil + } + + // Third, check the catalog to find the config.yaml + path = CreateCatalogPath() + pathFind, err := findConfigFile(path) + if err == nil { + return pathFind, nil + } + + // Forth, use the Default path. + return Constant.Default, nil +} + +func FlagParse() (string, int, bool, bool, error) { + var configFile string + flag.StringVar(&configFile, "config_folder_path", "", "Config full path") + + var ginPort int + flag.IntVar(&ginPort, "port", 10009, "get ginServerPort from cmd") + + var hide bool + flag.BoolVar(&hide, "hide", false, "hide the ComponentCheck result") + + // Version flag + var showVersion bool + flag.BoolVar(&showVersion, "version", false, "show version and exit") + + flag.Parse() + + configFile, err := findConfigPath(configFile) + if err != nil { + return "", 0, false, false, err + } + return configFile, ginPort, hide, showVersion, nil +} diff --git a/pkg/common/config/version b/pkg/common/config/version index 70cdc0898..56e50151c 100644 --- a/pkg/common/config/version +++ b/pkg/common/config/version @@ -1 +1 @@ -v3.1.1 \ No newline at end of file +v3.3.1 \ No newline at end of file diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 2a0b92c2e..ca53f5747 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -16,11 +16,20 @@ package constant import "github.com/OpenIMSDK/protocol/constant" +// config path +const ( + ConfigPath1 = "../config/config.yaml" + ConfigPath2 = "../../../../../config/config.yaml" + + OpenIMConfig = "OpenIMConfig" // environment variables + Default = "../../../config/config.yaml" +) + const ( // verificationCode used for. - VerificationCodeForRegister = 1 // 注册 - VerificationCodeForResetPassword = 2 // 重置密码 - VerificationCodeForLogin = 3 // 登录 + VerificationCodeForRegister = 1 // Register + VerificationCodeForResetPassword = 2 // Reset password + VerificationCodeForLogin = 3 // Login VerificationCodeForRegisterSuffix = "_forRegister" VerificationCodeForResetSuffix = "_forReset" @@ -56,8 +65,8 @@ const ( // AddFriendCtrl. const ( - OrdinaryUserAddFriendEnable = 1 // 允许普通用户添加好友 - OrdinaryUserAddFriendDisable = -1 // 不允许普通用户添加好友 + OrdinaryUserAddFriendEnable = 1 // Allow ordinary users to add friends + OrdinaryUserAddFriendDisable = -1 // Do not allow ordinary users to add friends ) // minioUpload. @@ -74,15 +83,15 @@ const ( ) const ( - ScreenInvitationRegisterAll = 0 // 全部 - ScreenInvitationRegisterUsed = 1 // 已使用 - ScreenInvitationRegisterNotUsed = 2 // 未使用 + ScreenInvitationRegisterAll = 0 // All + ScreenInvitationRegisterUsed = 1 // Used + ScreenInvitationRegisterNotUsed = 2 // Unused ) // 1 block; 2 unblock. const ( - UserBlock = 1 // 封号 - UserUnblock = 2 // 解封 + UserBlock = 1 // Account ban + UserUnblock = 2 // Unban ) const ( @@ -116,14 +125,14 @@ const ( ) const ( - NotNeedInvitationCodeRegister = 0 // 不需要邀请码 - NeedInvitationCodeRegister = 1 // 需要邀请码 + NotNeedInvitationCodeRegister = 0 // No invitation code required + NeedInvitationCodeRegister = 1 // Invitation code required ) -// 小程序. +// mini-app const ( - StatusOnShelf = 1 // 上架 - StatusUnShelf = 2 // 下架 + StatusOnShelf = 1 // OnShelf + StatusUnShelf = 2 // UnShelf ) const ( @@ -132,22 +141,22 @@ const ( ) const ( - LimitNil = 0 // 无 - LimitEmpty = 1 // 都不限制 - LimitOnlyLoginIP = 2 // 仅限制登录 - LimitOnlyRegisterIP = 3 // 仅限制注册 - LimitLoginIP = 4 // 限制登录 - LimitRegisterIP = 5 // 限制注册 - LimitLoginRegisterIP = 6 // 限制登录注册 + LimitNil = 0 // None + LimitEmpty = 1 // Neither are restricted + LimitOnlyLoginIP = 2 // Only login is restricted + LimitOnlyRegisterIP = 3 // Only registration is restricted + LimitLoginIP = 4 // Restrict login + LimitRegisterIP = 5 // Restrict registration + LimitLoginRegisterIP = 6 // Restrict both login and registration ) const ( - InvitationCodeAll = 0 // 全部 - InvitationCodeUsed = 1 // 已使用 - InvitationCodeUnused = 2 // 未使用 + InvitationCodeAll = 0 // All + InvitationCodeUsed = 1 // Used + InvitationCodeUnused = 2 // Unused ) -// 默认发现页面. +// Default discovery page const DefaultDiscoverPageURL = "https://doc.rentsoft.cn/#/" // const OperationID = "operationID" diff --git a/pkg/common/db/model/admin/admin.go b/pkg/common/db/model/admin/admin.go index f4e8d04a6..befd2659f 100644 --- a/pkg/common/db/model/admin/admin.go +++ b/pkg/common/db/model/admin/admin.go @@ -18,9 +18,11 @@ import ( "context" "crypto/md5" "encoding/hex" - "github.com/OpenIMSDK/tools/log" "time" + + "github.com/OpenIMSDK/tools/log" + "github.com/OpenIMSDK/chat/pkg/common/config" "github.com/OpenIMSDK/chat/pkg/common/db/table/admin" "github.com/OpenIMSDK/tools/errs" diff --git a/pkg/common/db/model/chat/log.go b/pkg/common/db/model/chat/log.go index 5cb15119e..df138f5f8 100644 --- a/pkg/common/db/model/chat/log.go +++ b/pkg/common/db/model/chat/log.go @@ -2,11 +2,12 @@ package chat import ( "context" + "time" + "github.com/OpenIMSDK/chat/pkg/common/db/table/chat" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/ormutil" "gorm.io/gorm" - "time" ) type Logs struct { @@ -39,6 +40,7 @@ func (l *Logs) Get(ctx context.Context, logIDs []string, userID string) ([]*chat } return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Find(&logs).Error) } + func NewLogs(db *gorm.DB) chat.LogInterface { return &Logs{db: db} } diff --git a/pkg/common/db/model/chat/user_login_record.go b/pkg/common/db/model/chat/user_login_record.go index 54fe1756f..cb3b1f6e0 100644 --- a/pkg/common/db/model/chat/user_login_record.go +++ b/pkg/common/db/model/chat/user_login_record.go @@ -16,9 +16,10 @@ package chat import ( "context" + "time" + "github.com/OpenIMSDK/tools/errs" - "time" "gorm.io/gorm" diff --git a/pkg/common/version/base.go b/pkg/common/version/base.go new file mode 100644 index 000000000..6d2c2dc8f --- /dev/null +++ b/pkg/common/version/base.go @@ -0,0 +1,47 @@ +package version + +// Base version information. +// +// This is the fallback data used when version information from git is not +// provided via go ldflags. It provides an approximation of the Kubernetes +// version for ad-hoc builds (e.g. `go build`) that cannot get the version +// information from git. +// +// If you are looking at these fields in the git tree, they look +// strange. They are modified on the fly by the build process. The +// in-tree values are dummy values used for "git archive", which also +// works for GitHub tar downloads. +// +// When releasing a new Kubernetes version, this file is updated by +// build/mark_new_version.sh to reflect the new version, and then a +// git annotated tag (using format vX.Y where X == Major version and Y +// == Minor version) is created to point to the commit that updates +var ( + // TODO: Deprecate gitMajor and gitMinor, use only gitVersion + // instead. First step in deprecation, keep the fields but make + // them irrelevant. (Next we'll take it out, which may muck with + // scripts consuming the kubectl version output - but most of + // these should be looking at gitVersion already anyways.) + gitMajor string = "" // major version, always numeric + gitMinor string = "" // minor version, numeric possibly followed by "+" + + // semantic version, derived by build scripts (see + // https://github.com/kubernetes/sig-release/blob/master/release-engineering/versioning.md#kubernetes-release-versioning + // https://kubernetes.io/releases/version-skew-policy/ + // for a detailed discussion of this field) + // + // TODO: This field is still called "gitVersion" for legacy + // reasons. For prerelease versions, the build metadata on the + // semantic version is a git hash, but the version itself is no + // longer the direct output of "git describe", but a slight + // translation to be semver compliant. + + // NOTE: The $Format strings are replaced during 'git archive' thanks to the + // companion .gitattributes file containing 'export-subst' in this same + // directory. See also https://git-scm.com/docs/gitattributes + gitVersion string = "latest" + gitCommit string = "" // sha1 from git, output of $(git rev-parse HEAD) + gitTreeState string = "" // state of git tree, either "clean" or "dirty" + + buildDate string = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') +) \ No newline at end of file diff --git a/pkg/common/version/types.go b/pkg/common/version/types.go new file mode 100644 index 000000000..3778220bf --- /dev/null +++ b/pkg/common/version/types.go @@ -0,0 +1,20 @@ +package version + +// Info contains versioning information. +// TODO: Add []string of api versions supported? It's still unclear +// how we'll want to distribute that information. +type Info struct { + Major string `json:"major,omitempty"` + Minor string `json:"minor,omitempty"` + GitVersion string `json:"gitVersion"` + GitCommit string `json:"gitCommit,omitempty"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` + Compiler string `json:"compiler"` + Platform string `json:"platform"` +} + +// String returns info as a human-friendly version string. +func (info Info) String() string { + return info.GitVersion +} \ No newline at end of file diff --git a/pkg/common/version/version.go b/pkg/common/version/version.go new file mode 100644 index 000000000..f2fb64d96 --- /dev/null +++ b/pkg/common/version/version.go @@ -0,0 +1,38 @@ +package version + +import ( + "fmt" + "runtime" +) + +// Get returns the overall codebase version. It's for detecting +// what code a binary was built from. +func Get() Info { + // These variables typically come from -ldflags settings and in + // their absence fallback to the settings in ./base.go + return Info{ + Major: gitMajor, + Minor: gitMinor, + GitVersion: gitVersion, + GitCommit: gitCommit, + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } +} + +// GetSingleVersion returns single version of sealer +func GetSingleVersion() string { + return gitVersion +} + +type Output struct { + OpenIMChatVersion Info `json:"OpenIMChatVersion,omitempty" yaml:"OpenIMChatVersion,omitempty"` + OpenIMServerVersion *OpenIMServerVersion `json:"OpenIMServerVersion,omitempty" yaml:"OpenIMServerVersion,omitempty"` +} + +type OpenIMServerVersion struct { + ServerVersion string `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"` + ClientVersion string `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"` //sdk core version +} \ No newline at end of file diff --git a/pkg/discovery_register/k8s_discovery_register.go b/pkg/discovery_register/k8s_discovery_register.go new file mode 100644 index 000000000..a8ee5fff0 --- /dev/null +++ b/pkg/discovery_register/k8s_discovery_register.go @@ -0,0 +1,94 @@ +package discovery_register + +import ( + "context" + "errors" + "fmt" + "time" + + "github.com/OpenIMSDK/chat/pkg/common/config" + "github.com/OpenIMSDK/tools/discoveryregistry" + openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" + "github.com/OpenIMSDK/tools/log" + "google.golang.org/grpc" +) + +func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistry, error) { + var client discoveryregistry.SvcDiscoveryRegistry + var err error + switch envType { + case "zookeeper": + client, err = openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + openkeeper.WithFreq(time.Hour), openkeeper.WithUserNameAndPassword( + config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password, + ), openkeeper.WithRoundRobin(), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger())) + case "k8s": + client, err = NewK8sDiscoveryRegister() + default: + client = nil + err = errors.New("envType not correct") + } + return client, err +} + +type K8sDR struct { + options []grpc.DialOption + rpcRegisterAddr string +} + +func NewK8sDiscoveryRegister() (discoveryregistry.SvcDiscoveryRegistry, error) { + return &K8sDR{}, nil +} + +func (cli *K8sDR) Register(serviceName, host string, port int, opts ...grpc.DialOption) error { + cli.rpcRegisterAddr = serviceName + return nil +} + +func (cli *K8sDR) UnRegister() error { + return nil +} + +func (cli *K8sDR) CreateRpcRootNodes(serviceNames []string) error { + return nil +} + +func (cli *K8sDR) RegisterConf2Registry(key string, conf []byte) error { + return nil +} + +func (cli *K8sDR) GetConfFromRegistry(key string) ([]byte, error) { + return nil, nil +} + +func (cli *K8sDR) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) { + conn, err := grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) + return []*grpc.ClientConn{conn}, err +} + +func (cli *K8sDR) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { + return grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) +} + +func (cli *K8sDR) GetSelfConnTarget() string { + return cli.rpcRegisterAddr +} + +func (cli *K8sDR) AddOption(opts ...grpc.DialOption) { + cli.options = append(cli.options, opts...) +} + +func (cli *K8sDR) CloseConn(conn *grpc.ClientConn) { + conn.Close() +} + +// do not use this method for call rpc. +func (cli *K8sDR) GetClientLocalConns() map[string][]*grpc.ClientConn { + fmt.Println("should not call this function!!!!!!!!!!!!!!!!!!!!!!!!!") + return nil +} + +func (cli *K8sDR) Close() { + return +} diff --git a/pkg/proto/chat/chat.go b/pkg/proto/chat/chat.go index 61cc24b1d..d6e8cd056 100644 --- a/pkg/proto/chat/chat.go +++ b/pkg/proto/chat/chat.go @@ -15,10 +15,11 @@ package chat import ( - "github.com/OpenIMSDK/tools/utils" "regexp" "strconv" + "github.com/OpenIMSDK/tools/utils" + "github.com/OpenIMSDK/chat/pkg/common/constant" constant2 "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/errs" @@ -221,6 +222,7 @@ func (x *UploadLogsReq) Check() error { } return nil } + func (x *SearchLogsReq) Check() error { if x.Pagination == nil { return errs.ErrArgs.Wrap("Pagination is empty") diff --git a/pkg/sms/sms.go b/pkg/sms/sms.go index 7a6f4a3e8..8966822d9 100644 --- a/pkg/sms/sms.go +++ b/pkg/sms/sms.go @@ -17,9 +17,9 @@ package sms import ( "context" "fmt" - "github.com/OpenIMSDK/chat/pkg/common/config" "strings" + "github.com/OpenIMSDK/chat/pkg/common/config" ) func New() (SMS, error) { diff --git a/scripts/check_all.sh b/scripts/check_all.sh index 3e28955ff..a087f5e61 100755 --- a/scripts/check_all.sh +++ b/scripts/check_all.sh @@ -47,16 +47,35 @@ service_port_name=( switch=$(cat $config_path | grep demoswitch |awk -F '[:]' '{print $NF}') for i in ${service_port_name[*]}; do - list=$(cat $config_path | grep -w ${i} | awk -F '[:]' '{print $NF}') - list_to_string $list - for j in ${ports_array}; do - port=$(ps -ef |grep -E 'api|rpc|open_im' |awk '{print $10}'| grep -w ${j}) - if [[ ${port} -ne ${j} ]]; then - echo -e ${YELLOW_PREFIX}${i}${COLOR_SUFFIX}${RED_PREFIX}" service does not start normally,not initiated port is "${COLOR_SUFFIX}${YELLOW_PREFIX}${j}${COLOR_SUFFIX} - echo -e ${RED_PREFIX}"please check ../logs/openIM.log "${COLOR_SUFFIX} + case $i in + "openImChatApiPort") + new_service_name="chat-api" + new_service_port="10008" + ;; + "openImAdminApiPort") + new_service_name="admin-rpc" + new_service_port="30200" + ;; + "openImAdminPort") + new_service_name="chat-rpc" + new_service_port="30300" + ;; + "openImChatPort") + new_service_name="admin-api" + new_service_port="10009" + ;; + *) + echo "Invalid service name: $i" exit -1 - else - echo -e ${j}${GREEN_PREFIX}" port has been listening,belongs service is "${i}${COLOR_SUFFIX} - fi - done -done + ;; + esac + + port=$(ss -tunlp | grep "$new_service_name" | awk '{print $5}' | awk -F '[:]' '{print $NF}') + if [[ "$port" != "$new_service_port" ]]; then + echo -e "${YELLOW_PREFIX}${i}${COLOR_SUFFIX}${RED_PREFIX} service does not start normally, not initiated port is ${COLOR_SUFFIX}${YELLOW_PREFIX}${new_service_port}${COLOR_SUFFIX}" + echo -e "${RED_PREFIX}please check ${SCRIPTS_ROOT}/../logs/openIM.log ${COLOR_SUFFIX}" + exit -1 + else + echo -e "${new_service_port}${GREEN_PREFIX} port has been listening, belongs service is ${i}${COLOR_SUFFIX}" + fi +done \ No newline at end of file diff --git a/scripts/start_all.sh b/scripts/start_all.sh index c0616ebf0..4c6c14abb 100755 --- a/scripts/start_all.sh +++ b/scripts/start_all.sh @@ -108,4 +108,4 @@ for ((i = 0; i < ${#service_filename[*]}; i++)); do # pid="netstat -ntlp|grep $j |awk '{printf \$7}'|cut -d/ -f1" # echo -e "${GREEN_PREFIX}${service_filename[$i]} start success,port number:${service_ports[$j]} pid:$(eval $pid)$COLOR_SUFFIX" done -done +done \ No newline at end of file diff --git a/tools/component/component.go b/tools/component/component.go index 3f28862f5..a6edfe135 100644 --- a/tools/component/component.go +++ b/tools/component/component.go @@ -2,15 +2,16 @@ package component import ( "fmt" - "github.com/OpenIMSDK/chat/pkg/common/config" "github.com/OpenIMSDK/protocol/constant" + "os" + "time" + + "github.com/OpenIMSDK/chat/pkg/common/config" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/go-zookeeper/zk" "github.com/pkg/errors" "gopkg.in/yaml.v3" - "os" - "time" ) func initCfg(cfgPath string) error { @@ -31,15 +32,18 @@ func ComponentCheck(cfgPath string, hide bool) error { errorPrint(errs.Wrap(err).Error(), hide) return err } - var zkConn *zk.Conn - if zkConn, err = checkNewZkClient(hide); err != nil { - errorPrint(fmt.Sprintf("%v.Please check if your openIM server has started", err.Error()), hide) - return err - } - if err = checkGetCfg(zkConn, hide); err != nil { - errorPrint(fmt.Sprintf("%v.Please check if your openIM server has started", err.Error()), hide) - return err + if config.Config.Envs.Discovery != "k8s" { + var zkConn *zk.Conn + if zkConn, err = checkNewZkClient(hide); err != nil { + errorPrint(fmt.Sprintf("%v.Please check if your openIM server has started", err.Error()), hide) + return err + } + if err = checkGetCfg(zkConn, hide); err != nil { + errorPrint(fmt.Sprintf("%v.Please check if your openIM server has started", err.Error()), hide) + return err + } } + return nil }