Skip to content

Commit cf630d5

Browse files
committed
Get and put the cluster state in cluster discovery payload
1 parent 0043784 commit cf630d5

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

internal/core/cluster/cluster.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
stonithResourceMissing string = "notconfigured"
3131
stonithAgent string = "stonith:"
3232
sbdFencingAgentName string = "external/sbd"
33+
unknownState string = "unknown"
3334
)
3435

3536
type DiscoveryTools struct {
@@ -41,6 +42,7 @@ type DiscoveryTools struct {
4142
SBDConfigPath string
4243
CommandExecutor utils.CommandExecutor
4344
SystemdConnector systemd.Systemd
45+
CmdClient CmdClient
4446
}
4547

4648
type BasicInfo struct {
@@ -57,6 +59,7 @@ type Cluster struct {
5759
DC bool
5860
Provider string
5961
Online bool
62+
State string
6063
}
6164

6265
func Md5sumFile(filePath string) (string, error) {
@@ -90,6 +93,7 @@ func NewCluster(ctx context.Context) (*Cluster, error) {
9093
SBDConfigPath: SBDConfigPath,
9194
CommandExecutor: utils.Executor{},
9295
SystemdConnector: systemdConn,
96+
CmdClient: NewDefaultCmdClient(),
9397
})
9498
}
9599

@@ -105,7 +109,7 @@ func NewClusterWithDiscoveryTools(ctx context.Context, discoveryTools *Discovery
105109
if !isHostOnline(ctx, discoveryTools) {
106110
return makeOfflineHostPayload(detectedCluster)
107111
}
108-
return makeOnlineHostPayload(detectedCluster, discoveryTools)
112+
return makeOnlineHostPayload(ctx, detectedCluster, discoveryTools)
109113
}
110114

111115
func detectCluster(discoveryTools *DiscoveryTools) (BasicInfo, bool, error) {
@@ -153,14 +157,22 @@ func makeOfflineHostPayload(detectedCluster BasicInfo) (*Cluster, error) {
153157
}, nil
154158
}
155159

156-
func makeOnlineHostPayload(detectedCluster BasicInfo, discoveryTools *DiscoveryTools) (*Cluster, error) {
160+
func makeOnlineHostPayload(
161+
ctx context.Context, detectedCluster BasicInfo, discoveryTools *DiscoveryTools,
162+
) (*Cluster, error) {
157163
cibParser := cib.NewCibAdminParser(discoveryTools.CibAdmPath)
158164

159165
cibConfig, err := cibParser.Parse()
160166
if err != nil {
161167
return nil, err
162168
}
163169

170+
state, err := discoveryTools.CmdClient.GetState(ctx)
171+
if err != nil {
172+
slog.Error("Error discovering cluster state", "error", err)
173+
state = unknownState
174+
}
175+
164176
var cluster = &Cluster{
165177
Cib: cib.Root{}, //nolint
166178
Crmmon: crmmon.Root{}, //nolint
@@ -170,6 +182,7 @@ func makeOnlineHostPayload(detectedCluster BasicInfo, discoveryTools *DiscoveryT
170182
DC: false,
171183
Provider: "",
172184
Online: true,
185+
State: strings.ToLower(strings.TrimPrefix(state, "S_")),
173186
}
174187

175188
cluster.Cib = cibConfig

internal/core/cluster/cluster_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/trento-project/agent/internal/core/cluster"
1212
"github.com/trento-project/agent/internal/core/cluster/cib"
1313
"github.com/trento-project/agent/internal/core/cluster/crmmon"
14+
mocksCluster "github.com/trento-project/agent/internal/core/cluster/mocks"
1415
mocksSystemd "github.com/trento-project/agent/internal/core/systemd/mocks"
1516
"github.com/trento-project/agent/pkg/utils/mocks"
1617
"github.com/trento-project/agent/test/helpers"
@@ -42,6 +43,9 @@ func (suite *ClusterTestSuite) TestNewClusterWithDiscoveryTools() {
4243
mockSystemd.On("IsActive", ctx, "corosync.service").Return(true, nil)
4344
mockSystemd.On("IsActive", ctx, "pacemaker.service").Return(true, nil)
4445

46+
mockCmdClient := new(mocksCluster.MockCmdClient)
47+
mockCmdClient.On("GetState", ctx).Return("S_IDLE", nil)
48+
4549
c, err := cluster.NewClusterWithDiscoveryTools(ctx, &cluster.DiscoveryTools{
4650
CibAdmPath: helpers.GetFixturePath("discovery/cluster/fake_cibadmin.sh"),
4751
CrmmonAdmPath: helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"),
@@ -51,13 +55,15 @@ func (suite *ClusterTestSuite) TestNewClusterWithDiscoveryTools() {
5155
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config"),
5256
CommandExecutor: mockCommand,
5357
SystemdConnector: mockSystemd,
58+
CmdClient: mockCmdClient,
5459
})
5560

5661
suite.Equal("hana_cluster", c.Name)
5762
suite.Equal("47d1190ffb4f781974c8356d7f863b03", c.ID)
5863
suite.Equal(false, c.DC)
5964
suite.Equal("azure", c.Provider)
6065
suite.Equal("/dev/vdc;/dev/vdb", c.SBD.Config["SBD_DEVICE"])
66+
suite.Equal("idle", c.State)
6167
suite.NoError(err)
6268
}
6369

@@ -71,6 +77,9 @@ func (suite *ClusterTestSuite) TestNewClusterDisklessSBD() {
7177
mockSystemd.On("IsActive", ctx, "corosync.service").Return(true, nil)
7278
mockSystemd.On("IsActive", ctx, "pacemaker.service").Return(true, nil)
7379

80+
mockCmdClient := new(mocksCluster.MockCmdClient)
81+
mockCmdClient.On("GetState", ctx).Return("S_IDLE", nil)
82+
7483
c, err := cluster.NewClusterWithDiscoveryTools(ctx, &cluster.DiscoveryTools{
7584
CibAdmPath: helpers.GetFixturePath("discovery/cluster/fake_cibadmin.sh"),
7685
CrmmonAdmPath: helpers.GetFixturePath("discovery/cluster/fake_crm_mon_diskless_sbd.sh"),
@@ -80,6 +89,7 @@ func (suite *ClusterTestSuite) TestNewClusterDisklessSBD() {
8089
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config_no_device"),
8190
CommandExecutor: mockCommand,
8291
SystemdConnector: mockSystemd,
92+
CmdClient: mockCmdClient,
8393
})
8494

8595
suite.Equal("hana_cluster", c.Name)
@@ -116,6 +126,7 @@ func (suite *ClusterTestSuite) TestNewClusterWithOfflineHost() {
116126
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config"),
117127
CommandExecutor: mockCommand,
118128
SystemdConnector: mockSystemd,
129+
CmdClient: new(mocksCluster.MockCmdClient),
119130
})
120131

121132
suite.Equal("hana_cluster", c.Name)
@@ -148,6 +159,7 @@ func (suite *ClusterTestSuite) TestNewClusterWithOfflineHostNoName() {
148159
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config"),
149160
CommandExecutor: mockCommand,
150161
SystemdConnector: mockSystemd,
162+
CmdClient: new(mocksCluster.MockCmdClient),
151163
})
152164

153165
suite.Equal("", c.Name)
@@ -156,6 +168,39 @@ func (suite *ClusterTestSuite) TestNewClusterWithOfflineHostNoName() {
156168
suite.NoError(err)
157169
}
158170

171+
func (suite *ClusterTestSuite) TestNewClusterWithUnknownState() {
172+
ctx := context.Background()
173+
mockCommand := new(mocks.MockCommandExecutor)
174+
mockCommand.On("Output", "/usr/sbin/dmidecode", "-s", "chassis-asset-tag").
175+
Return([]byte("7783-7084-3265-9085-8269-3286-77"), nil)
176+
mockCommand.On("Output", "/usr/sbin/sbd", "-d", "/dev/vdb", "dump").Return(mockSbdDump(), nil)
177+
mockCommand.On("Output", "/usr/sbin/sbd", "-d", "/dev/vdb", "list").Return(mockSbdList(), nil)
178+
mockCommand.On("Output", "/usr/sbin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDump(), nil)
179+
mockCommand.On("Output", "/usr/sbin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdList(), nil)
180+
181+
mockSystemd := new(mocksSystemd.MockSystemd)
182+
mockSystemd.On("IsActive", ctx, "corosync.service").Return(true, nil)
183+
mockSystemd.On("IsActive", ctx, "pacemaker.service").Return(true, nil)
184+
185+
mockCmdClient := new(mocksCluster.MockCmdClient)
186+
mockCmdClient.On("GetState", ctx).Return("", errors.New(""))
187+
188+
c, err := cluster.NewClusterWithDiscoveryTools(ctx, &cluster.DiscoveryTools{
189+
CibAdmPath: helpers.GetFixturePath("discovery/cluster/fake_cibadmin.sh"),
190+
CrmmonAdmPath: helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"),
191+
CorosyncKeyPath: helpers.GetFixturePath("discovery/cluster/authkey"),
192+
CorosyncConfigPath: helpers.GetFixturePath("discovery/cluster/corosync.conf"),
193+
SBDPath: "/usr/sbin/sbd",
194+
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config"),
195+
CommandExecutor: mockCommand,
196+
SystemdConnector: mockSystemd,
197+
CmdClient: mockCmdClient,
198+
})
199+
200+
suite.Equal("unknown", c.State)
201+
suite.NoError(err)
202+
}
203+
159204
func (suite *ClusterTestSuite) TestNewClusterCorosyncNotConfigured() {
160205
ctx := context.Background()
161206
c, err := cluster.NewClusterWithDiscoveryTools(ctx, &cluster.DiscoveryTools{
@@ -167,6 +212,7 @@ func (suite *ClusterTestSuite) TestNewClusterCorosyncNotConfigured() {
167212
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config_no_device"),
168213
CommandExecutor: new(mocks.MockCommandExecutor),
169214
SystemdConnector: new(mocksSystemd.MockSystemd),
215+
CmdClient: new(mocksCluster.MockCmdClient),
170216
})
171217

172218
suite.Nil(c)
@@ -184,6 +230,7 @@ func (suite *ClusterTestSuite) TestNewClusterCorosyncNoAuthkeyConfigured() {
184230
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config_no_device"),
185231
CommandExecutor: new(mocks.MockCommandExecutor),
186232
SystemdConnector: new(mocksSystemd.MockSystemd),
233+
CmdClient: new(mocksCluster.MockCmdClient),
187234
})
188235

189236
suite.Nil(c)

internal/discovery/mocks/discovered_cluster_mock.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/trento-project/agent/internal/core/cloud"
77
"github.com/trento-project/agent/internal/core/cluster"
8+
mocksCluster "github.com/trento-project/agent/internal/core/cluster/mocks"
89
mocksSystemd "github.com/trento-project/agent/internal/core/systemd/mocks"
910
mocksUtils "github.com/trento-project/agent/pkg/utils/mocks"
1011
"github.com/trento-project/agent/test/helpers"
@@ -44,6 +45,9 @@ func NewDiscoveredClusterMock() *cluster.Cluster {
4445
mockSystemd.On("IsActive", ctx, "corosync.service").Return(true, nil)
4546
mockSystemd.On("IsActive", ctx, "pacemaker.service").Return(true, nil)
4647

48+
mockCmdClient := new(mocksCluster.MockCmdClient)
49+
mockCmdClient.On("GetState", ctx).Return("S_IDLE", nil)
50+
4751
cluster, _ := cluster.NewClusterWithDiscoveryTools(ctx, &cluster.DiscoveryTools{
4852
CibAdmPath: helpers.GetFixturePath("discovery/cluster/fake_cibadmin.sh"),
4953
CrmmonAdmPath: helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"),
@@ -53,6 +57,7 @@ func NewDiscoveredClusterMock() *cluster.Cluster {
5357
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config"),
5458
CommandExecutor: mockCommand,
5559
SystemdConnector: mockSystemd,
60+
CmdClient: mockCmdClient,
5661
})
5762

5863
cluster.Provider = cloud.Azure

test/fixtures/discovery/cluster/expected_published_cluster_discovery.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@
10381038
"Name": "hana_cluster",
10391039
"DC": false,
10401040
"Provider": "azure",
1041-
"Online": true
1041+
"Online": true,
1042+
"State": "idle"
10421043
}
10431044
}

0 commit comments

Comments
 (0)