Skip to content

Commit 22505e3

Browse files
author
eliranb
committed
Refactor determineWorkloadType function to improve configuration validation for LightrunJavaAgent. Enhance error handling by ensuring only one configuration method (legacy or new) is used, and provide clearer error messages for invalid configurations.
1 parent 33831b3 commit 22505e3

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

internal/controller/lightrunjavaagent_controller.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,33 @@ func (r *LightrunJavaAgentReconciler) Reconcile(ctx context.Context, req ctrl.Re
8585
}
8686

8787
func (r *LightrunJavaAgentReconciler) determineWorkloadType(lightrunJavaAgent *agentv1beta.LightrunJavaAgent) (agentv1beta.WorkloadType, error) {
88+
// Get the spec from the LightrunJavaAgent resource
8889
spec := lightrunJavaAgent.Spec
89-
// Check which configuration approach is being used
90+
91+
// Check if legacy deploymentName field is configured
9092
var isDeploymentConfigured bool = spec.DeploymentName != ""
91-
var isWorkloadConfigured bool = spec.WorkloadName != "" || spec.WorkloadType != ""
93+
// Check if new workload configuration fields are set
94+
var isWorkloadConfigured bool = spec.WorkloadName != "" && spec.WorkloadType != ""
9295

93-
// === Case 1: Legacy only — DeploymentName only ===
94-
if isDeploymentConfigured && !isWorkloadConfigured {
95-
r.Log.Info("Using deprecated field deploymentName, consider migrating to workloadName and workloadType")
96-
return agentv1beta.WorkloadTypeDeployment, nil
96+
// Error if both legacy and new configuration methods are used
97+
if isDeploymentConfigured && isWorkloadConfigured {
98+
return "", errors.New("invalid configuration: use either deploymentName (legacy) OR workloadName with workloadType, not both")
9799
}
98100

99-
// === Case 2: New fields — WorkloadName + WorkloadType ===
100-
if !isDeploymentConfigured && isWorkloadConfigured {
101-
if spec.WorkloadType == "" {
102-
return "", errors.New("workloadType must be set when using workloadName")
103-
}
104-
if spec.WorkloadName == "" {
105-
return "", errors.New("workloadName must be set when workloadType is specified")
106-
}
107-
return spec.WorkloadType, nil
101+
// Error if neither configuration method is used
102+
if !isDeploymentConfigured && !isWorkloadConfigured {
103+
return "", errors.New("invalid configuration: must set either DeploymentName (legacy) or WorkloadName with WorkloadType")
108104
}
109105

110-
// === Case 3: Misconfigured — Both fields exists or both are empty ===
111-
if isDeploymentConfigured && isWorkloadConfigured {
112-
return "", errors.New("invalid configuration: use either deploymentName (legacy) OR workloadName with workloadType, not both")
106+
switch {
107+
case isDeploymentConfigured:
108+
r.Log.Info("Using deprecated field deploymentName, consider migrating to workloadName and workloadType")
109+
return agentv1beta.WorkloadTypeDeployment, nil
110+
case isWorkloadConfigured:
111+
return spec.WorkloadType, nil
112+
default:
113+
return "", errors.New("unable to determine workload type, please check your configuration")
113114
}
114-
115-
// === Case 4: Fully empty or malformed ===
116-
return "", errors.New("invalid configuration: must set either DeploymentName (legacy) or WorkloadName with WorkloadType")
117115
}
118116

119117
// reconcileDeployment handles the reconciliation logic for Deployment workloads

0 commit comments

Comments
 (0)