Add Fission version API to router for CLI consumption (#2612)
* add version API to router * return empty struct in case of error in getserverinfo
This commit is contained in:
@@ -18,8 +18,10 @@ package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -39,8 +41,6 @@ import (
|
||||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/v1"
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/controller/client/rest"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/console"
|
||||
@@ -149,20 +149,48 @@ func GetVersion(ctx context.Context, input cli.Input, cmdClient cmd.Client) info
|
||||
}
|
||||
|
||||
func GetServerInfo(input cli.Input, cmdClient cmd.Client) *info.ServerInfo {
|
||||
serverUrl, err := GetServerURL(input, cmdClient)
|
||||
if err != nil {
|
||||
return &info.ServerInfo{}
|
||||
}
|
||||
restClient := rest.NewRESTClient(serverUrl)
|
||||
client := client.MakeClientset(restClient)
|
||||
|
||||
serverInfo, err := client.V1().Misc().ServerInfo()
|
||||
var serverInfo info.ServerInfo
|
||||
serverURL, err := getRouterURL(input.Context(), cmdClient)
|
||||
if err != nil {
|
||||
console.Warn("could not connect to server")
|
||||
return &serverInfo
|
||||
}
|
||||
// make request
|
||||
resp, err := http.Get(serverURL.String() + "/_version")
|
||||
if err != nil {
|
||||
console.Warn("could not get data from server")
|
||||
return &serverInfo
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
msg := fmt.Sprintf("HTTP error %v", resp.StatusCode)
|
||||
console.Warn(msg)
|
||||
return &serverInfo
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&serverInfo)
|
||||
if err != nil {
|
||||
console.Warn(fmt.Sprintf("Error getting Fission API version: %v", err))
|
||||
serverInfo = &info.ServerInfo{}
|
||||
serverInfo = info.ServerInfo{}
|
||||
}
|
||||
|
||||
return serverInfo
|
||||
return &serverInfo
|
||||
}
|
||||
|
||||
func getRouterURL(ctx context.Context, cmdClient cmd.Client) (serverURL *url.URL, err error) {
|
||||
// Portforward to the fission router
|
||||
localRouterPort, err := SetupPortForward(ctx, cmdClient, GetFissionNamespace(), "application=fission-router")
|
||||
if err != nil {
|
||||
return serverURL, err
|
||||
}
|
||||
|
||||
serverURL, err = url.Parse("http://127.0.0.1:" + localRouterPort)
|
||||
if err != nil {
|
||||
return serverURL, err
|
||||
}
|
||||
return serverURL, err
|
||||
}
|
||||
|
||||
func GetServerURL(input cli.Input, client cmd.Client) (serverUrl string, err error) {
|
||||
|
||||
@@ -24,14 +24,17 @@ import (
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"go.uber.org/zap"
|
||||
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
k8sCache "k8s.io/client-go/tools/cache"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/v1"
|
||||
ferror "github.com/fission/fission/pkg/error"
|
||||
executorClient "github.com/fission/fission/pkg/executor/client"
|
||||
config "github.com/fission/fission/pkg/featureconfig"
|
||||
"github.com/fission/fission/pkg/generated/clientset/versioned"
|
||||
"github.com/fission/fission/pkg/info"
|
||||
"github.com/fission/fission/pkg/throttler"
|
||||
"github.com/fission/fission/pkg/utils"
|
||||
"github.com/fission/fission/pkg/utils/metrics"
|
||||
@@ -107,6 +110,21 @@ func routerHealthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func versionHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_, err := w.Write([]byte(info.ApiInfo().String()))
|
||||
if err != nil {
|
||||
se, ok := err.(*kerrors.StatusError)
|
||||
if ok {
|
||||
http.Error(w, se.Error(), int(se.ErrStatus.Code))
|
||||
return
|
||||
}
|
||||
|
||||
code, msg := ferror.GetHTTPError(err)
|
||||
http.Error(w, msg, code)
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *HTTPTriggerSet) getRouter(fnTimeoutMap map[types.UID]int) *mux.Router {
|
||||
|
||||
featureConfig, _ := config.GetFeatureConfig()
|
||||
@@ -267,6 +285,8 @@ func (ts *HTTPTriggerSet) getRouter(fnTimeoutMap map[types.UID]int) *mux.Router
|
||||
|
||||
// Healthz endpoint for the router.
|
||||
muxRouter.HandleFunc("/router-healthz", routerHealthHandler).Methods("GET")
|
||||
// version of application.
|
||||
muxRouter.HandleFunc("/_version", versionHandler).Methods("GET")
|
||||
|
||||
return muxRouter
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user