Skip to content

Commit a1db480

Browse files
committed
Merge pull request #409 from bryk/yaml-propagate
Propagate apiclient to YAML deploy pipeline
2 parents 28c220f + 8ccd2fe commit a1db480

File tree

6 files changed

+50
-91
lines changed

6 files changed

+50
-91
lines changed

src/app/backend/apihandler.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
restful "github.com/emicklei/go-restful"
2424
client "k8s.io/kubernetes/pkg/client/unversioned"
25+
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
2526
)
2627

2728
const (
@@ -55,8 +56,10 @@ func FormatResponseLog(resp *restful.Response, req *restful.Request) string {
5556
}
5657

5758
// Creates a new HTTP handler that handles all requests to the API of the backend.
58-
func CreateHttpApiHandler(client *client.Client, heapsterClient HeapsterClient) http.Handler {
59-
apiHandler := ApiHandler{client, heapsterClient}
59+
func CreateHttpApiHandler(client *client.Client, heapsterClient HeapsterClient,
60+
clientConfig clientcmd.ClientConfig) http.Handler {
61+
62+
apiHandler := ApiHandler{client, heapsterClient, clientConfig}
6063
wsContainer := restful.NewContainer()
6164

6265
deployWs := new(restful.WebService)
@@ -181,6 +184,7 @@ func CreateHttpApiHandler(client *client.Client, heapsterClient HeapsterClient)
181184
type ApiHandler struct {
182185
client *client.Client
183186
heapsterClient HeapsterClient
187+
clientConfig clientcmd.ClientConfig
184188
}
185189

186190
// Handles deploy API call.
@@ -206,7 +210,8 @@ func (apiHandler *ApiHandler) handleDeployFromFile(request *restful.Request, res
206210
return
207211
}
208212

209-
isDeployed, err := DeployAppFromFile(deploymentSpec, CreateObjectFromInfoFn)
213+
isDeployed, err := DeployAppFromFile(
214+
deploymentSpec, CreateObjectFromInfoFn, apiHandler.clientConfig)
210215
if !isDeployed {
211216
handleInternalError(response, err)
212217
return
@@ -218,9 +223,9 @@ func (apiHandler *ApiHandler) handleDeployFromFile(request *restful.Request, res
218223
}
219224

220225
response.WriteHeaderAndEntity(http.StatusCreated, AppDeploymentFromFileResponse{
221-
Name: deploymentSpec.Name,
226+
Name: deploymentSpec.Name,
222227
Content: deploymentSpec.Content,
223-
Error: errorMessage,
228+
Error: errorMessage,
224229
})
225230
}
226231

src/app/backend/apiserverclient.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,42 @@ import (
1818
"log"
1919

2020
client "k8s.io/kubernetes/pkg/client/unversioned"
21+
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
22+
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
2123
)
2224

23-
// Factory that creates Kubernetes API clients.
24-
type ClientFactory interface {
25-
// Creates new API client assuming that the binary runs in a cluster.
26-
NewInCluster() (*client.Client, error)
27-
28-
// Creates new API client from the given config.
29-
New(*client.Config) (*client.Client, error)
30-
}
31-
3225
// Creates new Kubernetes Apiserver client. When apiserverHost param is empty string the function
3326
// assumes that it is running inside a Kubernetes cluster and attempts to discover the Apiserver.
3427
// Otherwise, it connects to the Apiserver specified.
35-
// apiserverHost param is in the format of protocol://address:port, e.g., http://localhost:8001.
36-
func CreateApiserverClient(apiserverHost string,
37-
clientFactory ClientFactory) (*client.Client, error) {
38-
log.Printf("Creating API client for %s", apiserverHost)
39-
40-
if apiserverHost == "" {
41-
return clientFactory.NewInCluster()
42-
} else {
43-
cfg := client.Config{
44-
Host: apiserverHost,
45-
}
46-
return clientFactory.New(&cfg)
28+
//
29+
// apiserverHost param is in the format of protocol://address:port/pathPrefix, e.g.,
30+
// http://localhost:8001.
31+
//
32+
// Returns created client and its configuration.
33+
func CreateApiserverClient(apiserverHost string) (*client.Client, clientcmd.ClientConfig, error) {
34+
35+
overrides := &clientcmd.ConfigOverrides{}
36+
37+
if apiserverHost != "" {
38+
overrides.ClusterInfo = clientcmdapi.Cluster{Server: apiserverHost}
4739
}
48-
}
4940

50-
// Default implementation of the ClientFactory. It uses k8s.io package API.
51-
type ClientFactoryImpl struct{}
41+
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
42+
&clientcmd.ClientConfigLoadingRules{}, overrides)
5243

53-
func (ClientFactoryImpl) New(cfg *client.Config) (*client.Client, error) {
54-
return client.New(cfg)
55-
}
44+
cfg, err := clientConfig.ClientConfig()
45+
46+
if err != nil {
47+
return nil, nil, err
48+
}
49+
50+
log.Printf("Creating API server client for %s", cfg.Host)
51+
52+
client, err := client.New(cfg)
53+
54+
if err != nil {
55+
return nil, nil, err
56+
}
5657

57-
func (ClientFactoryImpl) NewInCluster() (*client.Client, error) {
58-
return client.NewInCluster()
58+
return client, clientConfig, nil
5959
}

src/app/backend/dashboard.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func main() {
4141

4242
log.Printf("Starting HTTP server on port %d", *argPort)
4343

44-
apiserverClient, err := CreateApiserverClient(*argApiserverHost, new(ClientFactoryImpl))
44+
apiserverClient, config, err := CreateApiserverClient(*argApiserverHost)
4545
if err != nil {
4646
log.Fatalf("Error while initializing connection to Kubernetes master: %s. Quitting.", err)
4747
}
@@ -54,6 +54,6 @@ func main() {
5454
// Run a HTTP server that serves static public files from './public' and handles API calls.
5555
// TODO(bryk): Disable directory listing.
5656
http.Handle("/", http.FileServer(http.Dir("./public")))
57-
http.Handle("/api/", CreateHttpApiHandler(apiserverClient, heapsterRESTClient))
57+
http.Handle("/api/", CreateHttpApiHandler(apiserverClient, heapsterRESTClient, config))
5858
log.Print(http.ListenAndServe(fmt.Sprintf(":%d", *argPort), nil))
5959
}

src/app/backend/deploy.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"k8s.io/kubernetes/pkg/api"
2323
"k8s.io/kubernetes/pkg/api/resource"
2424
client "k8s.io/kubernetes/pkg/client/unversioned"
25+
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
2526
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2627
kubectlResource "k8s.io/kubernetes/pkg/kubectl/resource"
2728
"k8s.io/kubernetes/pkg/util/intstr"
@@ -281,17 +282,18 @@ type createObjectFromInfo func(info *kubectlResource.Info) (bool, error)
281282
// Implementation of createObjectFromInfo
282283
func CreateObjectFromInfoFn(info *kubectlResource.Info) (bool, error) {
283284
createdResource, err := kubectlResource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object)
284-
return createdResource != nil , err
285+
return createdResource != nil, err
285286
}
286287

287288
// Deploys an app based on the given yaml or json file.
288-
func DeployAppFromFile(spec *AppDeploymentFromFileSpec, createObjectFromInfoFn createObjectFromInfo) (bool, error) {
289+
func DeployAppFromFile(spec *AppDeploymentFromFileSpec,
290+
createObjectFromInfoFn createObjectFromInfo, clientConfig clientcmd.ClientConfig) (bool, error) {
289291
const (
290292
validate = true
291293
emptyCacheDir = ""
292294
)
293295

294-
factory := cmdutil.NewFactory(nil)
296+
factory := cmdutil.NewFactory(clientConfig)
295297
schema, err := factory.Validator(validate, emptyCacheDir)
296298
if err != nil {
297299
return false, err
@@ -307,12 +309,12 @@ func DeployAppFromFile(spec *AppDeploymentFromFileSpec, createObjectFromInfoFn c
307309
Flatten().
308310
Do()
309311

310-
deployedResourcesCount:= 0
312+
deployedResourcesCount := 0
311313

312314
err = r.Visit(func(info *kubectlResource.Info, err error) error {
313315
isDeployed, err := createObjectFromInfoFn(info)
314316
if isDeployed {
315-
deployedResourcesCount ++
317+
deployedResourcesCount++
316318
log.Printf("%s is deployed", info.Name)
317319
}
318320
return err

src/test/backend/apiserverclient_test.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/test/backend/deploy_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestDeployAppFromFileWithValidContent(t *testing.T) {
193193
}
194194
fakeCreateObjectFromInfo := func(info *kubectlResource.Info) (bool, error) { return true, nil }
195195

196-
isDeployed, err := DeployAppFromFile(spec, fakeCreateObjectFromInfo)
196+
isDeployed, err := DeployAppFromFile(spec, fakeCreateObjectFromInfo, nil)
197197
if err != nil {
198198
t.Errorf("Expected return value to have %#v but got %#v", nil, err)
199199
}
@@ -210,7 +210,7 @@ func TestDeployAppFromFileWithInvalidContent(t *testing.T) {
210210
// return is set to true to check if the validation prior to this function really works
211211
fakeCreateObjectFromInfo := func(info *kubectlResource.Info) (bool, error) { return true, nil }
212212

213-
isDeployed, err := DeployAppFromFile(spec, fakeCreateObjectFromInfo)
213+
isDeployed, err := DeployAppFromFile(spec, fakeCreateObjectFromInfo, nil)
214214
if err == nil {
215215
t.Errorf("Expected return value to have an error but got %#v", nil)
216216
}

0 commit comments

Comments
 (0)