From b6cb36ff3e32eafd46981179244972c73dd2c15e Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 19 Apr 2024 15:38:23 +0200 Subject: [PATCH] Put setupWorkdir in critical section --- deploy/sthome-webhook/Chart.yaml | 4 +-- deploy/sthome-webhook/values.yaml | 2 +- pkg/dns/shell.go | 2 +- pkg/dns/utils.go | 53 +++++++++++++++++++------------ version.txt | 6 ++-- 5 files changed, 39 insertions(+), 28 deletions(-) diff --git a/deploy/sthome-webhook/Chart.yaml b/deploy/sthome-webhook/Chart.yaml index 730ece9..2cb7f59 100644 --- a/deploy/sthome-webhook/Chart.yaml +++ b/deploy/sthome-webhook/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v1 -appVersion: v0.0.5-alpha.106 +appVersion: v0.0.5-alpha.109 description: Cert-Manager webhook for sthome name: sthome-webhook -version: 0.0.5-alpha.106 +version: 0.0.5-alpha.109 diff --git a/deploy/sthome-webhook/values.yaml b/deploy/sthome-webhook/values.yaml index 37f24de..738307c 100644 --- a/deploy/sthome-webhook/values.yaml +++ b/deploy/sthome-webhook/values.yaml @@ -31,7 +31,7 @@ clusterIssuer: image: repository: stuurmcp/cert-manager-webhook-sthome #repository: wstat.sthome.net:5000/cert-manager-webhook-sthome - tag: 0.0.5-alpha.106 + tag: 0.0.5-alpha.109 #pullPolicy should be IfNotPresent. Set to Always for testing purposes pullPolicy: IfNotPresent diff --git a/pkg/dns/shell.go b/pkg/dns/shell.go index 3aa365e..a3bbc27 100644 --- a/pkg/dns/shell.go +++ b/pkg/dns/shell.go @@ -70,7 +70,7 @@ func Execute(shell string, arg ...string) (bool, error) { //fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr) errb := bytes.TrimSuffix(stderr.Bytes(), crlf) errb = bytes.TrimSuffix(errb, lf) - if stderr != nil { + if errb != nil { klog.Infof("err:\n%s\n", string(errb)) } return true, nil diff --git a/pkg/dns/utils.go b/pkg/dns/utils.go index 2aed93d..187e8d6 100644 --- a/pkg/dns/utils.go +++ b/pkg/dns/utils.go @@ -36,22 +36,15 @@ func LoadConfig(cfgJSON *extapi.JSON) (LocalDNSProviderConfig, error) { } func setupWorkdir() { - if _, err := os.Stat(Workdir); os.IsNotExist(err) { - klog.Infof("Folder \"%s\" does not exist!. Creating it.", Workdir) - err = os.Mkdir(Workdir, 0755) - if err != nil { - panic(err) - } + err := createWorkdir() + if err != nil { + panic(err) } - copySrcDestDirFile(AcmeDir, Workdir, AuthScript, AuthScriptMode) - copySrcDestDirFile(AcmeDir, Workdir, DnsUpdScript, DnsUpdScriptMode) - copySrcDestDirFile(AcmeDir, Workdir, KrbConf, KrbConfMode) - copySrcDestDirFile(AcmeDir, Workdir, Keytab, KeytabMode) - AcmeAuthCmd = Workdir + "/" + AuthScript + updateWorkdir() } func updateWorkdir() { - wg.Add(4) // Add a count of two, one for each goroutine. + wg.Add(4) // Add a count, one for each goroutine. go updateIfStale(AuthScript, AuthScriptMode) go updateIfStale(DnsUpdScript, DnsUpdScriptMode) @@ -61,6 +54,22 @@ func updateWorkdir() { wg.Wait() // Wait for the goroutines to finish. } +func createWorkdir() error { + defer mutex.Unlock() + mutex.Lock() + finfo, err := os.Stat(Workdir) + if os.IsNotExist(err) { + klog.Infof("Creating folder \"%s\".", Workdir) + err = os.Mkdir(Workdir, 0755) + if err != nil { + panic(err) + } + } else if !finfo.IsDir() { + return fmt.Errorf("\"%s\" exists, but it's not a folder", Workdir) + } + return nil +} + func updateIfStale(filename string, mode os.FileMode) error { defer wg.Done() // Schedule the call to Done to tell main we are done. mutex.Lock() @@ -68,12 +77,15 @@ func updateIfStale(filename string, mode os.FileMode) error { destFile := Workdir + "/" + filename result, err := cmpModTime(sourceFile, destFile) if result > 0 { - klog.Infof("Updating \"%s\" from \"%s\" folder.", destFile, AcmeDir) err = CopyFile(sourceFile, destFile, mode) + if err == nil { + klog.Infof("Updated \"%s\" from \"%s\" folder.", destFile, AcmeDir) + } } mutex.Unlock() return err } + func cmpModTime(file1 string, file2 string) (int, error) { // Get the fileinfo fileInfo, err := os.Stat(file1) @@ -82,6 +94,11 @@ func cmpModTime(file1 string, file2 string) (int, error) { } modtime1 := fileInfo.ModTime() fileInfo, err = os.Stat(file2) + // if file2 does not exist, return as if file1.modtime > file2.modtime + if os.IsNotExist(err) { + return 1, nil + } + // for any other error, return fatal err if err != nil { klog.Fatal(err) } @@ -96,12 +113,6 @@ func cmpModTime(file1 string, file2 string) (int, error) { return 0, nil } -func copySrcDestDirFile(sourcedir string, destdir string, filename string, mode os.FileMode) error { - sourceFile := sourcedir + "/" + filename - destFile := destdir + "/" + filename - return CopyFile(sourceFile, destFile, mode) -} - func CopyFile(sourceFile string, destFile string, mode os.FileMode) error { source, err := os.Open(sourceFile) //open the source file if err != nil { @@ -119,8 +130,8 @@ func CopyFile(sourceFile string, destFile string, mode os.FileMode) error { panic(err) } err = os.Chmod(destFile, mode) - klog.Infof("Copied %s to %s.", sourceFile, destFile) - return nil + //klog.Infof("Copied %s to %s.", sourceFile, destFile) + return err } /* diff --git a/version.txt b/version.txt index dd2a000..924b241 100644 --- a/version.txt +++ b/version.txt @@ -1,3 +1,3 @@ -0.0.5-alpha.106 -20240418-1937 -106 \ No newline at end of file +0.0.5-alpha.109 +20240419-1520 +109 \ No newline at end of file