From 02e5bb5090663e38484f5b7b08a4ec824e4d2b0a Mon Sep 17 00:00:00 2001 From: spchortis Date: Tue, 26 Sep 2023 15:32:34 +0300 Subject: [PATCH 01/12] chore: adding configuration file env vars --- src/services/k8s/knative.go | 59 ++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/services/k8s/knative.go b/src/services/k8s/knative.go index 744dd6f..0b79913 100644 --- a/src/services/k8s/knative.go +++ b/src/services/k8s/knative.go @@ -7,7 +7,10 @@ import ( "path" "text/template" "time" - + "strings" + "bufio" + "os" + "github.com/charmbracelet/log" "github.com/nearform/initium-cli/src/services/docker" "github.com/nearform/initium-cli/src/services/project" @@ -105,6 +108,60 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock return fmt.Errorf("cannot create namespace %s, failed with %v", serviceManifest.ObjectMeta.Namespace, err) } + if _, err := os.Stat(".env"); err != nil { + if os.IsNotExist(err) { + log.Info("No environment variables file (.env) to Load!") + } else { + log.Fatalf("Error checking .env file: %v", err) + } + } else { + log.Info("Environment variables file (.env) found! Loading..") + file, err := os.Open(".env") + if err != nil { + log.Fatalf("Error opening .env file: %v", err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + envVariables := make(map[string]string) + + checkFormat := func(line string) bool { + parts := strings.SplitN(line, "=", 2) + return len(parts) == 2 + } + + for scanner.Scan() { + line := scanner.Text() + if checkFormat(line) { + parts := strings.SplitN(line, "=", 2) + key := strings.TrimSpace(parts[0]) + value := strings.TrimSpace(parts[1]) + envVariables[key] = value + } else { + log.Warnf("Environment variables file (.env) line won't be processed due to invalid format: %s. Accepted: KEY=value", line) + } + } + + if err := scanner.Err(); err != nil { + log.Errorf("Error reading environment variables file (.env): %v", err) + } + + if len(envVariables) > 0 { + var envVarList []corev1.EnvVar + for key, value := range envVariables { + envVar := corev1.EnvVar{ + Name: key, + Value: value, + } + envVarList = append(envVarList, envVar) + } + serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...) + log.Info("Environment variables file (.env) content is now loaded!") + } else { + log.Warnf("Environment file (.env) is empty, Nothing to load!") + } + } + service, err := servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{}) var deployedService *servingv1.Service if err != nil { From 15dfe8771ae40a393458a9e38d7897cb9e2a27df Mon Sep 17 00:00:00 2001 From: spchortis Date: Tue, 26 Sep 2023 16:23:48 +0300 Subject: [PATCH 02/12] chore: fmt --- src/services/k8s/knative.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/services/k8s/knative.go b/src/services/k8s/knative.go index 0b79913..1366b94 100644 --- a/src/services/k8s/knative.go +++ b/src/services/k8s/knative.go @@ -1,16 +1,16 @@ package k8s import ( + "bufio" "bytes" "context" "fmt" + "os" "path" + "strings" "text/template" "time" - "strings" - "bufio" - "os" - + "github.com/charmbracelet/log" "github.com/nearform/initium-cli/src/services/docker" "github.com/nearform/initium-cli/src/services/project" @@ -109,11 +109,11 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock } if _, err := os.Stat(".env"); err != nil { - if os.IsNotExist(err) { + if os.IsNotExist(err) { log.Info("No environment variables file (.env) to Load!") - } else { - log.Fatalf("Error checking .env file: %v", err) - } + } else { + log.Fatalf("Error checking .env file: %v", err) + } } else { log.Info("Environment variables file (.env) found! Loading..") file, err := os.Open(".env") @@ -124,7 +124,7 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock scanner := bufio.NewScanner(file) envVariables := make(map[string]string) - + checkFormat := func(line string) bool { parts := strings.SplitN(line, "=", 2) return len(parts) == 2 @@ -132,11 +132,11 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock for scanner.Scan() { line := scanner.Text() - if checkFormat(line) { + if checkFormat(line) { parts := strings.SplitN(line, "=", 2) key := strings.TrimSpace(parts[0]) value := strings.TrimSpace(parts[1]) - envVariables[key] = value + envVariables[key] = value } else { log.Warnf("Environment variables file (.env) line won't be processed due to invalid format: %s. Accepted: KEY=value", line) } @@ -161,7 +161,7 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock log.Warnf("Environment file (.env) is empty, Nothing to load!") } } - + service, err := servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{}) var deployedService *servingv1.Service if err != nil { From 327d78d31c46da5793912510e67d6e0d87157ca8 Mon Sep 17 00:00:00 2001 From: spchortis Date: Tue, 26 Sep 2023 17:36:27 +0300 Subject: [PATCH 03/12] chore: adding test case --- src/services/k8s/knative_test.go | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/services/k8s/knative_test.go b/src/services/k8s/knative_test.go index 8ba5320..93d24e5 100644 --- a/src/services/k8s/knative_test.go +++ b/src/services/k8s/knative_test.go @@ -9,6 +9,11 @@ import ( "github.com/nearform/initium-cli/src/services/docker" "github.com/nearform/initium-cli/src/services/project" + + "github.com/charmbracelet/log" + "bufio" + "strings" + corev1 "k8s.io/api/core/v1" ) var root = "../../../" @@ -73,3 +78,69 @@ func TestLoadManifest(t *testing.T) { } } + +func TestLoadEnvVars(t *testing.T) { + validateEnv := func(envFile string) error { + if _, err := os.Stat(envFile); err != nil { + if os.IsNotExist(err) { + log.Info("No environment variables file (.env) to Load!") + } else { + log.Fatalf("Error checking .env file: %v", err) + } + } else { + log.Info("Environment variables file (.env) found! Loading..") + file, err := os.Open(".env") + if err != nil { + log.Fatalf("Error opening .env file: %v", err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + envVariables := make(map[string]string) + + checkFormat := func(line string) bool { + parts := strings.SplitN(line, "=", 2) + return len(parts) == 2 + } + + for scanner.Scan() { + line := scanner.Text() + if checkFormat(line) { + parts := strings.SplitN(line, "=", 2) + key := strings.TrimSpace(parts[0]) + value := strings.TrimSpace(parts[1]) + envVariables[key] = value + } else { + log.Warnf("Environment variables file (.env) line won't be processed due to invalid format: %s. Accepted: KEY=value", line) + } + } + + if err := scanner.Err(); err != nil { + log.Errorf("Error reading environment variables file (.env): %v", err) + } + + if len(envVariables) > 0 { + var envVarList []corev1.EnvVar + for key, value := range envVariables { + envVar := corev1.EnvVar{ + Name: key, + Value: value, + } + envVarList = append(envVarList, envVar) + } + //serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...) + log.Info("Environment variables file (.env) content is now loaded!") + } else { + log.Warnf("Environment file (.env) is empty, Nothing to load!") + } + } + return nil + } + + err := validateEnv("./example/.env.sample") + + if err != nil { + t.Fatalf(fmt.Sprintf("Error: %v", err)) + } + +} From 90fdd2bfc90167724d465d41b1419773919744a9 Mon Sep 17 00:00:00 2001 From: spchortis Date: Tue, 26 Sep 2023 17:36:50 +0300 Subject: [PATCH 04/12] chore: adding sample env file --- example/.env.sample | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 example/.env.sample diff --git a/example/.env.sample b/example/.env.sample new file mode 100644 index 0000000..e494495 --- /dev/null +++ b/example/.env.sample @@ -0,0 +1,4 @@ +MOCK1="value1" +MOCK2="value2" +MOCK3:"value3" +MOCK4 "value4" From ee638f35f773307f593d5b08cb85b4760e34bb0b Mon Sep 17 00:00:00 2001 From: spchortis Date: Wed, 27 Sep 2023 14:08:32 +0300 Subject: [PATCH 05/12] chore: update env file steps to function & adding tests --- src/services/k8s/knative.go | 87 +++++++++++++++++--------------- src/services/k8s/knative_test.go | 70 +++---------------------- 2 files changed, 54 insertions(+), 103 deletions(-) diff --git a/src/services/k8s/knative.go b/src/services/k8s/knative.go index 1366b94..37e8fa3 100644 --- a/src/services/k8s/knative.go +++ b/src/services/k8s/knative.go @@ -74,51 +74,19 @@ func loadManifest(project *project.Project, dockerImage docker.DockerImage) (*se return service, nil } -func Apply(namespace string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage) error { - log.Info("Deploying Knative service", "host", config.Host, "name", project.Name, "namespace", namespace) - ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5) - defer cancel() - - serviceManifest, err := loadManifest(project, dockerImage) - if err != nil { - return err - } - - // Create a new Knative Serving client - servingClient, err := servingv1client.NewForConfig(config) - if err != nil { - return fmt.Errorf("Error creating the knative client %v", err) - } - - serviceManifest.ObjectMeta.Namespace = namespace - serviceManifest.ObjectMeta.Name = project.Name - - client, err := kubernetes.NewForConfig(config) - if err != nil { - return fmt.Errorf("Creating Kubernetes client %v", err) - } - - _, err = client.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ - ObjectMeta: metav1.ObjectMeta{ - Name: serviceManifest.ObjectMeta.Namespace, - }, - }, metav1.CreateOptions{}) - - if err != nil && !apimachineryErrors.IsAlreadyExists(err) { - return fmt.Errorf("cannot create namespace %s, failed with %v", serviceManifest.ObjectMeta.Namespace, err) - } - - if _, err := os.Stat(".env"); err != nil { +func loadEnvFile(envFile string) ([]corev1.EnvVar, error) { + var envVarList []corev1.EnvVar + if _, err := os.Stat(envFile); err != nil { if os.IsNotExist(err) { log.Info("No environment variables file (.env) to Load!") } else { - log.Fatalf("Error checking .env file: %v", err) + return nil, fmt.Errorf("Error checking .env file: %v", err) } } else { log.Info("Environment variables file (.env) found! Loading..") - file, err := os.Open(".env") + file, err := os.Open(envFile) if err != nil { - log.Fatalf("Error opening .env file: %v", err) + return nil, fmt.Errorf("Error opening .env file: %v", err) } defer file.Close() @@ -143,11 +111,10 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock } if err := scanner.Err(); err != nil { - log.Errorf("Error reading environment variables file (.env): %v", err) + return nil, fmt.Errorf("Error reading environment variables file (.env): %v", err) } if len(envVariables) > 0 { - var envVarList []corev1.EnvVar for key, value := range envVariables { envVar := corev1.EnvVar{ Name: key, @@ -155,12 +122,50 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock } envVarList = append(envVarList, envVar) } - serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...) log.Info("Environment variables file (.env) content is now loaded!") } else { log.Warnf("Environment file (.env) is empty, Nothing to load!") } } + return envVarList, nil +} + +func Apply(namespace string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage) error { + log.Info("Deploying Knative service", "host", config.Host, "name", project.Name, "namespace", namespace) + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5) + defer cancel() + + serviceManifest, err := loadManifest(project, dockerImage) + if err != nil { + return err + } + + // Create a new Knative Serving client + servingClient, err := servingv1client.NewForConfig(config) + if err != nil { + return fmt.Errorf("Error creating the knative client %v", err) + } + + serviceManifest.ObjectMeta.Namespace = namespace + serviceManifest.ObjectMeta.Name = project.Name + + client, err := kubernetes.NewForConfig(config) + if err != nil { + return fmt.Errorf("Creating Kubernetes client %v", err) + } + + _, err = client.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: serviceManifest.ObjectMeta.Namespace, + }, + }, metav1.CreateOptions{}) + + if err != nil && !apimachineryErrors.IsAlreadyExists(err) { + return fmt.Errorf("cannot create namespace %s, failed with %v", serviceManifest.ObjectMeta.Namespace, err) + } + + envVarList, err := loadEnvFile(".env") + serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...) service, err := servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{}) var deployedService *servingv1.Service diff --git a/src/services/k8s/knative_test.go b/src/services/k8s/knative_test.go index 93d24e5..9df4810 100644 --- a/src/services/k8s/knative_test.go +++ b/src/services/k8s/knative_test.go @@ -9,11 +9,6 @@ import ( "github.com/nearform/initium-cli/src/services/docker" "github.com/nearform/initium-cli/src/services/project" - - "github.com/charmbracelet/log" - "bufio" - "strings" - corev1 "k8s.io/api/core/v1" ) var root = "../../../" @@ -80,67 +75,18 @@ func TestLoadManifest(t *testing.T) { } func TestLoadEnvVars(t *testing.T) { - validateEnv := func(envFile string) error { - if _, err := os.Stat(envFile); err != nil { - if os.IsNotExist(err) { - log.Info("No environment variables file (.env) to Load!") - } else { - log.Fatalf("Error checking .env file: %v", err) - } - } else { - log.Info("Environment variables file (.env) found! Loading..") - file, err := os.Open(".env") - if err != nil { - log.Fatalf("Error opening .env file: %v", err) - } - defer file.Close() - - scanner := bufio.NewScanner(file) - envVariables := make(map[string]string) - - checkFormat := func(line string) bool { - parts := strings.SplitN(line, "=", 2) - return len(parts) == 2 - } - - for scanner.Scan() { - line := scanner.Text() - if checkFormat(line) { - parts := strings.SplitN(line, "=", 2) - key := strings.TrimSpace(parts[0]) - value := strings.TrimSpace(parts[1]) - envVariables[key] = value - } else { - log.Warnf("Environment variables file (.env) line won't be processed due to invalid format: %s. Accepted: KEY=value", line) - } - } - - if err := scanner.Err(); err != nil { - log.Errorf("Error reading environment variables file (.env): %v", err) - } - - if len(envVariables) > 0 { - var envVarList []corev1.EnvVar - for key, value := range envVariables { - envVar := corev1.EnvVar{ - Name: key, - Value: value, - } - envVarList = append(envVarList, envVar) - } - //serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...) - log.Info("Environment variables file (.env) content is now loaded!") - } else { - log.Warnf("Environment file (.env) is empty, Nothing to load!") - } - } - return nil - } - err := validateEnv("./example/.env.sample") + fmt.Println("Testing .env sample with mixed accepted & invalid lines") + _, err := loadEnvFile(path.Join(root, "example/.env.sample")) if err != nil { t.Fatalf(fmt.Sprintf("Error: %v", err)) } + fmt.Println("Testing non existing .env sample file") + _, err = loadEnvFile(path.Join(root, "example/.env.non-exist")) + + if err != nil { + t.Fatalf(fmt.Sprintf("Error: %v", err)) + } } From 5c0a305bea96c9536f484b931abcd0ecea035567 Mon Sep 17 00:00:00 2001 From: spchortis Date: Thu, 28 Sep 2023 15:23:06 +0300 Subject: [PATCH 06/12] chore: adding non root user in node dockerfile template --- assets/docker/Dockerfile.node.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/assets/docker/Dockerfile.node.tmpl b/assets/docker/Dockerfile.node.tmpl index 1b598cd..f76ea97 100644 --- a/assets/docker/Dockerfile.node.tmpl +++ b/assets/docker/Dockerfile.node.tmpl @@ -8,4 +8,5 @@ RUN npm test FROM gcr.io/distroless/nodejs20-debian11 COPY --from=build-env /src /src WORKDIR /src +USER nonroot CMD ["index.js"] From 1a8bc36ea64f2aad3f3e3f1b1671bdfdbedd79a7 Mon Sep 17 00:00:00 2001 From: spchortis Date: Fri, 29 Sep 2023 15:20:35 +0300 Subject: [PATCH 07/12] chore: adding env var file as a flag --- src/cli/deploy.go | 2 +- src/cli/flags.go | 6 ++++++ src/services/k8s/knative.go | 20 ++++++++++---------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/cli/deploy.go b/src/cli/deploy.go index 134ad05..3ec71be 100644 --- a/src/cli/deploy.go +++ b/src/cli/deploy.go @@ -20,7 +20,7 @@ func (c *icli) Deploy(cCtx *cli.Context) error { return err } - return knative.Apply(cCtx.String(namespaceFlag), config, project, c.dockerImage) + return knative.Apply(cCtx.String(namespaceFlag), config, project, c.dockerImage, cCtx.String(envVarFileFlag)) } func (c icli) DeployCMD() *cli.Command { diff --git a/src/cli/flags.go b/src/cli/flags.go index 0def54b..56ab3b7 100644 --- a/src/cli/flags.go +++ b/src/cli/flags.go @@ -44,6 +44,7 @@ const ( namespaceFlag string = "namespace" stopOnBuildFlag string = "stop-on-build" stopOnPushFlag string = "stop-on-push" + envVarFileFlag string = "env-var-file" ) type flags struct { @@ -100,6 +101,11 @@ func InitFlags() flags { Required: true, Category: "deploy", }, + &cli.StringFlag{ + Name: envVarFileFlag, + Value: ".env", + Category: "deploy", + }, }, Registry: []cli.Flag{ &cli.StringFlag{ diff --git a/src/services/k8s/knative.go b/src/services/k8s/knative.go index 37e8fa3..6994657 100644 --- a/src/services/k8s/knative.go +++ b/src/services/k8s/knative.go @@ -78,15 +78,15 @@ func loadEnvFile(envFile string) ([]corev1.EnvVar, error) { var envVarList []corev1.EnvVar if _, err := os.Stat(envFile); err != nil { if os.IsNotExist(err) { - log.Info("No environment variables file (.env) to Load!") + log.Info("No environment variables file to Load!") } else { - return nil, fmt.Errorf("Error checking .env file: %v", err) + return nil, fmt.Errorf("Error checking %v file: %v", envFile, err) } } else { - log.Info("Environment variables file (.env) found! Loading..") + log.Info(fmt.Sprintf("Environment variables file %s found! Loading..", envFile)) file, err := os.Open(envFile) if err != nil { - return nil, fmt.Errorf("Error opening .env file: %v", err) + return nil, fmt.Errorf("Error opening %v file: %v", envFile, err) } defer file.Close() @@ -106,12 +106,12 @@ func loadEnvFile(envFile string) ([]corev1.EnvVar, error) { value := strings.TrimSpace(parts[1]) envVariables[key] = value } else { - log.Warnf("Environment variables file (.env) line won't be processed due to invalid format: %s. Accepted: KEY=value", line) + log.Warnf("Environment variables file %v line won't be processed due to invalid format: %s. Accepted: KEY=value", envFile, line) } } if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("Error reading environment variables file (.env): %v", err) + return nil, fmt.Errorf("Error reading environment variables file %v: %v", envFile, err) } if len(envVariables) > 0 { @@ -122,15 +122,15 @@ func loadEnvFile(envFile string) ([]corev1.EnvVar, error) { } envVarList = append(envVarList, envVar) } - log.Info("Environment variables file (.env) content is now loaded!") + log.Info(fmt.Sprintf("Environment variables file %v content is now loaded!", envFile)) } else { - log.Warnf("Environment file (.env) is empty, Nothing to load!") + log.Warnf("Environment file %v is empty, Nothing to load!", envFile) } } return envVarList, nil } -func Apply(namespace string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage) error { +func Apply(namespace string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage, envFile string) error { log.Info("Deploying Knative service", "host", config.Host, "name", project.Name, "namespace", namespace) ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5) defer cancel() @@ -164,7 +164,7 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock return fmt.Errorf("cannot create namespace %s, failed with %v", serviceManifest.ObjectMeta.Namespace, err) } - envVarList, err := loadEnvFile(".env") + envVarList, err := loadEnvFile(envFile) serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...) service, err := servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{}) From c90ae1e1e11753c55f7a6be33db1f6e3994f4010 Mon Sep 17 00:00:00 2001 From: spchortis Date: Fri, 29 Sep 2023 17:19:08 +0300 Subject: [PATCH 08/12] chore: upd config test to support new flag --- src/cli/init_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cli/init_test.go b/src/cli/init_test.go index 35e0288..56953f5 100644 --- a/src/cli/init_test.go +++ b/src/cli/init_test.go @@ -19,6 +19,7 @@ cluster-endpoint: null container-registry: %s default-branch: main dockerfile-name: null +env-var-file: .env registry-user: null runtime-version: null `, From a3fafd59e77b2b669b1af4fcbf7452e3a103173c Mon Sep 17 00:00:00 2001 From: spchortis Date: Mon, 2 Oct 2023 16:12:11 +0300 Subject: [PATCH 09/12] chore: update env var file default --- src/cli/flags.go | 2 +- src/cli/init_test.go | 2 +- src/services/k8s/knative.go | 10 +++++++--- src/services/k8s/knative_test.go | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cli/flags.go b/src/cli/flags.go index 56ab3b7..5172eb4 100644 --- a/src/cli/flags.go +++ b/src/cli/flags.go @@ -103,7 +103,7 @@ func InitFlags() flags { }, &cli.StringFlag{ Name: envVarFileFlag, - Value: ".env", + Value: ".env.initium", Category: "deploy", }, }, diff --git a/src/cli/init_test.go b/src/cli/init_test.go index 56953f5..333c2a5 100644 --- a/src/cli/init_test.go +++ b/src/cli/init_test.go @@ -19,7 +19,7 @@ cluster-endpoint: null container-registry: %s default-branch: main dockerfile-name: null -env-var-file: .env +env-var-file: .env.initium registry-user: null runtime-version: null `, diff --git a/src/services/k8s/knative.go b/src/services/k8s/knative.go index 6994657..a39f288 100644 --- a/src/services/k8s/knative.go +++ b/src/services/k8s/knative.go @@ -77,10 +77,10 @@ func loadManifest(project *project.Project, dockerImage docker.DockerImage) (*se func loadEnvFile(envFile string) ([]corev1.EnvVar, error) { var envVarList []corev1.EnvVar if _, err := os.Stat(envFile); err != nil { - if os.IsNotExist(err) { - log.Info("No environment variables file to Load!") + if (os.IsNotExist(err)) && (path.Base(envFile) == ".env.initium") { + log.Info("No environment variables file .env.initium to Load!") } else { - return nil, fmt.Errorf("Error checking %v file: %v", envFile, err) + return nil, fmt.Errorf("Error loading %v file: %v", envFile, err) } } else { log.Info(fmt.Sprintf("Environment variables file %s found! Loading..", envFile)) @@ -165,6 +165,10 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock } envVarList, err := loadEnvFile(envFile) + if err != nil { + return err + } + serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...) service, err := servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{}) diff --git a/src/services/k8s/knative_test.go b/src/services/k8s/knative_test.go index 9df4810..13c1bd2 100644 --- a/src/services/k8s/knative_test.go +++ b/src/services/k8s/knative_test.go @@ -83,8 +83,8 @@ func TestLoadEnvVars(t *testing.T) { t.Fatalf(fmt.Sprintf("Error: %v", err)) } - fmt.Println("Testing non existing .env sample file") - _, err = loadEnvFile(path.Join(root, "example/.env.non-exist")) + fmt.Println("Testing non existing default .env.initium config file") + _, err = loadEnvFile(path.Join(root, "example/.env.initium")) if err != nil { t.Fatalf(fmt.Sprintf("Error: %v", err)) From f03a56923e255426cc84f951482e9ccb22512987 Mon Sep 17 00:00:00 2001 From: spchortis Date: Mon, 2 Oct 2023 16:13:44 +0300 Subject: [PATCH 10/12] chore: revert dockerfile node command --- assets/docker/Dockerfile.node.tmpl | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/docker/Dockerfile.node.tmpl b/assets/docker/Dockerfile.node.tmpl index f76ea97..1b598cd 100644 --- a/assets/docker/Dockerfile.node.tmpl +++ b/assets/docker/Dockerfile.node.tmpl @@ -8,5 +8,4 @@ RUN npm test FROM gcr.io/distroless/nodejs20-debian11 COPY --from=build-env /src /src WORKDIR /src -USER nonroot CMD ["index.js"] From d8a1c8d9756ae6f2c2b2ac3a8c14efe94fcf094b Mon Sep 17 00:00:00 2001 From: spchortis Date: Mon, 2 Oct 2023 22:14:05 +0300 Subject: [PATCH 11/12] chore: adding env file to defaults & moving load func to load manifest --- src/cli/flags.go | 2 +- src/services/k8s/knative.go | 21 +++++++++++---------- src/services/k8s/knative_test.go | 18 +----------------- src/utils/defaults/defaults.go | 1 + 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/cli/flags.go b/src/cli/flags.go index 5172eb4..b399cc6 100644 --- a/src/cli/flags.go +++ b/src/cli/flags.go @@ -103,7 +103,7 @@ func InitFlags() flags { }, &cli.StringFlag{ Name: envVarFileFlag, - Value: ".env.initium", + Value: defaults.EnvVarFile, Category: "deploy", }, }, diff --git a/src/services/k8s/knative.go b/src/services/k8s/knative.go index a39f288..a7b1fba 100644 --- a/src/services/k8s/knative.go +++ b/src/services/k8s/knative.go @@ -14,6 +14,7 @@ import ( "github.com/charmbracelet/log" "github.com/nearform/initium-cli/src/services/docker" "github.com/nearform/initium-cli/src/services/project" + "github.com/nearform/initium-cli/src/utils/defaults" corev1 "k8s.io/api/core/v1" apimachineryErrors "k8s.io/apimachinery/pkg/api/errors" @@ -40,7 +41,7 @@ func Config(endpoint string, token string, caCrt []byte) (*rest.Config, error) { }, nil } -func loadManifest(project *project.Project, dockerImage docker.DockerImage) (*servingv1.Service, error) { +func loadManifest(project *project.Project, dockerImage docker.DockerImage, envFile string) (*servingv1.Service, error) { knativeTemplate := path.Join("assets", "knative", "service.yaml.tmpl") template, err := template.ParseFS(project.Resources, knativeTemplate) if err != nil { @@ -71,13 +72,20 @@ func loadManifest(project *project.Project, dockerImage docker.DockerImage) (*se return nil, fmt.Errorf("decoded object is not a Knative Service: %v", obj) } + envVarList, err := loadEnvFile(envFile) + if err != nil { + return nil, err + } + + service.Spec.Template.Spec.Containers[0].Env = append(service.Spec.Template.Spec.Containers[0].Env, envVarList...) + return service, nil } func loadEnvFile(envFile string) ([]corev1.EnvVar, error) { var envVarList []corev1.EnvVar if _, err := os.Stat(envFile); err != nil { - if (os.IsNotExist(err)) && (path.Base(envFile) == ".env.initium") { + if (os.IsNotExist(err)) && (path.Base(envFile) == defaults.EnvVarFile) { log.Info("No environment variables file .env.initium to Load!") } else { return nil, fmt.Errorf("Error loading %v file: %v", envFile, err) @@ -135,7 +143,7 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5) defer cancel() - serviceManifest, err := loadManifest(project, dockerImage) + serviceManifest, err := loadManifest(project, dockerImage, envFile) if err != nil { return err } @@ -164,13 +172,6 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock return fmt.Errorf("cannot create namespace %s, failed with %v", serviceManifest.ObjectMeta.Namespace, err) } - envVarList, err := loadEnvFile(envFile) - if err != nil { - return err - } - - serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...) - service, err := servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{}) var deployedService *servingv1.Service if err != nil { diff --git a/src/services/k8s/knative_test.go b/src/services/k8s/knative_test.go index 13c1bd2..5d363ab 100644 --- a/src/services/k8s/knative_test.go +++ b/src/services/k8s/knative_test.go @@ -66,7 +66,7 @@ func TestLoadManifest(t *testing.T) { Tag: "v1.1.0", } - _, err := loadManifest(proj_knative, docker_image) + _, err := loadManifest(proj_knative, docker_image, path.Join(root, "example/.env.sample")) if err != nil { t.Fatalf(fmt.Sprintf("Error: %v", err)) @@ -74,19 +74,3 @@ func TestLoadManifest(t *testing.T) { } -func TestLoadEnvVars(t *testing.T) { - - fmt.Println("Testing .env sample with mixed accepted & invalid lines") - _, err := loadEnvFile(path.Join(root, "example/.env.sample")) - - if err != nil { - t.Fatalf(fmt.Sprintf("Error: %v", err)) - } - - fmt.Println("Testing non existing default .env.initium config file") - _, err = loadEnvFile(path.Join(root, "example/.env.initium")) - - if err != nil { - t.Fatalf(fmt.Sprintf("Error: %v", err)) - } -} diff --git a/src/utils/defaults/defaults.go b/src/utils/defaults/defaults.go index bc4ae9b..78c6728 100644 --- a/src/utils/defaults/defaults.go +++ b/src/utils/defaults/defaults.go @@ -8,6 +8,7 @@ const ( ConfigFile string = ".initium.yaml" AppVersion string = "latest" GeneratedDockerFile string = "Dockerfile.initium" + EnvVarFile string = ".env.initium" ) // renovate: datasource=docker depName=node From 2f5a7b0eb45cc07c0843301d8e0ebb6f1534b245 Mon Sep 17 00:00:00 2001 From: spchortis Date: Tue, 3 Oct 2023 09:20:35 +0300 Subject: [PATCH 12/12] chore: switching to log.infof --- src/services/k8s/knative.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/k8s/knative.go b/src/services/k8s/knative.go index 2b10382..2056850 100644 --- a/src/services/k8s/knative.go +++ b/src/services/k8s/knative.go @@ -98,12 +98,12 @@ func loadEnvFile(envFile string) ([]corev1.EnvVar, error) { var envVarList []corev1.EnvVar if _, err := os.Stat(envFile); err != nil { if (os.IsNotExist(err)) && (path.Base(envFile) == defaults.EnvVarFile) { - log.Info("No environment variables file .env.initium to Load!") + log.Infof("No environment variables file %s to Load!", defaults.EnvVarFile) } else { return nil, fmt.Errorf("Error loading %v file: %v", envFile, err) } } else { - log.Info(fmt.Sprintf("Environment variables file %s found! Loading..", envFile)) + log.Infof("Environment variables file %s found! Loading..", envFile) file, err := os.Open(envFile) if err != nil { return nil, fmt.Errorf("Error opening %v file: %v", envFile, err) @@ -142,7 +142,7 @@ func loadEnvFile(envFile string) ([]corev1.EnvVar, error) { } envVarList = append(envVarList, envVar) } - log.Info(fmt.Sprintf("Environment variables file %v content is now loaded!", envFile)) + log.Infof("Environment variables file %v content is now loaded!", envFile) } else { log.Warnf("Environment file %v is empty, Nothing to load!", envFile) }