mirror of https://github.com/helm/helm
Merge pull request #457 from sparkprime/expansion_service
Factor expansion service logic as an auxiliary librarypull/467/head
commit
e7119a9296
@ -1,90 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2015 The Kubernetes Authors 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 service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/kubernetes/helm/pkg/expansion"
|
|
||||||
"github.com/kubernetes/helm/pkg/util"
|
|
||||||
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
restful "github.com/emicklei/go-restful"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A Service wraps a web service that performs template expansion.
|
|
||||||
type Service struct {
|
|
||||||
*restful.WebService
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewService creates and returns a new Service, initialized with a new
|
|
||||||
// restful.WebService configured with a route that dispatches to the supplied
|
|
||||||
// handler. The new Service must be registered before accepting traffic by
|
|
||||||
// calling Register.
|
|
||||||
func NewService(handler restful.RouteFunction) *Service {
|
|
||||||
restful.EnableTracing(true)
|
|
||||||
webService := new(restful.WebService)
|
|
||||||
webService.Consumes(restful.MIME_JSON, restful.MIME_XML)
|
|
||||||
webService.Produces(restful.MIME_JSON, restful.MIME_XML)
|
|
||||||
webService.Route(webService.POST("/expand").To(handler).
|
|
||||||
Doc("Expand a template.").
|
|
||||||
Reads(&expansion.ServiceRequest{}).
|
|
||||||
Writes(&expansion.ServiceResponse{}))
|
|
||||||
return &Service{webService}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register adds the web service wrapped by the Service to the supplied
|
|
||||||
// container. If the supplied container is nil, then the default container is
|
|
||||||
// used, instead.
|
|
||||||
func (s *Service) Register(container *restful.Container) {
|
|
||||||
if container == nil {
|
|
||||||
container = restful.DefaultContainer
|
|
||||||
}
|
|
||||||
|
|
||||||
container.Add(s.WebService)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewExpansionHandler returns a route function that handles an incoming
|
|
||||||
// template expansion request, bound to the supplied expander.
|
|
||||||
func NewExpansionHandler(backend expansion.Expander) restful.RouteFunction {
|
|
||||||
return func(req *restful.Request, resp *restful.Response) {
|
|
||||||
util.LogHandlerEntry("expandybird: expand", req.Request)
|
|
||||||
request := &expansion.ServiceRequest{}
|
|
||||||
if err := req.ReadEntity(&request); err != nil {
|
|
||||||
logAndReturnErrorFromHandler(http.StatusBadRequest, err.Error(), resp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := backend.ExpandChart(request)
|
|
||||||
if err != nil {
|
|
||||||
message := fmt.Sprintf("error expanding chart: %s", err)
|
|
||||||
logAndReturnErrorFromHandler(http.StatusBadRequest, message, resp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
util.LogHandlerExit("expandybird", http.StatusOK, "OK", resp.ResponseWriter)
|
|
||||||
message := fmt.Sprintf("\nResources:\n%s\n", response.Resources)
|
|
||||||
util.LogHandlerText("expandybird", message)
|
|
||||||
resp.WriteEntity(response)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func logAndReturnErrorFromHandler(statusCode int, message string, resp *restful.Response) {
|
|
||||||
util.LogHandlerExit("expandybird: expand", statusCode, message, resp.ResponseWriter)
|
|
||||||
resp.WriteError(statusCode, errors.New(message))
|
|
||||||
}
|
|
@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2015 The Kubernetes Authors 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 expansion
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kubernetes/helm/pkg/util"
|
||||||
|
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
restful "github.com/emicklei/go-restful"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Service wraps a web service that performs template expansion.
|
||||||
|
type Service struct {
|
||||||
|
webService *restful.WebService
|
||||||
|
server *http.Server
|
||||||
|
container *restful.Container
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewService encapsulates code to open an HTTP server on the given address:port that serves the
|
||||||
|
// expansion API using the given Expander backend to do the actual expansion. After calling
|
||||||
|
// NewService, call ListenAndServe to start the returned service.
|
||||||
|
func NewService(address string, port int, backend Expander) *Service {
|
||||||
|
|
||||||
|
restful.EnableTracing(true)
|
||||||
|
webService := new(restful.WebService)
|
||||||
|
webService.Consumes(restful.MIME_JSON)
|
||||||
|
webService.Produces(restful.MIME_JSON)
|
||||||
|
handler := func(req *restful.Request, resp *restful.Response) {
|
||||||
|
util.LogHandlerEntry("expansion service", req.Request)
|
||||||
|
request := &ServiceRequest{}
|
||||||
|
if err := req.ReadEntity(&request); err != nil {
|
||||||
|
badRequest(resp, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response, err := backend.ExpandChart(request)
|
||||||
|
if err != nil {
|
||||||
|
badRequest(resp, fmt.Sprintf("error expanding chart: %s", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
util.LogHandlerExit("expansion service", http.StatusOK, "OK", resp.ResponseWriter)
|
||||||
|
message := fmt.Sprintf("\nResources:\n%s\n", response.Resources)
|
||||||
|
util.LogHandlerText("expansion service", message)
|
||||||
|
resp.WriteEntity(response)
|
||||||
|
}
|
||||||
|
webService.Route(
|
||||||
|
webService.POST("/expand").
|
||||||
|
To(handler).
|
||||||
|
Doc("Expand a chart.").
|
||||||
|
Reads(&ServiceRequest{}).
|
||||||
|
Writes(&ServiceResponse{}))
|
||||||
|
|
||||||
|
container := restful.DefaultContainer
|
||||||
|
container.Add(webService)
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: fmt.Sprintf("%s:%d", address, port),
|
||||||
|
Handler: container,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Service{
|
||||||
|
webService: webService,
|
||||||
|
server: server,
|
||||||
|
container: container,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenAndServe blocks forever, handling expansion requests.
|
||||||
|
func (s *Service) ListenAndServe() error {
|
||||||
|
return s.server.ListenAndServe()
|
||||||
|
}
|
||||||
|
|
||||||
|
func badRequest(resp *restful.Response, message string) {
|
||||||
|
statusCode := http.StatusBadRequest
|
||||||
|
util.LogHandlerExit("expansion service", statusCode, message, resp.ResponseWriter)
|
||||||
|
resp.WriteError(statusCode, errors.New(message))
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2015 The Kubernetes Authors 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 expansion
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ValidateRequest does basic sanity checks on the request.
|
||||||
|
func ValidateRequest(request *ServiceRequest) error {
|
||||||
|
if request.ChartInvocation == nil {
|
||||||
|
return fmt.Errorf("Request does not have invocation field")
|
||||||
|
}
|
||||||
|
if request.Chart == nil {
|
||||||
|
return fmt.Errorf("Request does not have chart field")
|
||||||
|
}
|
||||||
|
|
||||||
|
chartInv := request.ChartInvocation
|
||||||
|
chartFile := request.Chart.Chartfile
|
||||||
|
|
||||||
|
if chartInv.Type != chartFile.Name {
|
||||||
|
return fmt.Errorf("Request chart invocation does not match provided chart")
|
||||||
|
}
|
||||||
|
|
||||||
|
if chartFile.Expander == nil {
|
||||||
|
message := fmt.Sprintf("Chart JSON does not have expander field")
|
||||||
|
return fmt.Errorf("%s: %s", chartInv.Name, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in new issue