Skip to content

Commit

Permalink
add support for app config
Browse files Browse the repository at this point in the history
Signed-off-by: jtcheng <[email protected]>
  • Loading branch information
jtcheng committed Mar 9, 2023
1 parent 8c89d0c commit 6050c31
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 21 deletions.
18 changes: 18 additions & 0 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
Expand Down Expand Up @@ -116,3 +117,20 @@ func User(ctx context.Context) user.Info {
u, _ := request.UserFrom(ctx)
return u
}

// cfgKeyOfApp is the key that the config make is associated with.
type cfgKeyOfApp struct{}

// WithAppConfig associates a given config with the app context.
func WithAppConfig(ctx context.Context, cfg *rest.Config) context.Context {
return context.WithValue(ctx, cfgKeyOfApp{}, cfg)
}

// GetAppConfig gets the current config of app (pod) from the context.
func GetAppConfig(ctx context.Context) *rest.Config {
value := ctx.Value(cfgKeyOfApp{})
if value == nil {
return nil
}
return value.(*rest.Config)
}
15 changes: 15 additions & 0 deletions client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"testing"

"k8s.io/client-go/rest"

dynamicFake "k8s.io/client-go/dynamic/fake"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -79,3 +81,16 @@ func TestDynamicContext(t *testing.T) {
ctx = WithDynamicClient(ctx, client)
g.Expect(DynamicClient(ctx)).To(Equal(client))
}

func TestAppConfigContext(t *testing.T) {
g := NewGomegaWithT(t)

ctx := context.TODO()

cfg := GetAppConfig(ctx)
g.Expect(cfg).To(BeNil())

cfg = &rest.Config{}
ctx = WithAppConfig(ctx, cfg)
g.Expect(GetAppConfig(ctx)).To(Equal(cfg))
}
2 changes: 2 additions & 0 deletions client/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func ManagerFilter(ctx context.Context, mgr *Manager) restful.FilterFunction {
log := logging.FromContext(ctx).Named("manager-filter")
scheme := kscheme.Scheme(ctx)
serviceAccountClient := Client(ctx)
configInApp := GetAppConfig(ctx)

return func(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
start := time.Now()
Expand All @@ -115,6 +116,7 @@ func ManagerFilter(ctx context.Context, mgr *Manager) restful.FilterFunction {
config.Timeout = DefaultTimeout

reqCtx = injection.WithConfig(reqCtx, config)
reqCtx = WithAppConfig(reqCtx, configInApp)

user, err := userFromBearerToken(strings.TrimPrefix(req.Request.Header.Get("Authorization"), "Bearer "))
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions client/rbac_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func SubjectReviewFilterForResource(ctx context.Context, resourceAtt authv1.Reso

func isImpersonateRequest(reqCtx context.Context) bool {
var config = injection.GetConfig(reqCtx)
if config == nil {
return false
}
return config.Impersonate.UserName != "" || len(config.Impersonate.Groups) != 0 || len(config.Impersonate.Extra) != 0
}

Expand Down
1 change: 1 addition & 0 deletions plugin/storage/route/archive/v1alpha1/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewArchive(impl archivev1alpha1.ArchiveCapable) storage.VersionedRouter {
}

func (a *archive) Register(ctx context.Context, ws *restful.WebService) error {

storagePluginParam := ws.PathParameter("storageplugin", "storage plugin to be used")
ws.Route(
ws.POST("storageplugin/{storageplugin}/records").To(a.ListRecords).
Expand Down
1 change: 1 addition & 0 deletions plugin/storage/route/core/v1alpha1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func NewAuthCheck(impl corev1alpha1.AuthChecker) storage.VersionedRouter {
}

func (a *authCheck) Register(ctx context.Context, ws *restful.WebService) error {

ws.Route(
ws.POST("/auth/check").To(a.AuthCheck).
Doc("Storage plugin auth check").
Expand Down
10 changes: 0 additions & 10 deletions plugin/storage/route/filestore/v1alpha1/filemeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@ func (a *fileMeta) Register(ctx context.Context, ws *restful.WebService) error {
storagePluginParam := ws.PathParameter("storagePlugin", "storage plugin to be used")
objectNameParam := ws.PathParameter("objectName", "file object name in storage plugin")

if manager := kclient.ManagerCtx(ctx); manager != nil {
filters, err := manager.Filters(ctx)
if err != nil {
return err
}
for _, filter := range filters {
ws = ws.Filter(filter)
}
}

ws.Route(
ws.GET("/storageplugins/{storagePlugin}/filemetas/{objectName:*}").To(a.GetFileMeta).
Filter(kclient.SubjectReviewFilterForResource(ctx, v1alpha1.FileMetaResourceAttributes("get"), "", "")).
Expand Down
10 changes: 0 additions & 10 deletions plugin/storage/route/filestore/v1alpha1/fileobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ func (a *fileObject) Register(ctx context.Context, ws *restful.WebService) error
storagePluginParam := ws.PathParameter("storagePlugin", "storage plugin toe used")
objectNameParam := ws.PathParameter("objectName", "file object name in storage tools")

if manager := kclient.ManagerCtx(ctx); manager != nil {
filters, err := manager.Filters(ctx)
if err != nil {
return err
}
for _, filter := range filters {
ws = ws.Filter(filter)
}
}

ws.Route(
ws.PUT("storageplugins/{storagePlugin}/fileobjects/{objectName:*}").To(a.PutFileObject).
Filter(kclient.SubjectReviewFilterForResource(ctx, v1alpha1.FileObjectResourceAttributes("update"), "", "")).
Expand Down
1 change: 1 addition & 0 deletions plugin/storage/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func NewServicesWithContext(ctx context.Context, c client.Interface, filters ...
groups = append(groups, group)
servicesMap[groupVersionedPath] = group
}

r.Register(ctx, group)
}

Expand Down
8 changes: 7 additions & 1 deletion sharedmain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (a *AppBuilder) init() {
a.Config.Burst = Burst
}
a.Context, a.startInformers = injection.EnableInjectionOrDie(a.Context, a.Config)
a.Context = kclient.WithAppConfig(a.Context, a.Config)

restyClient := resty.NewWithClient(kclient.NewHTTPClient())
restyClient.SetDisableWarn(true)
Expand Down Expand Up @@ -343,6 +344,10 @@ func (a *AppBuilder) Controllers(ctors ...controllers.SetupChecker) *AppBuilder
a.Logger.Infow("inject resource lock")
}

// BaseContext provides Context values to Runnables
options.BaseContext = func() context.Context {
return a.Context
}
a.Manager, err = ctrl.NewManager(a.Config, options)
if err != nil {
a.Logger.Fatalw("unable to start manager", "err", err)
Expand Down Expand Up @@ -483,7 +488,8 @@ func (a *AppBuilder) StoragePlugins(plugins ...client.Interface) *AppBuilder {
if err := plugin.Setup(a.Context, a.Logger); err != nil {
a.Logger.Fatalw("plugin could not be setup correctly", "err", err, "plugin", plugin.Path())
}
wss, err := storageroute.NewServicesWithContext(a.Context, plugin)
filters, _ := a.ClientManager.Filters(a.Context)
wss, err := storageroute.NewServicesWithContext(a.Context, plugin, filters...)
if err != nil {
a.Logger.Fatalw("plugin could not start correctly", "err", err, "plugin", plugin.Path())
}
Expand Down

0 comments on commit 6050c31

Please sign in to comment.