Notification-Manager 原码分析(一) 本地部署运行环境搭建

notification-manager 原码分析(一) 本地部署运行环境搭建

原码构建部署

  • 拉取项目(项目地址: github.com/kubesphere/notification-manager)
  • 首先查看项目文档(docs目录)和了解构建、启动入口(Makefile文件)

构建、部署operator

  • 生成和安装k8s crd资源: make install ,若报错参见下方遇到问题及解决办法
  • 查看创建的资源
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # alias mk="minikube kubectl --" ### 本地使用minikube调试

    $ mk get crd ### crd资源声明已经创建

    NAME CREATED AT
    configs.notification.kubesphere.io 2023-11-05T05:06:50Z
    notificationmanagers.notification.kubesphere.io 2023-11-05T05:06:50Z
    receivers.notification.kubesphere.io 2023-11-05T05:06:51Z
    routers.notification.kubesphere.io 2023-11-05T05:06:51Z
    silences.notification.kubesphere.io 2023-11-05T05:06:51Z


make deploy 部署operator资源

1
2
3
4
5
6
mk create ns kubesphere-monitoring-system 

make deploy

kubecm namespace kubesphere-monitoring-system # 使用kubecm设置默认缺省namespace,后续命令不带namespace默认表示该namespace

1
2
3
4
5
6
7
8
9
10
11
12
13
$ mk get all
NAME READY STATUS RESTARTS AGE
pod/notification-manager-operator-5b8b7cd49f-rkst9 2/2 Running 0 31m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/notification-manager-controller-metrics ClusterIP 10.108.20.123 <none> 8443/TCP 31m
service/notification-manager-webhook ClusterIP 10.109.92.29 <none> 443/TCP 31m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/notification-manager-operator 1/1 1 1 31m

NAME DESIRED CURRENT READY AGE
replicaset.apps/notification-manager-operator-5b8b7cd49f 1 1 1 31m

构建、部署manager

make build 构建manager docker镜像

  • 构建镜像
1
make build-nm REGISTRY=my # 将镜像REGISTRY 自定义下避免去远端拉取
1
2
3
4
5
make build-nm REGISTRY=my
docker buildx build --push --platform linux/amd64,linux/arm64 -f cmd/notification-manager/Dockerfile . -t my/notification-manager:latest
[+] Building 0.0s (0/0) docker:desktop-linux
ERROR: Multiple platforms feature is currently not supported for docker driver. Please switch to a different driver (eg. "docker buildx create --use")
make: *** [build-nm] Error 1
  • mac端docker未支持此构建参数, 手动调整构建
1
2
3
4
5
# 构建镜像
docker build -f cmd/notification-manager/Dockerfile . -t my/notification-manager:v0.0.1
# minikube load本地镜像,这样minikube node上就有镜像了,不用推也不用拉,方便调试,避免网络问题。
minikube image load my/notification-manager:v0.0.1

部署notification manager

  • 部署消息模板默认配置
1
mk apply -f config/samples/template.yaml
  • 修改notification-manager镜像,并创建
1
2
3
4
5
6
7
8
9
10
# vi config/samples/notification_manager.yaml 


image: my/notification-manager:v0.0.1 # 修改image地址为: my/notification-manager:v0.0.1
imagePullPolicy: Never # 使用本地镜像,不从远端拉取


# 之后部署notification_manager
mk apply -f config/samples/notification_manager.yaml

  • 查看部署结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ mk get all
NAME READY STATUS RESTARTS AGE
pod/notification-manager-deployment-85f9c4655c-qmkpt 1/1 Running 0 11m
pod/notification-manager-operator-5b8b7cd49f-rkst9 2/2 Running 0 95m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/notification-manager-controller-metrics ClusterIP 10.108.20.123 <none> 8443/TCP 95m
service/notification-manager-svc ClusterIP 10.108.186.192 <none> 19093/TCP 25m
service/notification-manager-webhook ClusterIP 10.109.92.29 <none> 443/TCP 95m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/notification-manager-deployment 1/1 1 1 25m
deployment.apps/notification-manager-operator 1/1 1 1 95m

NAME DESIRED CURRENT READY AGE
replicaset.apps/notification-manager-deployment-84c4cbf56 0 0 0 25m
replicaset.apps/notification-manager-deployment-85f9c4655c 1 1 1 11m
replicaset.apps/notification-manager-operator-5b8b7cd49f 1 1 1 95m
  • 转发端口请求测试
1
2
3
4
5
6
# 转发notification-manager pod的端口到本地
mk port-forward notification-manager-deployment-85f9c4655c-qmkpt 19093

Forwarding from 127.0.0.1:19093 -> 19093
Forwarding from [::1]:19093 -> 19093

  • 访问测试
1
2
3
4
5
# 参见 docs/api/_index.md文档中的请求示例json数据,创建一个json文件做post请求体

$ curl -XPOST http://localhost:19093/api/v2/alerts -d @./docs/api/alert.json
{"Status":200,"Message":"Notification request accepted"}

  • 对应port-forward终端也可以看到一条转发请求,说明请求被转发进了k8s中
1
Handling connection for 19093

至此本地部署和调试请求流程已完成。


遇到问题及解决办法

malformed module path “io/fs”: missing

  • 报错提示内容
1
2
3
4
5
../../../../pkg/mod/golang.org/x/net@v0.7.0/http2/transport.go:19:2: malformed module path "io/fs": missing dot in first path element
Error: not all generators ran successfully
run `controller-gen object:headerFile=hack/boilerplate.go.txt paths=./pkg/apis/v2beta1 -w` to see all available markers, or `controller-gen object:headerFile=hack/boilerplate.go.txt paths=./pkg/apis/v2beta1 -h` for usage
make: *** [generate] Error 1

  • 解决办法: 参见go.mod go version切换到对应golang版本

controller-gen 版本问题

  • 如果本地已经安装过,运行报错,检查下版本确保和Makefile 写的版本保持一致
1
2
3
4
5
6
7
8
9
10
11
12
13
14
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
1
2
controller-gen --version
Version: v0.4.1

kustomize: command not found

  • 报错提示
1
2
3
/bin/sh: kustomize: command not found
make: *** [manifests] Error 127

  • 解决办法: 安装kustomize
1
2
3
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"  | bash
mv kustomize $GOPATH/bin