From d1a5240c34c67430df4cdc04b9fbbd1c994da983 Mon Sep 17 00:00:00 2001 From: Zpekii Date: Sat, 10 Jan 2026 19:56:51 +0800 Subject: [PATCH] feat: add configuration management and update deployment files --- .gitignore | 3 +- .linux-config-template.json | 8 +++++ .vscode/launch.json | 17 +++++++++++ Dockerfile | 6 ++-- cmn/conf/conf.go | 39 ++++++++++++++++++++++++ deployment.yaml | 60 ++++++++++++++++++++++++++++--------- go.mod | 17 +++++++++++ go.sum | 27 +++++++++++++++++ hello/hello.go | 8 +++++ main.go | 16 ++++++++-- 10 files changed, 180 insertions(+), 21 deletions(-) create mode 100644 .linux-config-template.json create mode 100644 .vscode/launch.json create mode 100644 cmn/conf/conf.go create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index 5dc5727..7b08788 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.exe *.prof certs/* -helloapp \ No newline at end of file +helloapp +.*-config.json \ No newline at end of file diff --git a/.linux-config-template.json b/.linux-config-template.json new file mode 100644 index 0000000..70c0730 --- /dev/null +++ b/.linux-config-template.json @@ -0,0 +1,8 @@ +{ + "server": { + "port": 8800 + }, + "certs": { + "testKeyPath": "./certs/test.key" + } +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..f706a87 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}", + "args": ["-f", ".linux-config.json"] + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index aa842d8..0fccd71 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,3 @@ FROM scratch - -COPY helloapp helloapp - -CMD ["helloapp"] \ No newline at end of file +COPY helloapp /helloapp +CMD ["/helloapp"] diff --git a/cmn/conf/conf.go b/cmn/conf/conf.go new file mode 100644 index 0000000..a5e37e2 --- /dev/null +++ b/cmn/conf/conf.go @@ -0,0 +1,39 @@ +package conf + +import ( + "os" + + "github.com/spf13/viper" +) + +func Init(file string) { + viper.AddConfigPath(".") + viper.AddConfigPath("..") + viper.SetConfigFile(file) + err := viper.ReadInConfig() + if err != nil { + panic(err) + } + + // read test.key file + var keyFile *os.File + keyFile, err = os.Open(viper.GetString("certs.testKeyPath")) + if err != nil { + panic(err) + } + defer keyFile.Close() + + var fileInfo os.FileInfo + fileInfo, err = keyFile.Stat() + if err != nil { + panic(err) + } + keyData := make([]byte, fileInfo.Size()) + _, err = keyFile.Read(keyData) + if err != nil { + panic(err) + } + + viper.Set("test.key", string(keyData)) + +} \ No newline at end of file diff --git a/deployment.yaml b/deployment.yaml index c31f6b9..c6d936b 100644 --- a/deployment.yaml +++ b/deployment.yaml @@ -2,18 +2,20 @@ apiVersion: v1 kind: Service metadata: name: k8s-example + namespace: helloapp spec: selector: app: k8s-example ports: - - port: 8800 - targetPort: 8800 + - port: 8800 + targetPort: 8800 type: LoadBalancer --- apiVersion: apps/v1 kind: Deployment metadata: name: k8s-example + namespace: helloapp spec: selector: matchLabels: @@ -25,15 +27,45 @@ spec: app: k8s-example spec: containers: - - name: k8s-example - image: localhost:8180/k8s-example/k8s-example:latest - imagePullPolicy: IfNotPresent - resources: - requests: - memory: "128Mi" - cpu: "500m" - limits: - memory: "128Mi" - cpu: "500m" - ports: - - containerPort: 8800 \ No newline at end of file + - name: k8s-example + image: localhost:8180/k8s-example/k8s-example:latest + imagePullPolicy: IfNotPresent + resources: + requests: + memory: "128Mi" + cpu: "500m" + limits: + memory: "128Mi" + cpu: "500m" + ports: + - containerPort: 8800 + volumeMounts: + - name: config-volume + mountPath: /config + - name: helloapp-test-key + mountPath: /certs + command: ["/helloapp"] + args: ["-f", "/config/.linux-config.json"] + volumes: + - name: config-volume + configMap: + name: k8s-example-config + - name: helloapp-test-key + secret: + secretName: helloapp-test-key +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: k8s-example-config + namespace: helloapp +data: + .linux-config.json: | + { + "server": { + "port": 8800 + }, + "certs": { + "testKeyPath": "./certs/test.key" + } + } diff --git a/go.mod b/go.mod index 2525375..4ad11d1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,20 @@ module k8s-example go 1.25.5 + +require github.com/spf13/viper v1.21.0 + +require ( + github.com/fsnotify/fsnotify v1.9.0 // indirect + github.com/go-viper/mapstructure/v2 v2.4.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/sagikazarmark/locafero v0.11.0 // indirect + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect + github.com/spf13/afero v1.15.0 // indirect + github.com/spf13/cast v1.10.0 // indirect + github.com/spf13/pflag v1.0.10 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.yaml.in/yaml/v3 v3.0.4 // indirect + golang.org/x/sys v0.29.0 // indirect + golang.org/x/text v0.28.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..58a7da3 --- /dev/null +++ b/go.sum @@ -0,0 +1,27 @@ +github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= +github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= +github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc= +github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik= +github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= +github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U= +github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I= +github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg= +github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= +github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= +github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= +golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/hello/hello.go b/hello/hello.go index 7e24e63..d562e2b 100644 --- a/hello/hello.go +++ b/hello/hello.go @@ -1,11 +1,19 @@ package hello import ( + "encoding/base64" + "fmt" "k8s-example/cmn/utils" "net/http" + + "github.com/spf13/viper" ) func Handler(w http.ResponseWriter, _ *http.Request) { utils.Fnc() _, _ = w.Write([]byte("Hello, World!")) + + encodedTestKey := base64.StdEncoding.EncodeToString([]byte(viper.GetString("test.key"))) + + fmt.Println(encodedTestKey) } diff --git a/main.go b/main.go index 5096c7d..6a1c41a 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,13 @@ package main import ( + "flag" "fmt" + "k8s-example/cmn/conf" "k8s-example/hello" "net/http" + + "github.com/spf13/viper" ) var ( @@ -11,10 +15,18 @@ var ( ) func main() { + + // read -f parameter (config file) + configPath := flag.String("f", ".linux-config.json", "config file") + flag.Parse() + conf.Init(*configPath) + http.HandleFunc("/hello", hello.Handler) - fmt.Println("server on :8800, version:", version) - err := http.ListenAndServe(":8800", nil) + port := viper.GetInt("server.port") + + fmt.Println("server on :", port, "version:", version) + err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil) if err != nil { panic(err) }