Skip to content

Commit 37d1de2

Browse files
authored
Support templating system to properly handle boolean values (#1162) (#1164)
* support templating system to properly handle boolean values * fix boolean value type error * handle path not found case for object parse * remove label and annotations from objects * polished code --------- Signed-off-by: YuChen <[email protected]>
1 parent ea1f64e commit 37d1de2

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

controllers/operator/manager.go

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,15 @@ func (m *ODLMOperator) processMapObject(ctx context.Context, key string, mapObj
814814
if err != nil {
815815
return err
816816
}
817+
818+
if valueRef == "true" {
819+
finalObject[key] = true
820+
continue
821+
} else if valueRef == "false" {
822+
finalObject[key] = false
823+
continue
824+
}
825+
817826
if valueRef != "" {
818827
// Check if the returned value is a JSON array string and the field should be an array
819828
if strings.HasPrefix(valueRef, "[") && strings.HasSuffix(valueRef, "]") {
@@ -869,6 +878,15 @@ func (m *ODLMOperator) processTemplateValue(ctx context.Context, value interface
869878
return valueRef, nil
870879
}
871880

881+
if boolValue, hasBool := templateRef["boolean"]; hasBool {
882+
if b, ok := boolValue.(bool); ok {
883+
if b {
884+
return "true", nil
885+
}
886+
return "false", nil
887+
}
888+
}
889+
872890
templateRefObj, err := m.convertToTemplateValueRef(templateRef, instanceType, instanceName, instanceNs)
873891
if err != nil {
874892
return "", err
@@ -929,7 +947,13 @@ func (m *ODLMOperator) GetValueFromBranch(ctx context.Context, branch *util.Valu
929947
return "", nil
930948
}
931949

932-
// Handle direct literal value first
950+
if branch.Boolean != nil {
951+
if *branch.Boolean {
952+
return "true", nil
953+
}
954+
return "false", nil
955+
}
956+
933957
if branch.Literal != "" {
934958
return branch.Literal, nil
935959
}
@@ -939,9 +963,14 @@ func (m *ODLMOperator) GetValueFromBranch(ctx context.Context, branch *util.Valu
939963
// Create a slice to hold processed values
940964
var processedValues []interface{}
941965

942-
// Iterate over the array items
943966
for _, item := range branch.Array {
944-
if item.Literal != "" {
967+
if item.Boolean != nil {
968+
if *item.Boolean {
969+
processedValues = append(processedValues, true)
970+
} else {
971+
processedValues = append(processedValues, false)
972+
}
973+
} else if item.Literal != "" {
945974
// For literal values in array, add directly
946975
processedValues = append(processedValues, item.Literal)
947976
} else if len(item.Map) > 0 {
@@ -1044,10 +1073,12 @@ func (m *ODLMOperator) EvaluateExpression(ctx context.Context, expr *util.Logica
10441073
// Helper function to get comparison values
10451074
getComparisonValues := func(left, right *util.ValueSource) (string, string, error) {
10461075
leftVal, err := m.GetValueFromSource(ctx, left, instanceType, instanceName, instanceNs)
1076+
10471077
if err != nil {
10481078
return "", "", err
10491079
}
10501080
rightVal, err := m.GetValueFromSource(ctx, right, instanceType, instanceName, instanceNs)
1081+
10511082
if err != nil {
10521083
return "", "", err
10531084
}
@@ -1178,6 +1209,13 @@ func (m *ODLMOperator) GetValueFromSource(ctx context.Context, source *util.Valu
11781209
return "", nil
11791210
}
11801211

1212+
if source.Boolean != nil {
1213+
if *source.Boolean {
1214+
return "true", nil
1215+
}
1216+
return "false", nil
1217+
}
1218+
11811219
if source.Literal != "" {
11821220
return source.Literal, nil
11831221
}
@@ -1375,26 +1413,33 @@ func (m *ODLMOperator) GetValueRefFromObject(ctx context.Context, instanceType,
13751413
return "", errors.Wrapf(err, "failed to get %s %s/%s", objKind, objNs, objName)
13761414
}
13771415

1378-
// Set the Value Reference label for the object
1379-
m.EnsureLabel(obj, map[string]string{
1380-
constant.ODLMWatchedLabel: "true",
1381-
})
1382-
// Set the Value Reference Annotation for the Secret
1383-
m.EnsureAnnotation(obj, map[string]string{
1384-
constant.ODLMReferenceAnnotation: instanceType + "." + instanceNs + "." + instanceName,
1385-
})
1386-
// Update the object with the Value Reference label
1387-
if err := m.Update(ctx, &obj); err != nil {
1388-
return "", errors.Wrapf(err, "failed to update %s %s/%s", objKind, obj.GetNamespace(), obj.GetName())
1389-
}
1390-
klog.V(2).Infof("Set the Value Reference label for %s %s/%s", objKind, obj.GetNamespace(), obj.GetName())
1416+
// Comment out the following lines as we do not need to set labels and annotations for the CRD
1417+
1418+
// // Set the Value Reference label for the object
1419+
// m.EnsureLabel(obj, map[string]string{
1420+
// constant.ODLMWatchedLabel: "true",
1421+
// })
1422+
// // Set the Value Reference Annotation for the Secret
1423+
// m.EnsureAnnotation(obj, map[string]string{
1424+
// constant.ODLMReferenceAnnotation: instanceType + "." + instanceNs + "." + instanceName,
1425+
// })
1426+
// // Update the object with the Value Reference label
1427+
// if err := m.Update(ctx, &obj); err != nil {
1428+
// return "", errors.Wrapf(err, "failed to update %s %s/%s", objKind, obj.GetNamespace(), obj.GetName())
1429+
// }
1430+
// klog.V(2).Infof("Set the Value Reference label for %s %s/%s", objKind, obj.GetNamespace(), obj.GetName())
13911431

13921432
if path == "" {
13931433
return "", nil
13941434
}
13951435

13961436
sanitizedString, err := util.SanitizeObjectString(path, obj.Object)
13971437
if err != nil {
1438+
// Instead of returning an error for a missing path, log it as a warning and return empty string
1439+
if strings.Contains(err.Error(), "not found") {
1440+
klog.Warningf("Path %v from %s %s/%s was not found: %v", path, objKind, objNs, objName, err)
1441+
return "", nil
1442+
}
13981443
return "", errors.Wrapf(err, "failed to parse path %v from %s %s/%s", path, obj.GetKind(), obj.GetNamespace(), obj.GetName())
13991444
}
14001445

controllers/util/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type ValueSource struct {
8686
Map map[string]interface{} `json:"map,omitempty"` // Add this for arbitrary key-value pairs
8787
Array []ValueSource `json:"array,omitempty"`
8888
Required bool `json:"required,omitempty"` // Add this line to support required flag
89+
Boolean *bool `json:"boolean,omitempty"` // Add boolean support
8990

9091
// Dynamic references
9192
ConfigMapKeyRef *ConfigMapRef `json:"configMapKeyRef,omitempty"`

0 commit comments

Comments
 (0)