Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Support off action #124

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions controllers/fenceagentsremediation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ const (
// errors
errorMissingParams = "nodeParameters or sharedParameters or both are missing, and they cannot be empty"
errorMissingNodeParams = "node parameter is required, and cannot be empty"
errorUnsupportedAction = "FAR doesn't support any other action than reboot and off"

SuccessFAResponse = "Success: Rebooted"
parameterActionName = "--action"
parameterActionValue = "reboot"
SuccessFAResponse = "Success: Rebooted"
parameterActionName = "--action"
parameterRebootActionValue = "reboot"
parameterOffActionValue = "off"
)

// FenceAgentsRemediationReconciler reconciles a FenceAgentsRemediation object
Expand Down Expand Up @@ -279,9 +281,10 @@ func (r *FenceAgentsRemediationReconciler) updateStatus(ctx context.Context, far
}

// buildFenceAgentParams collects the FAR's parameters for the node based on FAR CR, and if the CR is missing parameters
// or the CR's name don't match nodeParameter name or it has an action which is different than reboot, then return an error
// or the CR's name don't match nodeParameter name or it has an action which is different than reboot and off, then return an error
func buildFenceAgentParams(far *v1alpha1.FenceAgentsRemediation) ([]string, error) {
logger := ctrl.Log.WithName("build-fa-parameters")
parameterActionValue := parameterRebootActionValue
if far.Spec.NodeParameters == nil || far.Spec.SharedParameters == nil {
err := errors.New(errorMissingParams)
logger.Error(err, "Missing parameters")
Expand All @@ -292,11 +295,16 @@ func buildFenceAgentParams(far *v1alpha1.FenceAgentsRemediation) ([]string, erro
for paramName, paramVal := range far.Spec.SharedParameters {
if paramName != parameterActionName {
fenceAgentParams = appendParamToSlice(fenceAgentParams, paramName, paramVal)
} else if paramVal != parameterActionValue {
// --action attribute was selected but it is different than reboot
err := errors.New("FAR doesn't support any other action than reboot")
logger.Error(err, "can't build CR with this action attribute", "action", paramVal)
return nil, err
} else {
switch paramVal {
case parameterRebootActionValue, parameterOffActionValue:
parameterActionValue = paramVal
default:
// --action attribute was selected but it is different than reboot and off
err := errors.New(errorUnsupportedAction)
logger.Error(err, "can't build CR with this action attribute", "action", paramVal)
return nil, err
}
}
}
// if --action attribute was not selected, then its default value is reboot
Expand Down
41 changes: 41 additions & 0 deletions controllers/fenceagentsremediation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,47 @@ var _ = Describe("FAR Controller", func() {
})

Context("buildFenceAgentParams", func() {
Context("build fence agent params", func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this Context has the same description as the above Context, can we have a better description please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I will change the comment.

baseShareParam := map[v1alpha1.ParameterName]string{
"--username": "admin",
"--password": "password",
"--ip": "192.168.111.1",
"--lanplus": "",
}
testCases := []struct {
name string
action string
expect error
}{
{"reboot action", "reboot", nil},
{"off action", "off", nil},
{"unsupported action", "cycle", errors.New(errorUnsupportedAction)},
}

for _, tc := range testCases {
When(fmt.Sprintf("FAR includes %s", tc.name), func() {
It("should return expected result", func() {
shareParam := baseShareParam
shareParam["--action"] = tc.action
far := getFenceAgentsRemediation(workerNode, fenceAgentIPMI, shareParam, testNodeParam)
shareString, err := buildFenceAgentParams(far)
if tc.expect == nil {
Expect(err).NotTo(HaveOccurred())
Expect(shareString).To(ConsistOf([]string{
"--lanplus",
"--password=password",
"--username=admin",
fmt.Sprintf("--action=%s", tc.action),
"--ip=192.168.111.1",
"--ipport=6233"}))
} else {
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(tc.expect))
}
})
})
}
})
When("FAR include different action than reboot", func() {
It("should succeed with a warning", func() {
invalidValTestFAR := getFenceAgentsRemediation(workerNode, fenceAgentIPMI, invalidShareParam, testNodeParam)
Expand Down