Refactor: Add context.Context to more VFS methods

This allows for propagation of opentelemetry traces.
This commit is contained in:
justinsb 2023-10-14 14:31:05 -04:00
parent dfc4717221
commit 27dd371c08
19 changed files with 87 additions and 105 deletions

View File

@ -131,7 +131,7 @@ func (c *RESTClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster
return err
}
err = vfsclientset.DeleteAllClusterState(configBase)
err = vfsclientset.DeleteAllClusterState(ctx, configBase)
if err != nil {
return err
}

View File

@ -131,8 +131,8 @@ func (c *VFSClientset) pkiPath(cluster *kops.Cluster) (vfs.Path, error) {
}
}
func DeleteAllClusterState(basePath vfs.Path) error {
paths, err := basePath.ReadTree()
func DeleteAllClusterState(ctx context.Context, basePath vfs.Path) error {
paths, err := basePath.ReadTree(ctx)
if err != nil {
return fmt.Errorf("error listing files in state store: %v", err)
}
@ -180,7 +180,7 @@ func DeleteAllClusterState(basePath vfs.Path) error {
return fmt.Errorf("refusing to delete: unknown file found: %s", path)
}
err = basePath.RemoveAll()
err = basePath.RemoveAll(ctx)
if err != nil {
return fmt.Errorf("error deleting cluster files in %s: %w", basePath, err)
}
@ -197,11 +197,11 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)
return err
}
err = path.Join("openid/v1/jwks").Remove()
err = path.Join("openid/v1/jwks").Remove(ctx)
if err != nil {
return err
}
err = path.Join(".well-known/openid-configuration").Remove()
err = path.Join(".well-known/openid-configuration").Remove(ctx)
if err != nil {
return err
}
@ -214,7 +214,7 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)
if err != nil {
return err
}
err = path.RemoveAll()
err = path.RemoveAll(ctx)
if err != nil {
return err
}
@ -226,7 +226,7 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)
if err != nil {
return err
}
err = path.RemoveAll()
err = path.RemoveAll(ctx)
if err != nil {
return err
}
@ -237,7 +237,7 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)
return err
}
return DeleteAllClusterState(configBase)
return DeleteAllClusterState(ctx, configBase)
}
func NewVFSClientset(vfsContext *vfs.VFSContext, basePath vfs.Path) simple.Clientset {

View File

@ -75,7 +75,7 @@ func (c *ClusterVFS) configBase(clusterName string) (vfs.Path, error) {
func (c *ClusterVFS) List(options metav1.ListOptions) (*api.ClusterList, error) {
ctx := context.TODO()
names, err := c.listNames()
names, err := c.listNames(ctx)
if err != nil {
return nil, err
}
@ -163,8 +163,8 @@ func (r *ClusterVFS) Update(c *api.Cluster, status *api.ClusterStatus) (*api.Clu
// List returns a slice containing all the cluster names
// It skips directories that don't look like clusters
func (r *ClusterVFS) listNames() ([]string, error) {
paths, err := r.basePath.ReadTree()
func (r *ClusterVFS) listNames(ctx context.Context) ([]string, error) {
paths, err := r.basePath.ReadTree(ctx)
if err != nil {
return nil, fmt.Errorf("error reading state store: %v", err)
}

View File

@ -204,7 +204,7 @@ func (c *commonVFS) update(ctx context.Context, cluster *kops.Cluster, i runtime
func (c *commonVFS) delete(ctx context.Context, name string, options metav1.DeleteOptions) error {
p := c.basePath.Join(name)
err := p.Remove()
err := p.Remove(ctx)
if err != nil {
if os.IsNotExist(err) {
return nil

View File

@ -55,7 +55,7 @@ func (t *Templates) Find(key string) fi.Resource {
}
func (t *Templates) loadFrom(ctx context.Context, base vfs.Path) error {
files, err := base.ReadTree()
files, err := base.ReadTree(ctx)
if err != nil {
return fmt.Errorf("error reading from %s", base)
}

View File

@ -97,16 +97,16 @@ func (p *AssetPath) ReadDir() ([]vfs.Path, error) {
return paths, nil
}
func (p *AssetPath) ReadTree() ([]vfs.Path, error) {
func (p *AssetPath) ReadTree(ctx context.Context) ([]vfs.Path, error) {
var paths []vfs.Path
err := readTree(p.location, &paths)
err := readTree(ctx, p.location, &paths)
if err != nil {
return nil, err
}
return paths, nil
}
func readTree(base string, dest *[]vfs.Path) error {
func readTree(ctx context.Context, base string, dest *[]vfs.Path) error {
files, err := content.ReadDir(base)
if err != nil {
if _, ok := err.(*fs.PathError); ok {
@ -117,7 +117,7 @@ func readTree(base string, dest *[]vfs.Path) error {
for _, f := range files {
p := path.Join(base, f.Name())
if f.IsDir() {
childFiles, err := NewAssetPath(p).ReadTree()
childFiles, err := NewAssetPath(p).ReadTree(ctx)
if err != nil {
return err
}
@ -141,14 +141,14 @@ func (p *AssetPath) String() string {
return p.Path()
}
func (p *AssetPath) Remove() error {
func (p *AssetPath) Remove(ctx context.Context) error {
return ReadOnlyError
}
func (p *AssetPath) RemoveAll() error {
func (p *AssetPath) RemoveAll(ctx context.Context) error {
return ReadOnlyError
}
func (p *AssetPath) RemoveAllVersions() error {
return p.Remove()
func (p *AssetPath) RemoveAllVersions(ctx context.Context) error {
return p.Remove(ctx)
}

View File

@ -89,8 +89,10 @@ func (c *VFSSecretStore) MirrorTo(ctx context.Context, basedir vfs.Path) error {
// DeleteSecret implements fi.SecretStore DeleteSecret
func (c *VFSSecretStore) DeleteSecret(name string) error {
ctx := context.TODO()
p := c.buildSecretPath(name)
return p.Remove()
return p.Remove(ctx)
}
func (c *VFSSecretStore) ListSecrets() ([]string, error) {

View File

@ -160,7 +160,7 @@ func (c *VFSCAStore) ListKeysets() (map[string]*Keyset, error) {
ctx := context.TODO()
baseDir := c.basedir.Join("private")
files, err := baseDir.ReadTree()
files, err := baseDir.ReadTree(ctx)
if err != nil {
return nil, fmt.Errorf("error reading directory %q: %v", baseDir, err)
}
@ -341,6 +341,8 @@ func (c *VFSCAStore) FindSSHPublicKeys() ([]*kops.SSHCredential, error) {
}
func (c *VFSCAStore) DeleteSSHCredential() error {
ctx := context.TODO()
p := c.basedir.Join("ssh", "public", "admin")
files, err := p.ReadDir()
@ -351,7 +353,7 @@ func (c *VFSCAStore) DeleteSSHCredential() error {
return err
}
for _, f := range files {
if err := f.Remove(); err != nil {
if err := f.Remove(ctx); err != nil {
return err
}
}

View File

@ -91,7 +91,7 @@ func TestVFSCAStoreRoundTrip(t *testing.T) {
}
}
paths, err := basePath.ReadTree()
paths, err := basePath.ReadTree(ctx)
if err != nil {
t.Fatalf("error from ReadTree: %v", err)
}

View File

@ -202,9 +202,7 @@ func (p *AzureBlobPath) WriteFile(ctx context.Context, data io.ReadSeeker, acl A
}
// Remove deletes the blob.
func (p *AzureBlobPath) Remove() error {
ctx := context.TODO()
func (p *AzureBlobPath) Remove(ctx context.Context) error {
client, err := p.getClient(ctx)
if err != nil {
return err
@ -219,14 +217,14 @@ func (p *AzureBlobPath) Remove() error {
return err
}
func (p *AzureBlobPath) RemoveAll() error {
tree, err := p.ReadTree()
func (p *AzureBlobPath) RemoveAll(ctx context.Context) error {
tree, err := p.ReadTree(ctx)
if err != nil {
return err
}
for _, blobPath := range tree {
err := blobPath.Remove()
err := blobPath.Remove(ctx)
if err != nil {
return fmt.Errorf("error removing file %s: %w", blobPath, err)
}
@ -235,9 +233,7 @@ func (p *AzureBlobPath) RemoveAll() error {
return nil
}
func (p *AzureBlobPath) RemoveAllVersions() error {
ctx := context.TODO()
func (p *AzureBlobPath) RemoveAllVersions(ctx context.Context) error {
client, err := p.getClient(ctx)
if err != nil {
return err
@ -317,9 +313,7 @@ func (p *AzureBlobPath) ReadDir() ([]Path, error) {
}
// ReadTree lists all blobs (recursively) in the subtree rooted at the current Path.
func (p *AzureBlobPath) ReadTree() ([]Path, error) {
ctx := context.TODO()
func (p *AzureBlobPath) ReadTree(ctx context.Context) ([]Path, error) {
client, err := p.getClient(ctx)
if err != nil {
return nil, err

View File

@ -149,7 +149,7 @@ func (p *FSPath) ReadDir() ([]Path, error) {
return paths, nil
}
func (p *FSPath) ReadTree() ([]Path, error) {
func (p *FSPath) ReadTree(ctx context.Context) ([]Path, error) {
var paths []Path
err := readTree(p.location, &paths)
if err != nil {
@ -191,18 +191,18 @@ func (p *FSPath) String() string {
return p.Path()
}
func (p *FSPath) Remove() error {
func (p *FSPath) Remove(ctx context.Context) error {
return os.Remove(p.location)
}
func (p *FSPath) RemoveAll() error {
tree, err := p.ReadTree()
func (p *FSPath) RemoveAll(ctx context.Context) error {
tree, err := p.ReadTree(ctx)
if err != nil {
return err
}
for _, filePath := range tree {
err := filePath.Remove()
err := filePath.Remove(ctx)
if err != nil {
return fmt.Errorf("error removing file %s: %w", filePath, err)
}
@ -211,8 +211,8 @@ func (p *FSPath) RemoveAll() error {
return nil
}
func (p *FSPath) RemoveAllVersions() error {
return p.Remove()
func (p *FSPath) RemoveAllVersions(ctx context.Context) error {
return p.Remove(ctx)
}
func (p *FSPath) PreferredHash() (*hashing.Hash, error) {

View File

@ -118,8 +118,7 @@ func (p *GSPath) String() string {
return p.Path()
}
func (p *GSPath) Remove() error {
ctx := context.TODO()
func (p *GSPath) Remove(ctx context.Context) error {
done, err := RetryWithBackoff(gcsWriteBackoff, func() (bool, error) {
client, err := p.getStorageClient(ctx)
if err != nil {
@ -142,14 +141,14 @@ func (p *GSPath) Remove() error {
}
}
func (p *GSPath) RemoveAll() error {
tree, err := p.ReadTree()
func (p *GSPath) RemoveAll(ctx context.Context) error {
tree, err := p.ReadTree(ctx)
if err != nil {
return err
}
for _, objectPath := range tree {
err := objectPath.Remove()
err := objectPath.Remove(ctx)
if err != nil {
return fmt.Errorf("error removing file %s: %w", objectPath, err)
}
@ -158,8 +157,8 @@ func (p *GSPath) RemoveAll() error {
return nil
}
func (p *GSPath) RemoveAllVersions() error {
return p.Remove()
func (p *GSPath) RemoveAllVersions(ctx context.Context) error {
return p.Remove(ctx)
}
func (p *GSPath) Join(relativePath ...string) Path {
@ -346,9 +345,7 @@ func (p *GSPath) ReadDir() ([]Path, error) {
}
// ReadTree implements Path::ReadTree
func (p *GSPath) ReadTree() ([]Path, error) {
ctx := context.TODO()
func (p *GSPath) ReadTree(ctx context.Context) ([]Path, error) {
var ret []Path
done, err := RetryWithBackoff(gcsReadBackoff, func() (bool, error) {
// No delimiter for recursive search

View File

@ -67,16 +67,16 @@ func (p *KubernetesPath) String() string {
return p.Path()
}
func (p *KubernetesPath) Remove() error {
func (p *KubernetesPath) Remove(ctx context.Context) error {
return fmt.Errorf("KubernetesPath::Remove not supported")
}
func (p *KubernetesPath) RemoveAll() error {
func (p *KubernetesPath) RemoveAll(ctx context.Context) error {
return fmt.Errorf("KubernetesPath::RemoveAll not supported")
}
func (p *KubernetesPath) RemoveAllVersions() error {
return p.Remove()
func (p *KubernetesPath) RemoveAllVersions(ctx context.Context) error {
return p.Remove(ctx)
}
func (p *KubernetesPath) Join(relativePath ...string) Path {
@ -117,7 +117,7 @@ func (p *KubernetesPath) ReadDir() ([]Path, error) {
return nil, fmt.Errorf("KubernetesPath::ReadDir not supported")
}
func (p *KubernetesPath) ReadTree() ([]Path, error) {
func (p *KubernetesPath) ReadTree(ctx context.Context) ([]Path, error) {
return nil, fmt.Errorf("KubernetesPath::ReadTree not supported")
}

View File

@ -153,7 +153,7 @@ func (p *MemFSPath) ReadDir() ([]Path, error) {
return paths, nil
}
func (p *MemFSPath) ReadTree() ([]Path, error) {
func (p *MemFSPath) ReadTree(ctx context.Context) ([]Path, error) {
var paths []Path
p.readTree(&paths)
return paths, nil
@ -183,19 +183,19 @@ func (p *MemFSPath) String() string {
return p.Path()
}
func (p *MemFSPath) Remove() error {
func (p *MemFSPath) Remove(ctx context.Context) error {
p.contents = nil
return nil
}
func (p *MemFSPath) RemoveAll() error {
tree, err := p.ReadTree()
func (p *MemFSPath) RemoveAll(ctx context.Context) error {
tree, err := p.ReadTree(ctx)
if err != nil {
return err
}
for _, filePath := range tree {
err := filePath.Remove()
err := filePath.Remove(ctx)
if err != nil {
return fmt.Errorf("error removing file %s: %w", filePath, err)
}
@ -204,8 +204,8 @@ func (p *MemFSPath) RemoveAll() error {
return nil
}
func (p *MemFSPath) RemoveAllVersions() error {
return p.Remove()
func (p *MemFSPath) RemoveAllVersions(ctx context.Context) error {
return p.Remove(ctx)
}
func (p *MemFSPath) Location() string {

View File

@ -117,6 +117,8 @@ func TestMemFsReadDir(t *testing.T) {
}
func TestMemFsReadTree(t *testing.T) {
ctx := testcontext.ForTest(t)
tests := []struct {
path string
subpaths []string
@ -146,7 +148,7 @@ func TestMemFsReadTree(t *testing.T) {
}
// Read dir tree
paths, err := memfspath.ReadTree()
paths, err := memfspath.ReadTree(ctx)
if err != nil {
t.Errorf("Failed reading dir tree %s, error: %v", test.path, err)
continue

View File

@ -90,9 +90,7 @@ func (p *S3Path) String() string {
return p.Path()
}
func (p *S3Path) Remove() error {
ctx := context.TODO()
func (p *S3Path) Remove(ctx context.Context) error {
client, err := p.client(ctx)
if err != nil {
return err
@ -114,15 +112,13 @@ func (p *S3Path) Remove() error {
return nil
}
func (p *S3Path) RemoveAll() error {
ctx := context.TODO()
func (p *S3Path) RemoveAll(ctx context.Context) error {
client, err := p.client(ctx)
if err != nil {
return err
}
tree, err := p.ReadTree()
tree, err := p.ReadTree(ctx)
if err != nil {
return err
}
@ -165,9 +161,7 @@ func (p *S3Path) RemoveAll() error {
return nil
}
func (p *S3Path) RemoveAllVersions() error {
ctx := context.TODO()
func (p *S3Path) RemoveAllVersions(ctx context.Context) error {
client, err := p.client(ctx)
if err != nil {
return err
@ -438,8 +432,7 @@ func (p *S3Path) ReadDir() ([]Path, error) {
return paths, nil
}
func (p *S3Path) ReadTree() ([]Path, error) {
ctx := context.TODO()
func (p *S3Path) ReadTree(ctx context.Context) ([]Path, error) {
client, err := p.client(ctx)
if err != nil {
return nil, err

View File

@ -96,9 +96,7 @@ func (p *SSHPath) String() string {
return p.Path()
}
func (p *SSHPath) Remove() error {
ctx := context.TODO()
func (p *SSHPath) Remove(ctx context.Context) error {
sftpClient, err := p.newClient(ctx)
if err != nil {
return err
@ -116,14 +114,14 @@ func (p *SSHPath) Remove() error {
return nil
}
func (p *SSHPath) RemoveAll() error {
tree, err := p.ReadTree()
func (p *SSHPath) RemoveAll(ctx context.Context) error {
tree, err := p.ReadTree(ctx)
if err != nil {
return err
}
for _, filePath := range tree {
err := filePath.Remove()
err := filePath.Remove(ctx)
if err != nil {
return fmt.Errorf("error removing file %s: %w", filePath, err)
}
@ -132,8 +130,8 @@ func (p *SSHPath) RemoveAll() error {
return nil
}
func (p *SSHPath) RemoveAllVersions() error {
return p.Remove()
func (p *SSHPath) RemoveAllVersions(ctx context.Context) error {
return p.Remove(ctx)
}
func (p *SSHPath) Join(relativePath ...string) Path {
@ -327,9 +325,7 @@ func (p *SSHPath) ReadDir() ([]Path, error) {
return children, nil
}
func (p *SSHPath) ReadTree() ([]Path, error) {
ctx := context.TODO()
func (p *SSHPath) ReadTree(ctx context.Context) ([]Path, error) {
sftpClient, err := p.newClient(ctx)
if err != nil {
return nil, err

View File

@ -281,9 +281,7 @@ func (p *SwiftPath) getClient(ctx context.Context) (*gophercloud.ServiceClient,
return p.vfsContext.getSwiftClient(ctx)
}
func (p *SwiftPath) Remove() error {
ctx := context.TODO()
func (p *SwiftPath) Remove(ctx context.Context) error {
done, err := RetryWithBackoff(swiftWriteBackoff, func() (bool, error) {
client, err := p.getClient(ctx)
if err != nil {
@ -308,10 +306,8 @@ func (p *SwiftPath) Remove() error {
}
}
func (p *SwiftPath) RemoveAll() error {
ctx := context.TODO()
tree, err := p.ReadTree()
func (p *SwiftPath) RemoveAll(ctx context.Context) error {
tree, err := p.ReadTree(ctx)
if err != nil {
return err
}
@ -362,8 +358,8 @@ func (p *SwiftPath) RemoveAll() error {
return nil
}
func (p *SwiftPath) RemoveAllVersions() error {
return p.Remove()
func (p *SwiftPath) RemoveAllVersions(ctx context.Context) error {
return p.Remove(ctx)
}
func (p *SwiftPath) Join(relativePath ...string) Path {
@ -587,7 +583,7 @@ func (p *SwiftPath) ReadDir() ([]Path, error) {
}
// ReadTree implements Path::ReadTree.
func (p *SwiftPath) ReadTree() ([]Path, error) {
func (p *SwiftPath) ReadTree(ctx context.Context) ([]Path, error) {
prefix := p.key
if prefix != "" && !strings.HasSuffix(prefix, "/") {
prefix += "/"

View File

@ -55,13 +55,13 @@ type Path interface {
CreateFile(ctx context.Context, data io.ReadSeeker, acl ACL) error
// Remove deletes the file
Remove() error
Remove(ctx context.Context) error
// RemoveAll deletes all files recursively, deletes only files as returned per ReadTree
RemoveAll() error
RemoveAll(ctx context.Context) error
// RemoveAllVersions completely deletes the file (with all its versions and markers).
RemoveAllVersions() error
RemoveAllVersions(ctx context.Context) error
// Base returns the base name (last element)
Base() string
@ -74,7 +74,7 @@ type Path interface {
// ReadTree lists all files (recursively) in the subtree rooted at the current Path
/// Note: returns only files, not directories
ReadTree() ([]Path, error)
ReadTree(ctx context.Context) ([]Path, error)
}
// TerraformPath is a Path that can render to Terraform.