Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor config writing #419

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 24 additions & 29 deletions internal/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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 {
Expand All @@ -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
}