Skip to content

Commit

Permalink
Get VFSContext from caller in CopyFile
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Jul 18, 2023
1 parent 1358851 commit 5c343b0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/kops/get_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func RunGetAssets(ctx context.Context, f *util.Factory, out io.Writer, options *
}

if options.Copy {
err := assets.Copy(updateClusterResults.ImageAssets, updateClusterResults.FileAssets, updateClusterResults.Cluster)
err := assets.Copy(updateClusterResults.ImageAssets, updateClusterResults.FileAssets, f.VFSContext(), updateClusterResults.Cluster)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/assets/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import (

"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/util/pkg/vfs"
)

type assetTask interface {
Run() error
}

func Copy(imageAssets []*ImageAsset, fileAssets []*FileAsset, cluster *kops.Cluster) error {
func Copy(imageAssets []*ImageAsset, fileAssets []*FileAsset, vfsContext *vfs.VFSContext, cluster *kops.Cluster) error {
tasks := map[string]assetTask{}

for _, imageAsset := range imageAssets {
Expand Down Expand Up @@ -56,6 +57,7 @@ func Copy(imageAssets []*ImageAsset, fileAssets []*FileAsset, cluster *kops.Clus
TargetFile: fileAsset.DownloadURL.String(),
SourceFile: fileAsset.CanonicalURL.String(),
SHA: fileAsset.SHAValue,
VFSContext: vfsContext,
Cluster: cluster,
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/assets/copyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type CopyFile struct {
SourceFile string
TargetFile string
SHA string
VFSContext *vfs.VFSContext
Cluster *kops.Cluster
}

Expand Down Expand Up @@ -66,7 +67,7 @@ func (e *CopyFile) Run() error {

targetSHAFile := e.TargetFile + shaExtension

targetSHABytes, err := vfs.Context.ReadFile(targetSHAFile)
targetSHABytes, err := e.VFSContext.ReadFile(targetSHAFile)
if err != nil {
if os.IsNotExist(err) {
klog.V(4).Infof("unable to download: %q, assuming target file is not present, and if not present may not be an error: %v",
Expand All @@ -91,7 +92,7 @@ func (e *CopyFile) Run() error {

klog.V(2).Infof("copying bits from %q to %q", source, target)

if err := transferFile(ctx, e.Cluster, source, target, sourceSha); err != nil {
if err := transferFile(ctx, e.VFSContext, e.Cluster, source, target, sourceSha); err != nil {
return fmt.Errorf("unable to transfer %q to %q: %v", source, target, err)
}

Expand All @@ -100,11 +101,11 @@ func (e *CopyFile) Run() error {

// transferFile downloads a file from the source location, validates the file matches the SHA,
// and uploads the file to the target location.
func transferFile(ctx context.Context, cluster *kops.Cluster, source string, target string, sha string) error {
func transferFile(ctx context.Context, vfsContext *vfs.VFSContext, cluster *kops.Cluster, source string, target string, sha string) error {
// TODO drop file to disk, as vfs reads file into memory. We load kubelet into memory for instance.
// TODO in s3 can we do a copy file ... would need to test

data, err := vfs.Context.ReadFile(source)
data, err := vfsContext.ReadFile(source)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("file not found %q: %v", source, err)
Expand All @@ -118,7 +119,7 @@ func transferFile(ctx context.Context, cluster *kops.Cluster, source string, tar
return err
}

uploadVFS, err := vfs.Context.BuildVfsPath(objectStore)
uploadVFS, err := vfsContext.BuildVfsPath(objectStore)
if err != nil {
return fmt.Errorf("error building path %q: %v", objectStore, err)
}
Expand All @@ -129,7 +130,7 @@ func transferFile(ctx context.Context, cluster *kops.Cluster, source string, tar
}

shaTarget := objectStore + shaExtension
shaVFS, err := vfs.Context.BuildVfsPath(shaTarget)
shaVFS, err := vfsContext.BuildVfsPath(shaTarget)
if err != nil {
return fmt.Errorf("error building path %q: %v", shaTarget, err)
}
Expand Down

0 comments on commit 5c343b0

Please sign in to comment.