rrajesh-cloudera commented on code in PR #915:
URL: https://github.com/apache/yunikorn-k8shim/pull/915#discussion_r1804179188
##########
test/e2e/framework/helpers/k8s/k8s_utils.go:
##########
@@ -1091,6 +1097,58 @@ func (k *KubeCtl) CreateSecret(secret *v1.Secret,
namespace string) (*v1.Secret,
return k.clientSet.CoreV1().Secrets(namespace).Create(context.TODO(),
secret, metav1.CreateOptions{})
}
+// SetClientFromConfig initializes a new Kubernetes clientset from the given
config and sets it in kClient
+func (k *KubeCtl) SetClientFromConfig(newConf *rest.Config) error {
+ // Create a new Kubernetes clientset from the new configuration
+ clientset, err := kubernetes.NewForConfig(newConf)
+ if err != nil {
+ return err // Return the error if clientset creation fails
+ }
+
+ // Set the new clientset to the kClient object
+ k.clientSet = clientset
+ return nil
+}
+
+func WriteConfigToFile(config *rest.Config, kubeconfigPath string) error {
+ // Build the kubeconfig API object from the rest.Config
+ kubeConfig := &clientcmdapi.Config{
+ Clusters: map[string]*clientcmdapi.Cluster{
+ "default-cluster": {
+ Server: config.Host,
+ CertificateAuthorityData: config.CAData,
+ InsecureSkipTLSVerify: config.Insecure,
+ },
+ },
+ AuthInfos: map[string]*clientcmdapi.AuthInfo{
+ "default-auth": {
+ Token: config.BearerToken,
+ },
+ },
+ Contexts: map[string]*clientcmdapi.Context{
+ "default-context": {
+ Cluster: "default-cluster",
+ AuthInfo: "default-auth",
+ },
+ },
+ CurrentContext: "default-context",
+ }
+
+ // Ensure the directory where the file is being written exists
+ err := os.MkdirAll(filepath.Dir(kubeconfigPath), os.ModePerm)
+ if err != nil {
+ return fmt.Errorf("failed to create directory for kubeconfig
file: %v", err)
+ }
+
+ // Write the kubeconfig to the specified file
+ err = clientcmd.WriteToFile(*kubeConfig, kubeconfigPath)
+ if err != nil {
+ return fmt.Errorf("failed to write kubeconfig to file: %v", err)
+ }
+
+ return nil
+}
Review Comment:
We need a temp file to write the kubeconfig and reload the kubeconfig of the
temp file to apply the changes to Kubernetes clientset.
Tried without writing the file and copying the existing kubeconfig to
newConf with bearertoken value set but that approach is not working well so
that's why we needed to add the write config file to temp store the kubeconfig
and reload for the k8s clientset.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]