From 562dff0d19708a2e68a5d3e5a4ba7780c3286020 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Fri, 21 Jan 2022 14:58:27 +0530 Subject: [PATCH] cleanup: use os.WriteFile to write files as ioutil.WriteFile is deprecated and suggestion is to use os.WriteFile as per https://pkg.go.dev/io/ioutil updating the same. Signed-off-by: Madhu Rajanna --- internal/kms/vault_sa.go | 2 +- internal/rbd/rbd_util.go | 4 ++-- internal/util/cephconf.go | 3 +-- internal/util/cluster_mapping_test.go | 10 +++++----- internal/util/conn_pool_test.go | 3 +-- internal/util/csiconfig_test.go | 15 +++++++-------- 6 files changed, 17 insertions(+), 20 deletions(-) diff --git a/internal/kms/vault_sa.go b/internal/kms/vault_sa.go index aec648022..bdeea0f56 100644 --- a/internal/kms/vault_sa.go +++ b/internal/kms/vault_sa.go @@ -320,7 +320,7 @@ func (kms *VaultTenantSA) getTokenPath() (string, error) { return "", err } - err = ioutil.WriteFile(dir+"/token", []byte(token), 0600) + err = os.WriteFile(dir+"/token", []byte(token), 0600) if err != nil { return "", fmt.Errorf("failed to write token for ServiceAccount %s/%s: %w", kms.tenantSAName, kms.Tenant, err) } diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index 3301f9a01..ad4dafc8f 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -1625,7 +1625,7 @@ func stashRBDImageMetadata(volOptions *rbdVolume, metaDataPath string) error { } fPath := filepath.Join(metaDataPath, stashFileName) - err = ioutil.WriteFile(fPath, encodedBytes, 0o600) + err = os.WriteFile(fPath, encodedBytes, 0o600) if err != nil { return fmt.Errorf("failed to stash JSON image metadata for image (%s) at path (%s): %w", volOptions, fPath, err) } @@ -1673,7 +1673,7 @@ func updateRBDImageMetadataStash(metaDataPath, device string) error { } fPath := filepath.Join(metaDataPath, stashFileName) - err = ioutil.WriteFile(fPath, encodedBytes, 0600) + err = os.WriteFile(fPath, encodedBytes, 0600) if err != nil { return fmt.Errorf("failed to stash JSON image metadata at path: (%s) for spec:(%s) : %w", fPath, imgMeta.String(), err) diff --git a/internal/util/cephconf.go b/internal/util/cephconf.go index e8e3e26fa..006b53e6f 100644 --- a/internal/util/cephconf.go +++ b/internal/util/cephconf.go @@ -17,7 +17,6 @@ limitations under the License. package util import ( - "io/ioutil" "os" ) @@ -57,7 +56,7 @@ func WriteCephConfig() error { // create config file if it does not exist to support backward compatibility if _, err = os.Stat(CephConfigPath); os.IsNotExist(err) { - err = ioutil.WriteFile(CephConfigPath, cephConfig, 0o600) + err = os.WriteFile(CephConfigPath, cephConfig, 0o600) } if err != nil { diff --git a/internal/util/cluster_mapping_test.go b/internal/util/cluster_mapping_test.go index ded989e35..6f6d5a9d8 100644 --- a/internal/util/cluster_mapping_test.go +++ b/internal/util/cluster_mapping_test.go @@ -20,7 +20,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "os" "reflect" "strings" "testing" @@ -142,7 +142,7 @@ func TestGetClusterMappingInfo(t *testing.T) { t.Parallel() mappingConfigFile := fmt.Sprintf("%s/mapping-%d.json", mappingBasePath, currentI) if len(currentTT.mappingFilecontent) != 0 { - err = ioutil.WriteFile(mappingConfigFile, currentTT.mappingFilecontent, 0o600) + err = os.WriteFile(mappingConfigFile, currentTT.mappingFilecontent, 0o600) if err != nil { t.Errorf("failed to write to %q, error = %v", mappingConfigFile, err) } @@ -158,7 +158,7 @@ func TestGetClusterMappingInfo(t *testing.T) { } clusterMappingConfigFile = fmt.Sprintf("%s/mapping.json", mappingBasePath) - err = ioutil.WriteFile(clusterMappingConfigFile, mappingFileContent, 0o600) + err = os.WriteFile(clusterMappingConfigFile, mappingFileContent, 0o600) if err != nil { t.Errorf("failed to write mapping content error = %v", err) } @@ -318,7 +318,7 @@ func TestFetchMappedClusterIDAndMons(t *testing.T) { if err != nil { t.Errorf("failed to marshal csi config info %v", err) } - err = ioutil.WriteFile(csiConfigFile, csiConfigFileContent, 0o600) + err = os.WriteFile(csiConfigFile, csiConfigFileContent, 0o600) if err != nil { t.Errorf("failed to write %s file content: %v", CsiConfigFile, err) } @@ -351,7 +351,7 @@ func TestFetchMappedClusterIDAndMons(t *testing.T) { if err != nil { t.Errorf("failed to marshal mapping info %v", err) } - err = ioutil.WriteFile(clusterMappingConfigFile, clusterMappingFileContent, 0o600) + err = os.WriteFile(clusterMappingConfigFile, clusterMappingFileContent, 0o600) if err != nil { t.Errorf("failed to write %s file content: %v", clusterMappingFileContent, err) } diff --git a/internal/util/conn_pool_test.go b/internal/util/conn_pool_test.go index 39251f24c..6fe3f094b 100644 --- a/internal/util/conn_pool_test.go +++ b/internal/util/conn_pool_test.go @@ -17,7 +17,6 @@ limitations under the License. package util import ( - "io/ioutil" "os" "testing" "time" @@ -81,7 +80,7 @@ func TestConnPool(t *testing.T) { // create a keyfile with some contents keyfile := "/tmp/conn_utils.keyfile" - err := ioutil.WriteFile(keyfile, []byte("the-key"), 0o600) + err := os.WriteFile(keyfile, []byte("the-key"), 0o600) if err != nil { t.Errorf("failed to create keyfile: %v", err) diff --git a/internal/util/csiconfig_test.go b/internal/util/csiconfig_test.go index a946dd76c..fdcd24038 100644 --- a/internal/util/csiconfig_test.go +++ b/internal/util/csiconfig_test.go @@ -17,7 +17,6 @@ limitations under the License. package util import ( - "io/ioutil" "os" "testing" ) @@ -54,7 +53,7 @@ func TestCSIConfig(t *testing.T) { } data = "" - err = ioutil.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) + err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) if err != nil { t.Errorf("Test setup error %s", err) } @@ -66,7 +65,7 @@ func TestCSIConfig(t *testing.T) { } data = "[{\"clusterIDBad\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}]" - err = ioutil.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) + err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) if err != nil { t.Errorf("Test setup error %s", err) } @@ -78,7 +77,7 @@ func TestCSIConfig(t *testing.T) { } data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitorsBad\":[\"mon1\",\"mon2\",\"mon3\"]}]" - err = ioutil.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) + err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) if err != nil { t.Errorf("Test setup error %s", err) } @@ -90,7 +89,7 @@ func TestCSIConfig(t *testing.T) { } data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",2,\"mon3\"]}]" - err = ioutil.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) + err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) if err != nil { t.Errorf("Test setup error %s", err) } @@ -102,7 +101,7 @@ func TestCSIConfig(t *testing.T) { } data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}]" - err = ioutil.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) + err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) if err != nil { t.Errorf("Test setup error %s", err) } @@ -121,7 +120,7 @@ func TestCSIConfig(t *testing.T) { data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}," + "{\"clusterID\":\"" + clusterID1 + "\",\"monitors\":[\"mon4\",\"mon5\",\"mon6\"]}]" - err = ioutil.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) + err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) if err != nil { t.Errorf("Test setup error %s", err) } @@ -134,7 +133,7 @@ func TestCSIConfig(t *testing.T) { data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}," + "{\"clusterID\":\"" + clusterID1 + "\",\"monitors\":[\"mon4\",\"mon5\",\"mon6\"]}]" - err = ioutil.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) + err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600) if err != nil { t.Errorf("Test setup error %s", err) }