@@ -18,6 +18,7 @@ package main
18
18
19
19
import (
20
20
"flag"
21
+ "fmt"
21
22
"net/http"
22
23
"os"
23
24
"time"
@@ -29,8 +30,10 @@ import (
29
30
"go.uber.org/zap/zapcore"
30
31
"k8s.io/apimachinery/pkg/runtime"
31
32
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
33
+ "k8s.io/client-go/discovery"
32
34
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
33
35
_ "k8s.io/client-go/plugin/pkg/client/auth"
36
+ "k8s.io/client-go/rest"
34
37
ctrl "sigs.k8s.io/controller-runtime"
35
38
"sigs.k8s.io/controller-runtime/pkg/healthz"
36
39
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -41,6 +44,7 @@ import (
41
44
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
42
45
"github.com/operator-framework/operator-controller/internal/controllers"
43
46
"github.com/operator-framework/operator-controller/pkg/features"
47
+ carvelv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
44
48
)
45
49
46
50
var (
@@ -54,6 +58,7 @@ func init() {
54
58
utilruntime .Must (ocv1alpha1 .AddToScheme (scheme ))
55
59
utilruntime .Must (rukpakv1alpha2 .AddToScheme (scheme ))
56
60
utilruntime .Must (catalogd .AddToScheme (scheme ))
61
+ utilruntime .Must (carvelv1alpha1 .AddToScheme (scheme ))
57
62
58
63
//+kubebuilder:scaffold:scheme
59
64
}
@@ -124,8 +129,18 @@ func main() {
124
129
os .Exit (1 )
125
130
}
126
131
132
+ hasKappApis , err := HasKappApis (mgr .GetConfig ())
133
+ // We should probably prefer to proceed with creating BD instead of exiting?
134
+ if err != nil {
135
+ setupLog .Error (err , "unable to evaluate if App needs to be created" )
136
+ os .Exit (1 )
137
+ }
138
+
127
139
if err = (& controllers.ExtensionReconciler {
128
- Client : cl ,
140
+ Client : cl ,
141
+ BundleProvider : catalogClient ,
142
+ Resolver : resolver ,
143
+ HasKappApis : hasKappApis ,
129
144
}).SetupWithManager (mgr ); err != nil {
130
145
setupLog .Error (err , "unable to create controller" , "controller" , "Extension" )
131
146
os .Exit (1 )
@@ -147,3 +162,23 @@ func main() {
147
162
os .Exit (1 )
148
163
}
149
164
}
165
+
166
+ // HasKappApis checks whether the cluster has Kapp APIs installed in the cluster.
167
+ // This does not guarantee that the controller is present to reconcile the App CRs.
168
+ func HasKappApis (config * rest.Config ) (bool , error ) {
169
+ discoveryClient , err := discovery .NewDiscoveryClientForConfig (config )
170
+ if err != nil {
171
+ return false , fmt .Errorf ("creating discovery client: %v" , err )
172
+ }
173
+ apiResourceList , err := discoveryClient .ServerResourcesForGroupVersion (carvelv1alpha1 .SchemeGroupVersion .String ())
174
+ if err != nil {
175
+ return false , fmt .Errorf ("listing resource APIs: %v" , err )
176
+ }
177
+
178
+ for _ , resource := range apiResourceList .APIResources {
179
+ if resource .Kind == "App" {
180
+ return true , nil
181
+ }
182
+ }
183
+ return false , nil
184
+ }
0 commit comments