Skip to content
Snippets Groups Projects
Unverified Commit 7015c4e6 authored by Jess's avatar Jess Committed by GitHub
Browse files

Merge pull request #334 from jessicayuen/api-version-from-swagger

Resolve api spec based on swagger version
parents cd2592ec e24e3b43
No related branches found
No related tags found
No related merge requests found
...@@ -16,9 +16,15 @@ ...@@ -16,9 +16,15 @@
package client package client
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http"
"net/url"
"os" "os"
"path"
"reflect" "reflect"
"time"
"github.com/ksonnet/ksonnet/metadata" "github.com/ksonnet/ksonnet/metadata"
str "github.com/ksonnet/ksonnet/strings" str "github.com/ksonnet/ksonnet/strings"
...@@ -61,6 +67,59 @@ func InitClient(env string) (dynamic.ClientPool, discovery.DiscoveryInterface, s ...@@ -61,6 +67,59 @@ func InitClient(env string) (dynamic.ClientPool, discovery.DiscoveryInterface, s
return clientConfig.RestClient(&env) return clientConfig.RestClient(&env)
} }
// GetAPISpec reads the kubernetes API version from this client's swagger.json.
// We anticipate the swagger.json to be located at <server>/swagger.json.
// If no swagger is found, or we are unable to authenticate to the server, we
// will default to version:v1.7.0.
func (c *Config) GetAPISpec(server string) string {
const (
defaultVersion = "version:v1.7.0"
)
type Info struct {
Version string `json:"version"`
}
type Spec struct {
Info Info `json:"info"`
}
u, err := url.Parse(server)
u.Path = path.Join(u.Path, "swagger.json")
url := u.String()
client := http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Debugf("Failed to create request at %s\n%s", url, err.Error())
return defaultVersion
}
res, err := client.Do(req)
if err != nil {
log.Debugf("Failed to open swagger at %s\n%s", url, err.Error())
return defaultVersion
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Debugf("Failed to read swagger at %s\n%s", url, err.Error())
return defaultVersion
}
spec := Spec{}
jsonErr := json.Unmarshal(body, &spec)
if jsonErr != nil {
log.Debugf("Failed to parse swagger at %s\n%s", url, err.Error())
return defaultVersion
}
return fmt.Sprintf("version:%s", spec.Info.Version)
}
// Namespace returns the namespace for the provided ClientConfig. // Namespace returns the namespace for the provided ClientConfig.
func (c *Config) Namespace() (string, error) { func (c *Config) Namespace() (string, error) {
ns, _, err := c.Config.Namespace() ns, _, err := c.Config.Namespace()
......
...@@ -140,6 +140,9 @@ var envAddCmd = &cobra.Command{ ...@@ -140,6 +140,9 @@ var envAddCmd = &cobra.Command{
if err != nil { if err != nil {
return err return err
} }
if specFlag == "" {
specFlag = envClientConfig.GetAPISpec(server)
}
c, err := kubecfg.NewEnvAddCmd(name, server, namespace, specFlag, manager) c, err := kubecfg.NewEnvAddCmd(name, server, namespace, specFlag, manager)
if err != nil { if err != nil {
......
...@@ -23,7 +23,6 @@ import ( ...@@ -23,7 +23,6 @@ import (
"github.com/ksonnet/ksonnet/client" "github.com/ksonnet/ksonnet/client"
"github.com/ksonnet/ksonnet/pkg/kubecfg" "github.com/ksonnet/ksonnet/pkg/kubecfg"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -39,7 +38,7 @@ var ( ...@@ -39,7 +38,7 @@ var (
func init() { func init() {
RootCmd.AddCommand(initCmd) RootCmd.AddCommand(initCmd)
// TODO: We need to make this default to checking the `kubeconfig` file. // TODO: We need to make this default to checking the `kubeconfig` file.
initCmd.PersistentFlags().String(flagAPISpec, "version:v1.7.0", initCmd.PersistentFlags().String(flagAPISpec, "",
"Manually specified Kubernetes API version. The corresponding OpenAPI spec is used to generate ksonnet's Kubernetes libraries") "Manually specified Kubernetes API version. The corresponding OpenAPI spec is used to generate ksonnet's Kubernetes libraries")
initClientConfig = client.NewDefaultClientConfig() initClientConfig = client.NewDefaultClientConfig()
...@@ -72,17 +71,19 @@ var initCmd = &cobra.Command{ ...@@ -72,17 +71,19 @@ var initCmd = &cobra.Command{
return err return err
} }
specFlag, err := flags.GetString(flagAPISpec) server, namespace, err := resolveEnvFlags(flags)
if err != nil { if err != nil {
return err return err
} }
server, namespace, err := resolveEnvFlags(flags) specFlag, err := flags.GetString(flagAPISpec)
if err != nil { if err != nil {
return err return err
} }
if specFlag == "" {
specFlag = initClientConfig.GetAPISpec(server)
}
log.Infof("Creating a new app '%s' at path '%s'", appName, appRoot)
c, err := kubecfg.NewInitCmd(appName, appRoot, &specFlag, &server, &namespace) c, err := kubecfg.NewInitCmd(appName, appRoot, &specFlag, &server, &namespace)
if err != nil { if err != nil {
return err return err
......
...@@ -81,7 +81,7 @@ ks init app-name --dir=custom-location ...@@ -81,7 +81,7 @@ ks init app-name --dir=custom-location
### Options ### Options
``` ```
--api-spec string Manually specified Kubernetes API version. The corresponding OpenAPI spec is used to generate ksonnet's Kubernetes libraries (default "version:v1.7.0") --api-spec string Manually specified Kubernetes API version. The corresponding OpenAPI spec is used to generate ksonnet's Kubernetes libraries
--as string Username to impersonate for the operation --as string Username to impersonate for the operation
--as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups. --as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
--certificate-authority string Path to a cert file for the certificate authority --certificate-authority string Path to a cert file for the certificate authority
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment