Skip to content

Commit d153bea

Browse files
authored
fix: all client (#121)
* fix: all client * fix: tests * chore: use fatal instead of error
1 parent 21e3f20 commit d153bea

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

internal/controller/store_controller.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package controller
22

33
import (
44
"context"
5+
"net/url"
6+
"strings"
57

8+
"github.com/kcp-dev/logicalcluster/v3"
69
"github.com/platform-mesh/golang-commons/controller/lifecycle/builder"
710
"k8s.io/apimachinery/pkg/types"
11+
"k8s.io/client-go/rest"
812
ctrl "sigs.k8s.io/controller-runtime"
913
mcbuilder "sigs.k8s.io/multicluster-runtime/pkg/builder"
1014
mccontext "sigs.k8s.io/multicluster-runtime/pkg/context"
@@ -32,12 +36,38 @@ type StoreReconciler struct {
3236
}
3337

3438
func NewStoreReconciler(log *logger.Logger, fga openfgav1.OpenFGAServiceClient, mcMgr mcmanager.Manager) *StoreReconciler {
39+
40+
allCfg := rest.CopyConfig(mcMgr.GetLocalManager().GetConfig())
41+
42+
parsed, err := url.Parse(allCfg.Host)
43+
if err != nil {
44+
log.Fatal().Err(err).Msg("unable to parse host from config")
45+
}
46+
47+
parts := strings.Split(parsed.Path, "clusters")
48+
49+
parsed.Path, err = url.JoinPath(parts[0], "clusters", logicalcluster.Wildcard.String())
50+
if err != nil {
51+
log.Fatal().Err(err).Msg("unable to join path")
52+
}
53+
54+
allCfg.Host = parsed.String()
55+
56+
log.Info().Str("host", allCfg.Host).Msg("using host")
57+
58+
allClient, err := client.New(allCfg, client.Options{
59+
Scheme: mcMgr.GetLocalManager().GetScheme(),
60+
})
61+
if err != nil {
62+
log.Fatal().Err(err).Msg("unable to create new client")
63+
}
64+
3565
return &StoreReconciler{
3666
fga: fga,
3767
log: log,
3868
lifecycle: builder.NewBuilder("store", "StoreReconciler", []lifecyclesubroutine.Subroutine{
3969
subroutine.NewStoreSubroutine(fga, mcMgr),
40-
subroutine.NewAuthorizationModelSubroutine(fga, mcMgr),
70+
subroutine.NewAuthorizationModelSubroutine(fga, mcMgr, allClient, log),
4171
subroutine.NewTupleSubroutine(fga, mcMgr),
4272
}, log).WithConditionManagement().
4373
BuildMultiCluster(mcMgr),
@@ -59,7 +89,7 @@ func (r *StoreReconciler) SetupWithManager(mgr mcmanager.Manager, cfg *platforme
5989
Watches(
6090
&corev1alpha1.AuthorizationModel{},
6191
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
62-
model,ok := obj.(*corev1alpha1.AuthorizationModel)
92+
model, ok := obj.(*corev1alpha1.AuthorizationModel)
6393
if !ok {
6494
return nil
6595
}

internal/subroutine/authorization_model.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ import (
2222
const schemaVersion = "1.2"
2323

2424
type authorizationModelSubroutine struct {
25-
fga openfgav1.OpenFGAServiceClient
26-
mgr mcmanager.Manager
25+
fga openfgav1.OpenFGAServiceClient
26+
mgr mcmanager.Manager
27+
allClient client.Client
2728
}
2829

29-
func NewAuthorizationModelSubroutine(fga openfgav1.OpenFGAServiceClient, mgr mcmanager.Manager) *authorizationModelSubroutine {
30+
func NewAuthorizationModelSubroutine(fga openfgav1.OpenFGAServiceClient, mgr mcmanager.Manager, allClient client.Client, log *logger.Logger) *authorizationModelSubroutine {
3031
return &authorizationModelSubroutine{
31-
fga: fga,
32-
mgr: mgr,
32+
fga: fga,
33+
mgr: mgr,
34+
allClient: allClient,
3335
}
3436
}
3537

@@ -73,12 +75,7 @@ func (a *authorizationModelSubroutine) Process(ctx context.Context, instance run
7375
log := logger.LoadLoggerFromContext(ctx)
7476
store := instance.(*v1alpha1.Store)
7577

76-
cluster, err := a.mgr.ClusterFromContext(ctx)
77-
if err != nil {
78-
return ctrl.Result{}, errors.NewOperatorError(fmt.Errorf("unable to get cluster from context: %w", err), true, false)
79-
}
80-
81-
extendingModules, err := getRelatedAuthorizationModels(ctx, cluster.GetClient(), store)
78+
extendingModules, err := getRelatedAuthorizationModels(ctx, a.allClient, store)
8279
if err != nil {
8380
log.Error().Err(err).Msg("unable to get related authorization models")
8481
return ctrl.Result{}, errors.NewOperatorError(err, true, false)

internal/subroutine/authorization_model_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
openfgav1 "github.com/openfga/api/proto/openfga/v1"
1010
language "github.com/openfga/language/pkg/go/transformer"
1111
"github.com/platform-mesh/golang-commons/errors"
12+
"github.com/platform-mesh/golang-commons/logger/testlogger"
1213
"github.com/platform-mesh/security-operator/api/v1alpha1"
1314
"github.com/platform-mesh/security-operator/internal/subroutine"
1415
"github.com/platform-mesh/security-operator/internal/subroutine/mocks"
@@ -52,17 +53,17 @@ type user
5253
`
5354

5455
func TestAuthorizationModelGetName(t *testing.T) {
55-
subroutine := subroutine.NewAuthorizationModelSubroutine(nil, nil)
56+
subroutine := subroutine.NewAuthorizationModelSubroutine(nil, nil, nil, nil)
5657
assert.Equal(t, "AuthorizationModel", subroutine.GetName())
5758
}
5859

5960
func TestAuthorizationModelFinalizers(t *testing.T) {
60-
subroutine := subroutine.NewAuthorizationModelSubroutine(nil, nil)
61+
subroutine := subroutine.NewAuthorizationModelSubroutine(nil, nil, nil, nil)
6162
assert.Equal(t, []string(nil), subroutine.Finalizers(nil))
6263
}
6364

6465
func TestAuthorizationModelFinalize(t *testing.T) {
65-
subroutine := subroutine.NewAuthorizationModelSubroutine(nil, nil)
66+
subroutine := subroutine.NewAuthorizationModelSubroutine(nil, nil, nil, nil)
6667
_, err := subroutine.Finalize(context.Background(), nil)
6768
assert.Nil(t, err)
6869
}
@@ -326,10 +327,12 @@ func TestAuthorizationModelProcess(t *testing.T) {
326327
test.k8sMocks(client)
327328
}
328329

329-
manager.EXPECT().ClusterFromContext(mock.Anything).Return(cluster, nil)
330-
cluster.EXPECT().GetClient().Return(client)
330+
manager.EXPECT().ClusterFromContext(mock.Anything).Return(cluster, nil).Maybe()
331+
cluster.EXPECT().GetClient().Return(client).Maybe()
331332

332-
subroutine := subroutine.NewAuthorizationModelSubroutine(fga, manager)
333+
logger := testlogger.New()
334+
335+
subroutine := subroutine.NewAuthorizationModelSubroutine(fga, manager, client, logger.Logger)
333336
ctx := mccontext.WithCluster(context.Background(), string(logicalcluster.Name("path")))
334337

335338
_, err := subroutine.Process(ctx, test.store)

0 commit comments

Comments
 (0)