Skip to content

Commit

Permalink
Fix make [golangci|operator-lint]
Browse files Browse the repository at this point in the history
  • Loading branch information
lpiwowar committed Aug 5, 2024
1 parent ec08d8b commit 25276b6
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 101 deletions.
37 changes: 18 additions & 19 deletions controllers/ansibletest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *AnsibleTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// Ensure that there is an external counter and read its value
// We use the external counter to keep track of the workflow steps
r.WorkflowStepCounterCreate(ctx, instance, helper)
externalWorkflowCounter := r.WorkflowStepCounterRead(ctx, instance, helper)
externalWorkflowCounter := r.WorkflowStepCounterRead(ctx, instance)
if externalWorkflowCounter == -1 {
return ctrl.Result{RequeueAfter: requeueAfter}, nil
}
Expand Down Expand Up @@ -188,9 +188,8 @@ func (r *AnsibleTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
logging.Info("Can not acquire lock")
requeueAfter := time.Second * 60
return ctrl.Result{RequeueAfter: requeueAfter}, nil
} else {
logging.Info("Lock acquired")
}
logging.Info("Lock acquired")

if workflowActive {
r.WorkflowStepCounterIncrease(ctx, instance, helper)
Expand All @@ -201,9 +200,13 @@ func (r *AnsibleTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// Create a new job
mountCerts := r.CheckSecretExists(ctx, instance, "combined-ca-bundle")
jobName := r.GetJobName(instance, externalWorkflowCounter)
envVars, workflowOverrideParams := r.PrepareAnsibleEnv(ctx, serviceLabels, instance, helper, externalWorkflowCounter)
envVars, workflowOverrideParams := r.PrepareAnsibleEnv(instance, externalWorkflowCounter)
logsPVCName := r.GetPVCLogsName(instance, 0)
containerImage := r.GetContainerImage(ctx, helper, workflowOverrideParams["ContainerImage"], instance)
containerImage, err := r.GetContainerImage(ctx, workflowOverrideParams["ContainerImage"], instance)
if err != nil {
return ctrl.Result{}, err
}

jobDef := ansibletest.Job(
instance,
serviceLabels,
Expand Down Expand Up @@ -261,7 +264,6 @@ func (r *AnsibleTestReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *Reconciler) OverwriteAnsibleWithWorkflow(
ctx context.Context,
instance v1beta1.AnsibleTestSpec,
sectionName string,
workflowValueType string,
Expand Down Expand Up @@ -301,44 +303,41 @@ func (r *Reconciler) OverwriteAnsibleWithWorkflow(

// This function prepares env variables for a single workflow step.
func (r *AnsibleTestReconciler) PrepareAnsibleEnv(
ctx context.Context,
labels map[string]string,
instance *testv1beta1.AnsibleTest,
helper *helper.Helper,
step int,
) (map[string]env.Setter, map[string]string) {
// Prepare env vars
envVars := make(map[string]env.Setter)
workflowOverrideParams := make(map[string]string)

// volumes workflow override
workflowOverrideParams["WorkloadSSHKeySecretName"] = r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "WorkloadSSHKeySecretName", "string", step).(string)
workflowOverrideParams["ComputesSSHKeySecretName"] = r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "ComputesSSHKeySecretName", "string", step).(string)
workflowOverrideParams["ContainerImage"] = r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "ContainerImage", "string", step).(string)
workflowOverrideParams["WorkloadSSHKeySecretName"] = r.OverwriteAnsibleWithWorkflow(instance.Spec, "WorkloadSSHKeySecretName", "string", step).(string)
workflowOverrideParams["ComputesSSHKeySecretName"] = r.OverwriteAnsibleWithWorkflow(instance.Spec, "ComputesSSHKeySecretName", "string", step).(string)
workflowOverrideParams["ContainerImage"] = r.OverwriteAnsibleWithWorkflow(instance.Spec, "ContainerImage", "string", step).(string)

// bool
debug := r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "Debug", "pbool", step).(bool)
debug := r.OverwriteAnsibleWithWorkflow(instance.Spec, "Debug", "pbool", step).(bool)
if debug {
envVars["POD_DEBUG"] = env.SetValue("true")
}

// strings
extraVars := r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "AnsibleExtraVars", "string", step).(string)
extraVars := r.OverwriteAnsibleWithWorkflow(instance.Spec, "AnsibleExtraVars", "string", step).(string)
envVars["POD_ANSIBLE_EXTRA_VARS"] = env.SetValue(extraVars)

extraVarsFile := r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "AnsibleVarFiles", "string", step).(string)
extraVarsFile := r.OverwriteAnsibleWithWorkflow(instance.Spec, "AnsibleVarFiles", "string", step).(string)
envVars["POD_ANSIBLE_FILE_EXTRA_VARS"] = env.SetValue(extraVarsFile)

