Skip to content

Commit

Permalink
updating kubeconfig, troubleshooting permission denied on fresh node …
Browse files Browse the repository at this point in the history
…for kuubectl cmd
  • Loading branch information
endawkins committed Oct 1, 2024
1 parent 78a4cfb commit 0c385b3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/testcase/clusterrestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func TestClusterRestoreS3(

newServerIP := externalServerIP

setConfigFile(product, newServerIP[0])
shared.LogLevel("info", "config.yaml successfully created and copied to: /etc/rancher/%s/", product)

installProduct(
cluster,
newServerIP[0],
Expand All @@ -111,11 +114,20 @@ func TestClusterRestoreS3(
)
shared.LogLevel("info", "%s service successfully enabled", product)

path := fmt.Sprintf("/tmp/%s_kubeconfig", serverName[0])
shared.KubeConfigFile = path
shared.LogLevel("info", "kubeconfig path updated to %s", shared.KubeConfigFile)

newKubeConfig, newKubeConfigErr := shared.UpdateKubeConfig(newServerIP[0],
resourceName, product)
Expect(newKubeConfigErr).NotTo(HaveOccurred())
shared.LogLevel("info", "kubeconfig updated to %s\n", newKubeConfig)

if product == "rke2" {
exportKubectl(newServerIP[0])
shared.LogLevel("info", "kubectl commands successfully exported")
}

}

func testS3SnapshotSave(cluster *shared.Cluster, flags *customflag.FlagConfig) {
Expand Down Expand Up @@ -233,3 +245,41 @@ func enableAndStartService(
[]string{newClusterIP})
Expect(startServiceCmdErr).NotTo(HaveOccurred())
}

func exportKubectl(newClusterIP string) {
//update data directory for rpm installs (rhel systems)
importCmd := fmt.Sprintf("sudo cat <<EOF >>.bashrc\n" +
"export KUBECONFIG=/etc/rancher/rke2/rke2.yaml PATH=$PATH:/var/lib/rancher/rke2/bin:/opt/rke2/bin " +
"CRI_CONFIG_FILE=/var/lib/rancher/rke2/agent/etc/crictl.yaml && \n" +
"alias k=kubectl\n" +
"EOF")

sourceCmd := `source .bashrc`

_, importCmdErr := shared.RunCommandOnNode(importCmd, newClusterIP)
Expect(importCmdErr).NotTo(HaveOccurred())

_, sourceCmdErr := shared.RunCommandOnNode(sourceCmd, newClusterIP)
Expect(sourceCmdErr).NotTo(HaveOccurred())

}

func setConfigFile(product string, newClusterIP string) {
createConfigFileCmd := fmt.Sprintf("sudo cat <<EOF >>config.yaml\n" +
"write-kubeconfig-mode: 0644\n" +
"EOF")

path := fmt.Sprintf("/etc/rancher/%s/", product)
mkdirCmd := fmt.Sprintf("sudo mkdir -p %s", path)
copyConfigFileCmd := fmt.Sprintf("sudo cp config.yaml %s", path)

_, createConfigFileCmdErr := shared.RunCommandOnNode(createConfigFileCmd, newClusterIP)
Expect(createConfigFileCmdErr).NotTo(HaveOccurred())

_, mkdirCmdErr := shared.RunCommandOnNode(mkdirCmd, newClusterIP)
Expect(mkdirCmdErr).NotTo(HaveOccurred())

_, copycopyConfigFileCmdErr := shared.RunCommandOnNode(copyConfigFileCmd, newClusterIP)
Expect(copycopyConfigFileCmdErr).NotTo(HaveOccurred())

}
1 change: 1 addition & 0 deletions shared/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func updateKubeConfigLocal(newServerIP, resourceName, product string) (string, e
if writeErr != nil {
return "", ReturnLogError("failed to write updated kubeconfig file: %w\n", writeErr)
}

KubeConfigFile = updatedKubeConfig
// updatedKubeConfig = base64.StdEncoding.EncodeToString([]byte(updatedKubeConfig))

Expand Down

0 comments on commit 0c385b3

Please sign in to comment.