Skip to content

Commit

Permalink
#29 - refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
apanasiuk-el committed Nov 6, 2024
1 parent c4e02de commit 016ac74
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,19 @@ func (cc *ClusterCommands) initClusterCTLConfig() error {
return os.RemoveAll(clusterCTLConfig)
}

func (cc *ClusterCommands) mergeKubeConfig(clusterContext []byte) error {
func (cc *ClusterCommands) manageKubeConfigItem(itemType, itemName string) error {
cc.SpecCMD = cc.kubectl("config", itemType, itemName)
cc.SpecCMD.DisableStdOut = true
if err := releaseRunner(cc).runCMD(); err != nil {
return fmt.Errorf("%s", strings.ReplaceAll(cc.SpecCMD.StderrBuf.String(), "\n", ""))
}

zap.S().Infof("%s", strings.ReplaceAll(cc.SpecCMD.StdoutBuf.String(), "\n", ""))

return nil
}

func (cc *ClusterCommands) mergeKubeConfigs(clusterContext []byte) error {
var object interface{}

if err := yaml.Unmarshal(clusterContext, &object); err != nil {
Expand Down Expand Up @@ -177,12 +189,12 @@ func (cc *ClusterCommands) mergeKubeConfig(clusterContext []byte) error {
return err
}

json, err := runtime.Encode(clientcmdlatest.Codec, mergeConfig)
data, err := runtime.Encode(clientcmdlatest.Codec, mergeConfig)
if err != nil {
return err
}

kubeConfig, err := yaml2.JSONToYAML(json)
kubeConfig, err := yaml2.JSONToYAML(data)
if err != nil {
return err
}
Expand All @@ -191,9 +203,7 @@ func (cc *ClusterCommands) mergeKubeConfig(clusterContext []byte) error {
return err
}

cc.SpecCMD = cc.kubectl("config", "use-context", cc.Conf.Name)

return releaseRunner(cc).runCMD()
return cc.manageKubeConfigItem("use-context", cc.Conf.Name)
}

func (cc *ClusterCommands) getKubeContext() (string, string, error) {
Expand Down Expand Up @@ -251,17 +261,15 @@ func (cc *ClusterCommands) switchKubeContext() error {
}

if _, ok := kubeConfig.Contexts[util.CAPIContextName]; ok && currentContextName != util.CAPIContextName {
cc.SpecCMD = cc.kubectl("config", "use-context", util.CAPIContextName)
return releaseRunner(cc).runCMD()
return cc.manageKubeConfigItem("use-context", util.CAPIContextName)
} else if ok && currentContextName == util.CAPIContextName {
return nil
}
}

if len(contextName) > 0 && !cc.UpdateContext {
if contextName != currentContextName {
cc.SpecCMD = cc.kubectl("config", "use-context", contextName)
return releaseRunner(cc).runCMD()
return cc.manageKubeConfigItem("use-context", contextName)
}

return nil
Expand All @@ -278,7 +286,7 @@ func (cc *ClusterCommands) switchKubeContext() error {
return err
}

if err := cc.mergeKubeConfig(clusterContext); err != nil {
if err := cc.mergeKubeConfigs(clusterContext); err != nil {
return err
}
case azure_provider.AzureClusterProvider:
Expand All @@ -287,26 +295,14 @@ func (cc *ClusterCommands) switchKubeContext() error {
return err
}

if err := cc.mergeKubeConfig(clusterContext); err != nil {
if err := cc.mergeKubeConfigs(clusterContext); err != nil {
return err
}
}

return nil
}

func (cc *ClusterCommands) deleteKubeConfigItem(itemType, itemName string) error {
cc.SpecCMD = cc.kubectl("config", itemType, itemName)
cc.SpecCMD.DisableStdOut = true
if err := releaseRunner(cc).runCMD(); err != nil {
return fmt.Errorf("%s", strings.ReplaceAll(cc.SpecCMD.StderrBuf.String(), "\n", ""))
}

zap.S().Infof("%s", strings.ReplaceAll(cc.SpecCMD.StdoutBuf.String(), "\n", ""))

return nil
}

func (cc *ClusterCommands) provisionDestroyTargetCluster() error {
if cc.Ctx.Command.Category == util.CAPI {
cc.APICluster = true
Expand Down Expand Up @@ -339,7 +335,7 @@ func (cc *ClusterCommands) provisionDestroyTargetCluster() error {
return err
}

if err := cc.mergeKubeConfig(clusterContext); err != nil {
if err := cc.mergeKubeConfigs(clusterContext); err != nil {
return err
}
case azure_provider.AzureClusterProvider:
Expand All @@ -348,7 +344,7 @@ func (cc *ClusterCommands) provisionDestroyTargetCluster() error {
return err
}

if err := cc.mergeKubeConfig(clusterContext); err != nil {
if err := cc.mergeKubeConfigs(clusterContext); err != nil {
return err
}
}
Expand All @@ -373,28 +369,28 @@ func (cc *ClusterCommands) provisionDestroyTargetCluster() error {
}

if context, ok := kubeConfig.Contexts[cc.Conf.Name]; ok {
if err := cc.deleteKubeConfigItem("delete-context", cc.Conf.Name); err != nil {
if err := cc.manageKubeConfigItem("delete-context", cc.Conf.Name); err != nil {
return err
}

if err := cc.deleteKubeConfigItem("delete-cluster", context.Cluster); err != nil {
if err := cc.manageKubeConfigItem("delete-cluster", context.Cluster); err != nil {
return err
}

if err := cc.deleteKubeConfigItem("delete-user", context.AuthInfo); err != nil {
if err := cc.manageKubeConfigItem("delete-user", context.AuthInfo); err != nil {
return err
}
}

if err := cc.deleteKubeConfigItem("delete-context", cc.Conf.Name); err != nil {
if err := cc.manageKubeConfigItem("delete-context", cc.Conf.Name); err != nil {
return err
}

if err := cc.deleteKubeConfigItem("delete-cluster", cc.Conf.Name); err != nil {
if err := cc.manageKubeConfigItem("delete-cluster", cc.Conf.Name); err != nil {
return err
}

if err := cc.deleteKubeConfigItem("delete-user", cc.Conf.Name); err != nil {
if err := cc.manageKubeConfigItem("delete-user", cc.Conf.Name); err != nil {
return err
}
}
Expand Down

0 comments on commit 016ac74

Please sign in to comment.