Skip to content

Commit 9c5ae10

Browse files
[Carvel] Introduce support to kapp controller
This commit introduces support for creating App CR in extension controller. Signed-off-by: Varsha Prasad Narsing <[email protected]>
1 parent d51d908 commit 9c5ae10

File tree

8 files changed

+616
-18
lines changed

8 files changed

+616
-18
lines changed

cmd/manager/main.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"flag"
21+
"fmt"
2122
"net/http"
2223
"os"
2324
"time"
@@ -29,8 +30,10 @@ import (
2930
"go.uber.org/zap/zapcore"
3031
"k8s.io/apimachinery/pkg/runtime"
3132
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
33+
"k8s.io/client-go/discovery"
3234
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3335
_ "k8s.io/client-go/plugin/pkg/client/auth"
36+
"k8s.io/client-go/rest"
3437
ctrl "sigs.k8s.io/controller-runtime"
3538
"sigs.k8s.io/controller-runtime/pkg/healthz"
3639
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -41,6 +44,7 @@ import (
4144
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
4245
"github.com/operator-framework/operator-controller/internal/controllers"
4346
"github.com/operator-framework/operator-controller/pkg/features"
47+
carvelv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
4448
)
4549

4650
var (
@@ -54,6 +58,7 @@ func init() {
5458
utilruntime.Must(ocv1alpha1.AddToScheme(scheme))
5559
utilruntime.Must(rukpakv1alpha2.AddToScheme(scheme))
5660
utilruntime.Must(catalogd.AddToScheme(scheme))
61+
utilruntime.Must(carvelv1alpha1.AddToScheme(scheme))
5762

5863
//+kubebuilder:scaffold:scheme
5964
}
@@ -124,8 +129,18 @@ func main() {
124129
os.Exit(1)
125130
}
126131

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+
127139
if err = (&controllers.ExtensionReconciler{
128-
Client: cl,
140+
Client: cl,
141+
BundleProvider: catalogClient,
142+
Resolver: resolver,
143+
HasKappApis: hasKappApis,
129144
}).SetupWithManager(mgr); err != nil {
130145
setupLog.Error(err, "unable to create controller", "controller", "Extension")
131146
os.Exit(1)
@@ -147,3 +162,23 @@ func main() {
147162
os.Exit(1)
148163
}
149164
}
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+
}

config/rbac/role.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ rules:
2929
- patch
3030
- update
3131
- watch
32+
- apiGroups:
33+
- kappctrl.k14s.io
34+
resources:
35+
- apps
36+
verbs:
37+
- create
38+
- get
39+
- list
40+
- patch
41+
- update
42+
- watch
3243
- apiGroups:
3344
- olm.operatorframework.io
3445
resources:
@@ -73,5 +84,6 @@ rules:
7384
resources:
7485
- extensions/status
7586
verbs:
87+
- get
7688
- patch
7789
- update

go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/operator-framework/operator-controller
22

3-
go 1.20
3+
go 1.21
4+
5+
toolchain go1.21.0
46

57
require (
68
github.com/Masterminds/semver/v3 v3.2.1
@@ -13,6 +15,7 @@ require (
1315
github.com/operator-framework/rukpak v0.18.0
1416
github.com/spf13/pflag v1.0.5
1517
github.com/stretchr/testify v1.8.4
18+
github.com/vmware-tanzu/carvel-kapp-controller v0.50.0
1619
go.uber.org/zap v1.27.0
1720
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
1821
gopkg.in/yaml.v2 v2.4.0
@@ -117,6 +120,7 @@ require (
117120
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
118121
github.com/ulikunitz/xz v0.5.11 // indirect
119122
github.com/vbatts/tar-split v0.11.5 // indirect
123+
github.com/vmware-tanzu/carvel-vendir v0.36.0 // indirect
120124
go.etcd.io/bbolt v1.3.8 // indirect
121125
go.opencensus.io v0.24.0 // indirect
122126
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect

go.sum

+41-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)