@@ -705,79 +705,63 @@ func (a *Operator) copyToNamespace(csv *v1alpha1.ClusterServiceVersion, namespac
705
705
}
706
706
707
707
logger := a .logger .WithField ("operator-ns" , csv .GetNamespace ()).WithField ("target-ns" , namespace )
708
+
708
709
newCSV := csv .DeepCopy ()
710
+ newCSV .SetNamespace (namespace )
711
+ newCSV .SetResourceVersion ("" )
712
+ newCSV .SetLabels (utillabels .AddLabel (newCSV .GetLabels (), v1alpha1 .CopiedLabelKey , csv .GetNamespace ()))
713
+ // remove Operator object component labels before copying so that copied CSVs do not show up in the component list
714
+ for k := range newCSV .GetLabels () {
715
+ if strings .HasPrefix (k , decorators .ComponentLabelKeyPrefix ) {
716
+ delete (newCSV .Labels , k )
717
+ }
718
+ }
709
719
delete (newCSV .Annotations , v1 .OperatorGroupTargetsAnnotationKey )
710
720
711
- fetchedCSV , err := a .lister .OperatorsV1alpha1 ().ClusterServiceVersionLister ().ClusterServiceVersions (namespace ).Get (newCSV .GetName ())
712
- logger = logger .WithField ("csv" , csv .GetName ())
713
- if fetchedCSV != nil {
714
- logger .Debug ("checking annotations" )
715
-
716
- if ! reflect .DeepEqual (a .copyOperatorGroupAnnotations (& fetchedCSV .ObjectMeta ), a .copyOperatorGroupAnnotations (& newCSV .ObjectMeta )) ||
717
- len (decorators .OperatorNames (fetchedCSV .GetLabels ())) > 0 {
721
+ logger .Debug ("copying CSV to target" )
722
+ copiedCSV , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (namespace ).Create (context .TODO (), newCSV , metav1.CreateOptions {})
723
+ if k8serrors .IsAlreadyExists (err ) {
724
+ copiedCSV , err = a .lister .OperatorsV1alpha1 ().ClusterServiceVersionLister ().ClusterServiceVersions (namespace ).Get (newCSV .GetName ())
725
+ if ! reflect .DeepEqual (a .copyOperatorGroupAnnotations (& copiedCSV .ObjectMeta ), a .copyOperatorGroupAnnotations (& newCSV .ObjectMeta )) ||
726
+ len (decorators .OperatorNames (copiedCSV .GetLabels ())) > 0 {
718
727
// TODO: only copy over the opgroup annotations, not _all_ annotations
719
- fetchedCSV .Annotations = newCSV .Annotations
720
- fetchedCSV .SetLabels (utillabels .AddLabel (fetchedCSV .GetLabels (), v1alpha1 .CopiedLabelKey , csv .GetNamespace ()))
728
+ copiedCSV .Annotations = newCSV .Annotations
729
+ copiedCSV .SetLabels (utillabels .AddLabel (copiedCSV .GetLabels (), v1alpha1 .CopiedLabelKey , csv .GetNamespace ()))
721
730
722
731
// remove Operator object component labels before copying so that copied CSVs do not show up in the component list
723
- for k := range fetchedCSV .GetLabels () {
732
+ for k := range copiedCSV .GetLabels () {
724
733
if strings .HasPrefix (k , decorators .ComponentLabelKeyPrefix ) {
725
- delete (fetchedCSV .Labels , k )
734
+ delete (copiedCSV .Labels , k )
726
735
}
727
736
}
728
737
729
738
// CRs don't support strategic merge patching, but in the future if they do this should be updated to patch
730
- logger .Debug ("updating target CSV" )
731
- if fetchedCSV , err = a .client .OperatorsV1alpha1 ().ClusterServiceVersions (namespace ).Update (context .TODO (), fetchedCSV , metav1.UpdateOptions {}); err != nil {
739
+ logger .Debug ("updating existing target CSV" )
740
+ if copiedCSV , err = a .client .OperatorsV1alpha1 ().ClusterServiceVersions (namespace ).Update (context .TODO (), copiedCSV , metav1.UpdateOptions {}); err != nil {
732
741
logger .WithError (err ).Error ("update target CSV failed" )
733
742
return nil , err
734
743
}
735
744
}
745
+ } else if err != nil {
746
+ a .logger .Errorf ("Create for new CSV failed: %v" , err )
747
+ return nil , err
748
+ }
736
749
737
- logger .Debug ("checking status" )
738
- newCSV .Status = csv .Status
739
- newCSV .Status .Reason = v1alpha1 .CSVReasonCopied
740
- newCSV .Status .Message = fmt .Sprintf ("The operator is running in %s but is managing this namespace" , csv .GetNamespace ())
741
-
742
- if ! reflect .DeepEqual (fetchedCSV .Status , newCSV .Status ) {
743
- logger .Debug ("updating status" )
744
- // Must use fetchedCSV because UpdateStatus(...) checks resource UID.
745
- fetchedCSV .Status = newCSV .Status
746
- if fetchedCSV , err = a .client .OperatorsV1alpha1 ().ClusterServiceVersions (namespace ).UpdateStatus (context .TODO (), fetchedCSV , metav1.UpdateOptions {}); err != nil {
747
- logger .WithError (err ).Error ("status update for target CSV failed" )
748
- return nil , err
749
- }
750
- }
751
-
752
- return fetchedCSV , nil
753
-
754
- } else if k8serrors .IsNotFound (err ) {
755
- newCSV .SetNamespace (namespace )
756
- newCSV .SetResourceVersion ("" )
757
- newCSV .SetLabels (utillabels .AddLabel (newCSV .GetLabels (), v1alpha1 .CopiedLabelKey , csv .GetNamespace ()))
750
+ logger .Debug ("checking status" )
751
+ newCSV .Status = csv .Status
752
+ newCSV .Status .Reason = v1alpha1 .CSVReasonCopied
753
+ newCSV .Status .Message = fmt .Sprintf ("The operator is running in %s but is managing this namespace" , csv .GetNamespace ())
758
754
759
- logger .Debug ("copying CSV to target" )
760
- createdCSV , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (namespace ).Create (context .TODO (), newCSV , metav1.CreateOptions {})
761
- if err != nil {
762
- a .logger .Errorf ("Create for new CSV failed: %v" , err )
763
- return nil , err
764
- }
765
- createdCSV .Status .Reason = v1alpha1 .CSVReasonCopied
766
- createdCSV .Status .Message = fmt .Sprintf ("The operator is running in %s but is managing this namespace" , csv .GetNamespace ())
767
- if _ , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (namespace ).UpdateStatus (context .TODO (), createdCSV , metav1.UpdateOptions {}); err != nil {
768
- a .logger .Errorf ("Status update for CSV failed: %v" , err )
755
+ if ! reflect .DeepEqual (copiedCSV .Status , newCSV .Status ) {
756
+ logger .Debug ("updating status" )
757
+ // Must use fetchedCSV because UpdateStatus(...) checks resource UID.
758
+ copiedCSV .Status = newCSV .Status
759
+ if copiedCSV , err = a .client .OperatorsV1alpha1 ().ClusterServiceVersions (namespace ).UpdateStatus (context .TODO (), copiedCSV , metav1.UpdateOptions {}); err != nil {
760
+ logger .WithError (err ).Error ("status update for target CSV failed" )
769
761
return nil , err
770
762
}
771
-
772
- return createdCSV , nil
773
-
774
- } else if err != nil {
775
- logger .WithError (err ).Error ("couldn't get CSV" )
776
- return nil , err
777
763
}
778
-
779
- // this return shouldn't be hit
780
- return nil , fmt .Errorf ("unhandled code path" )
764
+ return copiedCSV , nil
781
765
}
782
766
783
767
func (a * Operator ) pruneFromNamespace (operatorGroupName , namespace string ) error {
0 commit comments