@@ -2,6 +2,7 @@ package controller
22
33import (
44 "context"
5+ "encoding/json"
56 "errors"
67 "fmt"
78 "reflect"
@@ -254,6 +255,11 @@ func (r *Reconciler) GetLastPod(
254255 maxPodWorkflowStep := 0
255256
256257 for _ , pod := range podList .Items {
258+ // Skip pods that are being deleted
259+ if pod .DeletionTimestamp != nil {
260+ continue
261+ }
262+
257263 workflowStep , err := strconv .Atoi (pod .Labels [workflowStepLabel ])
258264 if err != nil {
259265 return & corev1.Pod {}, err
@@ -914,3 +920,72 @@ func MergeSections(main interface{}, workflow interface{}) {
914920 }
915921 }
916922}
923+
924+ // CalculateConfigHash calculates a hash of the entire Spec to detect any changes
925+ func CalculateConfigHash (instance client.Object ) string {
926+ v := reflect .ValueOf (instance )
927+ spec , err := SafetyCheck (v , "Spec" )
928+ if err != nil {
929+ return ""
930+ }
931+
932+ data , err := json .Marshal (spec .Interface ())
933+ if err != nil {
934+ return ""
935+ }
936+
937+ hash := sha256 .Sum256 (data )
938+ return fmt .Sprintf ("%x" , hash [:8 ])
939+ }
940+
941+ // CheckConfigChange checks if the spec has changed and recreates all pods related to the instance if needed
942+ func (r * Reconciler ) CheckConfigChange (
943+ ctx context.Context ,
944+ instance client.Object ,
945+ newHash string ,
946+ ) (ctrl.Result , error ) {
947+ Log := r .GetLogger (ctx )
948+
949+ if newHash == "" {
950+ return ctrl.Result {}, nil
951+ }
952+
953+ labels := map [string ]string {instanceNameLabel : instance .GetName ()}
954+ podList := & corev1.PodList {}
955+ err := r .Client .List (ctx , podList ,
956+ client .InNamespace (instance .GetNamespace ()),
957+ client .MatchingLabels (labels ))
958+ if err != nil {
959+ return ctrl.Result {}, err
960+ }
961+
962+ var currentHash string
963+ for _ , pod := range podList .Items {
964+ if pod .DeletionTimestamp != nil {
965+ continue
966+ }
967+
968+ if hash := pod .Annotations ["test.openstack.org/config-hash" ]; hash != "" {
969+ currentHash = hash
970+ break
971+ }
972+ }
973+
974+ if currentHash == "" || currentHash == newHash {
975+ return ctrl.Result {}, nil
976+ }
977+
978+ for _ , pod := range podList .Items {
979+ if pod .DeletionTimestamp != nil {
980+ continue
981+ }
982+
983+ Log .Info ("Configuration changed, deleting pod" , "pod" , pod .Name )
984+
985+ if err := r .Client .Delete (ctx , & pod ); err != nil && ! k8s_errors .IsNotFound (err ) {
986+ return ctrl.Result {}, err
987+ }
988+ }
989+
990+ return ctrl.Result {Requeue : true }, nil
991+ }
0 commit comments