From 55981092efd001ab2dfda24f6936e58d3dae2ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Fri, 1 Mar 2024 01:08:05 -0500 Subject: [PATCH 1/2] incusd: Fix import shadowing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- cmd/incusd/api_cluster.go | 12 ++++++------ cmd/incusd/storage_pools_utils.go | 16 ++++++++-------- cmd/incusd/storage_volumes_backup.go | 12 ++++++------ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cmd/incusd/api_cluster.go b/cmd/incusd/api_cluster.go index 8b9a39f9277..ad176415a7a 100644 --- a/cmd/incusd/api_cluster.go +++ b/cmd/incusd/api_cluster.go @@ -243,13 +243,13 @@ func clusterGet(d *Daemon, r *http.Request) response.Response { // Fetch information about all node-specific configuration keys set on the // storage pools and networks of this cluster. -func clusterGetMemberConfig(ctx context.Context, cluster *db.Cluster) ([]api.ClusterMemberConfigKey, error) { +func clusterGetMemberConfig(ctx context.Context, clusterDB *db.Cluster) ([]api.ClusterMemberConfigKey, error) { var pools map[string]map[string]string var networks map[string]map[string]string keys := []api.ClusterMemberConfigKey{} - err := cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { + err := clusterDB.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { var err error pools, err = tx.GetStoragePoolsLocalConfig(ctx) @@ -2784,8 +2784,8 @@ type internalClusterPostHandoverRequest struct { Address string `json:"address" yaml:"address"` } -func clusterCheckStoragePoolsMatch(ctx context.Context, cluster *db.Cluster, reqPools []api.StoragePool) error { - return cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { +func clusterCheckStoragePoolsMatch(ctx context.Context, clusterDB *db.Cluster, reqPools []api.StoragePool) error { + return clusterDB.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { poolNames, err := tx.GetCreatedStoragePoolNames(ctx) if err != nil && !response.IsNotFoundError(err) { return err @@ -2829,8 +2829,8 @@ func clusterCheckStoragePoolsMatch(ctx context.Context, cluster *db.Cluster, req }) } -func clusterCheckNetworksMatch(ctx context.Context, cluster *db.Cluster, reqNetworks []api.InitNetworksProjectPost) error { - return cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { +func clusterCheckNetworksMatch(ctx context.Context, clusterDB *db.Cluster, reqNetworks []api.InitNetworksProjectPost) error { + return clusterDB.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { // Get a list of projects for networks. networkProjectNames, err := dbCluster.GetProjectNames(ctx, tx.Tx()) if err != nil { diff --git a/cmd/incusd/storage_pools_utils.go b/cmd/incusd/storage_pools_utils.go index 143560e46f6..a873454045f 100644 --- a/cmd/incusd/storage_pools_utils.go +++ b/cmd/incusd/storage_pools_utils.go @@ -65,9 +65,9 @@ func storagePoolValidate(s *state.State, poolName string, driverName string, con return nil } -func storagePoolCreateGlobal(ctx context.Context, state *state.State, req api.StoragePoolsPost, clientType request.ClientType) error { +func storagePoolCreateGlobal(ctx context.Context, s *state.State, req api.StoragePoolsPost, clientType request.ClientType) error { // Create the database entry. - id, err := storagePoolDBCreate(ctx, state, req.Name, req.Description, req.Driver, req.Config) + id, err := storagePoolDBCreate(ctx, s, req.Name, req.Description, req.Driver, req.Config) if err != nil { return err } @@ -79,9 +79,9 @@ func storagePoolCreateGlobal(ctx context.Context, state *state.State, req api.St revert := revert.New() defer revert.Fail() - revert.Add(func() { _ = dbStoragePoolDeleteAndUpdateCache(context.Background(), state, req.Name) }) + revert.Add(func() { _ = dbStoragePoolDeleteAndUpdateCache(context.Background(), s, req.Name) }) - _, err = storagePoolCreateLocal(ctx, state, id, req, clientType) + _, err = storagePoolCreateLocal(ctx, s, id, req, clientType) if err != nil { return err } @@ -92,13 +92,13 @@ func storagePoolCreateGlobal(ctx context.Context, state *state.State, req api.St // This performs local pool setup and updates DB record if config was changed during pool setup. // Returns resulting config. -func storagePoolCreateLocal(ctx context.Context, state *state.State, poolID int64, req api.StoragePoolsPost, clientType request.ClientType) (map[string]string, error) { +func storagePoolCreateLocal(ctx context.Context, s *state.State, poolID int64, req api.StoragePoolsPost, clientType request.ClientType) (map[string]string, error) { // Setup revert. revert := revert.New() defer revert.Fail() // Load pool record. - pool, err := storagePools.LoadByName(state, req.Name) + pool, err := storagePools.LoadByName(s, req.Name) if err != nil { return nil, err } @@ -129,7 +129,7 @@ func storagePoolCreateLocal(ctx context.Context, state *state.State, poolID int6 // see if something like this has happened. configDiff, _ := storagePools.ConfigDiff(req.Config, pool.Driver().Config()) if len(configDiff) > 0 { - err = state.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { + err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { // Update the database entry for the storage pool. return tx.UpdateStoragePool(ctx, req.Name, req.Description, pool.Driver().Config()) }) @@ -139,7 +139,7 @@ func storagePoolCreateLocal(ctx context.Context, state *state.State, poolID int6 } // Set storage pool node to storagePoolCreated. - err = state.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { + err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error { return tx.StoragePoolNodeCreated(poolID) }) if err != nil { diff --git a/cmd/incusd/storage_volumes_backup.go b/cmd/incusd/storage_volumes_backup.go index d1ef4431fc1..6c42f07586f 100644 --- a/cmd/incusd/storage_volumes_backup.go +++ b/cmd/incusd/storage_volumes_backup.go @@ -564,12 +564,12 @@ func storagePoolVolumeTypeCustomBackupGet(d *Daemon, r *http.Request) response.R fullName := volumeName + internalInstance.SnapshotDelimiter + backupName - backup, err := storagePoolVolumeBackupLoadByName(r.Context(), s, projectName, poolName, fullName) + entry, err := storagePoolVolumeBackupLoadByName(r.Context(), s, projectName, poolName, fullName) if err != nil { return response.SmartError(err) } - return response.SyncResponse(true, backup.Render()) + return response.SyncResponse(true, entry.Render()) } // swagger:operation POST /1.0/storage-pools/{poolName}/volumes/{type}/{volumeName}/backups/{backupName} storage storage_pool_volumes_type_backup_post @@ -675,7 +675,7 @@ func storagePoolVolumeTypeCustomBackupPost(d *Daemon, r *http.Request) response. oldName := volumeName + internalInstance.SnapshotDelimiter + backupName - backup, err := storagePoolVolumeBackupLoadByName(r.Context(), s, projectName, poolName, oldName) + entry, err := storagePoolVolumeBackupLoadByName(r.Context(), s, projectName, poolName, oldName) if err != nil { return response.SmartError(err) } @@ -683,7 +683,7 @@ func storagePoolVolumeTypeCustomBackupPost(d *Daemon, r *http.Request) response. newName := volumeName + internalInstance.SnapshotDelimiter + req.Name rename := func(op *operations.Operation) error { - err := backup.Rename(newName) + err := entry.Rename(newName) if err != nil { return err } @@ -791,13 +791,13 @@ func storagePoolVolumeTypeCustomBackupDelete(d *Daemon, r *http.Request) respons fullName := volumeName + internalInstance.SnapshotDelimiter + backupName - backup, err := storagePoolVolumeBackupLoadByName(r.Context(), s, projectName, poolName, fullName) + entry, err := storagePoolVolumeBackupLoadByName(r.Context(), s, projectName, poolName, fullName) if err != nil { return response.SmartError(err) } remove := func(op *operations.Operation) error { - err := backup.Delete() + err := entry.Delete() if err != nil { return err } From 22313c6c6a3ca93416b32ae76017c2d760551792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Fri, 1 Mar 2024 01:09:55 -0500 Subject: [PATCH 2/2] incusd/images: Fix potential race condition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- cmd/incusd/images.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/incusd/images.go b/cmd/incusd/images.go index 28a61b0203d..7d49d08d4b6 100644 --- a/cmd/incusd/images.go +++ b/cmd/incusd/images.go @@ -4545,14 +4545,14 @@ func autoSyncImages(ctx context.Context, s *state.State) error { for fingerprint, projects := range imageProjectInfo { ch := make(chan error) - go func() { - err := imageSyncBetweenNodes(ctx, s, nil, projects[0], fingerprint) + go func(projectName string, fingerprint string) { + err := imageSyncBetweenNodes(ctx, s, nil, projectName, fingerprint) if err != nil { logger.Error("Failed to synchronize images", logger.Ctx{"err": err, "fingerprint": fingerprint}) } ch <- nil - }() + }(projects[0], fingerprint) select { case <-ctx.Done():