inventory := r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "AnsibleInventory", "string", step).(string)
inventory := r.OverwriteAnsibleWithWorkflow(instance.Spec, "AnsibleInventory", "string", step).(string)
envVars["POD_ANSIBLE_INVENTORY"] = env.SetValue(inventory)

gitRepo := r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "AnsibleGitRepo", "string", step).(string)
gitRepo := r.OverwriteAnsibleWithWorkflow(instance.Spec, "AnsibleGitRepo", "string", step).(string)
envVars["POD_ANSIBLE_GIT_REPO"] = env.SetValue(gitRepo)

playbookPath := r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "AnsiblePlaybookPath", "string", step).(string)
playbookPath := r.OverwriteAnsibleWithWorkflow(instance.Spec, "AnsiblePlaybookPath", "string", step).(string)
envVars["POD_ANSIBLE_PLAYBOOK"] = env.SetValue(playbookPath)

ansibleCollections := r.OverwriteAnsibleWithWorkflow(ctx, instance.Spec, "AnsibleCollections", "string", step).(string)
ansibleCollections := r.OverwriteAnsibleWithWorkflow(instance.Spec, "AnsibleCollections", "string", step).(string)
envVars["POD_INSTALL_COLLECTIONS"] = env.SetValue(ansibleCollections)

return envVars, workflowOverrideParams
Expand Down
110 changes: 60 additions & 50 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,111 +65,126 @@ func GetCustomDataConfigMapName(instance interface{}, workflowStepNum int) strin

func (r *Reconciler) GetContainerImage(
ctx context.Context,
helper *helper.Helper,
containerImage string,
instance interface{},
) string {
) (string, error) {
cm := &corev1.ConfigMap{}
testOperatorConfigMapName := "test-operator-config"
if typedInstance, ok := instance.(*v1beta1.Tempest); ok {
if len(containerImage) > 0 {
return containerImage
return containerImage, nil
}

objectKey := client.ObjectKey{Namespace: typedInstance.Namespace, Name: testOperatorConfigMapName}
r.Client.Get(ctx, objectKey, cm)
err := r.Client.Get(ctx, objectKey, cm)
if err != nil {
return "", err
}

if cm.Data == nil {
return util.GetEnvVar("RELATED_IMAGE_TEST_TEMPEST_IMAGE_URL_DEFAULT", "")
return util.GetEnvVar("RELATED_IMAGE_TEST_TEMPEST_IMAGE_URL_DEFAULT", ""), nil

}

if cmImage, exists := cm.Data["tempest-image"]; exists {
return cmImage
return cmImage, nil
}

return util.GetEnvVar("RELATED_IMAGE_TEST_TEMPEST_IMAGE_URL_DEFAULT", "")
return util.GetEnvVar("RELATED_IMAGE_TEST_TEMPEST_IMAGE_URL_DEFAULT", ""), nil
} else if typedInstance, ok := instance.(*v1beta1.Tobiko); ok {
if len(containerImage) > 0 {
return containerImage
return containerImage, nil
}

objectKey := client.ObjectKey{Namespace: typedInstance.Namespace, Name: testOperatorConfigMapName}
r.Client.Get(ctx, objectKey, cm)
err := r.Client.Get(ctx, objectKey, cm)
if err != nil {
return "", err
}

if cm.Data == nil {
return util.GetEnvVar("RELATED_IMAGE_TEST_TOBIKO_IMAGE_URL_DEFAULT", "")
return util.GetEnvVar("RELATED_IMAGE_TEST_TOBIKO_IMAGE_URL_DEFAULT", ""), nil

}

if cmImage, exists := cm.Data["tobiko-image"]; exists {
return cmImage
return cmImage, nil
}

return util.GetEnvVar("RELATED_IMAGE_TEST_TOBIKO_IMAGE_URL_DEFAULT", "")
return util.GetEnvVar("RELATED_IMAGE_TEST_TOBIKO_IMAGE_URL_DEFAULT", ""), nil
} else if typedInstance, ok := instance.(*v1beta1.HorizonTest); ok {
if len(containerImage) > 0 {
return containerImage
return containerImage, nil
}

objectKey := client.ObjectKey{Namespace: typedInstance.Namespace, Name: testOperatorConfigMapName}
r.Client.Get(ctx, objectKey, cm)
err := r.Client.Get(ctx, objectKey, cm)
if err != nil {
return "", err
}

if cm.Data == nil {
return util.GetEnvVar("RELATED_IMAGE_TEST_HORIZONTEST_IMAGE_URL_DEFAULT", "")
return util.GetEnvVar("RELATED_IMAGE_TEST_HORIZONTEST_IMAGE_URL_DEFAULT", ""), nil

}

if cmImage, exists := cm.Data["horizontest-image"]; exists {
return cmImage
return cmImage, nil
}

return util.GetEnvVar("RELATED_IMAGE_TEST_HORIZONTEST_IMAGE_URL_DEFAULT", "")
return util.GetEnvVar("RELATED_IMAGE_TEST_HORIZONTEST_IMAGE_URL_DEFAULT", ""), nil
} else if typedInstance, ok := instance.(*v1beta1.AnsibleTest); ok {
if len(containerImage) > 0 {
return containerImage
return containerImage, nil
}

objectKey := client.ObjectKey{Namespace: typedInstance.Namespace, Name: testOperatorConfigMapName}
r.Client.Get(ctx, objectKey, cm)
err := r.Client.Get(ctx, objectKey, cm)
if err != nil {
return "", err
}

if cm.Data == nil {
return util.GetEnvVar("RELATED_IMAGE_TEST_ANSIBLETEST_IMAGE_URL_DEFAULT", "")
return util.GetEnvVar("RELATED_IMAGE_TEST_ANSIBLETEST_IMAGE_URL_DEFAULT", ""), nil
}

if cmImage, exists := cm.Data["ansibletest-image"]; exists {
return cmImage
return cmImage, nil
}

return util.GetEnvVar("RELATED_IMAGE_TEST_ANSIBLETEST_IMAGE_URL_DEFAULT", "")
return util.GetEnvVar("RELATED_IMAGE_TEST_ANSIBLETEST_IMAGE_URL_DEFAULT", ""), nil
}

return ""
return "", nil
}

