From 8da1b3182090d1028b6f45096b9a56e6c5dbcc0b Mon Sep 17 00:00:00 2001 From: qinpengfei Date: Tue, 6 Nov 2018 20:15:02 +0800 Subject: [PATCH] refactor write config Signed-off-by: qinpengfei refactor func AddOrUpdateDHParam && add more context to log message Signed-off-by: qinpengfei remove unnecessary log Signed-off-by: qinpengfei fix log message Co-Authored-By: feifeiiiiiiiiiii fix log message Co-Authored-By: feifeiiiiiiiiiii --- internal/nginx/nginx.go | 53 +++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/internal/nginx/nginx.go b/internal/nginx/nginx.go index 239ef9ee19..7321267b78 100644 --- a/internal/nginx/nginx.go +++ b/internal/nginx/nginx.go @@ -230,15 +230,9 @@ func (nginx *Controller) DeleteIngress(name string) { func (nginx *Controller) AddOrUpdateDHParam(dhparam string) (string, error) { fileName := nginx.nginxSecretsPath + "/" + dhparamFilename if !nginx.local { - pem, err := os.Create(fileName) + err := createFileAndWrite(fileName, []byte(dhparam)) if err != nil { - return fileName, fmt.Errorf("Couldn't create file %v: %v", fileName, err) - } - defer pem.Close() - - _, err = pem.WriteString(dhparam) - if err != nil { - return fileName, fmt.Errorf("Couldn't write to pem file %v: %v", fileName, err) + return fileName, fmt.Errorf("Failed to write pem file: %v", err) } } return fileName, nil @@ -402,15 +396,10 @@ func (nginx *Controller) UpdateMainConfigFile(cfg []byte) { } if !nginx.local { - w, err := os.Create(filename) - if err != nil { - glog.Fatalf("Failed to open %v: %v", filename, err) - } - _, err = w.Write(cfg) + err := createFileAndWrite(filename, cfg) if err != nil { - glog.Fatalf("Failed to write to %v: %v", filename, err) + glog.Fatalf("Failed to write NGINX conf: %v", err) } - defer w.Close() } glog.V(3).Infof("The main NGINX config file has been updated") } @@ -425,15 +414,10 @@ func (nginx *Controller) UpdateIngressConfigFile(name string, cfg []byte) { } if !nginx.local { - w, err := os.Create(filename) + err := createFileAndWrite(filename, cfg) if err != nil { - glog.Fatalf("Failed to open %v: %v", filename, err) + glog.Fatalf("Failed to write Ingress conf: %v", err) } - _, err = w.Write(cfg) - if err != nil { - glog.Fatalf("Failed to write to %v: %v", filename, err) - } - defer w.Close() } glog.V(3).Infof("The Ingress config file has been updated") } @@ -454,15 +438,10 @@ func (nginx *Controller) UpdateConfigVersionFile() { } if !nginx.local { - w, err := os.Create(tempname) + err := createFileAndWrite(tempname, cfg) if err != nil { - glog.Fatalf("Failed to open %v: %v", filename, err) + glog.Fatalf("Failed to write version config file: %v", err) } - _, err = w.Write(cfg) - if err != nil { - glog.Fatalf("Failed to write to %v: %v", filename, err) - } - w.Close() err = os.Rename(tempname, filename) if err != nil { @@ -471,3 +450,19 @@ func (nginx *Controller) UpdateConfigVersionFile() { } glog.V(3).Infof("The config version file has been updated.") } + +func createFileAndWrite(name string, b []byte) error { + w, err := os.Create(name) + if err != nil { + return fmt.Errorf("Failed to open %v: %v", name, err) + } + + defer w.Close() + + _, err = w.Write(b) + if err != nil { + return fmt.Errorf("Failed to write to %v: %v", name, err) + } + + return nil +}