diff --git a/cmd/cluster_list.go b/cmd/cluster_list.go index 07fc71132..92b269b4c 100644 --- a/cmd/cluster_list.go +++ b/cmd/cluster_list.go @@ -5,6 +5,8 @@ import ( "net/url" "os" + "github.com/dustin/go-humanize" + flags "github.com/jessevdk/go-flags" "github.com/mitchellh/cli" v1auth "github.com/nerdalize/nerd/nerd/client/auth/v1" @@ -66,13 +68,13 @@ func (cmd *ClusterList) Execute(args []string) (err error) { } // Add role (admin, team member ...) // Add star for current cluster - hdr := []string{"CLUSTER", "CPU", "MEMORY", "PODS"} + hdr := []string{"CLUSTER", "VCPUS", "MEMORY", "PODS"} rows := [][]string{} for _, cluster := range clusters.Clusters { rows = append(rows, []string{ cluster.Name, - fmt.Sprintf("%s/%s", cluster.Usage.CPU, cluster.Capacity.CPU), - fmt.Sprintf("%s/%s", cluster.Usage.Memory, cluster.Capacity.Memory), + fmt.Sprintf("%.1f/%.1f", cluster.Usage.CPU, cluster.Capacity.CPU), + fmt.Sprintf("%s/%s", humanize.Bytes(uint64(cluster.Usage.Memory)), humanize.Bytes(uint64(cluster.Capacity.Memory))), fmt.Sprintf("%d/%d", cluster.Usage.Pods, cluster.Capacity.Pods), }) } diff --git a/cmd/login.go b/cmd/login.go index bccb053ff..d88d42a3d 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -120,7 +120,6 @@ func (cmd *Login) Execute(args []string) (err error) { if err != nil { return err } - err = setCluster(&c, cmd.globalOpts.KubeOpts.KubeConfig, cluster) if err != nil { ok = false @@ -244,6 +243,13 @@ func setCluster(c *populator.Client, kubeConfig string, cluster *v1authpayload.G if kubeConfig == "" { kubeConfig = filepath.Join(hdir, ".kube", "config") } + kubeConfig, err = homedir.Expand(kubeConfig) + if err != nil { + return errors.Wrap(err, "failed to expand home directory in kube config file path") + } + //Normalize all slashes to native platform slashes (e.g. / to \ on Windows) + kubeConfig = filepath.FromSlash(kubeConfig) + p, err = populator.New(c, "generic", kubeConfig, hdir, cluster) if err != nil { return err @@ -253,7 +259,9 @@ func setCluster(c *populator.Client, kubeConfig string, cluster *v1authpayload.G p.RemoveConfig(cluster.ShortName) return err } - // TODO ADD CHECK -> IF THERE IS NO NAMESPACE IT WILL PANIC + if len(cluster.Namespaces) == 0 { + return nil + } if err := checkNamespace(kubeConfig, cluster.Namespaces[0].Name); err != nil { p.RemoveConfig(cluster.ShortName) return err diff --git a/nerd/client/auth/v1/ops_client.go b/nerd/client/auth/v1/ops_client.go index ebbad4706..c71f61e41 100644 --- a/nerd/client/auth/v1/ops_client.go +++ b/nerd/client/auth/v1/ops_client.go @@ -87,7 +87,7 @@ func (c *OpsClient) doRequest(method, urlPath string, input, output interface{}) client.LogRequest(req, c.Logger) resp, err := c.Doer.Do(req) if err != nil { - return client.NewError("failed to create HTTP request 3", err) + return client.NewError("failed to create HTTP request", err) } client.LogResponse(resp, c.Logger) @@ -120,11 +120,10 @@ func (c *OpsClient) doRequest(method, urlPath string, input, output interface{}) func (c *OpsClient) GetOAuthCredentials(code, clientID, clientSecret, localServerURL string) (output *v1payload.GetOAuthCredentialsOutput, err error) { output = &v1payload.GetOAuthCredentialsOutput{} input := &v1payload.GetOAuthCredentialsInput{ - Code: code, - ClientID: clientID, - ClientSecret: clientSecret, - GrantType: "authorization_code", - RedirectURI: localServerURL, + Code: code, + ClientID: clientID, + GrantType: "authorization_code", + RedirectURI: localServerURL, } return output, c.doRequest(http.MethodPost, "o/token/", input, output) } @@ -135,7 +134,6 @@ func (c *OpsClient) RefreshOAuthCredentials(refreshToken, clientID, clientSecret input := &v1payload.RefreshOAuthCredentialsInput{ RefreshToken: refreshToken, ClientID: clientID, - ClientSecret: clientSecret, GrantType: "refresh_token", } return output, c.doRequest(http.MethodPost, "o/token/", input, output) diff --git a/nerd/client/auth/v1/payload/cluster.go b/nerd/client/auth/v1/payload/cluster.go index aaf8c2adc..283fc9d51 100644 --- a/nerd/client/auth/v1/payload/cluster.go +++ b/nerd/client/auth/v1/payload/cluster.go @@ -17,14 +17,14 @@ type GetClusterOutput struct { ServiceURL string `json:"service_url"` CaCertificate string `json:"ca_certificate"` Capacity struct { - CPU string `json:"cpu"` - Memory string `json:"memory"` - Pods int `json:"pods"` + CPU float64 `json:"cpu"` + Memory float64 `json:"memory"` + Pods int `json:"pods"` } `json:"capacity"` Usage struct { - CPU string `json:"cpu"` - Memory string `json:"memory"` - Pods int `json:"pods"` + CPU float64 `json:"cpu"` + Memory float64 `json:"memory"` + Pods int `json:"pods"` } `json:"usage"` KubeConfigUser struct { Token string `json:"token"` diff --git a/nerd/client/auth/v1/payload/oauth.go b/nerd/client/auth/v1/payload/oauth.go index ceda98b37..36b095daf 100644 --- a/nerd/client/auth/v1/payload/oauth.go +++ b/nerd/client/auth/v1/payload/oauth.go @@ -2,11 +2,10 @@ package v1payload //GetOAuthCredentialsInput is input for getting oauth credentials type GetOAuthCredentialsInput struct { - Code string `url:"code"` - ClientID string `url:"client_id"` - ClientSecret string `url:"client_secret"` - RedirectURI string `url:"redirect_uri"` - GrantType string `url:"grant_type"` + Code string `url:"code"` + ClientID string `url:"client_id"` + RedirectURI string `url:"redirect_uri"` + GrantType string `url:"grant_type"` } //GetOAuthCredentialsOutput is output when getting oauth credentials @@ -18,7 +17,6 @@ type GetOAuthCredentialsOutput struct { type RefreshOAuthCredentialsInput struct { RefreshToken string `url:"refresh_token"` ClientID string `url:"client_id"` - ClientSecret string `url:"client_secret"` GrantType string `url:"grant_type"` } diff --git a/nerd/conf/conf.go b/nerd/conf/conf.go index 662eab40a..bddc25cb8 100644 --- a/nerd/conf/conf.go +++ b/nerd/conf/conf.go @@ -48,8 +48,7 @@ func DevDefaults(endpoint string) *Config { OAuthLocalServer: "localhost:9876", OAuthSuccessURL: fmt.Sprintf("%s/do/login_complete/?client=CLI", endpoint), SecureClientID: "aK9Yo1QngPbZ", - // to remove - IDPIssuerURL: endpoint, + IDPIssuerURL: endpoint, PublicKey: `-----BEGIN PUBLIC KEY----- MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEBthEmchVCtA3ZPXqiCXdj+7/ZFuhxRgx grTxIHK+b0vEqKqA3O++ggD1GgjqtTfNLGUjLCE3KxyIN78TsK+HU4VVexTjlWXy @@ -70,10 +69,8 @@ func StagingDefaults() *Config { APIEndpoint: "https://auth.staging.nlze.nl/v1/", OAuthLocalServer: "localhost:9876", OAuthSuccessURL: "https://auth.staging.nlze.nl/do/login_complete/?client=CLI", - SecureClientID: "9O7olxjoiRoz", - // to remove - SecureClientSecret: "0c4feb1e9d11790451a4364e803284a60905cef1a5f9bf7bad5f0eeb", - IDPIssuerURL: "https://auth.staging.nlze.nl", + SecureClientID: "9O7olxjoiRoz", // to remove + IDPIssuerURL: "https://auth.staging.nlze.nl", PublicKey: `-----BEGIN PUBLIC KEY----- MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEBthEmchVCtA3ZPXqiCXdj+7/ZFuhxRgx grTxIHK+b0vEqKqA3O++ggD1GgjqtTfNLGUjLCE3KxyIN78TsK+HU4VVexTjlWXy diff --git a/pkg/populator/generic.go b/pkg/populator/generic.go index 8075d8516..ea0a0c49c 100644 --- a/pkg/populator/generic.go +++ b/pkg/populator/generic.go @@ -42,6 +42,7 @@ func (o *GenericPopulator) GetKubeConfigFile() string { //RemoveConfig deletes the precised cluster context and cluster info. func (o *GenericPopulator) RemoveConfig(cluster string) error { + cluster = fmt.Sprintf("%s-%s", Prefix, cluster) // read existing config or create new if does not exist kubecfg, err := ReadConfigOrNew(o.GetKubeConfigFile()) if err != nil { @@ -49,7 +50,7 @@ func (o *GenericPopulator) RemoveConfig(cluster string) error { } delete(kubecfg.Clusters, cluster) delete(kubecfg.AuthInfos, cluster) - delete(kubecfg.Contexts, fmt.Sprintf("%s-%s", Prefix, cluster)) + delete(kubecfg.Contexts, cluster) kubecfg.CurrentContext = "" // write back to disk @@ -68,11 +69,7 @@ func (o *GenericPopulator) PopulateKubeConfig(namespace string) error { if o.cluster.CaCertificate == "" { c.InsecureSkipTLSVerify = true } else { - cert, err := o.createCertificate(o.cluster.CaCertificate, o.cluster.ShortName, o.homedir) - if err != nil { - return err - } - c.CertificateAuthority = cert + c.CertificateAuthorityData = []byte(o.cluster.CaCertificate) } c.Server = o.cluster.ServiceURL @@ -101,19 +98,19 @@ func (o *GenericPopulator) PopulateKubeConfig(namespace string) error { // context context := api.NewContext() - context.Cluster = o.cluster.ShortName - context.AuthInfo = o.cluster.ShortName - context.Namespace = namespace clusterName := fmt.Sprintf("%s-%s", Prefix, o.cluster.ShortName) + context.Cluster = clusterName + context.AuthInfo = clusterName + context.Namespace = namespace // read existing config or create new if does not exist kubecfg, err := ReadConfigOrNew(o.GetKubeConfigFile()) if err != nil { return err } - kubecfg.Clusters[o.cluster.ShortName] = c + kubecfg.Clusters[clusterName] = c kubecfg.CurrentContext = clusterName - kubecfg.AuthInfos[o.cluster.ShortName] = auth + kubecfg.AuthInfos[clusterName] = auth kubecfg.Contexts[clusterName] = context // write back to disk diff --git a/pkg/populator/populator.go b/pkg/populator/populator.go index c81a17212..b06d69781 100644 --- a/pkg/populator/populator.go +++ b/pkg/populator/populator.go @@ -188,7 +188,7 @@ func NerdContext(filename string) bool { return false } - if strings.Contains(config.CurrentContext, Prefix) { + if strings.HasPrefix(config.CurrentContext, Prefix) { return true } diff --git a/pkg/populator/types.go b/pkg/populator/types.go index ffe70d4a0..e005172a3 100644 --- a/pkg/populator/types.go +++ b/pkg/populator/types.go @@ -5,7 +5,7 @@ const ( DirPermissions = 0755 //Prefix is used to know if a context comes from the cli. - Prefix = "nerd-cli" + Prefix = "nerdalize" ) // P is an interface that we can use to read from and to write to the kube config file.