@@ -19,8 +19,9 @@ import (
1919 "k8s.io/apimachinery/pkg/runtime"
2020 "k8s.io/apimachinery/pkg/util/wait"
2121 "k8s.io/apimachinery/pkg/watch"
22+ apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
2223
23- "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
24+ v1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
2425 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
2526 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
2627 "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
@@ -1744,6 +1745,72 @@ func TestCreateSameCSVWithOwnedAPIServiceMultiNamespace(t *testing.T) {
17441745 require .NoError (t , err )
17451746}
17461747
1748+ func TestOrphanedAPIServiceCleanUp (t * testing.T ) {
1749+ defer cleaner .NotifyTestComplete (t , true )
1750+
1751+ c := newKubeClient (t )
1752+
1753+ mockGroup := fmt .Sprintf ("hats.%s.redhat.com" , genName ("" ))
1754+ version := "v1alpha1"
1755+ apiServiceName := strings .Join ([]string {version , mockGroup }, "." )
1756+
1757+ apiService := & apiregistrationv1.APIService {
1758+ ObjectMeta : metav1.ObjectMeta {
1759+ Name : apiServiceName ,
1760+ },
1761+ Spec : apiregistrationv1.APIServiceSpec {
1762+ Group : mockGroup ,
1763+ Version : version ,
1764+ GroupPriorityMinimum : 100 ,
1765+ VersionPriority : 100 ,
1766+ },
1767+ }
1768+
1769+ watcher , err := c .ApiregistrationV1Interface ().ApiregistrationV1 ().APIServices ().Watch (metav1.ListOptions {FieldSelector : "metadata.name=" + apiServiceName })
1770+ require .NoError (t , err )
1771+
1772+ deleted := make (chan struct {})
1773+ quit := make (chan struct {})
1774+ defer close (quit )
1775+ go func () {
1776+ events := watcher .ResultChan ()
1777+ for {
1778+ select {
1779+ case <- quit :
1780+ return
1781+ case evt := <- events :
1782+ if evt .Type == watch .Deleted {
1783+ deleted <- struct {}{}
1784+ }
1785+ case <- time .After (pollDuration ):
1786+ require .FailNow (t , "orphaned apiservice not cleaned up as expected" )
1787+ }
1788+ }
1789+ }()
1790+
1791+ _ , err = c .CreateAPIService (apiService )
1792+ require .NoError (t , err , "error creating expected APIService" )
1793+ orphanedAPISvc , err := c .GetAPIService (apiServiceName )
1794+ require .NoError (t , err , "error getting expected APIService" )
1795+
1796+ newLabels := map [string ]string {"olm.owner" : "hat-serverfd4r5" , "olm.owner.kind" : "ClusterServiceVersion" , "olm.owner.namespace" : "nonexistent-namespace" }
1797+ orphanedAPISvc .SetLabels (newLabels )
1798+ _ , err = c .UpdateAPIService (orphanedAPISvc )
1799+ require .NoError (t , err , "error updating APIService" )
1800+ <- deleted
1801+
1802+ _ , err = c .CreateAPIService (apiService )
1803+ require .NoError (t , err , "error creating expected APIService" )
1804+ orphanedAPISvc , err = c .GetAPIService (apiServiceName )
1805+ require .NoError (t , err , "error getting expected APIService" )
1806+
1807+ newLabels = map [string ]string {"olm.owner" : "hat-serverfd4r5" , "olm.owner.kind" : "ClusterServiceVersion" , "olm.owner.namespace" : testNamespace }
1808+ orphanedAPISvc .SetLabels (newLabels )
1809+ _ , err = c .UpdateAPIService (orphanedAPISvc )
1810+ require .NoError (t , err , "error updating APIService" )
1811+ <- deleted
1812+ }
1813+
17471814func TestUpdateCSVSameDeploymentName (t * testing.T ) {
17481815 defer cleaner .NotifyTestComplete (t , true )
17491816
0 commit comments