-
Notifications
You must be signed in to change notification settings - Fork 0
feat: added multi-cluster runtime #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 23 commits
6f6ca05
b6fa6db
4252595
d39792c
b8ecc8d
a791d78
8bb6eff
0d464da
eeee7ce
20edb47
a9436cd
825f8cb
8ef79da
1006d1e
88c5326
32b9602
790482c
e1e48c3
37e4cd3
51ba5a7
7b58513
b6d3bfa
da0c6e5
51299d3
98124c4
26f5b3c
c719876
24128f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,9 @@ package cmd | |||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||
| "crypto/tls" | ||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| "github.com/kcp-dev/multicluster-provider/apiexport" | ||||||||||||||||||||||||||||
| platformeshcontext "github.com/platform-mesh/golang-commons/context" | ||||||||||||||||||||||||||||
| appsv1 "k8s.io/api/apps/v1" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -13,9 +15,9 @@ import ( | |||||||||||||||||||||||||||
| "k8s.io/client-go/rest" | ||||||||||||||||||||||||||||
| ctrl "sigs.k8s.io/controller-runtime" | ||||||||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/healthz" | ||||||||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/kcp" | ||||||||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||||||||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||||||||||||||||||||||||||||
| mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| securityv1alpha1 "github.com/platform-mesh/security-operator/api/v1alpha1" | ||||||||||||||||||||||||||||
| "github.com/platform-mesh/security-operator/internal/controller" | ||||||||||||||||||||||||||||
|
|
@@ -56,18 +58,31 @@ var modelGeneratorCmd = &cobra.Command{ | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| mgrOpts.LeaderElectionConfig = inClusterCfg | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| runtimeScheme := runtime.NewScheme() | ||||||||||||||||||||||||||||
| utilruntime.Must(appsv1.AddToScheme(runtimeScheme)) | ||||||||||||||||||||||||||||
| utilruntime.Must(securityv1alpha1.AddToScheme(runtimeScheme)) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| mgr, err := kcp.NewClusterAwareManager(cfg, mgrOpts) | ||||||||||||||||||||||||||||
| if mgrOpts.Scheme == nil { | ||||||||||||||||||||||||||||
| log.Error().Err(fmt.Errorf("scheme should not be nil")).Msg("scheme should not be nil") | ||||||||||||||||||||||||||||
| return fmt.Errorf("scheme should not be nil") | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+61
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant scheme initialization and check. A new Apply this diff to fix the scheme initialization: - runtimeScheme := runtime.NewScheme()
- utilruntime.Must(appsv1.AddToScheme(runtimeScheme))
- utilruntime.Must(securityv1alpha1.AddToScheme(runtimeScheme))
-
if mgrOpts.Scheme == nil {
log.Error().Err(fmt.Errorf("scheme should not be nil")).Msg("scheme should not be nil")
return fmt.Errorf("scheme should not be nil")
}Or, if you intended to use the new scheme: runtimeScheme := runtime.NewScheme()
utilruntime.Must(appsv1.AddToScheme(runtimeScheme))
utilruntime.Must(securityv1alpha1.AddToScheme(runtimeScheme))
+ mgrOpts.Scheme = runtimeScheme
if mgrOpts.Scheme == nil {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| provider, err := apiexport.New(cfg, apiexport.Options{ | ||||||||||||||||||||||||||||
| Scheme: mgrOpts.Scheme, | ||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| setupLog.Error(err, "unable to setup manager") | ||||||||||||||||||||||||||||
| log.Error().Err(err).Msg("Failed to create apiexport provider") | ||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| runtimeScheme := runtime.NewScheme() | ||||||||||||||||||||||||||||
| utilruntime.Must(appsv1.AddToScheme(runtimeScheme)) | ||||||||||||||||||||||||||||
| utilruntime.Must(securityv1alpha1.AddToScheme(runtimeScheme)) | ||||||||||||||||||||||||||||
| mgr, err := mcmanager.New(cfg, provider, mgrOpts) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| log.Error().Err(err).Msg("Failed to create manager") | ||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if err := controller.NewAPIBindingReconciler(mgr.GetClient(), log, logicalClusterClientFromKey(mgr, log)). | ||||||||||||||||||||||||||||
| SetupWithManager(mgr, log, defaultCfg); err != nil { | ||||||||||||||||||||||||||||
| if err := controller.NewAPIBindingReconciler(log, mgr). | ||||||||||||||||||||||||||||
| SetupWithManager(mgr, defaultCfg); err != nil { | ||||||||||||||||||||||||||||
| setupLog.Error(err, "unable to create controller", "controller", "Resource") | ||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
@@ -81,6 +96,12 @@ var modelGeneratorCmd = &cobra.Command{ | |||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||
| if err := provider.Run(ctx, mgr); err != nil { | ||||||||||||||||||||||||||||
| log.Fatal().Err(err).Msg("unable to run provider") | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| setupLog.Info("starting manager") | ||||||||||||||||||||||||||||
| if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { | ||||||||||||||||||||||||||||
| setupLog.Error(err, "problem running manager") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,7 @@ import ( | |||||||||||||||||||||
| apisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1" | ||||||||||||||||||||||
| kcpcorev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1" | ||||||||||||||||||||||
| "github.com/kcp-dev/logicalcluster/v3" | ||||||||||||||||||||||
| "github.com/kcp-dev/multicluster-provider/apiexport" | ||||||||||||||||||||||
| accountsv1alpha1 "github.com/platform-mesh/account-operator/api/v1alpha1" | ||||||||||||||||||||||
| "google.golang.org/grpc" | ||||||||||||||||||||||
| "google.golang.org/grpc/credentials/insecure" | ||||||||||||||||||||||
|
|
@@ -23,8 +24,9 @@ import ( | |||||||||||||||||||||
| ctrl "sigs.k8s.io/controller-runtime" | ||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/healthz" | ||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/kcp" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||||||||||||||||||||||
| mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| openfgav1 "github.com/openfga/api/proto/openfga/v1" | ||||||||||||||||||||||
| platformeshcontext "github.com/platform-mesh/golang-commons/context" | ||||||||||||||||||||||
|
|
@@ -35,15 +37,16 @@ import ( | |||||||||||||||||||||
| kcptenancyv1alphav1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1" | ||||||||||||||||||||||
| corev1alpha1 "github.com/platform-mesh/security-operator/api/v1alpha1" | ||||||||||||||||||||||
| "github.com/platform-mesh/security-operator/internal/controller" | ||||||||||||||||||||||
| "github.com/platform-mesh/security-operator/internal/subroutine" | ||||||||||||||||||||||
| // +kubebuilder:scaffold:imports | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var ( | ||||||||||||||||||||||
| scheme = runtime.NewScheme() | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func logicalClusterClientFromKey(mgr ctrl.Manager, log *logger.Logger) subroutine.NewLogicalClusterClientFunc { | ||||||||||||||||||||||
| type NewLogicalClusterClientFunc func(clusterKey logicalcluster.Name) (client.Client, error) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func logicalClusterClientFromKey(mgr ctrl.Manager, log *logger.Logger) NewLogicalClusterClientFunc { | ||||||||||||||||||||||
| return func(clusterKey logicalcluster.Name) (client.Client, error) { | ||||||||||||||||||||||
| cfg := rest.CopyConfig(mgr.GetConfig()) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -55,6 +58,8 @@ func logicalClusterClientFromKey(mgr ctrl.Manager, log *logger.Logger) subroutin | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| parsed.Path = fmt.Sprintf("/clusters/%s", clusterKey) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| log.Info().Msg(fmt.Sprintf("HOST from logical cluster client from key -- %s", parsed.String())) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| cfg.Host = parsed.String() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return client.New(cfg, client.Options{ | ||||||||||||||||||||||
|
|
@@ -110,9 +115,22 @@ var operatorCmd = &cobra.Command{ | |||||||||||||||||||||
| mgrOpts.LeaderElectionConfig = inClusterCfg | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| mgr, err := kcp.NewClusterAwareManager(cfg, mgrOpts) | ||||||||||||||||||||||
| if mgrOpts.Scheme == nil { | ||||||||||||||||||||||
| log.Error().Err(fmt.Errorf("scheme should not be nil")).Msg("scheme should not be nil") | ||||||||||||||||||||||
| return fmt.Errorf("scheme should not be nil") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| provider, err := apiexport.New(cfg, apiexport.Options{ | ||||||||||||||||||||||
| Scheme: mgrOpts.Scheme, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| setupLog.Error(err, "unable to construct cluster provider") | ||||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| mgr, err := mcmanager.New(cfg, provider, mgrOpts) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| log.Error().Err(err).Msg("unable to start manager") | ||||||||||||||||||||||
| setupLog.Error(err, "Failed to create manager") | ||||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -124,14 +142,14 @@ var operatorCmd = &cobra.Command{ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| fga := openfgav1.NewOpenFGAServiceClient(conn) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if err = controller.NewStoreReconciler(log, mgr.GetClient(), fga, logicalClusterClientFromKey(mgr, log)). | ||||||||||||||||||||||
| SetupWithManager(mgr, defaultCfg, log); err != nil { | ||||||||||||||||||||||
| if err = controller.NewStoreReconciler(log, fga, mgr). | ||||||||||||||||||||||
| SetupWithManager(mgr, defaultCfg); err != nil { | ||||||||||||||||||||||
| log.Error().Err(err).Str("controller", "store").Msg("unable to create controller") | ||||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if err = controller. | ||||||||||||||||||||||
| NewAuthorizationModelReconciler(log, mgr.GetClient(), fga, logicalClusterClientFromKey(mgr, log)). | ||||||||||||||||||||||
| SetupWithManager(mgr, defaultCfg, log); err != nil { | ||||||||||||||||||||||
| NewAuthorizationModelReconciler(log, fga, mgr). | ||||||||||||||||||||||
| SetupWithManager(mgr, defaultCfg); err != nil { | ||||||||||||||||||||||
| log.Error().Err(err).Str("controller", "authorizationmodel").Msg("unable to create controller") | ||||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -146,6 +164,12 @@ var operatorCmd = &cobra.Command{ | |||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||
| if err := provider.Run(ctx, mgr); err != nil { | ||||||||||||||||||||||
| log.Fatal().Err(err).Msg("unable to run provider") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }() | ||||||||||||||||||||||
|
Comment on lines
+167
to
+171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid fatal exit when the provider stops. Line 169 calls - go func() {
- if err := provider.Run(ctx, mgr); err != nil {
- log.Fatal().Err(err).Msg("unable to run provider")
- }
- }()
+ go func() {
+ if err := provider.Run(ctx, mgr); err != nil && !errors.Is(err, context.Canceled) {
+ log.Error().Err(err).Msg("provider exited with error")
+ }
+ }()(Remember to import 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| setupLog.Info("starting manager") | ||||||||||||||||||||||
| if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { | ||||||||||||||||||||||
| log.Error().Err(err).Msg("problem running manager") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Lets replace it witth log.Fatal() which does basically the same(plus some additional flushing), but it will have more severe log level and is more relevant to this situation.