opportunistically attempt to refresh RESTMapper

if GVK resolution fails.
This commit is contained in:
Jiahui Feng 2023-10-26 10:24:21 -07:00
parent 05cd3e66ef
commit 38fecc8319

View File

@ -238,7 +238,7 @@ func (c *TypeChecker) typesToCheck(p *v1beta1.ValidatingAdmissionPolicy) []schem
if p.Spec.MatchConstraints == nil || len(p.Spec.MatchConstraints.ResourceRules) == 0 { if p.Spec.MatchConstraints == nil || len(p.Spec.MatchConstraints.ResourceRules) == 0 {
return nil return nil
} }
restMapperRefreshAttempted := false // at most once per policy, refresh RESTMapper and retry resolution.
for _, rule := range p.Spec.MatchConstraints.ResourceRules { for _, rule := range p.Spec.MatchConstraints.ResourceRules {
groups := extractGroups(&rule.Rule) groups := extractGroups(&rule.Rule)
if len(groups) == 0 { if len(groups) == 0 {
@ -268,8 +268,17 @@ func (c *TypeChecker) typesToCheck(p *v1beta1.ValidatingAdmissionPolicy) []schem
} }
resolved, err := c.RestMapper.KindsFor(gvr) resolved, err := c.RestMapper.KindsFor(gvr)
if err != nil { if err != nil {
if restMapperRefreshAttempted {
// RESTMapper refresh happens at most once per policy
continue continue
} }
c.tryRefreshRESTMapper()
restMapperRefreshAttempted = true
resolved, err = c.RestMapper.KindsFor(gvr)
if err != nil {
continue
}
}
for _, r := range resolved { for _, r := range resolved {
if !r.Empty() { if !r.Empty() {
gvks.Insert(r) gvks.Insert(r)
@ -344,6 +353,13 @@ func sortGVKList(list []schema.GroupVersionKind) []schema.GroupVersionKind {
return list return list
} }
// tryRefreshRESTMapper refreshes the RESTMapper if it supports refreshing.
func (c *TypeChecker) tryRefreshRESTMapper() {
if r, ok := c.RestMapper.(meta.ResettableRESTMapper); ok {
r.Reset()
}
}
func buildEnv(hasParams bool, hasAuthorizer bool, types typeOverwrite) (*cel.Env, error) { func buildEnv(hasParams bool, hasAuthorizer bool, types typeOverwrite) (*cel.Env, error) {
baseEnv := environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion()) baseEnv := environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion())
requestType := plugincel.BuildRequestType() requestType := plugincel.BuildRequestType()