package generic
import (
+ "context"
"fmt"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
BindingMatches(a admission.Attributes, o admission.ObjectInterfaces, binding BindingAccessor) (bool, error)
// GetNamespace retrieves the Namespace resource by the given name. The name may be empty, in which case
- // GetNamespace must return nil, nil
- GetNamespace(name string) (*corev1.Namespace, error)
+ // GetNamespace must return nil, NotFound
+ GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error)
}
var errNilSelector = "a nil %s selector was passed, please ensure selectors are initialized properly"
return isMatch, err
}
-func (c *matcher) GetNamespace(name string) (*corev1.Namespace, error) {
- return c.Matcher.GetNamespace(name)
+func (c *matcher) GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error) {
+ return c.Matcher.GetNamespace(ctx, name)
}
var _ matching.MatchCriteria = &matchCriteria{}
package matching
import (
+ "context"
"fmt"
v1 "k8s.io/api/admissionregistration/v1"
objectMatcher *object.Matcher
}
-func (m *Matcher) GetNamespace(name string) (*corev1.Namespace, error) {
- return m.namespaceMatcher.GetNamespace(name)
+func (m *Matcher) GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error) {
+ return m.namespaceMatcher.GetNamespace(ctx, name)
}
// NewMatcher initialize the matcher with dependencies requires
// if it is cluster scoped, namespaceName will be empty
// Otherwise, get the Namespace resource.
if namespaceName != "" {
- namespace, err = d.matcher.GetNamespace(namespaceName)
+ namespace, err = d.matcher.GetNamespace(ctx, namespaceName)
if err != nil {
+ var statusError *k8serrors.StatusError
+ if errors.As(err, &statusError) {
+ return nil, statusError
+ }
return nil, k8serrors.NewNotFound(schema.GroupResource{Group: "", Resource: "namespaces"}, namespaceName)
}
}
return nil
}
-func (f *fakeMatcher) GetNamespace(name string) (*v1.Namespace, error) {
+func (f *fakeMatcher) GetNamespace(ctx context.Context, name string) (*v1.Namespace, error) {
return nil, nil
}
// if it is cluster scoped, namespaceName will be empty
// Otherwise, get the Namespace resource.
if namespaceName != "" {
- namespace, err = c.matcher.GetNamespace(namespaceName)
+ namespace, err = c.matcher.GetNamespace(ctx, namespaceName)
if err != nil {
return err
}
Client clientset.Interface
}
-func (m *Matcher) GetNamespace(name string) (*v1.Namespace, error) {
- return m.NamespaceLister.Get(name)
+func (m *Matcher) GetNamespace(ctx context.Context, name string) (*v1.Namespace, error) {
+ ns, err := m.NamespaceLister.Get(name)
+ if apierrors.IsNotFound(err) && len(name) > 0 {
+ // in case of latency in our caches, make a call direct to storage to verify that it truly exists or not
+ ns, err = m.Client.CoreV1().Namespaces().Get(ctx, name, metav1.GetOptions{})
+ }
+ return ns, err
}
// Validate checks if the Matcher has a NamespaceLister and Client.