feat: add configuration management and update deployment files

This commit is contained in:
2026-01-10 19:56:51 +08:00
parent 6730ceafa6
commit d1a5240c34
10 changed files with 180 additions and 21 deletions

3
.gitignore vendored
View File

@@ -1,4 +1,5 @@
*.exe *.exe
*.prof *.prof
certs/* certs/*
helloapp helloapp
.*-config.json

View File

@@ -0,0 +1,8 @@
{
"server": {
"port": 8800
},
"certs": {
"testKeyPath": "./certs/test.key"
}
}

17
.vscode/launch.json vendored Normal file
View File

@@ -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"]
}
]
}

View File

@@ -1,5 +1,3 @@
FROM scratch FROM scratch
COPY helloapp /helloapp
COPY helloapp helloapp CMD ["/helloapp"]
CMD ["helloapp"]

39
cmn/conf/conf.go Normal file
View File

@@ -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))
}

View File

@@ -2,18 +2,20 @@ apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: k8s-example name: k8s-example
namespace: helloapp
spec: spec:
selector: selector:
app: k8s-example app: k8s-example
ports: ports:
- port: 8800 - port: 8800
targetPort: 8800 targetPort: 8800
type: LoadBalancer type: LoadBalancer
--- ---
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: k8s-example name: k8s-example
namespace: helloapp
spec: spec:
selector: selector:
matchLabels: matchLabels:
@@ -25,15 +27,45 @@ spec:
app: k8s-example app: k8s-example
spec: spec:
containers: containers:
- name: k8s-example - name: k8s-example
image: localhost:8180/k8s-example/k8s-example:latest image: localhost:8180/k8s-example/k8s-example:latest
imagePullPolicy: IfNotPresent imagePullPolicy: IfNotPresent
resources: resources:
requests: requests:
memory: "128Mi" memory: "128Mi"
cpu: "500m" cpu: "500m"
limits: limits:
memory: "128Mi" memory: "128Mi"
cpu: "500m" cpu: "500m"
ports: ports:
- containerPort: 8800 - 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"
}
}

17
go.mod
View File

@@ -1,3 +1,20 @@
module k8s-example module k8s-example
go 1.25.5 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
)

27
go.sum Normal file
View File

@@ -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=

View File

@@ -1,11 +1,19 @@
package hello package hello
import ( import (
"encoding/base64"
"fmt"
"k8s-example/cmn/utils" "k8s-example/cmn/utils"
"net/http" "net/http"
"github.com/spf13/viper"
) )
func Handler(w http.ResponseWriter, _ *http.Request) { func Handler(w http.ResponseWriter, _ *http.Request) {
utils.Fnc() utils.Fnc()
_, _ = w.Write([]byte("Hello, World!")) _, _ = w.Write([]byte("Hello, World!"))
encodedTestKey := base64.StdEncoding.EncodeToString([]byte(viper.GetString("test.key")))
fmt.Println(encodedTestKey)
} }

16
main.go
View File

@@ -1,9 +1,13 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"k8s-example/cmn/conf"
"k8s-example/hello" "k8s-example/hello"
"net/http" "net/http"
"github.com/spf13/viper"
) )
var ( var (
@@ -11,10 +15,18 @@ var (
) )
func main() { 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) http.HandleFunc("/hello", hello.Handler)
fmt.Println("server on :8800, version:", version) port := viper.GetInt("server.port")
err := http.ListenAndServe(":8800", nil)
fmt.Println("server on :", port, "version:", version)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }