Skip to content

Commit

Permalink
Feat remove env vars (#184)
Browse files Browse the repository at this point in the history
* fix: update version

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* feat: remove environment variables requirement, set values via command line

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* feat: remove environment variables requirement, set values via command line

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* add k1 folder

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* add k1 folder

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* update profile

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

Co-authored-by: 6za <53096417+6za@users.noreply.github.com>
  • Loading branch information
João Paulo Vanzuita and 6za committed Aug 3, 2022
1 parent 3c2c7f7 commit dbb02ff
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 61 deletions.
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ The Kubefirst CLI is a cloud provisioning tool. With simple setup and two CLI co

- [DNS Setup](#dns-setup)
- [Clone the Repository](#clone-the-repository)
- [Environment Variables](#environment-variables)
- [Start the Container](#start-the-container)
- [Connect to the Container](#connect-to-the-container)
- [Initialization](#initialization)
Expand Down Expand Up @@ -74,15 +73,6 @@ git clone https://github.com/kubefirst/kubefirst.git
git clone git@github.com:kubefirst/kubefirst.git
```

## Environment Variables

Create a `.env` file in the root of the `kubefirst` repository, and add the following variables:

```env
AWS_PROFILE=default
AWS_REGION=eu-central-1
```

## Start the Container

We run everything in isolation with Docker, for that, start the container with:
Expand All @@ -106,6 +96,7 @@ Some process requires previous initialization, for that, run:
```bash
kubefirst init \
--cloud aws \
--profile default \
--region eu-central-1 \
--admin-email user@example.com \
--cluster-name your_cluster_name \
Expand Down
4 changes: 0 additions & 4 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ var infoCmd = &cobra.Command{
if err != nil {
log.Panic(err)
}
err = configs.CheckEnvironment()
if err != nil {
log.Panic(err)
}
fmt.Printf("----------- \n")

fmt.Println(reports.StyleMessage(infoSummary.String()))
Expand Down
40 changes: 36 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"
"strings"
Expand Down Expand Up @@ -56,6 +58,16 @@ to quickly create a Cobra application.`,
trackers[pkg.CreateSSHKey] = &pkg.ActionTracker{Tracker: pkg.CreateTracker(pkg.CreateSSHKey, 1)}
trackers[pkg.CreateBuckets] = &pkg.ActionTracker{Tracker: pkg.CreateTracker(pkg.CreateBuckets, 1)}
trackers[pkg.SendTelemetry] = &pkg.ActionTracker{Tracker: pkg.CreateTracker(pkg.SendTelemetry, 1)}

k1Dir := fmt.Sprintf("%s", config.K1FolderPath)
if _, err := os.Stat(k1Dir); errors.Is(err, os.ErrNotExist) {
if err := os.Mkdir(k1Dir, os.ModePerm); err != nil {
log.Panicf("info: could not create directory %q - error: %s", config.K1FolderPath, err)
}
} else {
log.Printf("info: %s already exist", k1Dir)
}

infoCmd.Run(cmd, args)
hostedZoneName, _ := cmd.Flags().GetString("hosted-zone-name")
metricName := "kubefirst.init.started"
Expand Down Expand Up @@ -86,9 +98,11 @@ to quickly create a Cobra application.`,
log.Println("adminEmail:", adminEmail)
viper.Set("adminemail", adminEmail)

// region
// name of the cloud region to provision resources when resources are region-specific
region, _ := cmd.Flags().GetString("region")
// profile
region, err := cmd.Flags().GetString("region")
if err != nil {
log.Println(err)
}
viper.Set("aws.region", region)
// propagate it to local environment
err = os.Setenv("AWS_REGION", region)
Expand All @@ -97,6 +111,18 @@ to quickly create a Cobra application.`,
}
log.Println("region:", region)

profile, err := cmd.Flags().GetString("profile")
if err != nil {
log.Println(err)
}
viper.Set("aws.profile", profile)
// propagate it to local environment
err = os.Setenv("AWS_PROFILE", profile)
if err != nil {
log.Panicf("unable to set environment variable AWS_PROFILE, error is: %v", err)
}
log.Println("profile:", profile)

// cluster name
clusterName, err := cmd.Flags().GetString("cluster-name")
if err != nil {
Expand Down Expand Up @@ -222,11 +248,17 @@ func init() {
if err != nil {
log.Panic(err)
}
initCmd.Flags().String("region", "", "the region to provision the cloud resources in")
initCmd.Flags().String("region", "eu-west-1", "the region to provision the cloud resources in")
err = initCmd.MarkFlagRequired("region")
if err != nil {
log.Panic(err)
}

initCmd.Flags().String("profile", "default", "AWS profile located at ~/.aws/config")
err = initCmd.MarkFlagRequired("profile")
if err != nil {
log.Panic(err)
}
initCmd.Flags().Bool("clean", false, "delete any local kubefirst content ~/.kubefirst, ~/.k1")

log.SetPrefix("LOG: ")
Expand Down
3 changes: 1 addition & 2 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ environment variables and general config data.
// Config host application configuration
// todo: some of these values can be moved to the .env
type Config struct {
AwsProfile string `env:"AWS_PROFILE"`
LocalOs string
LocalArchitecture string
InstallerEmail string
Expand Down Expand Up @@ -75,7 +74,7 @@ func ReadConfig() *Config {
// todo adopt latest helmVersion := "v3.9.0"
config.HelmVersion = "v3.2.1"

config.KubefirstVersion = "1.8.2"
config.KubefirstVersion = "1.8.4"

config.InstallerEmail = "kubefirst-bot@kubefirst.com"

Expand Down
27 changes: 0 additions & 27 deletions configs/envvars.go

This file was deleted.

2 changes: 0 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ services:
context: .
dockerfile: ./build/Dockerfile
container_name: kubefirst
env_file:
- .env
environment:
TERM: xterm-256color
ports:
Expand Down
19 changes: 13 additions & 6 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ import (

func BucketRand(dryRun bool) {

sess, err := session.NewSession(&aws.Config{
Region: aws.String(viper.GetString("aws.region"))},
)
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(viper.GetString("aws.region")),
},
Profile: viper.GetString("aws.profile"),
})

if err != nil {
log.Println("failed to attempt bucket creation ", err.Error())
os.Exit(1)
Expand Down Expand Up @@ -305,9 +309,12 @@ func DestroyBucket(bucketName string) {
}

func GetAWSSession() *session.Session {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(viper.GetString("aws.region"))},
)
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(viper.GetString("aws.region")),
},
Profile: viper.GetString("aws.profile"),
})
if err != nil {
log.Panicf("failed to get session ", err.Error())
}
Expand Down
5 changes: 3 additions & 2 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func AwaitHost(appName string, dryRun bool) {
AwaitHostNTimes(appName, dryRun, 200)
}

func AwaitHostNTimes(appName string, dryRun bool, times int) bool{
func AwaitHostNTimes(appName string, dryRun bool, times int) bool {
log.Println("AwaitHostNTimes called")
if dryRun {
log.Printf("[#99] Dry-run mode, AwaitHost skipped.")
Expand Down Expand Up @@ -258,7 +258,7 @@ func ApplyGitlabTerraform(dryRun bool, directory string) {
//* https://registry.terraform.io/providers/hashicorp/aws/2.34.0/docs#shared-credentials-file
envs := map[string]string{}
envs["AWS_SDK_LOAD_CONFIG"] = "1"
envs["AWS_PROFILE"] = config.AwsProfile
envs["AWS_PROFILE"] = viper.GetString("aws.profile")
// Prepare for terraform gitlab execution
envs["GITLAB_TOKEN"] = viper.GetString("gitlab.token")
envs["GITLAB_BASE_URL"] = viper.GetString("gitlab.local.service")
Expand Down Expand Up @@ -329,6 +329,7 @@ func DestroyGitlabTerraform(skipGitlabTerraform bool) {
config := configs.ReadConfig()
envs := map[string]string{}

envs["AWS_PROFILE"] = viper.GetString("aws.profile")
envs["AWS_REGION"] = viper.GetString("aws.region")
envs["AWS_ACCOUNT_ID"] = viper.GetString("aws.accountid")
envs["HOSTED_ZONE_NAME"] = viper.GetString("aws.hostedzonename")
Expand Down
9 changes: 6 additions & 3 deletions internal/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package terraform
import (
"bytes"
"fmt"
"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/viper"
"log"
"os"
"os/exec"
"strings"

"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/viper"
)

func ApplyBaseTerraform(dryRun bool, directory string) {
Expand All @@ -22,6 +23,7 @@ func ApplyBaseTerraform(dryRun bool, directory string) {
return
}
envs := map[string]string{}
envs["AWS_PROFILE"] = viper.GetString("aws.profile")
envs["TF_VAR_aws_account_id"] = viper.GetString("aws.accountid")
envs["TF_VAR_aws_region"] = viper.GetString("aws.region")
envs["TF_VAR_hosted_zone_name"] = viper.GetString("aws.hostedzonename")
Expand Down Expand Up @@ -70,6 +72,7 @@ func DestroyBaseTerraform(skipBaseTerraform bool) {
}

envs := map[string]string{}
envs["AWS_PROFILE"] = viper.GetString("aws.profile")
envs["TF_VAR_aws_account_id"] = viper.GetString("aws.accountid")
envs["TF_VAR_aws_region"] = viper.GetString("aws.region")
envs["TF_VAR_hosted_zone_name"] = viper.GetString("aws.hostedzonename")
Expand Down
2 changes: 1 addition & 1 deletion internal/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func ConfigureVault(dryRun bool) {
envs["VAULT_ADDR"] = "http://localhost:8200" //Should this come from init?
envs["VAULT_TOKEN"] = vaultToken
envs["AWS_SDK_LOAD_CONFIG"] = "1"
envs["AWS_PROFILE"] = config.AwsProfile
envs["AWS_PROFILE"] = viper.GetString("aws.profile")
envs["AWS_DEFAULT_REGION"] = viper.GetString("aws.region")

envs["TF_VAR_vault_addr"] = fmt.Sprintf("https://vault.%s", viper.GetString("aws.hostedzonename"))
Expand Down

0 comments on commit dbb02ff

Please sign in to comment.