提交 a72ddd61 编写于 作者: M Mateusz

Merge pull request #51 from kubernetes/gulp-backend-test-tasks-implement-apiserverclient

Function that creates k8s apiserver client
......@@ -89,7 +89,7 @@ gulp.task('serve', ['index'], serveDevelopmentMode);
/**
* Serves the application in production mode.
*/
gulp.task('serve:prod', ['build-frontend', 'spawn-backend'], function () {
gulp.task('serve:prod', ['build-frontend'], function () {
browserSyncInit(conf.paths.dist);
});
......
// 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 backend
import client "k8s.io/kubernetes/pkg/client/unversioned"
// Factory that creates Kubernetes API clients.
type ClientFactory interface {
// Creates new API client assuming that the binary runs in a cluster.
NewInCluster() (*client.Client, error)
// Creates new API client from the given config.
New(*client.Config) (*client.Client, error)
}
// Creates new Kubernetes Apiserver client. When apiserverHost param is empty string the function
// assumes that it is running inside a Kubernetes cluster and attempts to discover the Apiserver.
// Otherwise, it connects to the Apiserver specified.
// apiserverHost param is in the format of protocol://address:port, e.g., http://localhost:8001.
func CreateApiserverClient(apiserverHost string,
clientFactory ClientFactory) (*client.Client, error) {
if apiserverHost == "" {
return clientFactory.NewInCluster()
} else {
cfg := client.Config{
Host: apiserverHost,
}
return clientFactory.New(&cfg)
}
}
// Default implementation of the ClientFactory. It uses k8s.io package API.
type ClientFactoryImpl struct{}
func (ClientFactoryImpl) New(cfg *client.Config) (*client.Client, error) {
return client.New(cfg)
}
func (ClientFactoryImpl) NewInCluster() (*client.Client, error) {
return client.NewInCluster()
}
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package backend
import (
"flag"
......@@ -24,15 +24,33 @@ import (
)
var (
argPort = pflag.Int("port", 8080, "The port to listen to for incomming HTTP requests")
argPort = pflag.Int("port", 8080, "The port to listen to for incoming HTTP requests")
argApiserverHost = pflag.String("apiserver-host", "", "The address of the Kubernetes Apiserver "+
"to connect to in the format of protocol://address:port, e.g., "+
"http://localhost:8001. If not specified, the assumption is that the binary runs in a"+
"Kubernetes cluster and local discovery is attempted.")
)
func main() {
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
glog.Info("Starting HTTP server on port ", *argPort)
defer glog.Flush()
apiserverClient, err := CreateApiserverClient(*argApiserverHost, new(ClientFactoryImpl))
if err != nil {
glog.Fatal(err)
}
serverAPIVersion, err := apiserverClient.ServerAPIVersions()
if err != nil {
glog.Fatal(err)
}
// Display Apiserver version. This is just for tests.
println("Server API version: " + serverAPIVersion.GoString())
// Run a HTTP server that serves static files from current directory.
// TODO(bryk): Disable directory listing.
http.Handle("/", http.FileServer(http.Dir("./")))
......
// 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 backend
import (
"github.com/dashboard/src/app/backend"
client "k8s.io/kubernetes/pkg/client/unversioned"
"testing"
)
var fakeRemoteClient = new(client.Client)
var fakeInClusterClient = new(client.Client)
type FakeClientFactory struct{}
func (FakeClientFactory) New(cfg *client.Config) (*client.Client, error) {
return fakeRemoteClient, nil
}
func (FakeClientFactory) NewInCluster() (*client.Client, error) {
return fakeInClusterClient, nil
}
func TestCreateApiserverClient_inCluster(t *testing.T) {
client, _ := backend.CreateApiserverClient("", new(FakeClientFactory))
if client != fakeInClusterClient {
t.Fatal("Expected in cluster client to be created")
}
}
func TestCreateApiserverClient_remote(t *testing.T) {
client, _ := backend.CreateApiserverClient("http://foo:bar", new(FakeClientFactory))
if client != fakeRemoteClient {
t.Fatal("Expected remote client to be created")
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册