Skip to content

Commit 51fa95f

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 51fa95f

File tree

9 files changed

+629
-27
lines changed

9 files changed

+629
-27
lines changed

cmd/manager/main.go

+37-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,13 +30,17 @@ 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"
3740
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
3841

42+
carvelv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
43+
3944
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
4045
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
4146
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
@@ -54,6 +59,7 @@ func init() {
5459
utilruntime.Must(ocv1alpha1.AddToScheme(scheme))
5560
utilruntime.Must(rukpakv1alpha2.AddToScheme(scheme))
5661
utilruntime.Must(catalogd.AddToScheme(scheme))
62+
utilruntime.Must(carvelv1alpha1.AddToScheme(scheme))
5763

5864
//+kubebuilder:scaffold:scheme
5965
}
@@ -124,8 +130,18 @@ func main() {
124130
os.Exit(1)
125131
}
126132

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+
127140
if err = (&controllers.ExtensionReconciler{
128-
Client: cl,
141+
Client: cl,
142+
BundleProvider: catalogClient,
143+
Resolver: resolver,
144+
HasKappApis: hasKappApis,
129145
}).SetupWithManager(mgr); err != nil {
130146
setupLog.Error(err, "unable to create controller", "controller", "Extension")
131147
os.Exit(1)
@@ -147,3 +163,23 @@ func main() {
147163
os.Exit(1)
148164
}
149165
}
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+
}

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

+8-4
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
@@ -128,15 +132,15 @@ require (
128132
go.opentelemetry.io/otel/trace v1.21.0 // indirect
129133
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
130134
go.uber.org/multierr v1.11.0 // indirect
131-
golang.org/x/mod v0.14.0 // indirect
132-
golang.org/x/net v0.20.0 // indirect
135+
golang.org/x/mod v0.15.0 // indirect
136+
golang.org/x/net v0.21.0 // indirect
133137
golang.org/x/oauth2 v0.15.0 // indirect
134138
golang.org/x/sync v0.6.0 // indirect
135139
golang.org/x/sys v0.17.0 // indirect
136140
golang.org/x/term v0.17.0 // indirect
137141
golang.org/x/text v0.14.0 // indirect
138142
golang.org/x/time v0.3.0 // indirect
139-
golang.org/x/tools v0.16.1 // indirect
143+
golang.org/x/tools v0.18.0 // indirect
140144
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
141145
google.golang.org/appengine v1.6.8 // indirect
142146
google.golang.org/genproto v0.0.0-20240102182953-50ed04b92917 // indirect

0 commit comments

Comments
 (0)