Skip to content

Commit d7cd944

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 d7cd944

File tree

6 files changed

+440
-26
lines changed

6 files changed

+440
-26
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"
@@ -27,15 +28,20 @@ import (
2728
rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2"
2829
"github.com/spf13/pflag"
2930
"go.uber.org/zap/zapcore"
31+
"k8s.io/apimachinery/pkg/api/errors"
3032
"k8s.io/apimachinery/pkg/runtime"
3133
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
34+
"k8s.io/client-go/discovery"
3235
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3336
_ "k8s.io/client-go/plugin/pkg/client/auth"
37+
"k8s.io/client-go/rest"
3438
ctrl "sigs.k8s.io/controller-runtime"
3539
"sigs.k8s.io/controller-runtime/pkg/healthz"
3640
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3741
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
3842

43+
carvelv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
44+
3945
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
4046
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
4147
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
@@ -54,6 +60,7 @@ func init() {
5460
utilruntime.Must(ocv1alpha1.AddToScheme(scheme))
5561
utilruntime.Must(rukpakv1alpha2.AddToScheme(scheme))
5662
utilruntime.Must(catalogd.AddToScheme(scheme))
63+
utilruntime.Must(carvelv1alpha1.AddToScheme(scheme))
5764

5865
//+kubebuilder:scaffold:scheme
5966
}
@@ -124,8 +131,16 @@ func main() {
124131
os.Exit(1)
125132
}
126133

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+
127140
if err = (&controllers.ExtensionReconciler{
128-
Client: cl,
141+
Client: cl,
142+
BundleProvider: catalogClient,
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 && !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+
}

config/rbac/role.yaml

+11
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:

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)