Skip to content

Commit 9d11013

Browse files
Marcin Maciaszczykurcan
authored andcommitted
Updated Godeps
1 parent b27eb32 commit 9d11013

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

src/app/backend/apihandler.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ func CreateHttpApiHandler(client *client.Client, heapsterClient *unversioned.RES
8686
Writes(Protocols{}))
8787
wsContainer.Add(deployWs)
8888

89+
deployFromFileWs := new(restful.WebService)
90+
deployFromFileWs.Path("/api/appdeploymentfromfile").
91+
Consumes(restful.MIME_JSON).
92+
Produces(restful.MIME_JSON)
93+
deployFromFileWs.Route(
94+
deployFromFileWs.POST("").
95+
To(apiHandler.handleDeployFromFile).
96+
Reads(AppDeploymentFromFileSpec{}).
97+
Writes(AppDeploymentFromFileSpec{}))
98+
wsContainer.Add(deployFromFileWs)
99+
89100
replicaSetWs := new(restful.WebService)
90101
replicaSetWs.Filter(wsLogger)
91102
replicaSetWs.Path("/api/replicasets").
@@ -188,6 +199,22 @@ func (apiHandler *ApiHandler) handleDeploy(request *restful.Request, response *r
188199
response.WriteHeaderAndEntity(http.StatusCreated, appDeploymentSpec)
189200
}
190201

