2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-02-24 04:32:13 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2021-01-30 00:24:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-02-24 04:32:13 +00:00
|
|
|
)
|
|
|
|
|
2017-11-29 01:03:34 +00:00
|
|
|
// autopilotConfigTableSchema returns a new table schema used for storing
|
|
|
|
// the autopilot configuration
|
|
|
|
func autopilotConfigTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: "autopilot-config",
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2020-06-16 17:19:31 +00:00
|
|
|
"id": {
|
2017-11-29 01:03:34 +00:00
|
|
|
Name: "id",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.ConditionalIndex{
|
|
|
|
Conditional: func(obj interface{}) (bool, error) { return true, nil },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
// Autopilot is used to pull the autopilot config from the snapshot.
|
2020-09-25 17:46:38 +00:00
|
|
|
func (s *Snapshot) Autopilot() (*structs.AutopilotConfig, error) {
|
2017-03-21 23:36:44 +00:00
|
|
|
c, err := s.tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
config, ok := c.(*structs.AutopilotConfig)
|
2017-03-21 23:36:44 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Autopilot is used when restoring from a snapshot.
|
2020-09-25 17:46:38 +00:00
|
|
|
func (s *Restore) Autopilot(config *structs.AutopilotConfig) error {
|
2017-03-21 23:36:44 +00:00
|
|
|
if err := s.tx.Insert("autopilot-config", config); err != nil {
|
|
|
|
return fmt.Errorf("failed restoring autopilot config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 04:32:13 +00:00
|
|
|
// AutopilotConfig is used to get the current Autopilot configuration.
|
2020-09-25 17:46:38 +00:00
|
|
|
func (s *Store) AutopilotConfig() (uint64, *structs.AutopilotConfig, error) {
|
2017-02-24 04:32:13 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Get the autopilot config
|
|
|
|
c, err := tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
2017-02-24 21:08:49 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed autopilot config lookup: %s", err)
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
config, ok := c.(*structs.AutopilotConfig)
|
2017-02-24 04:32:13 +00:00
|
|
|
if !ok {
|
2017-02-24 21:08:49 +00:00
|
|
|
return 0, nil, nil
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
return config.ModifyIndex, config, nil
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
// AutopilotSetConfig is used to set the current Autopilot configuration.
|
2020-09-25 17:46:38 +00:00
|
|
|
func (s *Store) AutopilotSetConfig(idx uint64, config *structs.AutopilotConfig) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2017-02-24 04:32:13 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
if err := autopilotSetConfigTxn(tx, idx, config); err != nil {
|
2019-03-01 19:28:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-24 21:08:49 +00:00
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2017-02-24 21:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotCASConfig is used to try updating the Autopilot configuration with a
|
|
|
|
// given Raft index. If the CAS index specified is not equal to the last observed index
|
|
|
|
// for the config, then the call is a noop,
|
2020-09-25 17:46:38 +00:00
|
|
|
func (s *Store) AutopilotCASConfig(idx, cidx uint64, config *structs.AutopilotConfig) (bool, error) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2017-02-24 21:08:49 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Check for an existing config
|
|
|
|
existing, err := tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed autopilot config lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the existing index does not match the provided CAS
|
|
|
|
// index arg, then we shouldn't update anything and can safely
|
|
|
|
// return early here.
|
2020-09-25 17:46:38 +00:00
|
|
|
e, ok := existing.(*structs.AutopilotConfig)
|
2017-02-24 21:08:49 +00:00
|
|
|
if !ok || e.ModifyIndex != cidx {
|
|
|
|
return false, nil
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
if err := autopilotSetConfigTxn(tx, idx, config); err != nil {
|
2019-03-01 19:28:35 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2017-02-24 21:08:49 +00:00
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2017-02-24 21:08:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func autopilotSetConfigTxn(tx WriteTxn, idx uint64, config *structs.AutopilotConfig) error {
|
2017-02-24 21:08:49 +00:00
|
|
|
// Check for an existing config
|
|
|
|
existing, err := tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed autopilot config lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the indexes.
|
|
|
|
if existing != nil {
|
2020-09-25 17:46:38 +00:00
|
|
|
config.CreateIndex = existing.(*structs.AutopilotConfig).CreateIndex
|
2017-02-24 21:08:49 +00:00
|
|
|
} else {
|
|
|
|
config.CreateIndex = idx
|
|
|
|
}
|
|
|
|
config.ModifyIndex = idx
|
|
|
|
|
|
|
|
if err := tx.Insert("autopilot-config", config); err != nil {
|
|
|
|
return fmt.Errorf("failed updating autopilot config: %s", err)
|
|
|
|
}
|
2017-02-24 04:32:13 +00:00
|
|
|
return nil
|
|
|
|
}
|