@@ -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"
@@ -27,15 +28,20 @@ import (
27
28
rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2"
28
29
"github.com/spf13/pflag"
29
30
"go.uber.org/zap/zapcore"
31
+ "k8s.io/apimachinery/pkg/api/errors"
30
32
"k8s.io/apimachinery/pkg/runtime"
31
33
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
34
+ "k8s.io/client-go/discovery"
32
35
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
33
36
_ "k8s.io/client-go/plugin/pkg/client/auth"
37
+ "k8s.io/client-go/rest"
34
38
ctrl "sigs.k8s.io/controller-runtime"
35
39
"sigs.k8s.io/controller-runtime/pkg/healthz"
36
40
"sigs.k8s.io/controller-runtime/pkg/log/zap"
37
41
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
38
42
43
+ carvelv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
44
+
39
45
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
40
46
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
41
47
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
@@ -54,6 +60,7 @@ func init() {
54
60
utilruntime .Must (ocv1alpha1 .AddToScheme (scheme ))
55
61
utilruntime .Must (rukpakv1alpha2 .AddToScheme (scheme ))
56
62
utilruntime .Must (catalogd .AddToScheme (scheme ))
63
+ utilruntime .Must (carvelv1alpha1 .AddToScheme (scheme ))
57
64
58
65
//+kubebuilder:scaffold:scheme
59
66
}
@@ -124,8 +131,16 @@ func main() {
124
131
os .Exit (1 )
125
132
}
126
133
134
+ hasKappApis , err := hasKappApis (mgr .GetConfig ())
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
+ 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 && ! errors .IsNotFound (err ) {
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