Skip to content

Commit

Permalink
Merge pull request #409 from nerdalize/fix/client-id
Browse files Browse the repository at this point in the history
update client id and kube config prefix
  • Loading branch information
advanderveer committed May 18, 2018
2 parents d279a90 + 7f98039 commit 0d5dd4b
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 43 deletions.
8 changes: 5 additions & 3 deletions cmd/cluster_list.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
})
}
Expand Down
12 changes: 10 additions & 2 deletions cmd/login.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 5 additions & 7 deletions nerd/client/auth/v1/ops_client.go
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions nerd/client/auth/v1/payload/cluster.go
Expand Up @@ -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"`
Expand Down
10 changes: 4 additions & 6 deletions nerd/client/auth/v1/payload/oauth.go
Expand Up @@ -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
Expand All @@ -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"`
}

Expand Down
9 changes: 3 additions & 6 deletions nerd/conf/conf.go
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 8 additions & 11 deletions pkg/populator/generic.go
Expand Up @@ -42,14 +42,15 @@ 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 {
return err
}
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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/populator/populator.go
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/populator/types.go
Expand Up @@ -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.
Expand Down

0 comments on commit 0d5dd4b

Please sign in to comment.