@@ -43,7 +43,14 @@ type Storage struct {
4343 cw container.ContainerImpl
4444}
4545
46- func (a * Storage ) Close () {}
46+ func (a * Storage ) Close () {
47+ dir , err := a .getFilesDir ()
48+ if err == nil {
49+ if err := os .RemoveAll (dir ); err != nil {
50+ a .logger .Warnf ("unable to remove temp dir: %s" , dir )
51+ }
52+ }
53+ }
4754
4855func (a * Storage ) Ping () error {
4956 return a .check ()
@@ -313,22 +320,31 @@ func (a *Storage) parse(data []byte) (*Message, []string) {
313320 return res , logs
314321}
315322
323+ func (a * Storage ) getFilesDir () (string , error ) {
324+ switch a .cw .Type () {
325+ case container .BackendDocker :
326+ return a .config .DataDir (), nil
327+ case container .BackendKubernetes :
328+ tempDir := filepath .Join (os .TempDir (), "airbyte-secrets" , a .transfer .ID )
329+ if err := os .MkdirAll (tempDir , 0o755 ); err != nil && ! os .IsExist (err ) {
330+ return "" , xerrors .Errorf ("unable to create temp directory: %w" , err )
331+ }
332+ return tempDir , nil
333+ }
334+
335+ return "" , xerrors .New ("unknown container backend" )
336+ }
337+
316338func (a * Storage ) writeFile (fileName , fileData string ) error {
317339 var fullPath string
318340
319- // Check if running in Kubernetes by checking the container wrapper type
320- if a .cw .Type () == container .BackendKubernetes {
321- // Use temporary directory for Kubernetes
322- tempDir := "/tmp/airbyte-secrets"
323- if err := os .MkdirAll (tempDir , 0o755 ); err != nil {
324- return xerrors .Errorf ("unable to create temp directory: %w" , err )
325- }
326- fullPath = filepath .Join (tempDir , fileName )
327- } else {
328- // Use regular data directory for non-Kubernetes environments
329- fullPath = filepath .Join (a .config .DataDir (), fileName )
341+ dir , err := a .getFilesDir ()
342+ if err != nil {
343+ return xerrors .Errorf ("unable to get files dir: %w" , err )
330344 }
331345
346+ fullPath = filepath .Join (dir , fileName )
347+
332348 a .logger .Debugf ("%s -> \n %s" , fileName , fileData )
333349 defer a .logger .Infof ("file(%s) %s written" , format .SizeInt (len (fileData )), fullPath )
334350 return os .WriteFile (
@@ -419,32 +435,41 @@ func (a *Storage) baseOpts() container.ContainerOpts {
419435 AutoRemove : true ,
420436 }
421437
422- // Check if running in Kubernetes
423- if a .cw .Type () == container .BackendKubernetes {
424- // Check if temporary files exist
425- tempDir := "/tmp/airbyte-secrets"
426- if _ , err := os .Stat (tempDir ); err == nil {
438+ dir , err := a .getFilesDir ()
439+ if err != nil {
440+ a .logger .Errorf ("unable to specify data dir: %w" , err )
441+ }
442+
443+ switch a .cw .Type () {
444+ case container .BackendDocker :
445+ opts .Volumes = []container.Volume {
446+ {
447+ Name : "data" ,
448+ HostPath : dir ,
449+ ContainerPath : "/data" ,
450+ VolumeType : "bind" ,
451+ },
452+ }
453+ case container .BackendKubernetes :
454+ if _ , err := os .Stat (dir ); err == nil {
427455 // Create a unique secret name based on the transfer ID
428456 secretName := fmt .Sprintf ("airbyte-secret-%s" , a .transfer .ID )
429457
430458 // Create a map to store file contents for the secret
431459 secretData := make (map [string ][]byte )
432460
433461 // Read all files from the temporary directory
434- files , err := os .ReadDir (tempDir )
462+ files , err := os .ReadDir (dir )
435463 if err == nil && len (files ) > 0 {
436464 for _ , file := range files {
437465 if ! file .IsDir () {
438- filePath := filepath .Join (tempDir , file .Name ())
466+ filePath := filepath .Join (dir , file .Name ())
439467 data , err := os .ReadFile (filePath )
440468 if err == nil {
441469 secretData [file .Name ()] = data
442470 } else {
443471 a .logger .Warnf ("Failed to read file %s: %v" , filePath , err )
444472 }
445-
446- // Clean up the temporary file
447- _ = os .Remove (filePath )
448473 }
449474 }
450475
@@ -466,25 +491,11 @@ func (a *Storage) baseOpts() container.ContainerOpts {
466491 },
467492 }
468493
469- // Clean up the temporary directory
470- _ = os .RemoveAll (tempDir )
471-
472- a .logger .Infof ("Created Kubernetes secret %s with %d files" , secretName , len (secretData ))
473494 return opts
474495 }
475496 }
476497 }
477498
478- // Default volume configuration for non-Kubernetes or when no temp files exist
479- opts .Volumes = []container.Volume {
480- {
481- Name : "data" ,
482- HostPath : a .config .DataDir (),
483- ContainerPath : "/data" ,
484- VolumeType : "bind" ,
485- },
486- }
487-
488499 return opts
489500}
490501
0 commit comments