From ae0e4a248d3ae6a695e88200957fa6af4770ead7 Mon Sep 17 00:00:00 2001 From: CristhianF7 Date: Wed, 13 Mar 2024 14:20:48 -0500 Subject: [PATCH] fix: config version --- cmd/akamai/create.go | 9 +-- configs/config.go | 18 ------ configs/viperConfig.go | 107 -------------------------------- internal/common/common.go | 2 +- internal/progress/message.go | 14 ++++- internal/segment/segment.go | 2 +- internal/utilities/utilities.go | 48 ++++++++++---- 7 files changed, 53 insertions(+), 147 deletions(-) delete mode 100644 configs/config.go delete mode 100644 configs/viperConfig.go diff --git a/cmd/akamai/create.go b/cmd/akamai/create.go index 7dbf8ff74..0135ec70a 100644 --- a/cmd/akamai/create.go +++ b/cmd/akamai/create.go @@ -13,6 +13,7 @@ import ( "github.com/kubefirst/kubefirst/internal/catalog" "github.com/kubefirst/kubefirst/internal/cluster" "github.com/kubefirst/kubefirst/internal/gitShim" + "github.com/kubefirst/kubefirst/internal/launch" "github.com/kubefirst/kubefirst/internal/progress" "github.com/kubefirst/kubefirst/internal/provision" "github.com/kubefirst/kubefirst/internal/utilities" @@ -75,10 +76,10 @@ func createAkamai(cmd *cobra.Command, args []string) error { viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) viper.WriteConfig() - // k3dClusterCreationComplete := viper.GetBool("launch.deployed") - // if !k3dClusterCreationComplete { - // launch.Up(nil, true, cliFlags.UseTelemetry) - // } + k3dClusterCreationComplete := viper.GetBool("launch.deployed") + if !k3dClusterCreationComplete { + launch.Up(nil, true, cliFlags.UseTelemetry) + } err = pkg.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngresUrl()), "kubefirst api") if err != nil { diff --git a/configs/config.go b/configs/config.go deleted file mode 100644 index 3ca355fa1..000000000 --- a/configs/config.go +++ /dev/null @@ -1,18 +0,0 @@ -/* -Copyright (C) 2021-2023, Kubefirst - -This program is licensed under MIT. -See the LICENSE file for more details. -*/ -package configs - -/** -This is an initial implementation of Config. Please keep in mind we're still working to improve how we handle -environment variables and general config data. -*/ - -const DefaultK1Version = "development" - -// K1Version is used on version command. The value is dynamically updated on build time via ldflag. Built Kubefirst -// versions will follow semver value like 1.9.0, when not using the built version, "development" is used. -var K1Version = DefaultK1Version diff --git a/configs/viperConfig.go b/configs/viperConfig.go deleted file mode 100644 index 374db0935..000000000 --- a/configs/viperConfig.go +++ /dev/null @@ -1,107 +0,0 @@ -/* -Copyright (C) 2021-2023, Kubefirst - -This program is licensed under MIT. -See the LICENSE file for more details. -*/ -package configs - -// code from: https://github.com/carolynvs/stingoftheviper - -import ( - "fmt" - "strings" - - "github.com/spf13/cobra" - "github.com/spf13/pflag" - "github.com/spf13/viper" -) - -// it follows Viper precedence: -// 1. explicit call to Set -// 2. flag -// 3. env -// 4. config -// 5. key/value store -// 6. default - -// the input file that is able to provide is called kubefirst.yaml, and should be at the root folder, where the user has -// it's Kubefirst binary. Following the flag name convention is enough to have a Kubefirst config file. - -// FLAGS VARIABLE -// example loading values from flags: -// go run . command-name --admin-email user@example.com - -// ENVIRONMENT VARIABLE -// example loading environment variables: -// export KUBEFIRST_CLOUD=k3d -// command line commands loads the values from the environment variable and override the command flag. - -// YAML -// example of a YAML Kubefirst file: -// admin-email: user@example.com -// cloud: k3d -// command line commands loads the value from the kubefirst.yaml and override the command flags. - -const ( - // The name of our config file, without the file extension because viper supports many different config file languages. - defaultConfigFilename = "kubefirst-config" - - // The environment variable prefix of all environment variables bound to our command line flags. - // For example, --number is bound to STING_NUMBER. - envPrefix = "KUBEFIRST" -) - -func InitializeViperConfig(cmd *cobra.Command) error { - v := viper.New() - - // Set the base name of the config file, without the file extension. - v.SetConfigName(defaultConfigFilename) - v.SetConfigType("yaml") - - // Set as many paths as you like where viper should look for the - // config file. We are only looking in the current working directory. - v.AddConfigPath(".") - - // Attempt to read the config file, gracefully ignoring errors - // caused by a config file not being found. Return an error - // if we cannot parse the config file. - //if err := v.ReadInConfig(); err != nil { - // It's okay if there isn't a config file - //if _, ok := err.(viper.ConfigFileNotFoundError); !ok { - // return err - //} - //return err - //} - - // When we bind flags to environment variables expect that the - // environment variables are prefixed, e.g. a flag like --number - // binds to an environment variable STING_NUMBER. This helps - // avoid conflicts. - v.SetEnvPrefix(envPrefix) - - // Bind to environment variables - // Works great for simple config names, but needs help for names - // like --favorite-color which we fix in the bindFlags function - v.AutomaticEnv() - - // Bind the current command's flags to viper - bindFlags(cmd, v) - - return nil -} - -// Bind each cobra flag to its associated viper configuration (config file and environment variable) -func bindFlags(cmd *cobra.Command, v *viper.Viper) { - cmd.Flags().VisitAll(func(f *pflag.Flag) { - // Environment variables can't have dashes in them, so bind them to their equivalent - // keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR - v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) - - // Apply the viper config value to the flag when the flag is not set and viper has a value - if !f.Changed && v.IsSet(f.Name) { - val := v.Get(f.Name) - cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)) - } - }) -} diff --git a/internal/common/common.go b/internal/common/common.go index 4edfd7c57..4b74a533d 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -16,10 +16,10 @@ import ( "strings" "github.com/kubefirst/kubefirst-api/pkg/providerConfigs" - "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/cluster" "github.com/kubefirst/kubefirst/internal/launch" "github.com/kubefirst/kubefirst/internal/progress" + "github.com/kubefirst/runtime/configs" "github.com/kubefirst/runtime/pkg/docker" "github.com/rs/zerolog/log" "github.com/spf13/cobra" diff --git a/internal/progress/message.go b/internal/progress/message.go index 9de44b1ae..82e90c7d4 100644 --- a/internal/progress/message.go +++ b/internal/progress/message.go @@ -111,6 +111,14 @@ func DisplaySuccessMessage(cluster types.Cluster) successMsg { } + var fullDomainName string + + if cluster.SubdomainName != "" { + fullDomainName = fmt.Sprintf("%s.%s", cluster.SubdomainName, cluster.DomainName) + } else { + fullDomainName = cluster.DomainName + } + success := ` ## #### :tada: Success` + "`Cluster " + cluster.ClusterName + " is now up and running`" + ` @@ -125,11 +133,11 @@ func DisplaySuccessMessage(cluster types.Cluster) successMsg { ### Repos ` + fmt.Sprintf("`https://%s.com/%s/gitops` \n\n", cluster.GitProvider, cluster.GitAuth.Owner) + fmt.Sprintf("` https://%s.com/%s/metaphor`", cluster.GitProvider, cluster.GitAuth.Owner) + ` ## Kubefirst Console -### URL ` + fmt.Sprintf("`https://kubefirst.%s`", cluster.DomainName) + ` +### URL ` + fmt.Sprintf("`https://kubefirst.%s`", fullDomainName) + ` ## Argo CD -### URL ` + fmt.Sprintf("`https://argocd.%s`", cluster.DomainName) + ` +### URL ` + fmt.Sprintf("`https://argocd.%s`", fullDomainName) + ` ## Vault -### URL ` + fmt.Sprintf("`https://vault.%s`", cluster.DomainName) + ` +### URL ` + fmt.Sprintf("`https://vault.%s`", fullDomainName) + ` ### :bulb: Quick start examples: diff --git a/internal/segment/segment.go b/internal/segment/segment.go index 3dad6cab0..f2bbb5589 100644 --- a/internal/segment/segment.go +++ b/internal/segment/segment.go @@ -4,8 +4,8 @@ import ( "os" "github.com/denisbrodbeck/machineid" - "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/metrics-client/pkg/telemetry" + "github.com/kubefirst/runtime/configs" "github.com/kubefirst/runtime/pkg/k3d" ) diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go index 47651ed75..05ca01d70 100644 --- a/internal/utilities/utilities.go +++ b/internal/utilities/utilities.go @@ -9,7 +9,6 @@ package utilities import ( "bufio" "encoding/json" - "errors" "fmt" "io/ioutil" "net/http" @@ -18,9 +17,9 @@ import ( "time" apiTypes "github.com/kubefirst/kubefirst-api/pkg/types" - "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/progress" "github.com/kubefirst/kubefirst/internal/types" + "github.com/kubefirst/runtime/configs" "github.com/kubefirst/runtime/pkg/k8s" "github.com/rs/zerolog/log" "github.com/spf13/viper" @@ -45,10 +44,6 @@ func CreateK1ClusterDirectory(clusterName string) { } } -const ( - exportFilePath = "/tmp/api/cluster/export" -) - func CreateClusterRecordFromRaw( useTelemetry bool, gitOwner string, @@ -238,24 +233,23 @@ func ExportCluster(cluster apiTypes.Cluster, kcfg *k8s.KubernetesClient) error { time.Sleep(time.Second * 10) - payload, err := json.Marshal(cluster) + bytes, err := json.Marshal(cluster) if err != nil { log.Error().Msg(err.Error()) return err } + secretValuesMap, _ := ParseJSONToMap(string(bytes)) + secret := &v1secret.Secret{ - ObjectMeta: metav1.ObjectMeta{Name: "mongodb-state", Namespace: "kubefirst"}, - Data: map[string][]byte{ - "cluster-0": []byte(payload), - "cluster-name": []byte(cluster.ClusterName), - }, + ObjectMeta: metav1.ObjectMeta{Name: "kubefirst-initial-state", Namespace: "kubefirst"}, + Data: secretValuesMap, } err = k8s.CreateSecretV2(kcfg.Clientset, secret) if err != nil { - return errors.New(fmt.Sprintf("unable to save secret to management cluster. %s", err)) + return fmt.Errorf(fmt.Sprintf("unable to save secret to management cluster. %s", err)) } viper.Set("kubefirst-checks.secret-export-state", true) @@ -296,3 +290,31 @@ func ConsumeStream(url string) { return } } + +func ParseJSONToMap(jsonStr string) (map[string][]byte, error) { + var result map[string]interface{} + err := json.Unmarshal([]byte(jsonStr), &result) + if err != nil { + return nil, err + } + + secretData := make(map[string][]byte) + for key, value := range result { + switch v := value.(type) { + case map[string]interface{}, []interface{}: // For nested structures, marshal back to JSON + bytes, err := json.Marshal(v) + if err != nil { + return nil, err + } + secretData[key] = bytes + default: + bytes, err := json.Marshal(v) + if err != nil { + return nil, err + } + secretData[key] = bytes + } + } + + return secretData, nil +}