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 53e2a91
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 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)
}

// GetConfig gets the current config from the context.
func GetAppConfig(ctx context.Context) *rest.Config {
value := ctx.Value(cfgKeyOfApp{})
if value == nil {
return nil
}
return value.(*rest.Config)
}
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
4 changes: 4 additions & 0 deletions 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,9 @@ func (a *AppBuilder) Controllers(ctors ...controllers.SetupChecker) *AppBuilder
a.Logger.Infow("inject resource lock")
}

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

0 comments on commit 53e2a91

Please sign in to comment.