Skip to content

Commit

Permalink
convert env variables to string type and redo checks for said variabl…
Browse files Browse the repository at this point in the history
…es (#242)
  • Loading branch information
D-B-Hawk authored Nov 20, 2023
1 parent d5c26bc commit 19ec936
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
14 changes: 12 additions & 2 deletions internal/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool) (pkgtypes.Cluster,
// find the secret in mgmt cluster's kubefirst namespace and read import payload and clustername
var kcfg *k8s.KubernetesClient

if env.IsClusterZero == "" {
var isClusterZero bool = true
if env.IsClusterZero == "false" {
isClusterZero = false
}

if isClusterZero {
log.Info("IS_CLUSTER_ZERO is set to true, skipping import cluster logic.")
return pkgtypes.Cluster{}, nil
}
Expand All @@ -121,7 +126,12 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool) (pkgtypes.Cluster,
}
clusterDir := fmt.Sprintf("%s/.k1/%s", homeDir, "")

kcfg = k8s.CreateKubeConfig(env.InCluster, fmt.Sprintf("%s/kubeconfig", clusterDir))
var inCluster bool = false
if env.InCluster == "true" {
inCluster = true
}

kcfg = k8s.CreateKubeConfig(inCluster, fmt.Sprintf("%s/kubeconfig", clusterDir))

log.Infof("reading secret mongo-state to determine if import is needed")
secData, err := k8s.ReadSecretV2(kcfg.Clientset, "kubefirst", "mongodb-state")
Expand Down
6 changes: 3 additions & 3 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Env struct {
ServerPort int `env:"SERVER_PORT" envDefault:"8081"`
ServerPort string `env:"SERVER_PORT" envDefault:"8081"`
K1AccessToken string `env:"K1_ACCESS_TOKEN"`
MongoDBHost string `env:"MONGODB_HOST,notEmpty"`
MongoDBHostType string `env:"MONGODB_HOST_TYPE,notEmpty"`
Expand All @@ -24,8 +24,8 @@ type Env struct {
KubefirstTeamInfo string `env:"KUBEFIRST_TEAM_INFO"`
AWSRegion string `env:"AWS_REGION"`
AWSProfile string `env:"AWS_PROFILE"`
IsClusterZero string `env:"IS_CLUSTER_ZERO"`
InCluster bool `env:"IN_CLUSTER" envDefault:"false"`
IsClusterZero string `env:"IS_CLUSTER_ZERO" envDefault:"false"`
InCluster string `env:"IN_CLUSTER" envDefault:"false"`
EnterpriseApiUrl string `env:"ENTERPRISE_API_URL"`
}

Expand Down
14 changes: 8 additions & 6 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package env
import (
"os"
"testing"

"github.com/kubefirst/kubefirst-api/internal/constants"
)

func TestEnv(t *testing.T) {
Expand Down Expand Up @@ -51,13 +53,13 @@ func TestEnv(t *testing.T) {
}()

env := Env{}
env, err := GetEnv(true)
env, err := GetEnv(constants.SilenceGetEnv)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

if env.ServerPort != 8081 {
t.Errorf("expected ServerPort to be 8081, but got %d", env.ServerPort)
if env.ServerPort != "8081" {
t.Errorf("expected ServerPort to be 8081, but got %v", env.ServerPort)
}

if env.K1AccessToken != "k1_access_token" {
Expand Down Expand Up @@ -125,11 +127,11 @@ func TestEnv(t *testing.T) {
}

if env.IsClusterZero != "true" {
t.Errorf("expected IsClusterZero to be true, but got false")
t.Errorf("expected IsClusterZero to be true, but got %v", env.IsClusterZero)
}

if env.InCluster != false {
t.Errorf("expected InCluster to be false, but got true")
if env.InCluster != "false" {
t.Errorf("expected InCluster to be false, but got %v", env.InCluster)
}

if env.EnterpriseApiUrl != "enterprise_api_url" {
Expand Down
9 changes: 7 additions & 2 deletions internal/router/api/v1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,13 @@ func PostCreateCluster(c *gin.Context) {

env, _ := env.GetEnv(constants.SilenceGetEnv)

if env.InCluster {
kcfg := k8s.CreateKubeConfig(env.InCluster, "")
var inCluster bool = false
if env.InCluster == "true" {
inCluster = true
}

if inCluster {
kcfg := k8s.CreateKubeConfig(inCluster, "")
k1AuthSecret, err := k8s.ReadSecretV2(kcfg.Clientset, constants.KubefirstNamespace, constants.KubefirstAuthSecretName)
if err != nil {
log.Warnf("authentication secret does not exist, continuing: %s", err)
Expand Down
7 changes: 6 additions & 1 deletion internal/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ func CreateService(cl *pkgtypes.Cluster, serviceName string, appDef *types.Gitop

env, _ := env.GetEnv(constants.SilenceGetEnv)

kcfg = k8s.CreateKubeConfig(env.InCluster, fmt.Sprintf("%s/kubeconfig", tmpGitopsDir))
var inCluster bool = false
if env.InCluster == "true" {
inCluster = true
}

kcfg = k8s.CreateKubeConfig(inCluster, fmt.Sprintf("%s/kubeconfig", tmpGitopsDir))

var fullDomainName string
if cl.SubdomainName != "" {
Expand Down

0 comments on commit 19ec936

Please sign in to comment.