docker k8s二次开发(一) 注册自定义api
来源:互联网 发布:淘宝推广话术 编辑:程序博客网 时间:2024/05/21 09:10
package mainimport ("fmt""math/rand""os""time""wdcp-build/apiserver/app""wdcp-build/apiserver/options""github.com/spf13/pflag""k8s.io/apiserver/pkg/util/flag""k8s.io/kubernetes/pkg/util/logs")func main() {rand.Seed(time.Now().UTC().UnixNano())s := options.NewServerRunOptions()s.AddFlags(pflag.CommandLine)flag.InitFlags()logs.InitLogs()defer logs.FlushLogs()// verflag.PrintAndExitIfRequested()if err := app.Run(s); err != nil {fmt.Fprintf(os.Stderr, "%v\n", err)os.Exit(1)}}
package appimport ("wdcp-build/apiserver/options""wdcp-build/pkg/master")func Run(s *options.ServerRunOptions) error {// glog.Info("config-file>>>%s", s.ConfigFile)// glog.Info("pkg-mac>>>>%s", s.PackageMac)// glog.Info("docker-endpoints>>>%s", s.DockerEndpoints)// client, err := s.NewSelfClient()// if err != nil {// glog.Errorf("Failed to create clientset: %v", err)// }// sharedInformers := informers.NewSharedInformerFactory(client, 10*time.Minute)config := &master.Config{ServerRunConfig: s,}m, err := master.New(config)if err != nil {return err}// sharedInformers.Start(wait.NeverStop)m.Run(s)return nil}
package masterimport ("fmt""log""net/http""wdcp-build/apiserver/options""wdcp-build/pkg/apiserver""wdcp-build/pkg/util""github.com/emicklei/go-restful""github.com/golang/glog")type Master struct {HandlerContainer *restful.Container}type Config struct {ServerRunConfig *options.ServerRunOptions}func New(c *Config) (*Master, error) {isExist, err := util.PathExists(c.ServerRunConfig.ConfigFile)if !isExist {return nil, err}util.RunOption, err = util.ReadFile(c.ServerRunConfig.ConfigFile)if err != nil {return nil, err}// glog.Info("RunOptions>>>>>%s", util.RunOption)container := restful.NewContainer()m := &Master{HandlerContainer: container,}m.InstallAPIs(c)return m, nil}func (s *Master) Run(options *options.ServerRunOptions) {addr := fmt.Sprintf(":%d", util.RunOption.InsecurePort)http := &http.Server{Addr: addr,Handler: s.HandlerContainer,MaxHeaderBytes: 1 << 20,}log.Fatal(http.ListenAndServe())}func (m *Master) InstallAPIs(c *Config) {for _, installer := range apiserver.APIInstallerList {if err := installer.InstallREST(m.HandlerContainer, c.ServerRunConfig); err != nil {glog.Error("%v", err)}}}
package masterimport ("fmt""log""net/http""wdcp-build/apiserver/options""wdcp-build/pkg/apiserver""wdcp-build/pkg/util""github.com/emicklei/go-restful""github.com/golang/glog")type Master struct {HandlerContainer *restful.Container}type Config struct {ServerRunConfig *options.ServerRunOptions}func New(c *Config) (*Master, error) {isExist, err := util.PathExists(c.ServerRunConfig.ConfigFile)if !isExist {return nil, err}util.RunOption, err = util.ReadFile(c.ServerRunConfig.ConfigFile)if err != nil {return nil, err}// glog.Info("RunOptions>>>>>%s", util.RunOption)container := restful.NewContainer()m := &Master{HandlerContainer: container,}m.InstallAPIs(c)return m, nil}func (s *Master) Run(options *options.ServerRunOptions) {addr := fmt.Sprintf(":%d", util.RunOption.InsecurePort)http := &http.Server{Addr: addr,Handler: s.HandlerContainer,MaxHeaderBytes: 1 << 20,}log.Fatal(http.ListenAndServe())}func (m *Master) InstallAPIs(c *Config) {for _, installer := range apiserver.APIInstallerList {if err := installer.InstallREST(m.HandlerContainer, c.ServerRunConfig); err != nil {glog.Error("%v", err)}}}
package apiserverimport ("fmt""time""wdcp-build/apiserver/options""wdcp-build/pkg/api/unversioned"restful "github.com/emicklei/go-restful")type APIInstaller struct {groupVersion stringprefix stringapiMappings []ApiMappingminRequestTimeout time.DurationserverRunConfig *options.ServerRunOptions}type ApiMapping struct {path stringverb stringapiHandler restful.RouteFunction}// NewWebService creates a new restful webservice with the api installer's prefix and version.func (a *APIInstaller) NewWebService() *restful.WebService {ws := new(restful.WebService)wsPath := a.groupVersion + a.prefixfmt.Println(wsPath)ws.Path(wsPath)ws.Doc("API at " + wsPath)ws.Consumes(restful.MIME_JSON)ws.Produces(restful.MIME_JSON)return ws}// Installs handlers for API resources.func (a *APIInstaller) Install(ws *restful.WebService) (apiResources []unversioned.APIResource, errors []error) {errors = make([]error, 0)apiResource, err := a.registerResourceHandlers(ws)if apiResource != nil {apiResources = append(apiResources, *apiResource)}if err != nil {errors = append(errors, fmt.Errorf("error in registering resource: %s, %v", a.prefix, err))}return apiResources, errors}func (a *APIInstaller) registerResourceHandlers(ws *restful.WebService) (*unversioned.APIResource, error) {var apiResource unversioned.APIResourcevar router *restful.RouteBuilderfor _, apiMap := range a.apiMappings {// fmt.Println(apiMap.verb)// fmt.Println(apiMap.path)// fmt.Println(apiMap.apiHandler)switch apiMap.verb {case unversioned.VERB_POST:router = ws.POST(apiMap.path).To(apiMap.apiHandler)ws.Route(router)case unversioned.VERB_GET:router = ws.GET(apiMap.path).To(apiMap.apiHandler)ws.Route(router)case unversioned.VERB_PUT:router = ws.PUT(apiMap.path).To(apiMap.apiHandler)ws.Route(router)case unversioned.VERB_DELETE:router = ws.DELETE(apiMap.path).To(apiMap.apiHandler)ws.Route(router)}}return &apiResource, nil}
package apiserverimport ("wdcp-build/apiserver/options""wdcp-build/pkg/buildengine""wdcp-build/pkg/k8sengine""wdcp-build/pkg/task"restful "github.com/emicklei/go-restful"utilerrors "k8s.io/apimachinery/pkg/util/errors")var APIInstallerList = []APIInstaller{{groupVersion: "/v1",prefix: "/test",apiMappings: []ApiMapping{{path: "/{user-id}",verb: "GET",apiHandler: buildimage.TestHandler,},},},{groupVersion: "/v1",prefix: "/code/branch",apiMappings: []ApiMapping{{path: "",verb: "POST",apiHandler: buildimage.BranchHandler,},},},{groupVersion: "/v1",prefix: "/code/version",apiMappings: []ApiMapping{{path: "/{project_name}/{branch}/{task_event_uuid}",verb: "GET",apiHandler: buildimage.CodeVersionHandler,},},},{groupVersion: "/v1",prefix: "/build",apiMappings: []ApiMapping{{path: "",verb: "POST",apiHandler: buildimage.BuildHandler,},},},{groupVersion: "/v1",prefix: "/task",apiMappings: []ApiMapping{{path: "/{task_event_uuid}",verb: "GET",apiHandler: task.TaskHandler,},},},{groupVersion: "/v1",prefix: "/app",apiMappings: []ApiMapping{{path: "",verb: "POST",apiHandler: k8sengine.CreateK8sHandler,},{path: "",verb: "DELETE",apiHandler: k8sengine.DeleteHandler,},{path: "",verb: "PUT",apiHandler: k8sengine.UpdateK8sHandler,},{path: "/deployment/{cluster_level}/{tenant}/{project_name}",verb: "GET",apiHandler: k8sengine.GetDeploymentHandler,},{path: "/statefulset/{cluster_level}/{tenant}/{project_name}",verb: "GET",apiHandler: k8sengine.GetStatefulSetHandler,},},},{groupVersion: "/v1",prefix: "/ns",apiMappings: []ApiMapping{{path: "",verb: "POST",apiHandler: k8sengine.CreateNsHandler,},{path: "/{cluster_level}/{namespace}",verb: "GET",apiHandler: k8sengine.GetNamespaceHandler,},},},{groupVersion: "/v1",prefix: "/vaddr",apiMappings: []ApiMapping{{path: "/{cluster_level}/{tenant}/{project_name}",verb: "GET",apiHandler: k8sengine.GetVisitAddrHandler,},},},{groupVersion: "/v1",prefix: "/pod",apiMappings: []ApiMapping{{path: "/{cluster_level}/{tenant}/{project_name}",verb: "GET",apiHandler: k8sengine.GetPodStatusHandler,},{path: "/log/{cluster_level}/{tenant}/{project_name}/{pod_name}",verb: "GET",apiHandler: k8sengine.GetPodLogHandler,},},},{groupVersion: "/v1",prefix: "/pods",apiMappings: []ApiMapping{{path: "",verb: "POST",apiHandler: k8sengine.ListPodsStatusHandler,},},},{groupVersion: "/v1",prefix: "/scale",apiMappings: []ApiMapping{{path: "",verb: "POST",apiHandler: k8sengine.ScaleK8sHandler,},},},{groupVersion: "/v1",prefix: "/list",apiMappings: []ApiMapping{{path: "/ns/{cluster_level}/{tenant}",verb: "GET",apiHandler: k8sengine.ListResNamespaceHandler,},{path: "/node/{cluster_level}/{tenant}",verb: "GET",apiHandler: k8sengine.ListResNodeHandler,},{path: "/deployment/{cluster_level}/{tenant}",verb: "GET",apiHandler: k8sengine.ListResDeploymentHandler,},{path: "/statefulset/{cluster_level}/{tenant}",verb: "GET",apiHandler: k8sengine.ListResStatefulSetHandler,},{path: "/service/{cluster_level}/{tenant}",verb: "GET",apiHandler: k8sengine.ListResServiceHandler,},{path: "/pod/{cluster_level}/{tenant}",verb: "GET",apiHandler: k8sengine.ListResPodHandler,},},},}func (installer *APIInstaller) InstallREST(container *restful.Container, c *options.ServerRunOptions) error {ws := installer.NewWebService()_, registrationErrors := installer.Install(ws)container.Add(ws)return utilerrors.NewAggregate(registrationErrors)}
阅读全文
0 0
- docker k8s二次开发(一) 注册自定义api
- docker k8s二次开发(二)api的实现
- k8s,docker,mesos
- docker学习笔记(k8s)
- [k8s]docker entrypoint图解
- docker-k8s常用命令
- k8s docker集群搭建
- Docker宣布拥抱k8s,k8s将一统天下?
- k8s api object
- [k8s]api访问初探
- Docker Remote api(一)
- docker和k8s基本操作
- ArcEngine二次开发API之Bug集(一 )
- ECMALL用户注册二次开发
- iwebshop二次开发---邀请注册
- Docker学习笔记 — k8s部署
- k8s调用gpu,nvidia-docker使用gpu
- 容器平台选型:Docker、DC/OS、K8S ?
- Kylin安装
- 解决跨域问题
- struts2+spring框架搭建
- mybatis 动态sql(转载~mark下 )
- 源码探探之startActivity(二)
- docker k8s二次开发(一) 注册自定义api
- 网段,掩码,网关,路由器
- Jmeter参数化
- xcode提交app时出现icon缺少167.png图片的问题
- 【Java】Dubbo+zookeeper搭建分布式服务框架
- Shader编程学习笔记(四)—— Unity Shader的组织形式(ShaderLab)
- 理解typename的两个含义
- SpringCloud的服务注册与发现Eureka
- 推箱子寻求最少步数(广度优先搜索)