...

Source file src/github.com/redhat-developer/odo/pkg/kclient/user.go

Documentation: github.com/redhat-developer/odo/pkg/kclient

     1  package kclient
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  
     8  	oauthv1client "github.com/openshift/client-go/oauth/clientset/versioned/typed/oauth/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/client-go/tools/clientcmd"
    11  	"k8s.io/klog"
    12  )
    13  
    14  // RunLogout logs out the current user from cluster
    15  func (c *Client) RunLogout(stdout io.Writer) error {
    16  	output, err := c.userClient.Users().Get(context.TODO(), "~", metav1.GetOptions{})
    17  	if err != nil {
    18  		klog.V(1).Infof("%v : unable to get userinfo", err)
    19  	}
    20  
    21  	// read the current config form ~/.kube/config
    22  	conf, err := c.KubeConfig.ClientConfig()
    23  	if err != nil {
    24  		klog.V(1).Infof("%v : unable to get client config", err)
    25  	}
    26  	// initialising oauthv1client
    27  	client, err := oauthv1client.NewForConfig(conf)
    28  	if err != nil {
    29  		klog.V(1).Infof("%v : unable to create a new OauthV1Client", err)
    30  	}
    31  
    32  	// deleting token form the server
    33  	if e := client.OAuthAccessTokens().Delete(context.TODO(), conf.BearerToken, metav1.DeleteOptions{}); e != nil {
    34  		klog.V(1).Infof("%v", e)
    35  	}
    36  
    37  	rawConfig, err := c.KubeConfig.RawConfig()
    38  	if err != nil {
    39  		klog.V(1).Infof("%v : unable to switch to  project", err)
    40  	}
    41  
    42  	// deleting token for the current server from local config
    43  	for key, value := range rawConfig.AuthInfos {
    44  		if key == rawConfig.Contexts[rawConfig.CurrentContext].AuthInfo {
    45  			value.Token = ""
    46  		}
    47  	}
    48  	err = clientcmd.ModifyConfig(clientcmd.NewDefaultClientConfigLoadingRules(), rawConfig, true)
    49  	if err != nil {
    50  		klog.V(1).Infof("%v : unable to write config to config file", err)
    51  	}
    52  
    53  	_, err = io.WriteString(stdout, fmt.Sprintf("Logged %q out on %q\n", output.Name, conf.Host))
    54  	return err
    55  }
    56  

View as plain text