// private repo workaround // Will use this file and remove same content from main.go when github repo is made public package dns import ( "encoding/json" "fmt" "io" "os" "sync" "time" extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/klog/v2" ) var ( wg sync.WaitGroup // wg is used to wait for the program to finish. mutex sync.Mutex // mutex is used to define a critical section of code. ) // loadConfig is a small helper function that decodes JSON configuration into // the typed config struct. func LoadConfig(cfgJSON *extapi.JSON) (LocalDNSProviderConfig, error) { cfg := LocalDNSProviderConfig{} // handle the 'base case' where no configuration has been provided if cfgJSON == nil { return cfg, nil } if err := json.Unmarshal(cfgJSON.Raw, &cfg); err != nil { return cfg, fmt.Errorf("error decoding solver config: %v", err) } return cfg, nil } 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) } } copySrcDestDirFile(AcmeDir, Workdir, AuthScript, AuthScriptMode) copySrcDestDirFile(AcmeDir, Workdir, DnsUpdScript, DnsUpdScriptMode) copySrcDestDirFile(AcmeDir, Workdir, KrbConf, KrbConfMode) copySrcDestDirFile(AcmeDir, Workdir, Keytab, KeytabMode) AcmeAuthCmd = Workdir + "/" + AuthScript } func updateWorkdir() { wg.Add(4) // Add a count of two, one for each goroutine. go updateIfStale(AuthScript, AuthScriptMode) go updateIfStale(DnsUpdScript, DnsUpdScriptMode) go updateIfStale(KrbConf, KrbConfMode) go updateIfStale(Keytab, KeytabMode) wg.Wait() // Wait for the goroutines to finish. } func updateIfStale(filename string, mode os.FileMode) error { defer wg.Done() // Schedule the call to Done to tell main we are done. mutex.Lock() sourceFile := AcmeDir + "/" + filename 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) } mutex.Unlock() return err } func cmpModTime(file1 string, file2 string) (int, error) { // Get the fileinfo fileInfo, err := os.Stat(file1) if err != nil { klog.Fatal(err) } modtime1 := fileInfo.ModTime() fileInfo, err = os.Stat(file2) if err != nil { klog.Fatal(err) } modtime2 := fileInfo.ModTime() diff := modtime1.Sub(modtime2) if diff < (time.Duration(0) * time.Second) { return -1, nil } if diff > (time.Duration(0) * time.Second) { return 1, 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 { source, err := os.Open(sourceFile) //open the source file if err != nil { panic(err) } defer source.Close() destination, err := os.Create(destFile) //create the destination file if err != nil { panic(err) } defer destination.Close() _, err = io.Copy(destination, source) //copy the contents of source to destination file if err != nil { panic(err) } err = os.Chmod(destFile, mode) klog.Infof("Copied %s to %s.", sourceFile, destFile) return nil } /* // quote quotes the provide value func quote(value string) string { return fmt.Sprintf("\"%s\"", value) } // findRRSet searches through te list of rrsets for a matching entry // based on type and name. func findRRSet(rrsets []sthome.RRset, rrtype sthome.RRType, name string) *sthome.RRset { for _, rrset := range rrsets { if (rrset.Type != nil && *rrset.Type == sthome.RRTypeTXT) && (rrset.Name != nil && *rrset.Name == name) { return &rrset } } return nil } // findRecord locates the record entry with the matching content. func findRecord(records []sthome.Record, content string) (int, bool) { for indx, record := range records { if record.Content != nil && *record.Content == content { return indx, true } } return -1, false } */ // end of private repo workaround