一、环境介绍
系统:Centos7
Kubeflow版本:0.7.0
Pipeline版本:0.1.31
代码已上传我的github
https://github.com/reachyu/pipelines-0.1.31
二、开始coding
在
《kubeflow二次开发–pipeline后端在mysql数据库增加表》
中我们新增了一个表。
本次二次开发场景是增加一个应用管理模块,需要增加应用管理相关的表。
本次表模型文件名是app_manage.go,在数据库中创建的表的名称是app_manages。
现在我们开发两个服务,向此表增加数据以及从此表查询数据。
pipeline后端服务目录个人理解如下
服务调用路径:
1) 在backend\src\apiserver\client_manager.go初始化,创建实例
2) backend\src\apiserver\main.go定义访问路径和路径路由
3) main.go–>*server.go–>*resource.go–>*storage.go
1、增加数据库操作实现
增加文件backend\src\apiserver\storage\myapp_manage_store.go
并在同级目录的BUILD.bazel文件增加内容(bazel编译需要)
本次只实现两个功能:增加数据、查询数据
2、模块入口封装
修改backend\src\apiserver\resource\resource_manager.go
增加代码
修改\backend\src\apiserver\resource\client_manager_fake.go
增加代码
3、增加模块服务实现
增加文件backend\src\apiserver\server\myapp_manage_server.go
并在同级目录的BUILD.bazel文件增加内容(bazel编译需要)
4、初始化
修改backend\src\apiserver\client_manager.go
增加代码
5、定义接口访问地址
修改backend\src\apiserver\main.go
增加代码
6、制作和使用镜像
参见
《kubeflow二次开发–pipeline后端build image》
加载镜像
使用镜像
修改pod使用镜像
kubectl edit deployment admission-webhook-deployment -n kubeflow
把镜像版本修改为重新build的版本号
7、访问接口
访问查询接口
http://nodeip:port/pipeline/apis/v1beta1/myapp/get?id=
增加数据接口
7、一个关于数据类型的问题
问题
{"error_message":"Error get app .: InternalServerError: Failed to get application: sql: Scan error on column index 7, name \"update_at\": converting driver.Value type \u003cnil\u003e (\"\u003cnil\u003e\") to a int64: invalid syntax: sql: Scan error on column index 7, name \"update_at\": converting driver.Value type \u003cnil\u003e (\"\u003cnil\u003e\") to a int64: invalid syntax","error_details":"Error get app .: InternalServerError: Failed to get application: sql: Scan error on column index 7, name \"update_at\": converting driver.Value type \u003cnil\u003e (\"\u003cnil\u003e\") to a int64: invalid syntax: sql: Scan error on column index 7, name \"update_at\": converting driver.Value type \u003cnil\u003e (\"\u003cnil\u003e\") to a int64: invalid syntax"}
原因
从数据库读取可能为null值得值时,可以选择使用sql.NULL***来读取;或者使用IFNULL、COALESCE等命令让数据库查询值返回不为”“或者NULL。
若需要往数据库中插入null值,则依然可以使用sql.NULL***存储所需的值,然后进行插入NULL值。
直接使用sql.NULL***类型容易出现valid遗漏设置等问题,普通int、string与其转换时,请写几个简单的get、set函数。
解决
修改文件backend\src\apiserver\model\app_manage.go
允许为空的字段,类型改为如下类型
修改文件backend\src\apiserver\storage\app_manage_store.go
scanRows方法,允许为空的字段,类型改为如下类型
修改文件backend\src\apiserver\server\app_manage_server.go
Model定义的字段,类型是sql.NullString,sql.NullString是一个struct
从post取到的值赋值给model成员的时候,sql.NullString类型字段要赋值给String