@@ -1014,14 +1014,26 @@ func (r *Reconciler) createK8sResource(ctx context.Context, k8sResTemplate unstr
1014
1014
k8sResTemplate .Object [k ] = v
1015
1015
}
1016
1016
1017
+ k8sResConfigBytes , err := json .Marshal (k8sResConfigDecoded )
1018
+ if err != nil {
1019
+ return errors .Wrap (err , "failed to marshal k8sResConfigDecoded" )
1020
+ }
1021
+
1022
+ // Caculate the hash number of the new created template
1023
+ _ , templateHash := util .CalculateResHashes (nil , k8sResConfigBytes )
1024
+
1025
+ newAnnotations = util .AddHashAnnotation (& k8sResTemplate , constant .K8sHashedData , templateHash , newAnnotations )
1026
+
1017
1027
if kind == "Route" {
1018
1028
if host , found := k8sResConfigDecoded ["spec" ].(map [string ]interface {})["host" ].(string ); found {
1019
- hostHash := util .CalculateHash (host )
1029
+ hostHash := util .CalculateHash ([]byte (host ))
1030
+
1031
+ // if newAnnotations == nil {
1032
+ // newAnnotations = make(map[string]string)
1033
+ // }
1034
+ // newAnnotations[constant.RouteHash] = hostHash
1035
+ newAnnotations = util .AddHashAnnotation (& k8sResTemplate , constant .RouteHash , hostHash , newAnnotations )
1020
1036
1021
- if newAnnotations == nil {
1022
- newAnnotations = make (map [string ]string )
1023
- }
1024
- newAnnotations ["operator.ibm.com/odlm.route.hashedData" ] = hostHash
1025
1037
} else {
1026
1038
klog .Warningf ("spec.host not found in Route %s/%s" , namespace , name )
1027
1039
}
@@ -1064,29 +1076,30 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
1064
1076
return errors .Wrap (err , "failed to update Route" )
1065
1077
}
1066
1078
1079
+ // update the annotations of the Route host if the host is changed
1067
1080
if k8sResConfig != nil {
1068
1081
k8sResConfigDecoded := make (map [string ]interface {})
1069
1082
if k8sResConfigUnmarshalErr := json .Unmarshal (k8sResConfig .Raw , & k8sResConfigDecoded ); k8sResConfigUnmarshalErr != nil {
1070
1083
return errors .Wrap (k8sResConfigUnmarshalErr , "failed to unmarshal k8s Resource Config" )
1071
1084
}
1072
1085
1073
1086
if host , found := k8sResConfigDecoded ["spec" ].(map [string ]interface {})["host" ].(string ); found {
1074
- hostHash := util .CalculateHash (host )
1087
+ hostHash := util .CalculateHash ([] byte ( host ) )
1075
1088
1076
1089
if newAnnotations == nil {
1077
1090
newAnnotations = make (map [string ]string )
1078
1091
}
1079
- newAnnotations ["operator.ibm.com/odlm.route.hashedData" ] = hostHash
1092
+ newAnnotations [constant . RouteHash ] = hostHash
1080
1093
} else {
1081
1094
klog .Warningf ("spec.host not found in Route %s/%s" , namespace , name )
1082
1095
}
1083
1096
}
1084
1097
}
1085
1098
1086
- // Update the k8s res
1099
+ // Update the k8s resource
1087
1100
err := wait .PollImmediate (constant .DefaultCRFetchPeriod , constant .DefaultCRFetchTimeout , func () (bool , error ) {
1088
1101
1089
- existingK8sRes := unstructured.Unstructured {
1102
+ existingRes := unstructured.Unstructured {
1090
1103
Object : map [string ]interface {}{
1091
1104
"apiVersion" : apiversion ,
1092
1105
"kind" : kind ,
@@ -1096,40 +1109,56 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
1096
1109
err := r .Client .Get (ctx , types.NamespacedName {
1097
1110
Name : name ,
1098
1111
Namespace : namespace ,
1099
- }, & existingK8sRes )
1100
-
1112
+ }, & existingRes )
1101
1113
if err != nil {
1102
1114
return false , errors .Wrapf (err , "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
1103
1115
}
1116
+ resourceVersion := existingRes .GetResourceVersion ()
1104
1117
1105
- if ! r .CheckLabel (existingK8sRes , map [string ]string {constant .OpreqLabel : "true" }) && (newLabels == nil || newLabels [constant .OpreqLabel ] != "true" ) {
1118
+ if ! r .CheckLabel (existingRes , map [string ]string {constant .OpreqLabel : "true" }) && (newLabels == nil || newLabels [constant .OpreqLabel ] != "true" ) {
1106
1119
return true , nil
1107
1120
}
1108
1121
1109
1122
if k8sResConfig != nil {
1110
1123
1111
1124
// Convert existing k8s resource to string
1112
- existingK8sResRaw , err := json .Marshal (existingK8sRes .Object )
1125
+ existingResRaw , err := json .Marshal (existingRes .Object )
1113
1126
if err != nil {
1114
1127
return false , errors .Wrapf (err , "failed to marshal existing k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
1115
1128
}
1116
1129
1117
- // Merge the existing CR and the CR from the OperandConfig
1118
- updatedExistingK8sRes := util .MergeCR (existingK8sResRaw , k8sResConfig .Raw )
1119
- // Update the existing k8s resource with the merged CR
1120
- existingK8sRes .Object = updatedExistingK8sRes
1130
+ // Caculate the hash number of the new created template
1131
+ existingHash , templateHash := util .CalculateResHashes (& existingRes , k8sResConfig .Raw )
1121
1132
1122
- r .EnsureAnnotation (existingK8sRes , newAnnotations )
1123
- r .EnsureLabel (existingK8sRes , newLabels )
1124
- if err := r .setOwnerReferences (ctx , & existingK8sRes , ownerReferences ); err != nil {
1133
+ // If the hash number of the existing k8s resource is different from the hash number of template, update the k8s resource
1134
+ if existingHash != templateHash {
1135
+ k8sResConfigDecoded := make (map [string ]interface {})
1136
+ k8sResConfigUnmarshalErr := json .Unmarshal (k8sResConfig .Raw , & k8sResConfigDecoded )
1137
+ if k8sResConfigUnmarshalErr != nil {
1138
+ klog .Errorf ("failed to unmarshal k8s Resource Config: %v" , k8sResConfigUnmarshalErr )
1139
+ }
1140
+ for k , v := range k8sResConfigDecoded {
1141
+ existingRes .Object [k ] = v
1142
+ }
1143
+ newAnnotations = util .AddHashAnnotation (& existingRes , constant .K8sHashedData , templateHash , newAnnotations )
1144
+
1145
+ } else {
1146
+ // If the hash number are the same, then do the deep merge
1147
+ // Merge the existing CR and the CR from the OperandConfig
1148
+ updatedExistingRes := util .MergeCR (existingResRaw , k8sResConfig .Raw )
1149
+ // Update the existing k8s resource with the merged CR
1150
+ existingRes .Object = updatedExistingRes
1151
+ }
1152
+
1153
+ r .EnsureAnnotation (existingRes , newAnnotations )
1154
+ r .EnsureLabel (existingRes , newLabels )
1155
+ if err := r .setOwnerReferences (ctx , & existingRes , ownerReferences ); err != nil {
1125
1156
return false , errors .Wrapf (err , "failed to set ownerReferences for k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
1126
1157
}
1127
1158
1128
1159
klog .Infof ("updating k8s resource with apiversion: %s, kind: %s, %s/%s" , apiversion , kind , namespace , name )
1129
1160
1130
- resourceVersion := existingK8sRes .GetResourceVersion ()
1131
- err = r .Update (ctx , & existingK8sRes )
1132
-
1161
+ err = r .Update (ctx , & existingRes )
1133
1162
if err != nil {
1134
1163
return false , errors .Wrapf (err , "failed to update k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
1135
1164
}
@@ -1156,6 +1185,7 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
1156
1185
} else {
1157
1186
klog .Infof ("No updates on k8s resource with apiversion: %s, kind: %s, %s/%s" , apiversion , kind , namespace , name )
1158
1187
}
1188
+
1159
1189
}
1160
1190
return true , nil
1161
1191
})
@@ -1253,7 +1283,7 @@ func (r *Reconciler) updateK8sRoute(ctx context.Context, existingK8sRes unstruct
1253
1283
}
1254
1284
1255
1285
existingAnnos := existingRes .GetAnnotations ()
1256
- existingHostHash := existingAnnos ["operator.ibm.com/odlm.route.hashedData" ]
1286
+ existingHostHash := existingAnnos [constant . RouteHash ]
1257
1287
1258
1288
if k8sResConfig != nil {
1259
1289
k8sResConfigDecoded := make (map [string ]interface {})
@@ -1264,7 +1294,7 @@ func (r *Reconciler) updateK8sRoute(ctx context.Context, existingK8sRes unstruct
1264
1294
1265
1295
// Read the host from the OperandConfig
1266
1296
if newHost , found := k8sResConfigDecoded ["spec" ].(map [string ]interface {})["host" ].(string ); found {
1267
- newHostHash := util .CalculateHash (newHost )
1297
+ newHostHash := util .CalculateHash ([] byte ( newHost ) )
1268
1298
1269
1299
// Only re-create the route if the custom host has been removed
1270
1300
if newHost == "" && existingHostHash != newHostHash {
0 commit comments