Skip to content

Commit 7b74a6d

Browse files
liggittk8s-publishing-bot
authored andcommitted
Fallback to live ns lookup on admission if lister cannot find namespace
Kubernetes-commit: 2664ad0c8383585776ab69b4f3ca1b5dc63600a5
1 parent d24fa2b commit 7b74a6d

File tree

6 files changed

+22
-11
lines changed

6 files changed

+22
-11
lines changed

pkg/admission/plugin/policy/generic/policy_matcher.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package generic
1818

1919
import (
20+
"context"
2021
"fmt"
2122

2223
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
@@ -41,8 +42,8 @@ type PolicyMatcher interface {
4142
BindingMatches(a admission.Attributes, o admission.ObjectInterfaces, binding BindingAccessor) (bool, error)
4243

4344
// GetNamespace retrieves the Namespace resource by the given name. The name may be empty, in which case
44-
// GetNamespace must return nil, nil
45-
GetNamespace(name string) (*corev1.Namespace, error)
45+
// GetNamespace must return nil, NotFound
46+
GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error)
4647
}
4748

4849
type matcher struct {
@@ -82,8 +83,8 @@ func (c *matcher) BindingMatches(a admission.Attributes, o admission.ObjectInter
8283
return isMatch, err
8384
}
8485

85-
func (c *matcher) GetNamespace(name string) (*corev1.Namespace, error) {
86-
return c.Matcher.GetNamespace(name)
86+
func (c *matcher) GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error) {
87+
return c.Matcher.GetNamespace(ctx, name)
8788
}
8889

8990
var _ matching.MatchCriteria = &matchCriteria{}

pkg/admission/plugin/policy/matching/matching.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package matching
1818

1919
import (
20+
"context"
2021
"fmt"
2122

2223
v1 "k8s.io/api/admissionregistration/v1"
@@ -44,8 +45,8 @@ type Matcher struct {
4445
objectMatcher *object.Matcher
4546
}
4647

47-
func (m *Matcher) GetNamespace(name string) (*corev1.Namespace, error) {
48-
return m.namespaceMatcher.GetNamespace(name)
48+
func (m *Matcher) GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error) {
49+
return m.namespaceMatcher.GetNamespace(ctx, name)
4950
}
5051

5152
// NewMatcher initialize the matcher with dependencies requires

pkg/admission/plugin/policy/mutating/dispatcher.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ func (d *dispatcher) dispatchInvocations(
122122
// if it is cluster scoped, namespaceName will be empty
123123
// Otherwise, get the Namespace resource.
124124
if namespaceName != "" {
125-
namespace, err = d.matcher.GetNamespace(namespaceName)
125+
namespace, err = d.matcher.GetNamespace(ctx, namespaceName)
126126
if err != nil {
127+
var statusError *k8serrors.StatusError
128+
if errors.As(err, &statusError) {
129+
return nil, statusError
130+
}
127131
return nil, k8serrors.NewNotFound(schema.GroupResource{Group: "", Resource: "namespaces"}, namespaceName)
128132
}
129133
}

pkg/admission/plugin/policy/validating/admission_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (f *fakeMatcher) ValidateInitialization() error {
269269
return nil
270270
}
271271

272-
func (f *fakeMatcher) GetNamespace(name string) (*v1.Namespace, error) {
272+
func (f *fakeMatcher) GetNamespace(ctx context.Context, name string) (*v1.Namespace, error) {
273273
return nil, nil
274274
}
275275

pkg/admission/plugin/policy/validating/dispatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (c *dispatcher) Dispatch(ctx context.Context, a admission.Attributes, o adm
189189
// if it is cluster scoped, namespaceName will be empty
190190
// Otherwise, get the Namespace resource.
191191
if namespaceName != "" {
192-
namespace, err = c.matcher.GetNamespace(namespaceName)
192+
namespace, err = c.matcher.GetNamespace(ctx, namespaceName)
193193
if err != nil {
194194
return err
195195
}

pkg/admission/plugin/webhook/predicates/namespace/matcher.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ type Matcher struct {
4444
Client clientset.Interface
4545
}
4646

47-
func (m *Matcher) GetNamespace(name string) (*v1.Namespace, error) {
48-
return m.NamespaceLister.Get(name)
47+
func (m *Matcher) GetNamespace(ctx context.Context, name string) (*v1.Namespace, error) {
48+
ns, err := m.NamespaceLister.Get(name)
49+
if apierrors.IsNotFound(err) && len(name) > 0 {
50+
// in case of latency in our caches, make a call direct to storage to verify that it truly exists or not
51+
ns, err = m.Client.CoreV1().Namespaces().Get(ctx, name, metav1.GetOptions{})
52+
}
53+
return ns, err
4954
}
5055

5156
// Validate checks if the Matcher has a NamespaceLister and Client.

0 commit comments

Comments
 (0)