Put setupWorkdir in critical section

This commit is contained in:
Chris Stuurman 2024-04-19 15:38:23 +02:00
parent 2df7171d49
commit b6cb36ff3e
5 changed files with 39 additions and 28 deletions

View File

@ -1,5 +1,5 @@
apiVersion: v1 apiVersion: v1
appVersion: v0.0.5-alpha.106 appVersion: v0.0.5-alpha.109
description: Cert-Manager webhook for sthome description: Cert-Manager webhook for sthome
name: sthome-webhook name: sthome-webhook
version: 0.0.5-alpha.106 version: 0.0.5-alpha.109

View File

@ -31,7 +31,7 @@ clusterIssuer:
image: image:
repository: stuurmcp/cert-manager-webhook-sthome repository: stuurmcp/cert-manager-webhook-sthome
#repository: wstat.sthome.net:5000/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 should be IfNotPresent. Set to Always for testing purposes
pullPolicy: IfNotPresent pullPolicy: IfNotPresent

View File

@ -70,7 +70,7 @@ func Execute(shell string, arg ...string) (bool, error) {
//fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr) //fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)
errb := bytes.TrimSuffix(stderr.Bytes(), crlf) errb := bytes.TrimSuffix(stderr.Bytes(), crlf)
errb = bytes.TrimSuffix(errb, lf) errb = bytes.TrimSuffix(errb, lf)
if stderr != nil { if errb != nil {
klog.Infof("err:\n%s\n", string(errb)) klog.Infof("err:\n%s\n", string(errb))
} }
return true, nil return true, nil

View File

@ -36,22 +36,15 @@ func LoadConfig(cfgJSON *extapi.JSON) (LocalDNSProviderConfig, error) {
} }
func setupWorkdir() { func setupWorkdir() {
if _, err := os.Stat(Workdir); os.IsNotExist(err) { err := createWorkdir()
klog.Infof("Folder \"%s\" does not exist!. Creating it.", Workdir) if err != nil {
err = os.Mkdir(Workdir, 0755) panic(err)
if err != nil {
panic(err)
}
} }
copySrcDestDirFile(AcmeDir, Workdir, AuthScript, AuthScriptMode) updateWorkdir()
copySrcDestDirFile(AcmeDir, Workdir, DnsUpdScript, DnsUpdScriptMode)
copySrcDestDirFile(AcmeDir, Workdir, KrbConf, KrbConfMode)
copySrcDestDirFile(AcmeDir, Workdir, Keytab, KeytabMode)
AcmeAuthCmd = Workdir + "/" + AuthScript
} }
func 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(AuthScript, AuthScriptMode)
go updateIfStale(DnsUpdScript, DnsUpdScriptMode) go updateIfStale(DnsUpdScript, DnsUpdScriptMode)
@ -61,6 +54,22 @@ func updateWorkdir() {
wg.Wait() // Wait for the goroutines to finish. 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 { func updateIfStale(filename string, mode os.FileMode) error {
defer wg.Done() // Schedule the call to Done to tell main we are done. defer wg.Done() // Schedule the call to Done to tell main we are done.
mutex.Lock() mutex.Lock()
@ -68,12 +77,15 @@ func updateIfStale(filename string, mode os.FileMode) error {
destFile := Workdir + "/" + filename destFile := Workdir + "/" + filename
result, err := cmpModTime(sourceFile, destFile) result, err := cmpModTime(sourceFile, destFile)
if result > 0 { if result > 0 {
klog.Infof("Updating \"%s\" from \"%s\" folder.", destFile, AcmeDir)
err = CopyFile(sourceFile, destFile, mode) err = CopyFile(sourceFile, destFile, mode)
if err == nil {
klog.Infof("Updated \"%s\" from \"%s\" folder.", destFile, AcmeDir)
}
} }
mutex.Unlock() mutex.Unlock()
return err return err
} }
func cmpModTime(file1 string, file2 string) (int, error) { func cmpModTime(file1 string, file2 string) (int, error) {
// Get the fileinfo // Get the fileinfo
fileInfo, err := os.Stat(file1) fileInfo, err := os.Stat(file1)
@ -82,6 +94,11 @@ func cmpModTime(file1 string, file2 string) (int, error) {
} }
modtime1 := fileInfo.ModTime() modtime1 := fileInfo.ModTime()
fileInfo, err = os.Stat(file2) 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 { if err != nil {
klog.Fatal(err) klog.Fatal(err)
} }
@ -96,12 +113,6 @@ func cmpModTime(file1 string, file2 string) (int, error) {
return 0, nil 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 { func CopyFile(sourceFile string, destFile string, mode os.FileMode) error {
source, err := os.Open(sourceFile) //open the source file source, err := os.Open(sourceFile) //open the source file
if err != nil { if err != nil {
@ -119,8 +130,8 @@ func CopyFile(sourceFile string, destFile string, mode os.FileMode) error {
panic(err) panic(err)
} }
err = os.Chmod(destFile, mode) err = os.Chmod(destFile, mode)
klog.Infof("Copied %s to %s.", sourceFile, destFile) //klog.Infof("Copied %s to %s.", sourceFile, destFile)
return nil return err
} }
/* /*

View File

@ -1,3 +1,3 @@
0.0.5-alpha.106 0.0.5-alpha.109
20240418-1937 20240419-1520
106 109