@@ -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,13 +30,17 @@ 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"
37
40
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
38
41
42
+ carvelv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
43
+
39
44
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
40
45
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
41
46
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
@@ -54,6 +59,7 @@ func init() {
54
59
utilruntime .Must (ocv1alpha1 .AddToScheme (scheme ))
55
60
utilruntime .Must (rukpakv1alpha2 .AddToScheme (scheme ))
56
61
utilruntime .Must (catalogd .AddToScheme (scheme ))
62
+ utilruntime .Must (carvelv1alpha1 .AddToScheme (scheme ))
57
63
58
64
//+kubebuilder:scaffold:scheme
59
65
}
@@ -124,8 +130,18 @@ func main() {
124
130
os .Exit (1 )
125
131
}
126
132
133
+ hasKappApis , err := HasKappApis (mgr .GetConfig ())
134
+ // We should probably prefer to proceed with creating BD instead of exiting?
135
+ if err != nil {
136
+ setupLog .Error (err , "unable to evaluate if App needs to be created" )
137
+ os .Exit (1 )
138
+ }
139
+
127
140
if err = (& controllers.ExtensionReconciler {
128
- Client : cl ,
141
+ Client : cl ,
142
+ BundleProvider : catalogClient ,
143
+ Resolver : resolver ,
144
+ HasKappApis : hasKappApis ,
129
145
}).SetupWithManager (mgr ); err != nil {
130
146
setupLog .Error (err , "unable to create controller" , "controller" , "Extension" )
131
147
os .Exit (1 )
@@ -147,3 +163,23 @@ func main() {
147
163
os .Exit (1 )
148
164
}
149
165
}
166
+
167
+ // HasKappApis checks whether the cluster has Kapp APIs installed in the cluster.
168
+ // This does not guarantee that the controller is present to reconcile the App CRs.
169
+ func HasKappApis (config * rest.Config ) (bool , error ) {
170
+ discoveryClient , err := discovery .NewDiscoveryClientForConfig (config )
171
+ if err != nil {
172
+ return false , fmt .Errorf ("creating discovery client: %v" , err )
173
+ }
174
+ apiResourceList , err := discoveryClient .ServerResourcesForGroupVersion (carvelv1alpha1 .SchemeGroupVersion .String ())
175
+ if err != nil {
176
+ return false , fmt .Errorf ("listing resource APIs: %v" , err )
177
+ }
178
+
179
+ for _ , resource := range apiResourceList .APIResources {
180
+ if resource .Kind == "App" {
181
+ return true , nil
182
+ }
183
+ }
184
+ return false , nil
185
+ }
0 commit comments