From c5eb86063da8826f23cabcb002ac34b99b732960 Mon Sep 17 00:00:00 2001 From: Keiichi Kii Date: Wed, 17 Jan 2024 15:57:29 -0500 Subject: [PATCH] Support off action --- .../fenceagentsremediation_controller.go | 26 ++++++++---- .../fenceagentsremediation_controller_test.go | 41 +++++++++++++++++++ 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/controllers/fenceagentsremediation_controller.go b/controllers/fenceagentsremediation_controller.go index 20579940..9b57d3d2 100644 --- a/controllers/fenceagentsremediation_controller.go +++ b/controllers/fenceagentsremediation_controller.go @@ -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 @@ -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") @@ -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 diff --git a/controllers/fenceagentsremediation_controller_test.go b/controllers/fenceagentsremediation_controller_test.go index 01da0b11..b6f8864d 100644 --- a/controllers/fenceagentsremediation_controller_test.go +++ b/controllers/fenceagentsremediation_controller_test.go @@ -99,6 +99,47 @@ var _ = Describe("FAR Controller", func() { }) Context("buildFenceAgentParams", func() { + Context("build fence agent params", func() { + 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)