@@ -72,26 +72,46 @@ func (d *Deployer) deployOperatorViaOLM(ctx context.Context) error {
7272 return nil
7373}
7474
75- // checkOLMInstalled checks if OLM is installed in the cluster.
75+ // checkOLMInstalled checks if OLM is installed in the cluster by verifying
76+ // the API server is ready to serve the required OLM resource types.
7677func (d * Deployer ) checkOLMInstalled (ctx context.Context ) error {
77- // Check for OLM CRDs
78- requiredCRDs := []string {
78+ requiredResources := []string {
7979 "catalogsources.operators.coreos.com" ,
8080 "subscriptions.operators.coreos.com" ,
8181 "installplans.operators.coreos.com" ,
8282 "clusterserviceversions.operators.coreos.com" ,
8383 }
8484
85- for _ , crd := range requiredCRDs {
86- // TODO(ROX-34499): actually this is not the right way to check whether it's safe to create a resource of a given kind.
87- // A CRD can be present, but still being loaded or end up not accepted by the API server.
88- // Instead we should use the `kubectl api-resources` subcommand which exposes the status we're looking for.
89- _ , err := d .runKubectl (ctx , k8s.KubectlOptions {
90- Args : []string {"get" , "crd" , crd },
91- })
92- if err != nil {
93- return fmt .Errorf ("OLM not installed: CRD %s not found. Please install OLM first" , crd )
85+ result , err := d .runKubectl (ctx , k8s.KubectlOptions {
86+ Args : []string {"api-resources" , "--api-group=operators.coreos.com" , "-o" , "name" },
87+ })
88+ if err != nil {
89+ if result .Stderr != "" {
90+ d .logger .Error ("kubectl stderr:" )
91+ for stderrLine := range strings .SplitSeq (result .Stderr , "\n " ) {
92+ d .logger .Errorf ("stderr: %s" , stderrLine )
93+ }
94+ }
95+ return fmt .Errorf ("failed to query api-group operators.coreos.com: %w" , err )
96+ }
97+
98+ available := make (map [string ]bool )
99+ for line := range strings .SplitSeq (strings .TrimSpace (result .Stdout ), "\n " ) {
100+ name := strings .TrimSpace (line )
101+ available [name ] = true
102+ }
103+
104+ var missingResources []string
105+ for _ , resource := range requiredResources {
106+ if ! available [resource ] {
107+ missingResources = append (missingResources , resource )
108+ }
109+ }
110+ if len (missingResources ) > 0 {
111+ for _ , resource := range missingResources {
112+ d .logger .Errorf ("OLM resource not served by the API server: %s" , resource )
94113 }
114+ return fmt .Errorf ("OLM is not properly installed, %d required resource(s) missing" , len (missingResources ))
95115 }
96116
97117 d .logger .Success ("✓ OLM detected in cluster" )
0 commit comments