Implement object deep inspector (#8456)

This commit is contained in:
Ricardo Katz 2022-04-11 11:06:07 -03:00 committed by GitHub
parent 5737f16663
commit 89ed571d2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 468 additions and 1 deletions

View file

@ -41,6 +41,7 @@ import (
"k8s.io/ingress-nginx/internal/ingress/controller/ingressclass"
"k8s.io/ingress-nginx/internal/ingress/controller/store"
"k8s.io/ingress-nginx/internal/ingress/errors"
"k8s.io/ingress-nginx/internal/ingress/inspector"
"k8s.io/ingress-nginx/internal/ingress/metric/collectors"
"k8s.io/ingress-nginx/internal/k8s"
"k8s.io/ingress-nginx/internal/nginx"
@ -123,6 +124,7 @@ type Configuration struct {
InternalLoggerAddress string
IsChroot bool
DeepInspector bool
}
// GetPublishService returns the Service used to set the load-balancer status of Ingresses.
@ -237,7 +239,11 @@ func (n *NGINXController) CheckIngress(ing *networking.Ingress) error {
if !ing.DeletionTimestamp.IsZero() {
return nil
}
if n.cfg.DeepInspector {
if err := inspector.DeepInspect(ing); err != nil {
return fmt.Errorf("invalid object: %w", err)
}
}
// Do not attempt to validate an ingress that's not meant to be controlled by the current instance of the controller.
if ingressClass, err := n.store.GetIngressClass(ing, n.cfg.IngressClassConfiguration); ingressClass == "" {
klog.Warningf("ignoring ingress %v in %v based on annotation %v: %v", ing.Name, ing.ObjectMeta.Namespace, ingressClass, err)

View file

@ -2396,6 +2396,7 @@ func newNGINXController(t *testing.T) *NGINXController {
clientSet,
channels.NewRingChannel(10),
false,
true,
&ingressclass.IngressClassConfiguration{
Controller: "k8s.io/ingress-nginx",
AnnotationValue: "nginx",
@ -2460,6 +2461,7 @@ func newDynamicNginxController(t *testing.T, setConfigMap func(string) *v1.Confi
clientSet,
channels.NewRingChannel(10),
false,
true,
&ingressclass.IngressClassConfiguration{
Controller: "k8s.io/ingress-nginx",
AnnotationValue: "nginx",

View file

@ -131,6 +131,7 @@ func NewNGINXController(config *Configuration, mc metric.Collector) *NGINXContro
config.Client,
n.updateCh,
config.DisableCatchAll,
config.DeepInspector,
config.IngressClassConfiguration)
n.syncQueue = task.NewTaskQueue(n.syncIngress)

View file

@ -42,6 +42,7 @@ import (
clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/ingress-nginx/internal/ingress/inspector"
"k8s.io/klog/v2"
"k8s.io/ingress-nginx/internal/file"
@ -247,6 +248,7 @@ func New(
client clientset.Interface,
updateCh *channels.RingChannel,
disableCatchAll bool,
deepInspector bool,
icConfig *ingressclass.IngressClassConfiguration) Storer {
store := &k8sStore{
@ -426,6 +428,12 @@ func New(
klog.InfoS("Found valid IngressClass", "ingress", klog.KObj(ing), "ingressclass", ic)
if deepInspector {
if err := inspector.DeepInspect(ing); err != nil {
klog.ErrorS(err, "received invalid ingress", "ingress", klog.KObj(ing))
return
}
}
if hasCatchAllIngressRule(ing.Spec) && disableCatchAll {
klog.InfoS("Ignoring add for catch-all ingress because of --disable-catch-all", "ingress", klog.KObj(ing))
return
@ -482,6 +490,13 @@ func New(
return
}
if deepInspector {
if err := inspector.DeepInspect(curIng); err != nil {
klog.ErrorS(err, "received invalid ingress", "ingress", klog.KObj(curIng))
return
}
}
store.syncIngress(curIng)
store.updateSecretIngressMap(curIng)
store.syncSecrets(curIng)

View file

@ -124,6 +124,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)
@ -204,6 +205,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)
@ -307,6 +309,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)
@ -422,6 +425,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
ingressClassconfig)
storer.Run(stopCh)
@ -551,6 +555,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
ingressClassconfig)
storer.Run(stopCh)
@ -650,6 +655,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)
@ -743,6 +749,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)
@ -828,6 +835,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)
@ -923,6 +931,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)
@ -1046,6 +1055,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)
@ -1166,6 +1176,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)
storer.Run(stopCh)