Skip to content

Commit 298b721

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

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
@@ -722,6 +722,15 @@ func (m *ODLMOperator) processMapObject(ctx context.Context, key string, mapObj
722722
if err != nil {
723723
return err
724724
}
725+
726+
if valueRef == "true" {
727+
finalObject[key] = true
728+
continue
729+
} else if valueRef == "false" {
730+
finalObject[key] = false
731+
continue
732+
}
733+
725734
if valueRef != "" {
726735
// Check if the returned value is a JSON array string and the field should be an array
727736
if strings.HasPrefix(valueRef, "[") && strings.HasSuffix(valueRef, "]") {
@@ -777,6 +786,15 @@ func (m *ODLMOperator) processTemplateValue(ctx context.Context, value interface
777786
return valueRef, nil
778787
}
779788

789+
if boolValue, hasBool := templateRef["boolean"]; hasBool {
790+
if b, ok := boolValue.(bool); ok {
791+
if b {
792+
return "true", nil
793+
}
794+
return "false", nil
795+
}
796+
}
797+
780798
templateRefObj, err := m.convertToTemplateValueRef(templateRef, instanceType, instanceName, instanceNs)
781799
if err != nil {
782800
return "", err
@@ -837,7 +855,13 @@ func (m *ODLMOperator) GetValueFromBranch(ctx context.Context, branch *util.Valu
837855
return "", nil
838856
}
839857

840-
// Handle direct literal value first
858+
if branch.Boolean != nil {
859+
if *branch.Boolean {
860+
return "true", nil
861+
}
862+
return "false", nil
863+
}
864+
841865
if branch.Literal != "" {
842866
return branch.Literal, nil
843867
}
@@ -847,9 +871,14 @@ func (m *ODLMOperator) GetValueFromBranch(ctx context.Context, branch *util.Valu
847871
// Create a slice to hold processed values
848872
var processedValues []interface{}
849873

850-
// Iterate over the array items
851874
for _, item := range branch.Array {
852-
if item.Literal != "" {
875+
if item.Boolean != nil {
876+
if *item.Boolean {
877+
processedValues = append(processedValues, true)
878+
} else {
879+
processedValues = append(processedValues, false)
880+
}
881+
} else if item.Literal != "" {
853882
// For literal values in array, add directly
854883
processedValues = append(processedValues, item.Literal)
855884
} else if len(item.Map) > 0 {
@@ -952,10 +981,12 @@ func (m *ODLMOperator) EvaluateExpression(ctx context.Context, expr *util.Logica
952981
// Helper function to get comparison values
953982
getComparisonValues := func(left, right *util.ValueSource) (string, string, error) {
954983
leftVal, err := m.GetValueFromSource(ctx, left, instanceType, instanceName, instanceNs)
984+
955985
if err != nil {
956986
return "", "", err
957987
}
958988
rightVal, err := m.GetValueFromSource(ctx, right, instanceType, instanceName, instanceNs)
989+
959990
if err != nil {
960991
return "", "", err
961992
}
@@ -1086,6 +1117,13 @@ func (m *ODLMOperator) GetValueFromSource(ctx context.Context, source *util.Valu
10861117
return "", nil
10871118
}
10881119

1120+
if source.Boolean != nil {
1121+
if *source.Boolean {
1122+
return "true", nil
1123+
}
1124+
return "false", nil
1125+
}
1126+
10891127
if source.Literal != "" {
10901128
return source.Literal, nil
10911129
}
@@ -1283,26 +1321,33 @@ func (m *ODLMOperator) GetValueRefFromObject(ctx context.Context, instanceType,
12831321
return "", errors.Wrapf(err, "failed to get %s %s/%s", objKind, objNs, objName)
12841322
}
12851323

1286-
// Set the Value Reference label for the object
1287-
m.EnsureLabel(obj, map[string]string{
1288-
constant.ODLMWatchedLabel: "true",
1289-
})
1290-
// Set the Value Reference Annotation for the Secret
1291-
m.EnsureAnnotation(obj, map[string]string{
1292-
constant.ODLMReferenceAnnotation: instanceType + "." + instanceNs + "." + instanceName,
1293-
})
1294-
// Update the object with the Value Reference label
1295-
if err := m.Update(ctx, &obj); err != nil {
1296-
return "", errors.Wrapf(err, "failed to update %s %s/%s", objKind, obj.GetNamespace(), obj.GetName())
1297-
}
1298-
klog.V(2).Infof("Set the Value Reference label for %s %s/%s", objKind, obj.GetNamespace(), obj.GetName())
1324+
// Comment out the following lines as we do not need to set labels and annotations for the CRD
1325+
1326+
// // Set the Value Reference label for the object
1327+
// m.EnsureLabel(obj, map[string]string{
1328+
// constant.ODLMWatchedLabel: "true",
1329+
// })
1330+
// // Set the Value Reference Annotation for the Secret
1331+
// m.EnsureAnnotation(obj, map[string]string{
1332+
// constant.ODLMReferenceAnnotation: instanceType + "." + instanceNs + "." + instanceName,
1333+
// })
1334+
// // Update the object with the Value Reference label
1335+
// if err := m.Update(ctx, &obj); err != nil {
1336+
// return "", errors.Wrapf(err, "failed to update %s %s/%s", objKind, obj.GetNamespace(), obj.GetName())
1337+
// }
1338+
// klog.V(2).Infof("Set the Value Reference label for %s %s/%s", objKind, obj.GetNamespace(), obj.GetName())
12991339

13001340
if path == "" {
13011341
return "", nil
13021342
}
13031343

13041344
sanitizedString, err := util.SanitizeObjectString(path, obj.Object)
13051345
if err != nil {
1346+
// Instead of returning an error for a missing path, log it as a warning and return empty string
1347+
if strings.Contains(err.Error(), "not found") {
1348+
klog.Warningf("Path %v from %s %s/%s was not found: %v", path, objKind, objNs, objName, err)
1349+
return "", nil
1350+
}
13061351
return "", errors.Wrapf(err, "failed to parse path %v from %s %s/%s", path, obj.GetKind(), obj.GetNamespace(), obj.GetName())
13071352
}
13081353

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)