Skip to content

Commit

Permalink
Refactor: Add context.Context to more VFS methods
Browse files Browse the repository at this point in the history
This allows for propagation of opentelemetry traces.
  • Loading branch information
justinsb committed Oct 14, 2023
1 parent dfc4717 commit 27dd371
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 105 deletions.
2 changes: 1 addition & 1 deletion pkg/client/simple/api/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/client/simple/vfsclientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/simple/vfsclientset/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/simple/vfsclientset/commonvfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 8 additions & 8 deletions upup/models/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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)
}
4 changes: 3 additions & 1 deletion upup/pkg/fi/secrets/vfs_secretstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions upup/pkg/fi/vfs_castore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/vfs_castore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 6 additions & 12 deletions util/pkg/vfs/azureblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions util/pkg/vfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down
17 changes: 7 additions & 10 deletions util/pkg/vfs/gsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions util/pkg/vfs/k8sfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}

Expand Down
Loading

0 comments on commit 27dd371

Please sign in to comment.