提交 c161f128 编写于 作者: P Piotr Bryk

Merge pull request #68 from kubernetes/microservice-list-backend

Backend implementation of the service list view
......@@ -22,35 +22,67 @@ import (
)
// Creates a new HTTP handler that handles all requests to the API of the backend.
func CreateApiHandler(client *client.Client) http.Handler {
func CreateHttpApiHandler(client *client.Client) http.Handler {
apiHandler := ApiHandler{client}
wsContainer := restful.NewContainer()
// TODO(bryk): This is for tests only. Replace with real implementation once ready.
ws := new(restful.WebService)
ws.Path("/api/deploy").
deployWs := new(restful.WebService)
deployWs.Path("/api/deploy").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
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)
deployWs.Route(
deployWs.POST("").
To(apiHandler.handleDeploy).
Reads(DeployAppConfig{}).
Writes(DeployAppConfig{}))
wsContainer.Add(deployWs)
microserviceListWs := new(restful.WebService)
microserviceListWs.Path("/api/microservice").
Produces(restful.MIME_JSON)
microserviceListWs.Route(
microserviceListWs.GET("").
To(apiHandler.handleGetMicroserviceList).
Writes(MicroserviceList{}))
wsContainer.Add(microserviceListWs)
return wsContainer
}
type ApiHandler struct {
client *client.Client
}
// Handles deploy API call.
func (apiHandler *ApiHandler) handleDeploy(request *restful.Request, response *restful.Response) {
cfg := new(DeployAppConfig)
if err := request.ReadEntity(cfg); err != nil {
handleInternalError(response, err)
return
}
if err := DeployApp(cfg, apiHandler.client); err != nil {
handleInternalError(response, err)
return
}
response.WriteHeaderAndEntity(http.StatusCreated, cfg)
}
// Handles get microservice list API call.
func (apiHandler *ApiHandler) handleGetMicroserviceList(
request *restful.Request, response *restful.Response) {
result, err := GetMicroserviceList(apiHandler.client)
if err != nil {
handleInternalError(response, err)
return
}
response.WriteHeaderAndEntity(http.StatusCreated, result)
}
// Handler that writes the given error to the response and sets appropriate HTTP status headers.
func HandleInternalError(response *restful.Response, err error) {
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(apiserverClient))
http.Handle("/api/", CreateHttpApiHandler(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 (
"k8s.io/kubernetes/pkg/api"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
)
// List of microservices in the cluster.
type MicroserviceList struct {
// Unordered list of microservices.
Microservices []Microservice `json:"microservices"`
}
// Microservice is a Kubernetes replica set plus zero or more Kubernetes services.
type Microservice struct {
// Name of the microservice, derived from the replica set.
Name string `json:"name"`
// Replica set that represents the microservice.
ReplicaSet ReplicaSet `json:"replicaSet"`
// TODO(bryk): Add service field here.
}
// Replica set model to represent in the user interface.
type ReplicaSet struct {
// Number of pods that are currently running.
PodsRunning int `json:"podsRunning"`
// Number of pods that are desired to run in this replica set.
PodsDesired int `json:"podsDesired"`
// Container images of the replica set.
ContainerImages []string `json:"containerImages"`
}
// Returns a list of all microservices in the cluster.
func GetMicroserviceList(client *client.Client) (*MicroserviceList, error) {
list, err := client.ReplicationControllers(api.NamespaceAll).
List(labels.Everything(), fields.Everything())
if err != nil {
return nil, err
}
microserviceList := &MicroserviceList{}
for _, element := range list.Items {
var containerImages []string
for _, container := range element.Spec.Template.Spec.Containers {
containerImages = append(containerImages, container.Image)
}
microserviceList.Microservices = append(microserviceList.Microservices, Microservice{
Name: element.ObjectMeta.Name,
ReplicaSet: ReplicaSet{
ContainerImages: containerImages,
PodsRunning: element.Status.Replicas,
PodsDesired: element.Spec.Replicas,
},
})
}
return microserviceList, nil
}
......@@ -20,7 +20,7 @@ import chromeModule from './chrome/chrome.module';
import deployModule from './deploy/deploy.module';
import indexConfig from './index.config';
import routeConfig from './index.route';
import serviceListModule from './servicelist/servicelist.module';
import microserviceListModule from './microservicelist/microservicelist.module';
import zerostateModule from './zerostate/zerostate.module';
......@@ -36,7 +36,7 @@ export default angular.module(
'ui.router',
chromeModule.name,
deployModule.name,
serviceListModule.name,
microserviceListModule.name,
zerostateModule.name,
])
.config(indexConfig)
......
......@@ -18,9 +18,9 @@
*
* @final
*/
export default class ServiceListController {
export default class MicroserviceListController {
constructor() {
/** @export {!Array<string>} */
this.testServices = ['srvc1', 'srvc2'];
this.test = ['srvc1', 'srvc2'];
}
}
......@@ -16,6 +16,6 @@ limitations under the License.
<div layout="vertical" layout-fill>
<header>
Service page. {{ctrl.testServices}}
Service page. {{ctrl.test}}
</header>
</div>
......@@ -12,17 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import stateConfig from './servicelist.state';
import stateConfig from './microservicelist.state';
/**
* Angular module for the service list view.
* Angular module for the microservice list view.
*
* The view shows services running in the cluster and allows to manage them.
* The view shows microservices running in the cluster and allows to manage them.
*/
export default angular.module(
'kubernetesDashboard.serviceList',
'kubernetesDashboard.microserviceList',
[
'ngMaterial',
'ui.router',
])
.config(stateConfig);
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import ServiceListController from './servicelist.controller';
import MicroserviceListController from './microservicelist.controller';
/**
......@@ -22,10 +22,10 @@ import ServiceListController from './servicelist.controller';
* @ngInject
*/
export default function stateConfig($stateProvider) {
$stateProvider.state('servicelist', {
controller: ServiceListController,
$stateProvider.state('microservicelist', {
controller: MicroserviceListController,
controllerAs: 'ctrl',
url: '/servicelist',
templateUrl: 'servicelist/servicelist.html',
url: '/microservicelist',
templateUrl: 'microservicelist/microservicelist.html',
});
}
......@@ -16,7 +16,7 @@ limitations under the License.
<div layout="vertical" layout-fill>
<header>
Zero state page. <a ui-sref="servicelist">Go to services page</a> or
Zero state page. <a ui-sref="microservicelist">Go to microservices page</a> or
<a ui-sref="deploy">deploy an app</a>.
</header>
</div>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册