Skip to content

Commit

Permalink
feat: add config file
Browse files Browse the repository at this point in the history
  • Loading branch information
j4m3s-s committed Jan 5, 2022
1 parent 802e71c commit e7e35c4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
7 changes: 6 additions & 1 deletion chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ replicaCount: 2
image:
repository: registry.example.com/toto/k8s-proxy-image-swapper
pullPolicy: IfNotPresent
tag: "v0.2.0"
tag: "v0.3.0"

config: |
port: 8443
tlskeypath: "/tls/key.pem"
tlscertpath: "/tls/cert.pem"
securityContext:
#capabilities:
Expand Down
3 changes: 3 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
port: 8443
tlskeypath: "/tls/key.pem"
tlscertpath: "/tls/cert.pem"
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
CGO_ENABLED = "0";

pname = "k8s-proxy-image-swapper";
version = "0.2.0";
version = "0.3.0";

src = ./.;

Expand Down
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"time"
"gopkg.in/yaml.v2"
)

func handleRoot(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -48,7 +49,32 @@ func handleMutation(w http.ResponseWriter, r *http.Request) {
}
}

type Config struct {
TLSCertPath string `yaml:"tlscertpath"`
TLSKeyPath string `yaml:"tlskeypath"`
Port string `yaml:"port"`
}

func main() {
if len(os.Args) == 1 {
fmt.Errorf("Usage : %v $CONFIG_FILE_PATH", os.Args[0])
return
}
configFile, err := os.Open(os.Args[1])
if err != nil {
fmt.Errorf("Error opening %v : %v", os.Args[1], err)
return
}
defer configFile.Close()

var config Config
decoder := yaml.NewDecoder(configFile)
err = decoder.Decode(&config)
if err != nil {
fmt.Errorf("Error reading config : %v", err)
return
}

log.Println("Starting server ...")

mux := http.NewServeMux()
Expand All @@ -57,12 +83,12 @@ func main() {
mux.HandleFunc("/mutate", handleMutation)

s := &http.Server{
Addr: ":8443",
Addr: config.Port,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, // 1048576
MaxHeaderBytes: 1 << 20, // 1_048_576
}

log.Fatal(s.ListenAndServeTLS("/tls/cert.pem", "/tls/key.pem"))
log.Fatal(s.ListenAndServeTLS(config.TLSCertPath, config.TLSKeyPath))
}

0 comments on commit e7e35c4

Please sign in to comment.