feat: add configuration management and update deployment files
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
*.exe
|
||||
*.prof
|
||||
certs/*
|
||||
helloapp
|
||||
helloapp
|
||||
.*-config.json
|
||||
8
.linux-config-template.json
Normal file
8
.linux-config-template.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"server": {
|
||||
"port": 8800
|
||||
},
|
||||
"certs": {
|
||||
"testKeyPath": "./certs/test.key"
|
||||
}
|
||||
}
|
||||
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal 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"]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
FROM scratch
|
||||
|
||||
COPY helloapp helloapp
|
||||
|
||||
CMD ["helloapp"]
|
||||
COPY helloapp /helloapp
|
||||
CMD ["/helloapp"]
|
||||
|
||||
39
cmn/conf/conf.go
Normal file
39
cmn/conf/conf.go
Normal 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))
|
||||
|
||||
}
|
||||
@@ -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
|
||||
- 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"
|
||||
}
|
||||
}
|
||||
|
||||
17
go.mod
17
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
|
||||
)
|
||||
|
||||
27
go.sum
Normal file
27
go.sum
Normal 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=
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
16
main.go
16
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user