Fix nil pointer when CLI unable to get server version (#1223)

This commit is contained in:
Ta-Ching Chen
2019-07-07 09:55:00 +08:00
committed by GitHub
parent 9d13081803
commit 9e7dd5aa27
2 changed files with 17 additions and 6 deletions
+9 -1
View File
@@ -18,12 +18,16 @@ package client
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"time"
"golang.org/x/net/context/ctxhttp"
ferror "github.com/fission/fission/pkg/error" ferror "github.com/fission/fission/pkg/error"
"github.com/fission/fission/pkg/info" "github.com/fission/fission/pkg/info"
@@ -94,7 +98,11 @@ func (c *Client) handleCreateResponse(resp *http.Response) ([]byte, error) {
func (c *Client) ServerInfo() (*info.ServerInfo, error) { func (c *Client) ServerInfo() (*info.ServerInfo, error) {
url := fmt.Sprintf(c.Url) url := fmt.Sprintf(c.Url)
resp, err := http.Get(url)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
resp, err := ctxhttp.Get(ctx, &http.Client{}, url)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+8 -5
View File
@@ -18,27 +18,30 @@ type Versions struct {
} }
func GetVersion(client *client.Client) []byte { func GetVersion(client *client.Client) []byte {
serverInfo, err := client.ServerInfo()
if err != nil {
log.Warn(fmt.Sprintf("Error getting Fission API version: %v", err))
}
// Fetch client versions // Fetch client versions
versions := Versions{ versions := Versions{
Client: map[string]info.BuildMeta{ Client: map[string]info.BuildMeta{
"fission/core": info.BuildInfo(), "fission/core": info.BuildInfo(),
}, },
} }
for _, pmd := range plugin.FindAll() { for _, pmd := range plugin.FindAll() {
versions.Client[pmd.Name] = info.BuildMeta{ versions.Client[pmd.Name] = info.BuildMeta{
Version: pmd.Version, Version: pmd.Version,
} }
} }
serverInfo, err := client.ServerInfo()
if err != nil {
log.Warn(fmt.Sprintf("Error getting Fission API version: %v", err))
serverInfo = &info.ServerInfo{}
}
// Fetch server versions // Fetch server versions
versions.Server = map[string]info.BuildMeta{ versions.Server = map[string]info.BuildMeta{
"fission/core": serverInfo.Build, "fission/core": serverInfo.Build,
} }
// FUTURE: fetch versions of plugins server-side // FUTURE: fetch versions of plugins server-side
bs, err := yaml.Marshal(versions) bs, err := yaml.Marshal(versions)
if err != nil { if err != nil {