Skip to content

Commit 5c2ed6a

Browse files
committed
Rename WriteFiles to SyncDirectory
1 parent fbba5e5 commit 5c2ed6a

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

pkg/operator/staticpod/certsyncpod/certsync_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (c *CertSyncController) sync(ctx context.Context, syncCtx factory.SyncConte
152152
continue
153153
}
154154

155-
errors = append(errors, writeFiles(c.eventRecorder, "configmap", configMap.ObjectMeta, contentDir, data, 0644))
155+
errors = append(errors, syncDirectory(c.eventRecorder, "configmap", configMap.ObjectMeta, contentDir, data, 0644))
156156
}
157157

158158
klog.Infof("Syncing secrets: %v", c.secrets)
@@ -237,17 +237,17 @@ func (c *CertSyncController) sync(ctx context.Context, syncCtx factory.SyncConte
237237
continue
238238
}
239239

240-
errors = append(errors, writeFiles(c.eventRecorder, "secret", secret.ObjectMeta, contentDir, data, 0600))
240+
errors = append(errors, syncDirectory(c.eventRecorder, "secret", secret.ObjectMeta, contentDir, data, 0600))
241241
}
242242
return utilerrors.NewAggregate(errors)
243243
}
244244

245-
func writeFiles[C string | []byte](
245+
func syncDirectory[C string | []byte](
246246
eventRecorder events.Recorder,
247247
typeName string, o metav1.ObjectMeta,
248248
targetDir string, files map[string]C, filePerm os.FileMode,
249249
) error {
250-
if err := atomicfiles.WriteFiles(typeName, o, targetDir, files, filePerm); err != nil {
250+
if err := atomicfiles.SyncDirectory(typeName, o, targetDir, files, filePerm); err != nil {
251251
eventRecorder.Warning("CertificateUpdateFailed", err.Error())
252252
return err
253253
}

pkg/operator/staticpod/installerpod/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (o *InstallOptions) copySecretsAndConfigMaps(ctx context.Context, resourceD
257257
secretBaseName = o.prefixFor(secret.Name)
258258
}
259259
contentDir := path.Join(resourceDir, "secrets", secretBaseName)
260-
if err := atomicfiles.WriteFiles("secret", secret.ObjectMeta, contentDir, secret.Data, 0600); err != nil {
260+
if err := atomicfiles.SyncDirectory("secret", secret.ObjectMeta, contentDir, secret.Data, 0600); err != nil {
261261
return err
262262
}
263263
}
@@ -267,7 +267,7 @@ func (o *InstallOptions) copySecretsAndConfigMaps(ctx context.Context, resourceD
267267
configMapBaseName = o.prefixFor(configmap.Name)
268268
}
269269
contentDir := path.Join(resourceDir, "configmaps", configMapBaseName)
270-
if err := atomicfiles.WriteFiles("configmap", configmap.ObjectMeta, contentDir, configmap.Data, 0600); err != nil {
270+
if err := atomicfiles.SyncDirectory("configmap", configmap.ObjectMeta, contentDir, configmap.Data, 0600); err != nil {
271271
return err
272272
}
273273
}

pkg/operator/staticpod/internal/atomicfiles/write_files.go renamed to pkg/operator/staticpod/internal/atomicfiles/sync_directory.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import (
99
"k8s.io/klog/v2"
1010
)
1111

12-
// WriteFiles can be used to atomically write given files into the target directory.
12+
// SyncDirectory can be used to atomically synchronize target directory with the given file content map.
1313
// This is done by populating a temporary directory, then atomically swapping it with the target directory.
14-
func WriteFiles[C string | []byte](
14+
// This effectively means that any extra files in the target directory are pruned.
15+
//
16+
// SyncDirectory is supposed to be used with secrets/configmaps, so typeName is expected to be "configmap" or "secret".
17+
// This does not affect the logic, but it's included in error messages.
18+
func SyncDirectory[C string | []byte](
1519
typeName string, o metav1.ObjectMeta, targetDir string, files map[string]C, filePerm os.FileMode,
1620
) error {
17-
return writeFiles(&realFS, typeName, o, targetDir, files, filePerm)
21+
return syncDirectory(&realFS, typeName, o, targetDir, files, filePerm)
1822
}
1923

2024
type fileSystem struct {
@@ -33,7 +37,7 @@ var realFS = fileSystem{
3337
SwapDirectoriesAtomic: SwapDirectories,
3438
}
3539

36-
func writeFiles[C string | []byte](
40+
func syncDirectory[C string | []byte](
3741
fs *fileSystem, typeName string, o metav1.ObjectMeta,
3842
targetDir string, files map[string]C, filePerm os.FileMode,
3943
) error {

pkg/operator/staticpod/internal/atomicfiles/write_files_test.go renamed to pkg/operator/staticpod/internal/atomicfiles/sync_directory_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func TestWriteFiles(t *testing.T) {
307307
}
308308

309309
// Replace with the object data.
310-
err := writeFiles(tc.newFS(), typeName, om, contentDir, tc.objectData, 0600)
310+
err := syncDirectory(tc.newFS(), typeName, om, contentDir, tc.objectData, 0600)
311311

312312
// Check the resulting state.
313313
tc.checkState(t, contentDir, tc.directoryData, tc.objectData, err)

0 commit comments

Comments
 (0)