55 "fmt"
66 "strings"
77 "testing"
8+ "time"
89
910 "github.com/stretchr/testify/require"
1011 appsv1 "k8s.io/api/apps/v1"
@@ -17,6 +18,8 @@ import (
1718 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819 "k8s.io/apimachinery/pkg/runtime"
1920 "k8s.io/apimachinery/pkg/util/wait"
21+ "k8s.io/apimachinery/pkg/watch"
22+ apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
2023
2124 v1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
2225 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
@@ -1245,9 +1248,31 @@ func TestCreateCSVWithOwnedAPIService(t *testing.T) {
12451248 csv .SetName (depName )
12461249
12471250 // Create the APIService CSV
1248- cleanupCSV , err := createCSV (t , c , crc , csv , testNamespace , false , true )
1251+ cleanupCSV , err := createCSV (t , c , crc , csv , testNamespace , false , false )
12491252 require .NoError (t , err )
1250- defer cleanupCSV ()
1253+ defer func () {
1254+ watcher , err := c .ApiregistrationV1Interface ().ApiregistrationV1 ().APIServices ().Watch (metav1.ListOptions {FieldSelector : "metadata.name=" + apiServiceName })
1255+ require .NoError (t , err )
1256+
1257+ deleted := make (chan struct {})
1258+ go func () {
1259+ events := watcher .ResultChan ()
1260+ for {
1261+ select {
1262+ case evt := <- events :
1263+ if evt .Type == watch .Deleted {
1264+ deleted <- struct {}{}
1265+ return
1266+ }
1267+ case <- time .After (pollDuration ):
1268+ require .FailNow (t , "apiservice not cleaned up after CSV deleted" )
1269+ }
1270+ }
1271+ }()
1272+
1273+ cleanupCSV ()
1274+ <- deleted
1275+ }()
12511276
12521277 fetchedCSV , err := fetchCSV (t , crc , csv .Name , testNamespace , csvSucceededChecker )
12531278 require .NoError (t , err )
@@ -1748,6 +1773,72 @@ func TestCreateSameCSVWithOwnedAPIServiceMultiNamespace(t *testing.T) {
17481773 require .NoError (t , err )
17491774}
17501775
1776+ func TestOrphanedAPIServiceCleanUp (t * testing.T ) {
1777+ defer cleaner .NotifyTestComplete (t , true )
1778+
1779+ c := newKubeClient (t )
1780+
1781+ mockGroup := fmt .Sprintf ("hats.%s.redhat.com" , genName ("" ))
1782+ version := "v1alpha1"
1783+ apiServiceName := strings .Join ([]string {version , mockGroup }, "." )
1784+
1785+ apiService := & apiregistrationv1.APIService {
1786+ ObjectMeta : metav1.ObjectMeta {
1787+ Name : apiServiceName ,
1788+ },
1789+ Spec : apiregistrationv1.APIServiceSpec {
1790+ Group : mockGroup ,
1791+ Version : version ,
1792+ GroupPriorityMinimum : 100 ,
1793+ VersionPriority : 100 ,
1794+ },
1795+ }
1796+
1797+ watcher , err := c .ApiregistrationV1Interface ().ApiregistrationV1 ().APIServices ().Watch (metav1.ListOptions {FieldSelector : "metadata.name=" + apiServiceName })
1798+ require .NoError (t , err )
1799+
1800+ deleted := make (chan struct {})
1801+ quit := make (chan struct {})
1802+ defer close (quit )
1803+ go func () {
1804+ events := watcher .ResultChan ()
1805+ for {
1806+ select {
1807+ case <- quit :
1808+ return
1809+ case evt := <- events :
1810+ if evt .Type == watch .Deleted {
1811+ deleted <- struct {}{}
1812+ }
1813+ case <- time .After (pollDuration ):
1814+ require .FailNow (t , "orphaned apiservice not cleaned up as expected" )
1815+ }
1816+ }
1817+ }()
1818+
1819+ _ , err = c .CreateAPIService (apiService )
1820+ require .NoError (t , err , "error creating expected APIService" )
1821+ orphanedAPISvc , err := c .GetAPIService (apiServiceName )
1822+ require .NoError (t , err , "error getting expected APIService" )
1823+
1824+ newLabels := map [string ]string {"olm.owner" : "hat-serverfd4r5" , "olm.owner.kind" : "ClusterServiceVersion" , "olm.owner.namespace" : "nonexistent-namespace" }
1825+ orphanedAPISvc .SetLabels (newLabels )
1826+ _ , err = c .UpdateAPIService (orphanedAPISvc )
1827+ require .NoError (t , err , "error updating APIService" )
1828+ <- deleted
1829+
1830+ _ , err = c .CreateAPIService (apiService )
1831+ require .NoError (t , err , "error creating expected APIService" )
1832+ orphanedAPISvc , err = c .GetAPIService (apiServiceName )
1833+ require .NoError (t , err , "error getting expected APIService" )
1834+
1835+ newLabels = map [string ]string {"olm.owner" : "hat-serverfd4r5" , "olm.owner.kind" : "ClusterServiceVersion" , "olm.owner.namespace" : testNamespace }
1836+ orphanedAPISvc .SetLabels (newLabels )
1837+ _ , err = c .UpdateAPIService (orphanedAPISvc )
1838+ require .NoError (t , err , "error updating APIService" )
1839+ <- deleted
1840+ }
1841+
17511842func TestUpdateCSVSameDeploymentName (t * testing.T ) {
17521843 defer cleaner .NotifyTestComplete (t , true )
17531844
0 commit comments