-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to merge kubeconfig when cluster is created (#196)
* pkg/utils/exec: Simplify command invocation and set default PATH variable Signed-off-by: Din Music <[email protected]> * kubeconfig: Merge kubeconfig instead of overriding it Signed-off-by: Din Music <[email protected]> * pkg: Rename copyKubeconfig to mergeKubeconfig Signed-off-by: Din Music <[email protected]> * embed/presets: Set mergeKubeconfig to true for getting-started guide Signed-off-by: Din Music <[email protected]> * docs: Replace copyKubeconfig with mergeKubeconfig Signed-off-by: Din Music <[email protected]> * scripts: Use mergeKubeconfig in test scripts Signed-off-by: Din Music <[email protected]> * pkg/cluster/managers: Rewrite final kubeconfig Replace default occurences of context/cluster/user in kubeconfig with a cluster name. Signed-off-by: Din Music <[email protected]> --------- Signed-off-by: Din Music <[email protected]>
- Loading branch information
Showing
18 changed files
with
314 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,5 @@ cluster: | |
kubernetes: | ||
version: v1.28.6 | ||
networkPlugin: calico | ||
other: | ||
mergeKubeconfig: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
"github.com/MusicDin/kubitect/pkg/tools/ansible" | ||
"github.com/MusicDin/kubitect/pkg/tools/git" | ||
"github.com/MusicDin/kubitect/pkg/tools/virtualenv" | ||
"github.com/MusicDin/kubitect/pkg/ui" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
|
@@ -102,7 +103,27 @@ func (e *kubespray) Create() error { | |
return err | ||
} | ||
|
||
return e.Finalize() | ||
err = e.Finalize() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Rewrite kubeconfig before merging to prevent accidental | ||
// overwrite of an existing configuration. | ||
err = e.rewriteKubeconfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if e.Config.Kubernetes.Other.MergeKubeconfig { | ||
err := e.mergeKubeconfig() | ||
if err != nil { | ||
// Just warn about failure, since deployment has succeeded. | ||
ui.Print(ui.WARN, "Failed to merge kubeconfig:", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Upgrades upgrades a Kubernetes cluster by calling appropriate Kubespray | ||
|
@@ -113,7 +134,14 @@ func (e *kubespray) Upgrade() error { | |
return err | ||
} | ||
|
||
return e.Finalize() | ||
err = e.Finalize() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Rewrite kubeconfig on upgrade, because it is re-fetched | ||
// from the server. | ||
return e.rewriteKubeconfig() | ||
} | ||
|
||
// ScaleUp adds new nodes to the cluster. | ||
|
@@ -207,3 +235,15 @@ func (e *kubespray) generateGroupVars() error { | |
|
||
return nil | ||
} | ||
|
||
// rewriteKubeconfig replaces context/cluster/user in kubeconfig with the | ||
// cluster name. | ||
func (e *kubespray) rewriteKubeconfig() error { | ||
replaces := map[string]string{ | ||
"[email protected]": e.ClusterName, | ||
"kubernetes-admin": e.ClusterName, | ||
"cluster.local": e.ClusterName, | ||
} | ||
|
||
return e.common.rewriteKubeconfig(replaces) | ||
} |
Oops, something went wrong.