From 6734ea99b73cfd4981a0c662be203022a9a6b46b Mon Sep 17 00:00:00 2001
From: lijianguo <lijianguo1991@outlook.com>
Date: Tue, 19 Nov 2024 15:05:06 +0800
Subject: [PATCH] chore(builder): update config struct

---
 go.mod                            |  2 +-
 go.sum                            |  4 ++--
 pkg/gitreceive/build_type.go      | 17 +++++++++-------
 pkg/gitreceive/build_type_test.go | 20 +++++++++++++++----
 pkg/gitreceive/k8s_util.go        | 20 +++++++++++++------
 pkg/gitreceive/k8s_util_test.go   | 32 +++++++++++++++++++------------
 6 files changed, 63 insertions(+), 32 deletions(-)

diff --git a/go.mod b/go.mod
index a5879c0..24edae0 100644
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.23
 require (
 	github.com/aws/aws-sdk-go v1.55.5
 	github.com/distribution/distribution/v3 v3.0.0-beta.1.0.20241022144144-c427f845035d
-	github.com/drycc/controller-sdk-go v0.0.0-20241028091950-441681c47292
+	github.com/drycc/controller-sdk-go v0.0.0-20241122013249-f1d6fea5bf81
 	github.com/drycc/pkg v0.0.0-20241028091700-8caca001c932
 	github.com/google/uuid v1.6.0
 	github.com/kelseyhightower/envconfig v1.4.0
diff --git a/go.sum b/go.sum
index 4ead960..50fdb61 100644
--- a/go.sum
+++ b/go.sum
@@ -21,8 +21,8 @@ github.com/distribution/distribution/v3 v3.0.0-beta.1.0.20241022144144-c427f8450
 github.com/distribution/distribution/v3 v3.0.0-beta.1.0.20241022144144-c427f845035d/go.mod h1:Unn8+BXBntRw4BZHI7UVY9wJ7yGW/xtOspXPxo6hFIs=
 github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQV8=
 github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw=
-github.com/drycc/controller-sdk-go v0.0.0-20241028091950-441681c47292 h1:CLpq3s2NxjJs2evDvDqbTwUCDuGyEEjk6bIjn+g5H+M=
-github.com/drycc/controller-sdk-go v0.0.0-20241028091950-441681c47292/go.mod h1:F9ViuoVcZ/OG1WnfZBYH6j4v6mb5B+RW9FLaCopADoo=
+github.com/drycc/controller-sdk-go v0.0.0-20241122013249-f1d6fea5bf81 h1:y809MASIviVNxzUxP1CDU2EaB5AruRNAiFKjOMCx+0s=
+github.com/drycc/controller-sdk-go v0.0.0-20241122013249-f1d6fea5bf81/go.mod h1:F9ViuoVcZ/OG1WnfZBYH6j4v6mb5B+RW9FLaCopADoo=
 github.com/drycc/pkg v0.0.0-20241028091700-8caca001c932 h1:tUfkfjmFmlm9n0dYp1NoHcFK6+APD/wHZ2We6CKXDSI=
 github.com/drycc/pkg v0.0.0-20241028091700-8caca001c932/go.mod h1:upGcZF/RCEqidGjSnWHh/txRC1I4GPsRGvF8/+bDfwU=
 github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
diff --git a/pkg/gitreceive/build_type.go b/pkg/gitreceive/build_type.go
index 4ef6640..1c19dc3 100644
--- a/pkg/gitreceive/build_type.go
+++ b/pkg/gitreceive/build_type.go
@@ -41,13 +41,16 @@ func getStack(dirName string, config api.Config) map[string]string {
 	}
 	log.Debug("Stacks: %s", Stacks)
 	log.Debug("Config values %s", config.Values)
-	if stackInterface, ok := config.Values["DRYCC_STACK"]; ok {
-		if strStack, ok := stackInterface.(string); ok {
-			for _, stack := range Stacks {
-				if stack["name"] == strStack {
-					return stack
-				}
-			}
+	strStack := ""
+	for _, v := range config.Values {
+		if v.Group == "global" && v.Name == "DRYCC_STACK" {
+			strStack = v.Value.(string)
+		}
+	}
+
+	for _, stack := range Stacks {
+		if stack["name"] == strStack {
+			return stack
 		}
 	}
 
diff --git a/pkg/gitreceive/build_type_test.go b/pkg/gitreceive/build_type_test.go
index 515bf87..ae6e318 100644
--- a/pkg/gitreceive/build_type_test.go
+++ b/pkg/gitreceive/build_type_test.go
@@ -35,16 +35,28 @@ func TestGetStack(t *testing.T) {
 			t.Fatalf("failed to remove Procfile from %s (%s)", tmpDir, err)
 		}
 	}()
-	config.Values = map[string]interface{}{
-		"DRYCC_STACK": "buildpack",
+	config.Values = []api.ConfigValue{
+		{
+			Group: "global",
+			KV: api.KV{
+				Name:  "DRYCC_STACK",
+				Value: "buildpack",
+			},
+		},
 	}
 	stack = getStack(tmpDir, config)
 	if stack["name"] != "buildpack" {
 		t.Fatalf("expected procfile build, got %s", stack)
 	}
 
-	config.Values = map[string]interface{}{
-		"DRYCC_STACK": "container",
+	config.Values = []api.ConfigValue{
+		{
+			Group: "global",
+			KV: api.KV{
+				Name:  "DRYCC_STACK",
+				Value: "container",
+			},
+		},
 	}
 	stack = getStack(tmpDir, config)
 	if stack["name"] != "container" {
diff --git a/pkg/gitreceive/k8s_util.go b/pkg/gitreceive/k8s_util.go
index 7ca3dff..6d951aa 100644
--- a/pkg/gitreceive/k8s_util.go
+++ b/pkg/gitreceive/k8s_util.go
@@ -8,6 +8,7 @@ import (
 	"time"
 
 	"github.com/drycc/builder/pkg/k8s"
+	"github.com/drycc/controller-sdk-go/api"
 	"github.com/google/uuid"
 	batchv1 "k8s.io/api/batch/v1"
 	corev1 "k8s.io/api/core/v1"
@@ -40,7 +41,7 @@ func createBuilderJob(
 	debug bool,
 	name,
 	namespace string,
-	env map[string]interface{},
+	config []api.ConfigValue,
 	tarKey,
 	gitShortHash string,
 	imageName,
@@ -52,7 +53,7 @@ func createBuilderJob(
 	nodeSelector map[string]string,
 ) *batchv1.Job {
 
-	job := buildJob(debug, name, namespace, builderName, pullPolicy, securityContext, nodeSelector, env)
+	job := buildJob(debug, name, namespace, builderName, pullPolicy, securityContext, nodeSelector, config)
 	job.Spec.Template.Spec.Containers[0].Name = builderName
 	job.Spec.Template.Spec.Containers[0].Image = builderImage
 
@@ -79,7 +80,7 @@ func buildJob(
 	pullPolicy corev1.PullPolicy,
 	securityContext corev1.SecurityContext,
 	nodeSelector map[string]string,
-	env map[string]interface{}) batchv1.Job {
+	values []api.ConfigValue) batchv1.Job {
 	TTLSecondsAfterFinished := newInt32(21600)
 	if os.Getenv("TTL_SECONDS_AFTER_FINISHED") != "" {
 		ttl, err := strconv.ParseInt(os.Getenv("TTL_SECONDS_AFTER_FINISHED"), 10, 32)
@@ -142,10 +143,17 @@ func buildJob(
 	})
 
 	if len(job.Spec.Template.Spec.Containers) > 0 {
-		for k, v := range env {
+		kvs := []api.KV{}
+		// only take the value of the global group
+		for _, v := range values {
+			if v.Group == "global" {
+				kvs = append(kvs, v.KV)
+			}
+		}
+		for _, v := range kvs {
 			job.Spec.Template.Spec.Containers[0].Env = append(job.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{
-				Name:  k,
-				Value: fmt.Sprintf("%v", v),
+				Name:  v.Name,
+				Value: fmt.Sprintf("%v", v.Value),
 			})
 		}
 	}
diff --git a/pkg/gitreceive/k8s_util_test.go b/pkg/gitreceive/k8s_util_test.go
index b2741c8..ab5b879 100644
--- a/pkg/gitreceive/k8s_util_test.go
+++ b/pkg/gitreceive/k8s_util_test.go
@@ -7,6 +7,7 @@ import (
 	"testing"
 
 	"github.com/drycc/builder/pkg/k8s"
+	"github.com/drycc/controller-sdk-go/api"
 	"github.com/stretchr/testify/assert"
 	batchv1 "k8s.io/api/batch/v1"
 	corev1 "k8s.io/api/core/v1"
@@ -32,7 +33,7 @@ type imageBuildCase struct {
 	debug                       bool
 	name                        string
 	namespace                   string
-	env                         map[string]interface{}
+	env                         []api.ConfigValue
 	tarKey                      string
 	gitShortHash                string
 	imgName                     string
@@ -43,10 +44,17 @@ type imageBuildCase struct {
 }
 
 func TestBuildJob(t *testing.T) {
-	emptyEnv := make(map[string]interface{})
-
-	env := make(map[string]interface{})
-	env["KEY"] = "VALUE"
+	emptyValues := []api.ConfigValue{}
+
+	values := []api.ConfigValue{
+		{
+			Group: "global",
+			KV: api.KV{
+				Name:  "KEY",
+				Value: "VALUE",
+			},
+		},
+	}
 	var job *batchv1.Job
 
 	emptyNodeSelector := make(map[string]string)
@@ -59,13 +67,13 @@ func TestBuildJob(t *testing.T) {
 	nodeSelector2["network"] = "fast"
 
 	imageBuilds := []imageBuildCase{
-		{true, "test", "default", emptyEnv, "tar", "deadbeef", "imagebuilder", "", "", corev1.PullAlways, nodeSelector1},
-		{true, "test", "default", env, "tar", "deadbeef", "", "imagebuilder", "", corev1.PullAlways, nodeSelector2},
-		{true, "test", "default", emptyEnv, "tar", "deadbeef", "img", "imagebuilder", "", corev1.PullAlways, emptyNodeSelector},
-		{true, "test", "default", env, "tar", "deadbeef", "img", "imagebuilder", "", corev1.PullAlways, emptyNodeSelector},
-		{true, "test", "default", env, "tar", "deadbeef", "img", "imagebuilder", "customimage", corev1.PullAlways, emptyNodeSelector},
-		{true, "test", "default", env, "tar", "deadbeef", "img", "imagebuilder", "customimage", corev1.PullIfNotPresent, emptyNodeSelector},
-		{true, "test", "default", env, "tar", "deadbeef", "img", "imagebuilder", "customimage", corev1.PullNever, nil},
+		{true, "test", "default", emptyValues, "tar", "deadbeef", "imagebuilder", "", "", corev1.PullAlways, nodeSelector1},
+		{true, "test", "default", values, "tar", "deadbeef", "", "imagebuilder", "", corev1.PullAlways, nodeSelector2},
+		{true, "test", "default", emptyValues, "tar", "deadbeef", "img", "imagebuilder", "", corev1.PullAlways, emptyNodeSelector},
+		{true, "test", "default", values, "tar", "deadbeef", "img", "imagebuilder", "", corev1.PullAlways, emptyNodeSelector},
+		{true, "test", "default", values, "tar", "deadbeef", "img", "imagebuilder", "customimage", corev1.PullAlways, emptyNodeSelector},
+		{true, "test", "default", values, "tar", "deadbeef", "img", "imagebuilder", "customimage", corev1.PullIfNotPresent, emptyNodeSelector},
+		{true, "test", "default", values, "tar", "deadbeef", "img", "imagebuilder", "customimage", corev1.PullNever, nil},
 	}
 	buildImageEnv := map[string]string{"DRYCC_REGISTRY_LOCATION": "on-cluster"}
 	for _, build := range imageBuilds {
