diff --git a/Makefile b/Makefile index 20507c3ce..bea28dc8b 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ -VERSION ?= dev +GIT_VERSION = $(shell git describe --tags --dirty) +VERSION ?= $(GIT_VERSION) GO ?= go GOVERSIONS ?= go1.9 go1.10 OS := $(shell uname) SHELL := /bin/bash -GIT_VERSION = $(shell git describe --tags) .DEFAULT_GOAL := help @@ -22,7 +22,6 @@ GO_LDFLAGS += -X $(VERSION_PACKAGE).commit=$(shell git rev-parse HEAD) GO_LDFLAGS +=" PKGS := pkg -GIT_VERSION = $(shell git describe --tags) # ?= cannot be used for these variables as they should be evaulated only once per Makefile @@ -30,9 +29,7 @@ ifeq ($(PREFIX),) PREFIX := $(shell pwd) endif -ifeq ($(BUILD),) -BUILD := $(shell date +%s) -endif +TAG := $(VERSION) @@ -122,13 +119,13 @@ images: linux ci-images .PHONY: ci-values ci-values: - scripts/values.sh $(BUILD) + TAG=$(TAG) scripts/values.sh .PHONY: ci-images $(SERVICES) ci-images: ci-values $(SERVICES) $(SERVICES): - scripts/images.sh $@ $(BUILD) + TAG=$(TAG) scripts/images.sh $@ .PHONY: generate generate: ## run go generate diff --git a/charts/dispatch/charts/identity-manager/values.yaml b/charts/dispatch/charts/identity-manager/values.yaml index e45ebec09..c93c5036c 100644 --- a/charts/dispatch/charts/identity-manager/values.yaml +++ b/charts/dispatch/charts/identity-manager/values.yaml @@ -38,6 +38,7 @@ ingress: # host: dispatch.vmware.com paths: - /v1/iam + - /v1/version tls: {} # Secrets must be manually created in the namespace. # secretName: dispatch-tls diff --git a/pkg/api/v1/version.go b/pkg/api/v1/version.go new file mode 100644 index 000000000..62c538178 --- /dev/null +++ b/pkg/api/v1/version.go @@ -0,0 +1,19 @@ +/////////////////////////////////////////////////////////////////////// +// Copyright (c) 2018 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/////////////////////////////////////////////////////////////////////// + +package v1 + +// NO TESTS + +// Version describes version/build metadata +// swagger:model Version +type Version struct { + Version string `json:"version"` + Commit string `json:"commit"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` + Compiler string `json:"compiler"` + Platform string `json:"platform"` +} diff --git a/pkg/client/version.go b/pkg/client/version.go new file mode 100644 index 000000000..c80cc29b2 --- /dev/null +++ b/pkg/client/version.go @@ -0,0 +1,51 @@ +/////////////////////////////////////////////////////////////////////// +// Copyright (c) 2018 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/////////////////////////////////////////////////////////////////////// + +package client + +import ( + "context" + + "github.com/go-openapi/runtime" + "github.com/go-openapi/strfmt" + "github.com/pkg/errors" + "github.com/vmware/dispatch/pkg/identity-manager/gen/client/operations" + + "github.com/vmware/dispatch/pkg/api/v1" + swaggerclient "github.com/vmware/dispatch/pkg/identity-manager/gen/client" +) + +// VersionClient gets the version info from the API +type VersionClient interface { + // GetVersion returns either *v1.Version or error + GetVersion(ctx context.Context) (*v1.Version, error) +} + +// DefaultVersionClient is the implementation of VersionClient +type DefaultVersionClient struct { + client *swaggerclient.IdentityManager + auth runtime.ClientAuthInfoWriter +} + +// NewVersionClient creates an instance of *DefaultVersionClient +func NewVersionClient(host string, auth runtime.ClientAuthInfoWriter) *DefaultVersionClient { + transport := DefaultHTTPClient(host, swaggerclient.DefaultBasePath) + return &DefaultVersionClient{ + client: swaggerclient.New(transport, strfmt.Default), + auth: auth, + } +} + +// GetVersion returns either *v1.Version or error +func (vc *DefaultVersionClient) GetVersion(ctx context.Context) (*v1.Version, error) { + params := &operations.GetVersionParams{ + Context: context.Background(), + } + resp, err := vc.client.Operations.GetVersion(params, vc.auth) + if err != nil { + return nil, errors.Wrapf(err, "error fetching version info") + } + return resp.Payload, nil +} diff --git a/pkg/dispatchcli/cmd/rest_clients.go b/pkg/dispatchcli/cmd/rest_clients.go index b869efce1..17058a5b4 100644 --- a/pkg/dispatchcli/cmd/rest_clients.go +++ b/pkg/dispatchcli/cmd/rest_clients.go @@ -91,3 +91,7 @@ func eventManagerClient() client.EventsClient { func identityManagerClient() *identitymanager.IdentityManager { return identitymanager.New(httpTransport(identitymanager.DefaultBasePath), strfmt.Default) } + +func versionClient() client.VersionClient { + return client.NewVersionClient(getDispatchHost(), GetAuthInfoWriter()) +} diff --git a/pkg/dispatchcli/cmd/version.go b/pkg/dispatchcli/cmd/version.go index 643604724..3b77e09d4 100644 --- a/pkg/dispatchcli/cmd/version.go +++ b/pkg/dispatchcli/cmd/version.go @@ -6,24 +6,53 @@ package cmd import ( + "context" + "encoding/json" "fmt" "io" "github.com/spf13/cobra" - + "github.com/vmware/dispatch/pkg/api/v1" "github.com/vmware/dispatch/pkg/dispatchcli/i18n" "github.com/vmware/dispatch/pkg/version" ) +type versions struct { + Client *v1.Version `json:"client,omitempty"` + Server *v1.Version `json:"server,omitempty"` +} + // NewCmdVersion creates a version command for CLI func NewCmdVersion(out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: i18n.T("Print Dispatch CLI version."), Run: func(cmd *cobra.Command, args []string) { - v := version.Get() - fmt.Fprintf(out, "%s-%s\n", v.Version, v.Commit[0:7]) + clientVersion := version.Get() + if dispatchConfig.JSON { + encoder := json.NewEncoder(out) + encoder.SetIndent("", " ") + + serverVersion, err := getServerVersion() + CheckErr(err) + + encoder.Encode(&versions{Client: clientVersion, Server: serverVersion}) + return + } + + fmt.Fprintf(out, "Client: %s\n", clientVersion.Version) + serverVersion, err := getServerVersion() + CheckErr(err) + fmt.Fprintf(out, "Server: %s\n", serverVersion.Version) }, } return cmd } + +func getServerVersion() (*v1.Version, error) { + v, err := versionClient().GetVersion(context.Background()) + if err != nil { + return nil, err + } + return v, nil +} diff --git a/pkg/identity-manager/gen/client/operations/get_version_parameters.go b/pkg/identity-manager/gen/client/operations/get_version_parameters.go new file mode 100644 index 000000000..3aa4b4659 --- /dev/null +++ b/pkg/identity-manager/gen/client/operations/get_version_parameters.go @@ -0,0 +1,119 @@ +/////////////////////////////////////////////////////////////////////// +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/////////////////////////////////////////////////////////////////////// + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + "time" + + "golang.org/x/net/context" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" + cr "github.com/go-openapi/runtime/client" + + strfmt "github.com/go-openapi/strfmt" +) + +// NewGetVersionParams creates a new GetVersionParams object +// with the default values initialized. +func NewGetVersionParams() *GetVersionParams { + + return &GetVersionParams{ + + timeout: cr.DefaultTimeout, + } +} + +// NewGetVersionParamsWithTimeout creates a new GetVersionParams object +// with the default values initialized, and the ability to set a timeout on a request +func NewGetVersionParamsWithTimeout(timeout time.Duration) *GetVersionParams { + + return &GetVersionParams{ + + timeout: timeout, + } +} + +// NewGetVersionParamsWithContext creates a new GetVersionParams object +// with the default values initialized, and the ability to set a context for a request +func NewGetVersionParamsWithContext(ctx context.Context) *GetVersionParams { + + return &GetVersionParams{ + + Context: ctx, + } +} + +// NewGetVersionParamsWithHTTPClient creates a new GetVersionParams object +// with the default values initialized, and the ability to set a custom HTTPClient for a request +func NewGetVersionParamsWithHTTPClient(client *http.Client) *GetVersionParams { + + return &GetVersionParams{ + HTTPClient: client, + } +} + +/*GetVersionParams contains all the parameters to send to the API endpoint +for the get version operation typically these are written to a http.Request +*/ +type GetVersionParams struct { + timeout time.Duration + Context context.Context + HTTPClient *http.Client +} + +// WithTimeout adds the timeout to the get version params +func (o *GetVersionParams) WithTimeout(timeout time.Duration) *GetVersionParams { + o.SetTimeout(timeout) + return o +} + +// SetTimeout adds the timeout to the get version params +func (o *GetVersionParams) SetTimeout(timeout time.Duration) { + o.timeout = timeout +} + +// WithContext adds the context to the get version params +func (o *GetVersionParams) WithContext(ctx context.Context) *GetVersionParams { + o.SetContext(ctx) + return o +} + +// SetContext adds the context to the get version params +func (o *GetVersionParams) SetContext(ctx context.Context) { + o.Context = ctx +} + +// WithHTTPClient adds the HTTPClient to the get version params +func (o *GetVersionParams) WithHTTPClient(client *http.Client) *GetVersionParams { + o.SetHTTPClient(client) + return o +} + +// SetHTTPClient adds the HTTPClient to the get version params +func (o *GetVersionParams) SetHTTPClient(client *http.Client) { + o.HTTPClient = client +} + +// WriteToRequest writes these params to a swagger request +func (o *GetVersionParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { + + if err := r.SetTimeout(o.timeout); err != nil { + return err + } + var res []error + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/pkg/identity-manager/gen/client/operations/get_version_responses.go b/pkg/identity-manager/gen/client/operations/get_version_responses.go new file mode 100644 index 000000000..343a6735c --- /dev/null +++ b/pkg/identity-manager/gen/client/operations/get_version_responses.go @@ -0,0 +1,117 @@ +/////////////////////////////////////////////////////////////////////// +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/////////////////////////////////////////////////////////////////////// + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "fmt" + "io" + + "github.com/go-openapi/runtime" + + strfmt "github.com/go-openapi/strfmt" + + "github.com/vmware/dispatch/pkg/api/v1" +) + +// GetVersionReader is a Reader for the GetVersion structure. +type GetVersionReader struct { + formats strfmt.Registry +} + +// ReadResponse reads a server response into the received o. +func (o *GetVersionReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) { + switch response.Code() { + + case 200: + result := NewGetVersionOK() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return result, nil + + default: + result := NewGetVersionDefault(response.Code()) + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + if response.Code()/100 == 2 { + return result, nil + } + return nil, result + } +} + +// NewGetVersionOK creates a GetVersionOK with default headers values +func NewGetVersionOK() *GetVersionOK { + return &GetVersionOK{} +} + +/*GetVersionOK handles this case with default header values. + +version info +*/ +type GetVersionOK struct { + Payload *v1.Version +} + +func (o *GetVersionOK) Error() string { + return fmt.Sprintf("[GET /v1/version][%d] getVersionOK %+v", 200, o.Payload) +} + +func (o *GetVersionOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(v1.Version) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} + +// NewGetVersionDefault creates a GetVersionDefault with default headers values +func NewGetVersionDefault(code int) *GetVersionDefault { + return &GetVersionDefault{ + _statusCode: code, + } +} + +/*GetVersionDefault handles this case with default header values. + +error +*/ +type GetVersionDefault struct { + _statusCode int + + Payload *v1.Error +} + +// Code gets the status code for the get version default response +func (o *GetVersionDefault) Code() int { + return o._statusCode +} + +func (o *GetVersionDefault) Error() string { + return fmt.Sprintf("[GET /v1/version][%d] getVersion default %+v", o._statusCode, o.Payload) +} + +func (o *GetVersionDefault) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(v1.Error) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} diff --git a/pkg/identity-manager/gen/client/operations/operations_client.go b/pkg/identity-manager/gen/client/operations/operations_client.go index b6dcb7c22..cbf70db76 100644 --- a/pkg/identity-manager/gen/client/operations/operations_client.go +++ b/pkg/identity-manager/gen/client/operations/operations_client.go @@ -58,6 +58,35 @@ func (a *Client) Auth(params *AuthParams, authInfo runtime.ClientAuthInfoWriter) } +/* +GetVersion gets version info +*/ +func (a *Client) GetVersion(params *GetVersionParams, authInfo runtime.ClientAuthInfoWriter) (*GetVersionOK, error) { + // TODO: Validate the params before sending + if params == nil { + params = NewGetVersionParams() + } + + result, err := a.transport.Submit(&runtime.ClientOperation{ + ID: "getVersion", + Method: "GET", + PathPattern: "/v1/version", + ProducesMediaTypes: []string{"application/json"}, + ConsumesMediaTypes: []string{"application/json"}, + Schemes: []string{"http", "https"}, + Params: params, + Reader: &GetVersionReader{formats: a.formats}, + AuthInfo: authInfo, + Context: params.Context, + Client: params.HTTPClient, + }) + if err != nil { + return nil, err + } + return result.(*GetVersionOK), nil + +} + /* Home as placeholder home page */ diff --git a/pkg/identity-manager/gen/restapi/configure_identity_manager.go b/pkg/identity-manager/gen/restapi/configure_identity_manager.go index 039398a5e..32cdb2b46 100644 --- a/pkg/identity-manager/gen/restapi/configure_identity_manager.go +++ b/pkg/identity-manager/gen/restapi/configure_identity_manager.go @@ -97,6 +97,9 @@ func configureAPI(api *operations.IdentityManagerAPI) http.Handler { api.ServiceaccountGetServiceAccountsHandler = serviceaccount.GetServiceAccountsHandlerFunc(func(params serviceaccount.GetServiceAccountsParams, principal interface{}) middleware.Responder { return middleware.NotImplemented("operation serviceaccount.GetServiceAccounts has not yet been implemented") }) + api.GetVersionHandler = operations.GetVersionHandlerFunc(func(params operations.GetVersionParams, principal interface{}) middleware.Responder { + return middleware.NotImplemented("operation .GetVersion has not yet been implemented") + }) api.HomeHandler = operations.HomeHandlerFunc(func(params operations.HomeParams, principal interface{}) middleware.Responder { return middleware.NotImplemented("operation .Home has not yet been implemented") }) diff --git a/pkg/identity-manager/gen/restapi/embedded_spec.go b/pkg/identity-manager/gen/restapi/embedded_spec.go index 1ca6b4a47..67dd056ab 100644 --- a/pkg/identity-manager/gen/restapi/embedded_spec.go +++ b/pkg/identity-manager/gen/restapi/embedded_spec.go @@ -824,6 +824,26 @@ func init() { "required": true } ] + }, + "/v1/version": { + "get": { + "summary": "get version info", + "operationId": "getVersion", + "responses": { + "200": { + "description": "version info", + "schema": { + "$ref": "./models.json#/definitions/Version" + } + }, + "default": { + "description": "error", + "schema": { + "$ref": "./models.json#/definitions/Error" + } + } + } + } } }, "securityDefinitions": { @@ -1655,6 +1675,26 @@ func init() { "required": true } ] + }, + "/v1/version": { + "get": { + "summary": "get version info", + "operationId": "getVersion", + "responses": { + "200": { + "description": "version info", + "schema": { + "$ref": "#/definitions/version" + } + }, + "default": { + "description": "error", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } } }, "definitions": { @@ -1895,6 +1935,37 @@ func init() { } }, "x-go-package": "github.com/vmware/dispatch/pkg/api/v1" + }, + "version": { + "description": "Version describes version/build metadata", + "type": "object", + "properties": { + "buildDate": { + "type": "string", + "x-go-name": "BuildDate" + }, + "commit": { + "type": "string", + "x-go-name": "Commit" + }, + "compiler": { + "type": "string", + "x-go-name": "Compiler" + }, + "goVersion": { + "type": "string", + "x-go-name": "GoVersion" + }, + "platform": { + "type": "string", + "x-go-name": "Platform" + }, + "version": { + "type": "string", + "x-go-name": "Version" + } + }, + "x-go-package": "github.com/vmware/dispatch/pkg/api/v1" } }, "securityDefinitions": { diff --git a/pkg/identity-manager/gen/restapi/operations/get_version.go b/pkg/identity-manager/gen/restapi/operations/get_version.go new file mode 100644 index 000000000..dc4e94e73 --- /dev/null +++ b/pkg/identity-manager/gen/restapi/operations/get_version.go @@ -0,0 +1,76 @@ +/////////////////////////////////////////////////////////////////////// +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/////////////////////////////////////////////////////////////////////// + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "net/http" + + middleware "github.com/go-openapi/runtime/middleware" +) + +// GetVersionHandlerFunc turns a function with the right signature into a get version handler +type GetVersionHandlerFunc func(GetVersionParams, interface{}) middleware.Responder + +// Handle executing the request and returning a response +func (fn GetVersionHandlerFunc) Handle(params GetVersionParams, principal interface{}) middleware.Responder { + return fn(params, principal) +} + +// GetVersionHandler interface for that can handle valid get version params +type GetVersionHandler interface { + Handle(GetVersionParams, interface{}) middleware.Responder +} + +// NewGetVersion creates a new http.Handler for the get version operation +func NewGetVersion(ctx *middleware.Context, handler GetVersionHandler) *GetVersion { + return &GetVersion{Context: ctx, Handler: handler} +} + +/*GetVersion swagger:route GET /v1/version getVersion + +get version info + +*/ +type GetVersion struct { + Context *middleware.Context + Handler GetVersionHandler +} + +func (o *GetVersion) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + route, rCtx, _ := o.Context.RouteInfo(r) + if rCtx != nil { + r = rCtx + } + var Params = NewGetVersionParams() + + uprinc, aCtx, err := o.Context.Authorize(r, route) + if err != nil { + o.Context.Respond(rw, r, route.Produces, route, err) + return + } + if aCtx != nil { + r = aCtx + } + var principal interface{} + if uprinc != nil { + principal = uprinc + } + + if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params + o.Context.Respond(rw, r, route.Produces, route, err) + return + } + + res := o.Handler.Handle(Params, principal) // actually handle the request + + o.Context.Respond(rw, r, route.Produces, route, res) + +} diff --git a/pkg/identity-manager/gen/restapi/operations/get_version_parameters.go b/pkg/identity-manager/gen/restapi/operations/get_version_parameters.go new file mode 100644 index 000000000..5140fc601 --- /dev/null +++ b/pkg/identity-manager/gen/restapi/operations/get_version_parameters.go @@ -0,0 +1,50 @@ +/////////////////////////////////////////////////////////////////////// +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/////////////////////////////////////////////////////////////////////// + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime/middleware" +) + +// NewGetVersionParams creates a new GetVersionParams object +// no default values defined in spec. +func NewGetVersionParams() GetVersionParams { + + return GetVersionParams{} +} + +// GetVersionParams contains all the bound params for the get version operation +// typically these are obtained from a http.Request +// +// swagger:parameters getVersion +type GetVersionParams struct { + + // HTTP Request Object + HTTPRequest *http.Request `json:"-"` +} + +// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface +// for simple values it will use straight method calls. +// +// To ensure default values, the struct must have been initialized with NewGetVersionParams() beforehand. +func (o *GetVersionParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error { + var res []error + + o.HTTPRequest = r + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/pkg/identity-manager/gen/restapi/operations/get_version_responses.go b/pkg/identity-manager/gen/restapi/operations/get_version_responses.go new file mode 100644 index 000000000..0c6d0c673 --- /dev/null +++ b/pkg/identity-manager/gen/restapi/operations/get_version_responses.go @@ -0,0 +1,121 @@ +/////////////////////////////////////////////////////////////////////// +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/////////////////////////////////////////////////////////////////////// + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/runtime" + + "github.com/vmware/dispatch/pkg/api/v1" +) + +// GetVersionOKCode is the HTTP code returned for type GetVersionOK +const GetVersionOKCode int = 200 + +/*GetVersionOK version info + +swagger:response getVersionOK +*/ +type GetVersionOK struct { + + /* + In: Body + */ + Payload *v1.Version `json:"body,omitempty"` +} + +// NewGetVersionOK creates GetVersionOK with default headers values +func NewGetVersionOK() *GetVersionOK { + + return &GetVersionOK{} +} + +// WithPayload adds the payload to the get version o k response +func (o *GetVersionOK) WithPayload(payload *v1.Version) *GetVersionOK { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the get version o k response +func (o *GetVersionOK) SetPayload(payload *v1.Version) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *GetVersionOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(200) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} + +/*GetVersionDefault error + +swagger:response getVersionDefault +*/ +type GetVersionDefault struct { + _statusCode int + + /* + In: Body + */ + Payload *v1.Error `json:"body,omitempty"` +} + +// NewGetVersionDefault creates GetVersionDefault with default headers values +func NewGetVersionDefault(code int) *GetVersionDefault { + if code <= 0 { + code = 500 + } + + return &GetVersionDefault{ + _statusCode: code, + } +} + +// WithStatusCode adds the status to the get version default response +func (o *GetVersionDefault) WithStatusCode(code int) *GetVersionDefault { + o._statusCode = code + return o +} + +// SetStatusCode sets the status to the get version default response +func (o *GetVersionDefault) SetStatusCode(code int) { + o._statusCode = code +} + +// WithPayload adds the payload to the get version default response +func (o *GetVersionDefault) WithPayload(payload *v1.Error) *GetVersionDefault { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the get version default response +func (o *GetVersionDefault) SetPayload(payload *v1.Error) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *GetVersionDefault) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(o._statusCode) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} diff --git a/pkg/identity-manager/gen/restapi/operations/get_version_urlbuilder.go b/pkg/identity-manager/gen/restapi/operations/get_version_urlbuilder.go new file mode 100644 index 000000000..9f805304d --- /dev/null +++ b/pkg/identity-manager/gen/restapi/operations/get_version_urlbuilder.go @@ -0,0 +1,92 @@ +/////////////////////////////////////////////////////////////////////// +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/////////////////////////////////////////////////////////////////////// + +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "errors" + "net/url" + golangswaggerpaths "path" +) + +// GetVersionURL generates an URL for the get version operation +type GetVersionURL struct { + _basePath string +} + +// WithBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *GetVersionURL) WithBasePath(bp string) *GetVersionURL { + o.SetBasePath(bp) + return o +} + +// SetBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *GetVersionURL) SetBasePath(bp string) { + o._basePath = bp +} + +// Build a url path and query string +func (o *GetVersionURL) Build() (*url.URL, error) { + var result url.URL + + var _path = "/v1/version" + + _basePath := o._basePath + if _basePath == "" { + _basePath = "/" + } + result.Path = golangswaggerpaths.Join(_basePath, _path) + + return &result, nil +} + +// Must is a helper function to panic when the url builder returns an error +func (o *GetVersionURL) Must(u *url.URL, err error) *url.URL { + if err != nil { + panic(err) + } + if u == nil { + panic("url can't be nil") + } + return u +} + +// String returns the string representation of the path with query string +func (o *GetVersionURL) String() string { + return o.Must(o.Build()).String() +} + +// BuildFull builds a full url with scheme, host, path and query string +func (o *GetVersionURL) BuildFull(scheme, host string) (*url.URL, error) { + if scheme == "" { + return nil, errors.New("scheme is required for a full url on GetVersionURL") + } + if host == "" { + return nil, errors.New("host is required for a full url on GetVersionURL") + } + + base, err := o.Build() + if err != nil { + return nil, err + } + + base.Scheme = scheme + base.Host = host + return base, nil +} + +// StringFull returns the string representation of a complete url +func (o *GetVersionURL) StringFull(scheme, host string) string { + return o.Must(o.BuildFull(scheme, host)).String() +} diff --git a/pkg/identity-manager/gen/restapi/operations/identity_manager_api.go b/pkg/identity-manager/gen/restapi/operations/identity_manager_api.go index 4a93c51e3..aea7e9ea8 100644 --- a/pkg/identity-manager/gen/restapi/operations/identity_manager_api.go +++ b/pkg/identity-manager/gen/restapi/operations/identity_manager_api.go @@ -85,6 +85,9 @@ func NewIdentityManagerAPI(spec *loads.Document) *IdentityManagerAPI { ServiceaccountGetServiceAccountsHandler: serviceaccount.GetServiceAccountsHandlerFunc(func(params serviceaccount.GetServiceAccountsParams, principal interface{}) middleware.Responder { return middleware.NotImplemented("operation ServiceaccountGetServiceAccounts has not yet been implemented") }), + GetVersionHandler: GetVersionHandlerFunc(func(params GetVersionParams, principal interface{}) middleware.Responder { + return middleware.NotImplemented("operation GetVersion has not yet been implemented") + }), HomeHandler: HomeHandlerFunc(func(params HomeParams, principal interface{}) middleware.Responder { return middleware.NotImplemented("operation Home has not yet been implemented") }), @@ -184,6 +187,8 @@ type IdentityManagerAPI struct { ServiceaccountGetServiceAccountHandler serviceaccount.GetServiceAccountHandler // ServiceaccountGetServiceAccountsHandler sets the operation handler for the get service accounts operation ServiceaccountGetServiceAccountsHandler serviceaccount.GetServiceAccountsHandler + // GetVersionHandler sets the operation handler for the get version operation + GetVersionHandler GetVersionHandler // HomeHandler sets the operation handler for the home operation HomeHandler HomeHandler // RedirectHandler sets the operation handler for the redirect operation @@ -319,6 +324,10 @@ func (o *IdentityManagerAPI) Validate() error { unregistered = append(unregistered, "serviceaccount.GetServiceAccountsHandler") } + if o.GetVersionHandler == nil { + unregistered = append(unregistered, "GetVersionHandler") + } + if o.HomeHandler == nil { unregistered = append(unregistered, "HomeHandler") } @@ -520,6 +529,11 @@ func (o *IdentityManagerAPI) initHandlerCache() { } o.handlers["GET"]["/v1/iam/serviceaccount"] = serviceaccount.NewGetServiceAccounts(o.context, o.ServiceaccountGetServiceAccountsHandler) + if o.handlers["GET"] == nil { + o.handlers["GET"] = make(map[string]http.Handler) + } + o.handlers["GET"]["/v1/version"] = NewGetVersion(o.context, o.GetVersionHandler) + if o.handlers["GET"] == nil { o.handlers["GET"] = make(map[string]http.Handler) } diff --git a/pkg/identity-manager/handlers.go b/pkg/identity-manager/handlers.go index c8f184e34..a22b6fdd5 100644 --- a/pkg/identity-manager/handlers.go +++ b/pkg/identity-manager/handlers.go @@ -22,6 +22,7 @@ import ( "github.com/go-openapi/swag" "github.com/pkg/errors" log "github.com/sirupsen/logrus" + "github.com/vmware/dispatch/pkg/version" "github.com/vmware/dispatch/pkg/api/v1" "github.com/vmware/dispatch/pkg/controller" @@ -260,6 +261,7 @@ func (h *Handlers) ConfigureHandlers(api middleware.RoutableAPI) { a.HomeHandler = operations.HomeHandlerFunc(h.home) a.AuthHandler = operations.AuthHandlerFunc(h.auth) a.RedirectHandler = operations.RedirectHandlerFunc(h.redirect) + a.GetVersionHandler = operations.GetVersionHandlerFunc(h.getVersion) // Policy API Handlers a.PolicyAddPolicyHandler = policyOperations.AddPolicyHandlerFunc(h.addPolicy) a.PolicyGetPoliciesHandler = policyOperations.GetPoliciesHandlerFunc(h.getPolicies) @@ -356,6 +358,10 @@ func (h *Handlers) redirect(params operations.RedirectParams, principal interfac return operations.NewRedirectFound().WithLocation(location) } +func (h *Handlers) getVersion(params operations.GetVersionParams, principal interface{}) middleware.Responder { + return operations.NewGetVersionOK().WithPayload(version.Get()) +} + func getRequestAttributes(request *http.Request, subject string) (*attributesRecord, error) { log.Debugf("Headers: %s; Subject %s\n", request.Header, subject) diff --git a/pkg/middleware/health.go b/pkg/middleware/health.go index f69a335d4..197512772 100644 --- a/pkg/middleware/health.go +++ b/pkg/middleware/health.go @@ -6,11 +6,15 @@ package middleware import ( + "encoding/json" "net/http" "path/filepath" "strings" "github.com/justinas/alice" + + "github.com/vmware/dispatch/pkg/api/v1" + "github.com/vmware/dispatch/pkg/version" ) // NO TESTS @@ -45,6 +49,10 @@ func NewHealthCheck(basePath string, checker HealthChecker, next http.Handler) * } } +type statusInfo struct { + Version *v1.Version `json:"version"` +} + // ServeHTTP is the middleware interface implementation func (h *HealthCheck) ServeHTTP(rw http.ResponseWriter, r *http.Request) { if !strings.HasPrefix(r.URL.Path, filepath.Join(h.basePath, "healthz")) { @@ -57,12 +65,18 @@ func (h *HealthCheck) ServeHTTP(rw http.ResponseWriter, r *http.Request) { err = h.checker() } + var bs []byte + if err == nil { + bs, err = json.Marshal(&statusInfo{Version: version.Get()}) + } + if err != nil { rw.WriteHeader(http.StatusInternalServerError) rw.Write([]byte(err.Error())) return } + rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(http.StatusOK) - rw.Write([]byte("OK")) + rw.Write(bs) } diff --git a/pkg/version/version.go b/pkg/version/version.go index 24d883afa..ccf273228 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -8,8 +8,11 @@ package version import ( "fmt" "runtime" + + "github.com/vmware/dispatch/pkg/api/v1" ) +// Filled by -ldflags passed to `go build` var ( version string commit string @@ -18,20 +21,9 @@ var ( // NO TESTS -// BuildInfo describes build metadata -type BuildInfo struct { - Version string - Commit string - BuildDate string - GoVersion string - Compiler string - Platform string -} - -// Get returns information about the build -func Get() *BuildInfo { - // Filled by -ldflags passed to `go build` - return &BuildInfo{ +// Get returns information about the version/build +func Get() *v1.Version { + return &v1.Version{ Version: version, Commit: commit, BuildDate: buildDate, diff --git a/scripts/images.sh b/scripts/images.sh index 7ad4380ef..e8ced1757 100755 --- a/scripts/images.sh +++ b/scripts/images.sh @@ -4,7 +4,6 @@ PACKAGE=${1} BUILD=${2} -TAG=dev-${BUILD} if [ -n "$CI" ]; then TAG=$IMAGE_TAG diff --git a/scripts/values.sh b/scripts/values.sh index fdf143639..fabc82d9e 100755 --- a/scripts/values.sh +++ b/scripts/values.sh @@ -5,5 +5,5 @@ cat << EOF > $VALUES_PATH global: image: - tag: dev-${1} + tag: ${TAG} EOF diff --git a/swagger/identity-manager.yaml b/swagger/identity-manager.yaml index ba8090cfe..63f53315e 100644 --- a/swagger/identity-manager.yaml +++ b/swagger/identity-manager.yaml @@ -31,6 +31,19 @@ paths: description: error schema: $ref: "./models.json#/definitions/Error" + /v1/version: + get: + summary: get version info + operationId: getVersion + responses: + 200: + description: version info + schema: + $ref: "./models.json#/definitions/Version" + default: + description: error + schema: + $ref: "./models.json#/definitions/Error" /v1/iam/home: get: summary: a placeholder home page diff --git a/swagger/models.json b/swagger/models.json index c842f5f48..f5d2fc20b 100644 --- a/swagger/models.json +++ b/swagger/models.json @@ -1649,6 +1649,37 @@ } }, "x-go-package": "github.com/vmware/dispatch/pkg/api/v1" + }, + "Version": { + "description": "Version describes version/build metadata", + "type": "object", + "properties": { + "buildDate": { + "type": "string", + "x-go-name": "BuildDate" + }, + "commit": { + "type": "string", + "x-go-name": "Commit" + }, + "compiler": { + "type": "string", + "x-go-name": "Compiler" + }, + "goVersion": { + "type": "string", + "x-go-name": "GoVersion" + }, + "platform": { + "type": "string", + "x-go-name": "Platform" + }, + "version": { + "type": "string", + "x-go-name": "Version" + } + }, + "x-go-package": "github.com/vmware/dispatch/pkg/api/v1" } } } \ No newline at end of file