提交 0fec3533 编写于 作者: B bryk

Backend implementation for deploy app view

The implementation is basically a test one. It'll to verify that
end-to-end setup of the application works.
上级 59279a63
......@@ -15,26 +15,42 @@
package main
import (
"io"
"net/http"
restful "github.com/emicklei/go-restful"
client "k8s.io/kubernetes/pkg/client/unversioned"
)
// Creates a new HTTP handler that handles all requests to the API of the backend.
func createApiHandler() http.Handler {
func CreateApiHandler(client *client.Client) http.Handler {
wsContainer := restful.NewContainer()
// TODO(bryk): This is for tests only. Replace with real implementation once ready.
ws := new(restful.WebService)
ws.Path("/api/test").
ws.Path("/api/deploy").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("").To(func(req *restful.Request, resp *restful.Response) {
io.WriteString(resp, "Hello world")
}))
ws.Route(ws.POST("").To(func(request *restful.Request, response *restful.Response) {
cfg := new(DeployAppConfig)
if err := request.ReadEntity(cfg); err != nil {
HandleInternalError(response, err)
return
}
if err := DeployApp(cfg, client); err != nil {
HandleInternalError(response, err)
return
}
response.WriteHeaderAndEntity(http.StatusCreated, cfg)
}).Reads(DeployAppConfig{}).Writes(DeployAppConfig{}))
wsContainer.Add(ws)
return wsContainer
}
// Handler that writes the given error to the response and sets appropriate HTTP status headers.
func HandleInternalError(response *restful.Response, err error) {
response.AddHeader("Content-Type", "text/plain")
response.WriteErrorString(http.StatusInternalServerError, err.Error()+"\n")
}
......@@ -54,6 +54,6 @@ func main() {
// Run a HTTP server that serves static public files from './public' and handles API calls.
// TODO(bryk): Disable directory listing.
http.Handle("/", http.FileServer(http.Dir("./public")))
http.Handle("/api/", createApiHandler())
http.Handle("/api/", CreateApiHandler(apiserverClient))
glog.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *argPort), nil))
}
// Copyright 2015 Google Inc. 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 (
"math/rand"
"strconv"
"time"
api "k8s.io/kubernetes/pkg/api"
client "k8s.io/kubernetes/pkg/client/unversioned"
)
// Configuration for an app deployment.
type DeployAppConfig struct {
// Name of the application.
AppName string `json:"appName"`
// Docker image path for the application.
ContainerImage string `json:"containerImage"`
}
// Deploys an app based on the given configuration. The app is deployed using the given client.
func DeployApp(config *DeployAppConfig, client *client.Client) error {
// TODO(bryk): The implementation below is just for tests. To complete an end-to-end setup of
// the project. It'll be replaced with a real implementation.
rand.Seed(time.Now().UTC().UnixNano())
podName := config.AppName + "-" + strconv.Itoa(rand.Intn(10000))
pod := api.Pod{
ObjectMeta: api.ObjectMeta{
Name: podName,
},
Spec: api.PodSpec{
Containers: []api.Container{{
Name: config.AppName,
Image: config.ContainerImage,
}},
},
}
_, err := client.Pods(api.NamespaceDefault).Create(&pod)
return err
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册