Skip to content

Commit adf0063

Browse files
OlegErshovnexus49
andauthored
fix: added wait functionality for portal secret (#123)
* refactor(initializer): improve kubeconfig handling • Load kubeconfig from file to enhance flexibility • Update manager initialization to use the new configuration Signed-off-by: Bastian Echterhölter <[email protected]> On-behalf-of: @SAP <[email protected]> * refactor(config): enhance configuration structure for KCP • Introduces KCP kubeconfig handling in the config • Simplifies lifecycle manager import in the initializer Signed-off-by: Bastian Echterhölter <[email protected]> On-behalf-of: @SAP <[email protected]> * refactor(generator and operator): separated kcp and runtime kubeconfigs On-behalf-of: SAP [email protected] * fix: fixed getting config function On-behalf-of: SAP [email protected] * fix: removed extra error handler On-behalf-of: SAP [email protected] * fix: added wait functionaly for portal secret On-behalf-of: SAP [email protected] * chore: removed log On-behalf-of: SAP [email protected] * improved test coverage On-behalf-of: SAP [email protected] * chore: fixed typos On-behalf-of: SAP [email protected] * chore: refactored tests On-behalf-of: SAP [email protected] * feat: added timeout for secret waiting On-behalf-of: SAP [email protected] * chore: made timeout configurable by config On-behalf-of: SAP [email protected] * fix: fixed merge errors On-behalf-of: SAP [email protected] --------- Signed-off-by: Bastian Echterhölter <[email protected]> Co-authored-by: Bastian Echterhölter <[email protected]>
1 parent 44c7772 commit adf0063

13 files changed

+196
-70
lines changed

cmd/initializer.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ var initializerCmd = &cobra.Command{
3131
ctx, _, shutdown := pmcontext.StartContext(log, initializerCfg, defaultCfg.ShutdownTimeout)
3232
defer shutdown()
3333

34-
mgrCfg := ctrl.GetConfigOrDie()
34+
restCfg, err := getKubeconfigFromPath(initializerCfg.KCP.Kubeconfig)
35+
if err != nil {
36+
log.Error().Err(err).Msg("unable to get KCP kubeconfig")
37+
os.Exit(1)
38+
}
3539

3640
mgrOpts := ctrl.Options{
3741
Scheme: scheme,
@@ -57,7 +61,7 @@ var initializerCmd = &cobra.Command{
5761
mgrOpts.LeaderElectionConfig = inClusterCfg
5862
}
5963

60-
provider, err := initializingworkspaces.New(mgrCfg, initializingworkspaces.Options{
64+
provider, err := initializingworkspaces.New(restCfg, initializingworkspaces.Options{
6165
InitializerName: initializerCfg.InitializerName,
6266
Scheme: mgrOpts.Scheme,
6367
})
@@ -66,7 +70,7 @@ var initializerCmd = &cobra.Command{
6670
os.Exit(1)
6771
}
6872

69-
mgr, err := mcmanager.New(mgrCfg, provider, mgrOpts)
73+
mgr, err := mcmanager.New(restCfg, provider, mgrOpts)
7074
if err != nil {
7175
setupLog.Error(err, "Failed to create manager")
7276
os.Exit(1)
@@ -82,13 +86,9 @@ var initializerCmd = &cobra.Command{
8286
os.Exit(1)
8387
}
8488

85-
inClusterConfig, err := rest.InClusterConfig()
86-
if err != nil {
87-
log.Error().Err(err).Msg("Failed to create in cluster config")
88-
os.Exit(1)
89-
}
89+
k8sCfg := ctrl.GetConfigOrDie()
9090

91-
inClusterClient, err := client.New(inClusterConfig, client.Options{Scheme: scheme})
91+
runtimeClient, err := client.New(k8sCfg, client.Options{Scheme: scheme})
9292
if err != nil {
9393
log.Error().Err(err).Msg("Failed to create in cluster client")
9494
os.Exit(1)
@@ -98,7 +98,7 @@ var initializerCmd = &cobra.Command{
9898
initializerCfg.IDP.AdditionalRedirectURLs = []string{}
9999
}
100100

101-
if err := controller.NewLogicalClusterReconciler(log, orgClient, initializerCfg, inClusterClient, mgr).
101+
if err := controller.NewLogicalClusterReconciler(log, orgClient, initializerCfg, runtimeClient, mgr).
102102
SetupWithManager(mgr, defaultCfg); err != nil {
103103
setupLog.Error(err, "unable to create controller", "controller", "LogicalCluster")
104104
os.Exit(1)

cmd/model_generator.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ import (
2626
var modelGeneratorCmd = &cobra.Command{
2727
Use: "model-generator",
2828
RunE: func(cmd *cobra.Command, args []string) error {
29-
3029
ctrl.SetLogger(log.ComponentLogger("controller-runtime").Logr())
3130

3231
ctx, _, shutdown := platformeshcontext.StartContext(log, defaultCfg, defaultCfg.ShutdownTimeout)
3332
defer shutdown()
3433

35-
cfg := ctrl.GetConfigOrDie()
34+
restCfg, err := getKubeconfigFromPath(generatorCfg.KCP.Kubeconfig)
35+
if err != nil {
36+
log.Error().Err(err).Msg("unable to get KCP kubeconfig")
37+
return err
38+
}
3639

3740
mgrOpts := manager.Options{
3841
Scheme: scheme,
@@ -67,15 +70,15 @@ var modelGeneratorCmd = &cobra.Command{
6770
return fmt.Errorf("scheme should not be nil")
6871
}
6972

70-
provider, err := apiexport.New(cfg, apiexport.Options{
73+
provider, err := apiexport.New(restCfg, apiexport.Options{
7174
Scheme: mgrOpts.Scheme,
7275
})
7376
if err != nil {
7477
log.Error().Err(err).Msg("Failed to create apiexport provider")
7578
return err
7679
}
7780

78-
mgr, err := mcmanager.New(cfg, provider, mgrOpts)
81+
mgr, err := mcmanager.New(restCfg, provider, mgrOpts)
7982
if err != nil {
8083
log.Error().Err(err).Msg("Failed to create manager")
8184
return err

cmd/operator.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ var operatorCmd = &cobra.Command{
7777
ctx, _, shutdown := platformeshcontext.StartContext(log, defaultCfg, defaultCfg.ShutdownTimeout)
7878
defer shutdown()
7979

80+
restCfg, err := getKubeconfigFromPath(operatorCfg.KCP.Kubeconfig)
81+
if err != nil {
82+
log.Error().Err(err).Msg("unable to get KCP kubeconfig")
83+
return err
84+
}
85+
8086
if defaultCfg.Sentry.Dsn != "" {
8187
err := sentry.Start(ctx,
8288
defaultCfg.Sentry.Dsn, defaultCfg.Environment, defaultCfg.Region,
@@ -89,8 +95,6 @@ var operatorCmd = &cobra.Command{
8995
defer platformeshcontext.Recover(log)
9096
}
9197

92-
cfg := ctrl.GetConfigOrDie()
93-
9498
mgrOpts := ctrl.Options{
9599
Scheme: scheme,
96100
Metrics: metricsserver.Options{
@@ -121,15 +125,15 @@ var operatorCmd = &cobra.Command{
121125
return fmt.Errorf("scheme should not be nil")
122126
}
123127

124-
provider, err := apiexport.New(cfg, apiexport.Options{
128+
provider, err := apiexport.New(restCfg, apiexport.Options{
125129
Scheme: mgrOpts.Scheme,
126130
})
127131
if err != nil {
128132
setupLog.Error(err, "unable to construct cluster provider")
129133
return err
130134
}
131135

132-
mgr, err := mcmanager.New(cfg, provider, mgrOpts)
136+
mgr, err := mcmanager.New(restCfg, provider, mgrOpts)
133137
if err != nil {
134138
setupLog.Error(err, "Failed to create manager")
135139
return err

cmd/root.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"errors"
45
"flag"
56
"strings"
67

@@ -9,6 +10,8 @@ import (
910
"github.com/platform-mesh/golang-commons/logger"
1011
"github.com/spf13/cobra"
1112
"github.com/spf13/viper"
13+
"k8s.io/client-go/rest"
14+
"k8s.io/client-go/tools/clientcmd"
1215
ctrl "sigs.k8s.io/controller-runtime"
1316

1417
"github.com/platform-mesh/security-operator/internal/config"
@@ -18,6 +21,7 @@ var (
1821
defaultCfg *platformeshconfig.CommonServiceConfig
1922
initializerCfg config.Config
2023
operatorCfg config.Config
24+
generatorCfg config.Config
2125
log *logger.Logger
2226
setupLog logr.Logger
2327
)
@@ -43,6 +47,10 @@ func init() {
4347
if err := platformeshconfig.BindConfigToFlags(operatorV, operatorCmd, &operatorCfg); err != nil {
4448
panic(err)
4549
}
50+
generatorV := newViper()
51+
if err := platformeshconfig.BindConfigToFlags(generatorV, modelGeneratorCmd, &generatorCfg); err != nil {
52+
panic(err)
53+
}
4654
initializerV := newViper()
4755
if err := platformeshconfig.BindConfigToFlags(initializerV, initializerCmd, &initializerCfg); err != nil {
4856
panic(err)
@@ -51,6 +59,21 @@ func init() {
5159
cobra.OnInitialize(initLog)
5260
}
5361

62+
func getKubeconfigFromPath(kubeconfigPath string) (*rest.Config, error) {
63+
if kubeconfigPath == "" {
64+
return nil, errors.New("missing value for required flag --kcp-kubeconfig")
65+
}
66+
cfg, err := clientcmd.LoadFromFile(kubeconfigPath)
67+
if err != nil {
68+
return nil, err
69+
}
70+
restCfg, err := clientcmd.NewDefaultClientConfig(*cfg, &clientcmd.ConfigOverrides{}).ClientConfig()
71+
if err != nil {
72+
return restCfg, err
73+
}
74+
return restCfg, nil
75+
}
76+
5477
func newViper() *viper.Viper {
5578
v := viper.NewWithOptions(
5679
viper.EnvKeyReplacer(strings.NewReplacer("-", "_")),

internal/config/config.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ type Config struct {
1212
FGA struct {
1313
Target string `mapstructure:"fga-target"`
1414
} `mapstructure:",squash"`
15-
APIExportEndpointSliceName string `mapstructure:"api-export-endpoint-slice-name"`
16-
CoreModulePath string `mapstructure:"core-module-path"`
17-
WorkspaceDir string `mapstructure:"workspace-dir" default:"/operator/"`
18-
BaseDomain string `mapstructure:"base-domain" default:"portal.dev.local:8443"`
19-
GroupClaim string `mapstructure:"group-claim" default:"groups"`
20-
UserClaim string `mapstructure:"user-claim" default:"email"`
21-
InitializerName string `mapstructure:"initializer-name" default:"root:security"`
22-
DomainCALookup bool `mapstructure:"domain-ca-lookup" default:"false"`
23-
IDP struct {
15+
KCP struct {
16+
Kubeconfig string `mapstructure:"kcp-kubeconfig" default:"/api-kubeconfig/kubeconfig"`
17+
} `mapstructure:",squash"`
18+
APIExportEndpointSliceName string `mapstructure:"api-export-endpoint-slice-name"`
19+
CoreModulePath string `mapstructure:"core-module-path"`
20+
WorkspaceDir string `mapstructure:"workspace-dir" default:"/operator/"`
21+
BaseDomain string `mapstructure:"base-domain" default:"portal.dev.local:8443"`
22+
GroupClaim string `mapstructure:"group-claim" default:"groups"`
23+
UserClaim string `mapstructure:"user-claim" default:"email"`
24+
InitializerName string `mapstructure:"initializer-name" default:"root:security"`
25+
DomainCALookup bool `mapstructure:"domain-ca-lookup" default:"false"`
26+
SecretWaitingTimeoutInSeconds int `mapstructure:"secret-waiting-timeout-seconds" default:"60"`
27+
IDP struct {
2428
// SMTP settings
2529
SMTPServer string `mapstructure:"idp-smtp-server"`
2630
SMTPPort int `mapstructure:"idp-smtp-port"`

internal/controller/apibinding_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
ctrl "sigs.k8s.io/controller-runtime"
1212
"sigs.k8s.io/controller-runtime/pkg/predicate"
1313

14-
lifecyclecontrollerruntime "github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
14+
"github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
1515
"github.com/platform-mesh/security-operator/internal/subroutine"
1616
mccontext "sigs.k8s.io/multicluster-runtime/pkg/context"
1717
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
@@ -21,23 +21,23 @@ import (
2121
func NewAPIBindingReconciler(logger *logger.Logger, mcMgr mcmanager.Manager) *APIBindingReconciler {
2222
return &APIBindingReconciler{
2323
log: logger,
24-
lifecycle: builder.NewBuilder("apibinding", "apibinding-controller", []lifecyclesubroutine.Subroutine{
24+
mclifecycle: builder.NewBuilder("apibinding", "apibinding-controller", []lifecyclesubroutine.Subroutine{
2525
subroutine.NewAuthorizationModelGenerationSubroutine(mcMgr),
2626
}, logger).
2727
BuildMultiCluster(mcMgr),
2828
}
2929
}
3030

3131
type APIBindingReconciler struct {
32-
log *logger.Logger
33-
lifecycle *lifecyclecontrollerruntime.LifecycleManager
32+
log *logger.Logger
33+
mclifecycle *multicluster.LifecycleManager
3434
}
3535

3636
func (r *APIBindingReconciler) Reconcile(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
3737
ctxWithCluster := mccontext.WithCluster(ctx, req.ClusterName)
38-
return r.lifecycle.Reconcile(ctxWithCluster, req, &kcpv1alpha1.APIBinding{})
38+
return r.mclifecycle.Reconcile(ctxWithCluster, req, &kcpv1alpha1.APIBinding{})
3939
}
4040

4141
func (r *APIBindingReconciler) SetupWithManager(mgr mcmanager.Manager, cfg *platformeshconfig.CommonServiceConfig, evp ...predicate.Predicate) error {
42-
return r.lifecycle.SetupWithManager(mgr, cfg.MaxConcurrentReconciles, "apibinding-controller", &kcpv1alpha1.APIBinding{}, cfg.DebugLabelValue, r, r.log, evp...)
42+
return r.mclifecycle.SetupWithManager(mgr, cfg.MaxConcurrentReconciles, "apibinding-controller", &kcpv1alpha1.APIBinding{}, cfg.DebugLabelValue, r, r.log, evp...)
4343
}

internal/controller/authorization_model_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
openfgav1 "github.com/openfga/api/proto/openfga/v1"
77
platformeshconfig "github.com/platform-mesh/golang-commons/config"
88
"github.com/platform-mesh/golang-commons/controller/lifecycle/builder"
9-
lifecyclecontrollerruntime "github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
9+
"github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
1010
lifecyclesubroutine "github.com/platform-mesh/golang-commons/controller/lifecycle/subroutine"
1111
"github.com/platform-mesh/golang-commons/logger"
1212
corev1alpha1 "github.com/platform-mesh/security-operator/api/v1alpha1"
@@ -19,14 +19,14 @@ import (
1919
)
2020

2121
type AuthorizationModelReconciler struct {
22-
log *logger.Logger
23-
lifecycle *lifecyclecontrollerruntime.LifecycleManager
22+
log *logger.Logger
23+
mclifecycle *multicluster.LifecycleManager
2424
}
2525

2626
func NewAuthorizationModelReconciler(log *logger.Logger, fga openfgav1.OpenFGAServiceClient, mcMgr mcmanager.Manager) *AuthorizationModelReconciler {
2727
return &AuthorizationModelReconciler{
2828
log: log,
29-
lifecycle: builder.NewBuilder("authorizationmodel", "AuthorizationModelReconciler", []lifecyclesubroutine.Subroutine{
29+
mclifecycle: builder.NewBuilder("authorizationmodel", "AuthorizationModelReconciler", []lifecyclesubroutine.Subroutine{
3030
subroutine.NewTupleSubroutine(fga, mcMgr),
3131
}, log).
3232
BuildMultiCluster(mcMgr),
@@ -35,9 +35,9 @@ func NewAuthorizationModelReconciler(log *logger.Logger, fga openfgav1.OpenFGASe
3535

3636
func (r *AuthorizationModelReconciler) Reconcile(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
3737
ctxWithCluster := mccontext.WithCluster(ctx, req.ClusterName)
38-
return r.lifecycle.Reconcile(ctxWithCluster, req, &corev1alpha1.AuthorizationModel{})
38+
return r.mclifecycle.Reconcile(ctxWithCluster, req, &corev1alpha1.AuthorizationModel{})
3939
}
4040

4141
func (r *AuthorizationModelReconciler) SetupWithManager(mgr mcmanager.Manager, cfg *platformeshconfig.CommonServiceConfig, evp ...predicate.Predicate) error { // coverage-ignore
42-
return r.lifecycle.SetupWithManager(mgr, cfg.MaxConcurrentReconciles, "authorizationmodel", &corev1alpha1.AuthorizationModel{}, cfg.DebugLabelValue, r, r.log, evp...)
42+
return r.mclifecycle.SetupWithManager(mgr, cfg.MaxConcurrentReconciles, "authorizationmodel", &corev1alpha1.AuthorizationModel{}, cfg.DebugLabelValue, r, r.log, evp...)
4343
}

internal/controller/initializer_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
kcpcorev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
77
platformeshconfig "github.com/platform-mesh/golang-commons/config"
88
"github.com/platform-mesh/golang-commons/controller/lifecycle/builder"
9-
lifecyclecontrollerruntime "github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
9+
"github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
1010
lifecyclesubroutine "github.com/platform-mesh/golang-commons/controller/lifecycle/subroutine"
1111
"github.com/platform-mesh/golang-commons/logger"
1212
ctrl "sigs.k8s.io/controller-runtime"
@@ -23,18 +23,18 @@ import (
2323
type LogicalClusterReconciler struct {
2424
log *logger.Logger
2525

26-
lifecycle *lifecyclecontrollerruntime.LifecycleManager
26+
mclifecycle *multicluster.LifecycleManager
2727
}
2828

2929
func NewLogicalClusterReconciler(log *logger.Logger, orgClient client.Client, cfg config.Config, inClusterClient client.Client, mgr mcmanager.Manager) *LogicalClusterReconciler {
3030
return &LogicalClusterReconciler{
3131
log: log,
32-
lifecycle: builder.NewBuilder("logicalcluster", "LogicalClusterReconciler", []lifecyclesubroutine.Subroutine{
32+
mclifecycle: builder.NewBuilder("logicalcluster", "LogicalClusterReconciler", []lifecyclesubroutine.Subroutine{
3333
subroutine.NewWorkspaceInitializer(orgClient, cfg, mgr),
3434
subroutine.NewWorkspaceAuthConfigurationSubroutine(orgClient, inClusterClient, cfg),
3535
subroutine.NewRealmSubroutine(inClusterClient, &cfg, cfg.BaseDomain),
3636
subroutine.NewInviteSubroutine(orgClient, mgr),
37-
subroutine.NewRemoveInitializer(mgr, cfg.InitializerName),
37+
subroutine.NewRemoveInitializer(mgr, cfg, inClusterClient),
3838
}, log).
3939
WithReadOnly().
4040
BuildMultiCluster(mgr),
@@ -43,9 +43,9 @@ func NewLogicalClusterReconciler(log *logger.Logger, orgClient client.Client, cf
4343

4444
func (r *LogicalClusterReconciler) Reconcile(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
4545
ctxWithCluster := mccontext.WithCluster(ctx, req.ClusterName)
46-
return r.lifecycle.Reconcile(ctxWithCluster, req, &kcpcorev1alpha1.LogicalCluster{})
46+
return r.mclifecycle.Reconcile(ctxWithCluster, req, &kcpcorev1alpha1.LogicalCluster{})
4747
}
4848

4949
func (r *LogicalClusterReconciler) SetupWithManager(mgr mcmanager.Manager, cfg *platformeshconfig.CommonServiceConfig, evp ...predicate.Predicate) error {
50-
return r.lifecycle.SetupWithManager(mgr, cfg.MaxConcurrentReconciles, "LogicalCluster", &kcpcorev1alpha1.LogicalCluster{}, cfg.DebugLabelValue, r, r.log, evp...)
50+
return r.mclifecycle.SetupWithManager(mgr, cfg.MaxConcurrentReconciles, "LogicalCluster", &kcpcorev1alpha1.LogicalCluster{}, cfg.DebugLabelValue, r, r.log, evp...)
5151
}

internal/controller/invite_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
platformeshconfig "github.com/platform-mesh/golang-commons/config"
88
"github.com/platform-mesh/golang-commons/controller/lifecycle/builder"
9-
lifecycle "github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
9+
"github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
1010
lifecyclesubroutine "github.com/platform-mesh/golang-commons/controller/lifecycle/subroutine"
1111
"github.com/platform-mesh/golang-commons/logger"
1212
ctrl "sigs.k8s.io/controller-runtime"
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
type InviteReconciler struct {
23-
lifecycle *lifecycle.LifecycleManager
23+
mclifecycle *multicluster.LifecycleManager
2424
}
2525

2626
func NewInviteReconciler(ctx context.Context, mgr mcmanager.Manager, cfg *config.Config, log *logger.Logger) *InviteReconciler {
@@ -35,7 +35,7 @@ func NewInviteReconciler(ctx context.Context, mgr mcmanager.Manager, cfg *config
3535
}
3636

3737
return &InviteReconciler{
38-
lifecycle: builder.NewBuilder(
38+
mclifecycle: builder.NewBuilder(
3939
"invite",
4040
"InviteReconciler",
4141
[]lifecyclesubroutine.Subroutine{
@@ -46,11 +46,11 @@ func NewInviteReconciler(ctx context.Context, mgr mcmanager.Manager, cfg *config
4646
}
4747

4848
func (r *InviteReconciler) Reconcile(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
49-
return r.lifecycle.Reconcile(mccontext.WithCluster(ctx, req.ClusterName), req, &v1alpha1.Invite{})
49+
return r.mclifecycle.Reconcile(mccontext.WithCluster(ctx, req.ClusterName), req, &v1alpha1.Invite{})
5050
}
5151

5252
func (r *InviteReconciler) SetupWithManager(mgr mcmanager.Manager, cfg *platformeshconfig.CommonServiceConfig, log *logger.Logger) error { // coverage-ignore
53-
return r.lifecycle.
53+
return r.mclifecycle.
5454
SetupWithManager(
5555
mgr,
5656
cfg.MaxConcurrentReconciles,

0 commit comments

Comments
 (0)