Skip to content

Commit

Permalink
Merge pull request #99 from iits-consulting/Ninja243/aliases
Browse files Browse the repository at this point in the history
Add aliases
  • Loading branch information
Ninja243 authored Jul 11, 2024
2 parents 9ab17e3 + 2f743ca commit ca80a59
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
10 changes: 6 additions & 4 deletions cce/cce.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func GetClusterNames(projectName string) config.Clusters {
return clustersArr
}

func GetKubeConfig(configParams KubeConfigParams, skipKubeTLS bool, printKubeConfig bool) {
kubeConfig, err := getKubeConfig(configParams)
func GetKubeConfig(configParams KubeConfigParams, skipKubeTLS bool, printKubeConfig bool, alias string) {
kubeConfig, err := getKubeConfig(configParams, alias)
if err != nil {
common.ThrowError(err)
}
Expand Down Expand Up @@ -97,7 +97,9 @@ func getClustersForProjectFromServiceProvider(projectName string) ([]clusters.Cl
return clusters.List(client, clusters.ListOpts{})
}

func getClusterCertFromServiceProvider(kubeConfigParams KubeConfigParams, clusterID string) (api.Config, error) {
func getClusterCertFromServiceProvider(kubeConfigParams KubeConfigParams,
clusterID string, alias string,
) (api.Config, error) {
project := config.GetActiveCloudConfig().Projects.GetProjectByNameOrThrow(kubeConfigParams.ProjectName)
cloud := config.GetActiveCloudConfig()
provider, err := openstack.AuthenticatedClient(golangsdk.AuthOptions{
Expand All @@ -121,7 +123,7 @@ func getClusterCertFromServiceProvider(kubeConfigParams KubeConfigParams, cluste
}
cert := clusters.GetCertWithExpiration(client, clusterID, expOpts).Body
certWithContext := addContextInformationToKubeConfig(kubeConfigParams.ProjectName,
kubeConfigParams.ClusterName, string(cert))
kubeConfigParams.ClusterName, string(cert), alias)
extractedCert, err := clientcmd.NewClientConfigFromBytes([]byte(certWithContext))
if err != nil {
common.ThrowError(err)
Expand Down
23 changes: 14 additions & 9 deletions cce/kube_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
"k8s.io/client-go/util/homedir"
)

func getKubeConfig(kubeConfigParams KubeConfigParams) (api.Config, error) {
func getKubeConfig(kubeConfigParams KubeConfigParams, alias string) (api.Config, error) {
glog.V(1).Infof("info: getting kube config...")

clusterID, err := getClusterID(kubeConfigParams.ClusterName, kubeConfigParams.ProjectName)
if err != nil {
common.ThrowError(fmt.Errorf("fatal: error receiving cluster id: %w", err))
}

return getClusterCertFromServiceProvider(kubeConfigParams, clusterID)
return getClusterCertFromServiceProvider(kubeConfigParams, clusterID, alias)
}

func mergeKubeConfig(configParams KubeConfigParams, kubeConfig api.Config) {
Expand Down Expand Up @@ -74,15 +74,20 @@ func determineTargetLocation(targetLocation string) string {
return defaultKubeConfigLocation
}

func addContextInformationToKubeConfig(projectName string, clusterName string, kubeConfigData string) string {
func addContextInformationToKubeConfig(projectName string, clusterName string,
kubeConfigData string, alias string,
) string {
cloud := config.GetActiveCloudConfig()

kubeConfigData = strings.ReplaceAll(kubeConfigData, "internalCluster", fmt.Sprintf("%s/%s-intranet",
projectName, clusterName))
kubeConfigData = strings.ReplaceAll(kubeConfigData, "externalCluster", fmt.Sprintf("%s/%s", projectName, clusterName))
kubeConfigData = strings.ReplaceAll(kubeConfigData, "internal", fmt.Sprintf("%s/%s-intranet", projectName,
clusterName))
kubeConfigData = strings.ReplaceAll(kubeConfigData, "external", fmt.Sprintf("%s/%s", projectName, clusterName))
if alias == "" {
alias = fmt.Sprintf("%s/%s", projectName, clusterName)
}

kubeConfigData = strings.ReplaceAll(kubeConfigData, "internalCluster", fmt.Sprintf("%s-intranet",
alias))
kubeConfigData = strings.ReplaceAll(kubeConfigData, "externalCluster", alias)
kubeConfigData = strings.ReplaceAll(kubeConfigData, "internal", fmt.Sprintf("%s-intranet", alias))
kubeConfigData = strings.ReplaceAll(kubeConfigData, "external", alias)
kubeConfigData = strings.ReplaceAll(kubeConfigData, ":\"user\"",
fmt.Sprintf(":\"%s-%s-%s\"", projectName, clusterName, cloud.Username))

Expand Down
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ var cceGetKubeConfigCmd = &cobra.Command{
Server: server,
}

cce.GetKubeConfig(kubeConfigParams, skipKubeTLS, printKubeConfig)
cce.GetKubeConfig(kubeConfigParams, skipKubeTLS, printKubeConfig, alias)
},
}

Expand Down Expand Up @@ -391,6 +391,7 @@ func setupRootCmd() {
cceCmd.AddCommand(cceGetKubeConfigCmd)
cceGetKubeConfigCmd.Flags().BoolVarP(&printKubeConfig, printKubeConfigFlag, printKubeConfigShortFlag,
false, printKubeConfigUsage)
cceGetKubeConfigCmd.Flags().StringVarP(&alias, aliasFlag, aliasShortFlag, "", aliasUsage)
cceGetKubeConfigCmd.Flags().StringVarP(&clusterName, clusterNameFlag, clusterNameShortFlag, "", clusterNameUsage)
cceGetKubeConfigCmd.Flags().BoolVarP(&skipKubeTLS, skipKubeTLSFlag, "", false, skipKubeTLSUsage)
cceGetKubeConfigCmd.Flags().IntVarP(
Expand Down Expand Up @@ -507,6 +508,7 @@ var (
openStackConfigLocation string
skipTLS bool
printKubeConfig bool
alias string
clientSecret string
clientID string
oidcScopes []string
Expand Down Expand Up @@ -705,6 +707,9 @@ $ otc-auth access-token delete --token YourToken --os-domain-name YourDomain`
userIDEnv = "OS_USER_DOMAIN_ID"
userIDUsage = "User Id number, can be obtained on the \"My Credentials page\" on the OTC. Required if --totp is provided. Either provide this argument or set the environment variable " + userIDEnv
regionFlag = "region"
aliasFlag = "alias"
aliasShortFlag = "a"
aliasUsage = "Setting this changes the naming scheme for clusters in the Kube Config from {project name}/{cluster name} to the alias set"
skipKubeTLSFlag = "skip-kube-tls"
skipKubeTLSUsage = "Setting this adds the insecure-skip-tls-verify rule to the config for every cluster"
regionShortFlag = "r"
Expand Down

0 comments on commit ca80a59

Please sign in to comment.