func (r *Reconciler) GetJobName(instance interface{}, workflowStepNum int) string {
if typedInstance, ok := instance.(*v1beta1.Tobiko); ok {
if len(typedInstance.Spec.Workflow) == 0 || workflowStepNum == workflowStepNumInvalid {
return typedInstance.Name
} else {
workflowStepName := typedInstance.Spec.Workflow[workflowStepNum].StepName
return typedInstance.Name + "-" + workflowStepName + jobNameStepInfix + strconv.Itoa(workflowStepNum)
}

workflowStepName := typedInstance.Spec.Workflow[workflowStepNum].StepName
return typedInstance.Name + "-" + workflowStepName + jobNameStepInfix + strconv.Itoa(workflowStepNum)
} else if typedInstance, ok := instance.(*v1beta1.Tempest); ok {
if len(typedInstance.Spec.Workflow) == 0 || workflowStepNum == workflowStepNumInvalid {
return typedInstance.Name
} else {
workflowStepName := typedInstance.Spec.Workflow[workflowStepNum].StepName
return typedInstance.Name + "-" + workflowStepName + jobNameStepInfix + strconv.Itoa(workflowStepNum)
}

workflowStepName := typedInstance.Spec.Workflow[workflowStepNum].StepName
return typedInstance.Name + "-" + workflowStepName + jobNameStepInfix + strconv.Itoa(workflowStepNum)
} else if typedInstance, ok := instance.(*v1beta1.HorizonTest); ok {
return typedInstance.Name
} else if typedInstance, ok := instance.(*v1beta1.AnsibleTest); ok {
if len(typedInstance.Spec.Workflow) == 0 || workflowStepNum == workflowStepNumInvalid {
return typedInstance.Name
} else {
workflowStepName := typedInstance.Spec.Workflow[workflowStepNum].StepName
return typedInstance.Name + "-" + workflowStepName + jobNameStepInfix + strconv.Itoa(workflowStepNum)
}
} else {
return ""

workflowStepName := typedInstance.Spec.Workflow[workflowStepNum].StepName
return typedInstance.Name + "-" + workflowStepName + jobNameStepInfix + strconv.Itoa(workflowStepNum)
}

return ""
}

