2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-01-15 19:46:12 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2018-01-30 03:53:34 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-12-18 21:16:23 +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{
|
2018-01-18 22:28:58 +00:00
|
|
|
"id": {
|
2017-12-18 21:16:23 +00:00
|
|
|
Name: "id",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.ConditionalIndex{
|
|
|
|
Conditional: func(obj interface{}) (bool, error) { return true, nil },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotConfig is used to get the current Autopilot configuration.
|
2018-01-30 03:53:34 +00:00
|
|
|
func (s *StateStore) AutopilotConfig() (uint64, *structs.AutopilotConfig, error) {
|
2020-09-04 13:53:36 +00:00
|
|
|
tx := s.db.ReadTxn()
|
2017-12-18 21:16:23 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Get the autopilot config
|
|
|
|
c, err := tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed autopilot config lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
config, ok := c.(*structs.AutopilotConfig)
|
2017-12-18 21:16:23 +00:00
|
|
|
if !ok {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.ModifyIndex, config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotSetConfig is used to set the current Autopilot configuration.
|
2020-08-27 14:18:53 +00:00
|
|
|
func (s *StateStore) AutopilotSetConfig(index uint64, config *structs.AutopilotConfig) error {
|
|
|
|
tx := s.db.WriteTxn(index)
|
2017-12-18 21:16:23 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-04-21 06:54:50 +00:00
|
|
|
if err := s.autopilotSetConfigTxn(index, tx, config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-04-21 06:54:50 +00:00
|
|
|
return tx.Commit()
|
2017-12-18 21:16:23 +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-08-27 14:18:53 +00:00
|
|
|
func (s *StateStore) AutopilotCASConfig(index, cidx uint64, config *structs.AutopilotConfig) (bool, error) {
|
|
|
|
tx := s.db.WriteTxn(index)
|
2017-12-18 21:16:23 +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.
|
2018-01-30 03:53:34 +00:00
|
|
|
e, ok := existing.(*structs.AutopilotConfig)
|
2017-12-18 21:16:23 +00:00
|
|
|
if !ok || e.ModifyIndex != cidx {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 06:54:50 +00:00
|
|
|
if err := s.autopilotSetConfigTxn(index, tx, config); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-04-21 06:54:50 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 14:18:53 +00:00
|
|
|
func (s *StateStore) autopilotSetConfigTxn(idx uint64, tx *txn, config *structs.AutopilotConfig) error {
|
2017-12-18 21:16:23 +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 {
|
2018-01-30 03:53:34 +00:00
|
|
|
config.CreateIndex = existing.(*structs.AutopilotConfig).CreateIndex
|
2017-12-18 21:16:23 +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)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|