Skip to content

Commit

Permalink
fix: race condition when applying namespace quotas
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Davim <[email protected]>
  • Loading branch information
Luis Davim committed Mar 13, 2021
1 parent 2322933 commit 37e4015
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions internal/app/kube_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"path"
"os"
"strings"
"sync"

Expand Down Expand Up @@ -119,19 +119,10 @@ spec:
}

definition = definition + Indent(string(d), strings.Repeat(" ", 4))
targetFile := path.Join(createTempDir(tempFilesDir, "tmp"), "temp-LimitRange.yaml")
if err := ioutil.WriteFile(targetFile, []byte(definition), 0o666); err != nil {
log.Fatal(err.Error())
}

cmd := kubectl([]string{"apply", "-f", targetFile, "-n", ns, flags.getKubeDryRunFlag("apply")}, "Creating LimitRange in namespace [ "+ns+" ]")
result := cmd.Exec()

if result.code != 0 {
log.Fatal("Failed to create LimitRange in namespace [ " + ns + " ] with error: " + result.errors)
if err := apply(definition, ns, "LimitRange"); err != nil {
log.Fatal(err.Error())
}

deleteFile(targetFile)
}

func setQuotas(ns string, quotas *quotas) {
Expand Down Expand Up @@ -163,18 +154,31 @@ spec:

definition = definition + Indent(string(d), strings.Repeat(" ", 4))

if err := ioutil.WriteFile("temp-ResourceQuota.yaml", []byte(definition), 0o666); err != nil {
if err := apply(definition, ns, "ResourceQuota"); err != nil {
log.Fatal(err.Error())
}
}

cmd := kubectl([]string{"apply", "-f", "temp-ResourceQuota.yaml", "-n", ns, flags.getKubeDryRunFlag("apply")}, "Creating ResourceQuota in namespace [ "+ns+" ]")
result := cmd.Exec()
func apply(definition, ns, kind string) error {
targetFile, err := ioutil.TempFile(tempFilesDir, kind+"-*.yaml")
if err != nil {
return err
}
defer os.Remove(targetFile.Name())

deleteFile("temp-ResourceQuota.yaml")
if _, err = targetFile.Write([]byte(definition)); err != nil {
return err
}

cmd := kubectl([]string{"apply", "-f", targetFile.Name(), "-n", ns, flags.getKubeDryRunFlag("apply")},
"Creating "+kind+" in namespace [ "+ns+" ]")
result := cmd.Exec()

if result.code != 0 {
log.Fatal("ERROR: failed to create ResourceQuota in namespace [ " + ns + " ]: " + result.errors)
return fmt.Errorf("ERROR: failed to create %s in namespace [ %s ]: %s", kind, ns, result.errors)
}

return nil
}

// createContext creates a context -connecting to a k8s cluster- in kubectl config.
Expand Down

0 comments on commit 37e4015

Please sign in to comment.