@@ -808,55 +808,90 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
808808 apiversion := existingK8sRes .GetAPIVersion ()
809809 name := existingK8sRes .GetName ()
810810 namespace := existingK8sRes .GetNamespace ()
811-
812- existingK8sRes = unstructured.Unstructured {
813- Object : map [string ]interface {}{
814- "apiVersion" : apiversion ,
815- "kind" : kind ,
816- },
817- }
818-
819- if err := r .Client .Get (ctx , types.NamespacedName {
820- Name : name ,
821- Namespace : namespace ,
822- }, & existingK8sRes ); err != nil {
823- return errors .Wrapf (err , "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
824- }
825-
826- if ! checkLabel (existingK8sRes , map [string ]string {constant .OpreqLabel : "true" }) {
811+ if kind == "Job" {
812+ klog .V (2 ).Infof ("skip updating k8s resource with apiversion: %s, kind: %s, %s/%s" , apiversion , kind , namespace , name )
827813 return nil
828814 }
829815
830- // delete the existing k8s resource, some resources could not be updated after it has been created
831- if err := r .Client .Delete (ctx , & existingK8sRes ); err != nil {
832- return errors .Wrapf (err , "failed to delete existing k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
833- }
834-
835- // wait for the resource has been deleted
816+ // Update the k8s res
836817 err := wait .PollImmediate (constant .DefaultCRFetchPeriod , constant .DefaultCRFetchTimeout , func () (bool , error ) {
818+
819+ existingK8sRes := unstructured.Unstructured {
820+ Object : map [string ]interface {}{
821+ "apiVersion" : apiversion ,
822+ "kind" : kind ,
823+ },
824+ }
825+
837826 err := r .Client .Get (ctx , types.NamespacedName {
838827 Name : name ,
839828 Namespace : namespace ,
840829 }, & existingK8sRes )
841- if err == nil {
842- // continue wait for the deletion
843- return false , nil
844- } else if err != nil && apierrors .IsNotFound (err ) {
830+
831+ if err != nil {
832+ return false , errors .Wrapf (err , "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
833+ }
834+
835+ if ! checkLabel (existingK8sRes , map [string ]string {constant .OpreqLabel : "true" }) {
845836 return true , nil
846837 }
847- return false , errors .Wrapf (err , "failed to wait k8s resource deleted -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
838+
839+ // isEqual := checkAnnotation(existingK8sRes, newAnnotations) && checkLabel(existingK8sRes, newLabels)
840+ if k8sResConfig != nil {
841+ k8sResConfigDecoded := make (map [string ]interface {})
842+ k8sResConfigUnmarshalErr := json .Unmarshal (k8sResConfig .Raw , & k8sResConfigDecoded )
843+ if k8sResConfigUnmarshalErr != nil {
844+ klog .Errorf ("failed to unmarshal k8s Resource Config: %v" , k8sResConfigUnmarshalErr )
845+ }
846+
847+ for k , v := range k8sResConfigDecoded {
848+ // isEqual = isEqual && reflect.DeepEqual(existingK8sRes.Object[k], v)
849+ existingK8sRes .Object [k ] = v
850+ }
851+ }
852+
853+ CRgeneration := existingK8sRes .GetGeneration ()
854+
855+ // if isEqual {
856+ // return true, nil
857+ // }
858+
859+ ensureAnnotation (existingK8sRes , newAnnotations )
860+ ensureLabel (existingK8sRes , newLabels )
861+
862+ klog .V (2 ).Infof ("updating k8s resource with apiversion: %s, kind: %s, %s/%s" , apiversion , kind , namespace , name )
863+
864+ err = r .Update (ctx , & existingK8sRes )
865+
866+ if err != nil {
867+ return false , errors .Wrapf (err , "failed to update k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
868+ }
869+
870+ UpdatedK8sRes := unstructured.Unstructured {
871+ Object : map [string ]interface {}{
872+ "apiVersion" : apiversion ,
873+ "kind" : kind ,
874+ },
875+ }
876+
877+ err = r .Client .Get (ctx , types.NamespacedName {
878+ Name : name ,
879+ Namespace : namespace ,
880+ }, & UpdatedK8sRes )
881+
882+ if err != nil {
883+ return false , errors .Wrapf (err , "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
884+
885+ }
886+
887+ if UpdatedK8sRes .GetGeneration () != CRgeneration {
888+ klog .V (2 ).Infof ("Finish updating the k8s Resource: -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
889+ }
890+
891+ return true , nil
848892 })
849893
850894 if err != nil {
851- return errors .Wrapf (err , "failed to wait k8s resource deleted -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
852- }
853-
854- var k8sRes unstructured.Unstructured
855- k8sRes .SetAPIVersion (apiversion )
856- k8sRes .SetKind (kind )
857- k8sRes .SetName (name )
858- k8sRes .SetNamespace (namespace )
859- if err = r .createK8sResource (ctx , k8sRes , k8sResConfig , newLabels , newAnnotations ); err != nil {
860895 return errors .Wrapf (err , "failed to update k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
861896 }
862897
0 commit comments