Skip to content

Commit

Permalink
[Carvel] Introduce support to kapp controller
Browse files Browse the repository at this point in the history
This commit introduces support for creating App CR
in extension controller.

Signed-off-by: Varsha Prasad Narsing <varshaprasad96@gmail.com>
  • Loading branch information
varshaprasad96 committed Feb 26, 2024
1 parent d51d908 commit 9c5ae10
Show file tree
Hide file tree
Showing 8 changed files with 616 additions and 18 deletions.
37 changes: 36 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"fmt"
"net/http"
"os"
"time"
Expand All @@ -29,8 +30,10 @@ import (
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/discovery"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand All @@ -41,6 +44,7 @@ import (
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
"github.com/operator-framework/operator-controller/internal/controllers"
"github.com/operator-framework/operator-controller/pkg/features"
carvelv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
)

var (
Expand All @@ -54,6 +58,7 @@ func init() {
utilruntime.Must(ocv1alpha1.AddToScheme(scheme))
utilruntime.Must(rukpakv1alpha2.AddToScheme(scheme))
utilruntime.Must(catalogd.AddToScheme(scheme))
utilruntime.Must(carvelv1alpha1.AddToScheme(scheme))

//+kubebuilder:scaffold:scheme
}
Expand Down Expand Up @@ -124,8 +129,18 @@ func main() {
os.Exit(1)
}

hasKappApis, err := HasKappApis(mgr.GetConfig())
// We should probably prefer to proceed with creating BD instead of exiting?
if err != nil {
setupLog.Error(err, "unable to evaluate if App needs to be created")
os.Exit(1)
}

if err = (&controllers.ExtensionReconciler{
Client: cl,
Client: cl,
BundleProvider: catalogClient,
Resolver: resolver,
HasKappApis: hasKappApis,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Extension")
os.Exit(1)
Expand All @@ -147,3 +162,23 @@ func main() {
os.Exit(1)
}
}

// HasKappApis checks whether the cluster has Kapp APIs installed in the cluster.
// This does not guarantee that the controller is present to reconcile the App CRs.
func HasKappApis(config *rest.Config) (bool, error) {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return false, fmt.Errorf("creating discovery client: %v", err)
}
apiResourceList, err := discoveryClient.ServerResourcesForGroupVersion(carvelv1alpha1.SchemeGroupVersion.String())
if err != nil {
return false, fmt.Errorf("listing resource APIs: %v", err)
}

for _, resource := range apiResourceList.APIResources {
if resource.Kind == "App" {
return true, nil
}
}
return false, nil
}
12 changes: 12 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ rules:
- patch
- update
- watch
- apiGroups:
- kappctrl.k14s.io
resources:
- apps
verbs:
- create
- get
- list
- patch
- update
- watch
- apiGroups:
- olm.operatorframework.io
resources:
Expand Down Expand Up @@ -73,5 +84,6 @@ rules:
resources:
- extensions/status
verbs:
- get
- patch
- update
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/operator-framework/operator-controller

go 1.20
go 1.21

toolchain go1.21.0

require (
github.com/Masterminds/semver/v3 v3.2.1
Expand All @@ -13,6 +15,7 @@ require (
github.com/operator-framework/rukpak v0.18.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/vmware-tanzu/carvel-kapp-controller v0.50.0
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -117,6 +120,7 @@ require (
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
github.com/vmware-tanzu/carvel-vendir v0.36.0 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
Expand Down
42 changes: 41 additions & 1 deletion go.sum

Large diffs are not rendered by default.

Loading

0 comments on commit 9c5ae10

Please sign in to comment.