@@ -18,6 +18,7 @@ package writer
18
18
19
19
import (
20
20
"errors"
21
+ "fmt"
21
22
"io/ioutil"
22
23
"os"
23
24
"path"
@@ -91,21 +92,39 @@ func (f *fsCertWriter) doWrite() (*generator.Artifacts, error) {
91
92
if err != nil {
92
93
return nil , err
93
94
}
95
+
96
+ // AtomicWriter's algorithm only manages files using symbolic link.
97
+ // If a file is not a symbolic link, will ignore the update for it.
98
+ // We want to cleanup for AtomicWriter by removing old files that are not symbolic links.
99
+ err = prepareToWrite (f .Path )
100
+ if err != nil {
101
+ return nil , err
102
+ }
103
+
94
104
aw , err := atomic .NewAtomicWriter (f .Path , log .WithName ("atomic-writer" ).
95
105
WithValues ("task" , "processing webhook" ))
96
106
if err != nil {
97
107
return nil , err
98
108
}
99
- // AtomicWriter's algorithm only manages files using symbolic link.
100
- // If a file is not a symbolic link, will ignore the update for it.
101
- // We want to cleanup for AtomicWriter by removing old files that are not symbolic links.
102
- prepareToWrite (f .Path )
103
109
err = aw .Write (certToProjectionMap (certs ))
104
110
return certs , err
105
111
}
106
112
107
113
// prepareToWrite ensures it directory is compatible with the atomic.Writer library.
108
- func prepareToWrite (dir string ) {
114
+ func prepareToWrite (dir string ) error {
115
+ _ , err := os .Stat (dir )
116
+ switch {
117
+ case os .IsNotExist (err ):
118
+ log .Info (fmt .Sprintf ("cert directory %v doesn't exist, creating" , dir ))
119
+ // TODO: figure out if we can reduce the permission. (Now it's 0777)
120
+ err = os .MkdirAll (dir , 0777 )
121
+ if err != nil {
122
+ return fmt .Errorf ("can't create dir: %v" , dir )
123
+ }
124
+ case err != nil :
125
+ return err
126
+ }
127
+
109
128
filenames := []string {CACertName , ServerCertName , ServerKeyName }
110
129
for _ , f := range filenames {
111
130
abspath := path .Join (dir , f )
@@ -124,6 +143,7 @@ func prepareToWrite(dir string) {
124
143
}
125
144
}
126
145
}
146
+ return nil
127
147
}
128
148
129
149
func (f * fsCertWriter ) read () (* generator.Artifacts , error ) {
0 commit comments