202+
// Handles deploy from file API call.
203+
func (apiHandler *ApiHandler) handleDeployFromFile(request *restful.Request, response *restful.Response) {
204+
deploymentSpec := new(AppDeploymentFromFileSpec)
205+
if err := request.ReadEntity(deploymentSpec); err != nil {
206+
handleInternalError(response, err)
207+
return
208+
}
209+
if err := DeployAppFromFile(deploymentSpec, apiHandler.client); err != nil {
210+
handleInternalError(response, err)
211+
return
212+
}
213+
214+
response.WriteHeaderAndEntity(http.StatusCreated, deploymentSpec)
215+
216+
}
217+
191218
// Handles app name validation API call.
192219
func (apiHandler *ApiHandler) handleNameValidity(request *restful.Request, response *restful.Response) {
193220
spec := new(AppNameValiditySpec)

src/app/backend/deploy.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ package main
1616

1717
import (
1818
"fmt"
19+
ioutil "io/ioutil"
1920
"log"
21+
"os"
2022
"strings"
2123

2224
"k8s.io/kubernetes/pkg/api"
2325
"k8s.io/kubernetes/pkg/api/resource"
26+
create "k8s.io/kubernetes/pkg/kubectl/cmd"
27+
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2428
client "k8s.io/kubernetes/pkg/client/unversioned"
2529
"k8s.io/kubernetes/pkg/util/intstr"
2630
)
@@ -76,6 +80,15 @@ type AppDeploymentSpec struct {
7680
RunAsPrivileged bool `json:"runAsPrivileged"`
7781
}
7882

83+
// Specification for deployment from file
84+
type AppDeploymentFromFileSpec struct {
85+
// Name of the file
86+
Name string `json:"name"`
87+
88+
// File content
89+
Content string `json:"content"`
90+
}
91+
7992
// Port mapping for an application deployment.
8093
type PortMapping struct {
8194
// Port that will be exposed on the service.
@@ -231,3 +244,28 @@ func getLabelsMap(labels []Label) map[string]string {
231244

232245
return result
233246
}
247+
248+
// Deploys an app based on the given yaml or json file.
249+
func DeployAppFromFile(spec *AppDeploymentFromFileSpec, client *client.Client) error {
250+
// Creates temp file to use it in kubectl command
251+
file, err := ioutil.TempFile("", spec.Name)
252+
if err != nil {
253+
return err
254+
}
255+
_,err = file.WriteString(spec.Content)
256+
if err != nil {
257+
return err
258+
}
259+
// Runs kubectl create command with the temp file
260+
options := &create.CreateOptions{
261+
Filenames: []string {file.Name()},
262+
}
263+
cmd := create.NewCmdCreate(cmdutil.NewFactory(nil), os.Stdout)
264+
cmd.Flags().Set("filename", file.Name())
265+
cmd.Flags().Set("output", "name")
266+
err = create.RunCreate(cmdutil.NewFactory(nil), cmd, os.Stdout, options)
267+
268+
// Removes the created temp file after using it
269+
os.Remove(file.Name())
270+
return err
271+
}

src/app/externs/backendapi.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ backendApi.Label;
6060
*/
6161
backendApi.AppDeploymentSpec;
6262

63+
/**
64+
* @typedef {{
65+
* name: string,
66+
* content: string
67+
* }}
68+
*/
69+
backendApi.AppDeploymentFromFileSpec;
70+
6371
/**
6472
* @typedef {{
6573
* namespace: string,

src/app/frontend/common/errorhandling/errordialog_service.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,19 @@ export default class ErrorDialog {
4141
alert.ok('Close');
4242
this.mdDialog_.show(alert);
4343
}
44+
45+
/**
46+
* Opens a pop-up window that displays the error message with title
47+
*
48+
* @param {string} title
49+
* @param {string} message
50+
* @export
51+
*/
52+
openWithDetail(title, message) {
53+
let alert = this.mdDialog_.alert();
54+
alert.title(title);
55+
alert.textContent(message);
56+
alert.ok('Close');
57+
this.mdDialog_.show(alert);
58+
}
4459
}

src/app/frontend/deploy/deployfromfile_controller.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import {stateName as replicasetliststate} from 'replicasetlist/replicasetlist_state';
16+
1517
/**
1618
* Controller for the deploy from file directive.
1719
*
1820
* @final
1921
*/
2022
export default class DeployFromFileController {
21-
/** @ngInject */
22-
constructor() {
23+
/**
24+
* @param {!angular.$log} $log
25+
* @param {!ui.router.$state} $state
26+
* @param {!angular.$resource} $resource
27+
* @param {!angular.$q} $q
28+
* @ngInject */
29+
constructor($log, $state, $resource, $q, errorDialog) {
2330
/**
2431
* It initializes the scope output parameter
2532
*
@@ -33,5 +40,55 @@ export default class DeployFromFileController {
3340
* @export {{name:string, content:string}}
3441
*/
3542
this.file = {name: '', content: ''};
43+
44+
/** @private {!angular.$q} */
45+
this.q_ = $q;
46+
47+
/** @private {!angular.$resource} */
48+
this.resource_ = $resource;
49+
50+
/** @private {!angular.$log} */
51+
this.log_ = $log;
52+
53+
/** @private {!ui.router.$state} */
54+
this.state_ = $state;
55+
56+
/**
57+
* TODO (cheld) Set correct type after fixing issue #159
58+
* @private {!Object}
59+
*/
60+
this.errorDialog_ = errorDialog;
61+
}
62+
63+
/**
64+
* Deploys the application based on the state of the controller.
65+
*
66+
* @export
67+
* @return {angular.$q.Promise}
68+
*/
69+
deploy() {
70+
/** @type {!backendApi.AppDeploymentFromFileSpec} */
71+
let deploymentSpec = {
72+
name: this.file.name,
73+
content: this.file.content,
74+
};
75+
76+
let defer = this.q_.defer();
77+
78+
/** @type {!angular.Resource<!backendApi.AppDeploymentFromFileSpec>} */
79+
let resource = this.resource_('/api/appdeploymentfromfile');
80+
resource.save(
81+
deploymentSpec,
82+
(savedConfig) => {
83+
defer.resolve(savedConfig); // Progress ends
84+
this.log_.info('Successfully deployed application: ', savedConfig);
85+
this.state_.go(replicasetliststate);
86+
},
87+
(err) => {
88+
defer.reject(err); // Progress ends
89+
this.log_.error('Error deploying application:', err);
90+
this.errorDialog_.openWithDetail('File upload failed' ,err.data);
91+
});
92+
return defer.promise;
3693
}
3794
}

0 commit comments

Comments
 (0)