func (r *Reconciler) GetWorkflowConfigMapName(instance client.Object) string {
Expand All @@ -190,9 +205,9 @@ func (r *Reconciler) CheckSecretExists(ctx context.Context, instance client.Obje
err := r.Client.Get(ctx, client.ObjectKey{Namespace: instance.GetNamespace(), Name: secretName}, secret)
if err != nil && k8s_errors.IsNotFound(err) {
return false
} else {
return true
}

return true
}

func GetStringHash(str string, hashLength int) string {
Expand Down Expand Up @@ -267,19 +282,19 @@ func (r *Reconciler) GetScheme() *runtime.Scheme {
func (r *Reconciler) GetDefaultBool(variable bool) string {
if variable {
return "true"
} else {
return "false"
}

return "false"
}

func (r *Reconciler) GetDefaultInt(variable int64, defaultValue ...string) string {
if variable != 0 {
return strconv.FormatInt(variable, 10)
} else if len(defaultValue) > 0 {
return defaultValue[0]
} else {
return ""
}

return ""
}

func (r *Reconciler) AcquireLock(ctx context.Context, instance client.Object, h *helper.Helper, parallel bool) bool {
Expand All @@ -301,9 +316,9 @@ func (r *Reconciler) AcquireLock(ctx context.Context, instance client.Object, h

err = configmap.EnsureConfigMaps(ctx, h, instance, cms, nil)
return err == nil
} else {
return false
}

return false
}

func (r *Reconciler) ReleaseLock(ctx context.Context, instance client.Object) bool {
Expand Down Expand Up @@ -363,7 +378,7 @@ func (r *Reconciler) WorkflowStepCounterIncrease(ctx context.Context, instance c
return err == nil
}

func (r *Reconciler) WorkflowStepCounterRead(ctx context.Context, instance client.Object, h *helper.Helper) int {
func (r *Reconciler) WorkflowStepCounterRead(ctx context.Context, instance client.Object) int {
cm := &corev1.ConfigMap{}
err := r.Client.Get(ctx, client.ObjectKey{Namespace: instance.GetNamespace(), Name: r.GetWorkflowConfigMapName(instance)}, cm)
if err != nil {
Expand Down Expand Up @@ -392,12 +407,8 @@ func (r *Reconciler) CompletedJobExists(ctx context.Context, instance client.Obj
func (r *Reconciler) JobExists(ctx context.Context, instance client.Object, workflowStepNum int) bool {
job := &batchv1.Job{}
err := r.Client.Get(ctx, client.ObjectKey{Namespace: instance.GetNamespace(), Name: r.GetJobName(instance, workflowStepNum)}, job)
if err != nil {
if k8s_errors.IsNotFound(err) {
return false
} else {
return false
}
if err != nil && k8s_errors.IsNotFound(err) {
return false
}

return true
Expand All @@ -410,7 +421,6 @@ func (r *Reconciler) setConfigOverwrite(customData map[string]string, configOver
}

func (r *Reconciler) OverwriteValueWithWorkflow(
ctx context.Context,
instance v1beta1.TobikoSpec,
sectionName string,
workflowValueType string,
Expand Down
17 changes: 9 additions & 8 deletions controllers/horizontest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (r *HorizonTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// NOTE(lpiwowar): This is a workaround since the Horizontest CR does not support
// workflows. However, the label might be required by automation that
// consumes the test-operator (e.g., ci-framework).
"workflowStep": "0",
"workflowStep": "0",
}

result, err := r.EnsureHorizonTestCloudsYAML(ctx, instance, helper, serviceLabels)
Expand Down Expand Up @@ -166,10 +166,14 @@ func (r *HorizonTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
logging.Info("Lock acquired")

// Prepare HorizonTest env vars
envVars := r.PrepareHorizonTestEnvVars(ctx, serviceLabels, instance, helper)
envVars := r.PrepareHorizonTestEnvVars(instance)
jobName := r.GetJobName(instance, 0)
logsPVCName := r.GetPVCLogsName(instance, 0)
containerImage := r.GetContainerImage(ctx, helper, instance.Spec.ContainerImage, instance)
containerImage, err := r.GetContainerImage(ctx, instance.Spec.ContainerImage, instance)
if err != nil {
return ctrl.Result{}, err
}

jobDef := horizontest.Job(
instance,
serviceLabels,
Expand Down Expand Up @@ -236,8 +240,8 @@ func (r *HorizonTestReconciler) EnsureHorizonTestCloudsYAML(ctx context.Context,
}

clouds := result["clouds"].(map[string]interface{})
default_value := clouds["default"].(map[string]interface{})
auth := default_value["auth"].(map[string]interface{})
defaultValue := clouds["default"].(map[string]interface{})
auth := defaultValue["auth"].(map[string]interface{})

if _, ok := auth["password"].(string); !ok {
auth["password"] = "12345678"
Expand Down Expand Up @@ -267,10 +271,7 @@ func (r *HorizonTestReconciler) EnsureHorizonTestCloudsYAML(ctx context.Context,
}

func (r *HorizonTestReconciler) PrepareHorizonTestEnvVars(
ctx context.Context,
labels map[string]string,
instance *testv1beta1.HorizonTest,
helper *helper.Helper,
) map[string]env.Setter {
// Prepare env vars
envVars := make(map[string]env.Setter)
Expand Down
Loading

0 comments on commit 25276b6

Please sign in to comment.