1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-10 18:06:55 +00:00
kyverno/pkg/utils/generator/updaterequests.go
shuting 9e5c297dcf
feat: add a circuit breaker for updaterequests (#10382)
* feat: add generator abstraction

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* feat: replace urgenerator

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* fix: ko build

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* feat: load threshold from kyverno configmap

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* feat: add metadata client to get ur count

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* feat: add helm option to preserve configmap settings during upgrade

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* feat: add helm option to preserve configmap settings during upgrade 2

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* chore: rename imports

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* chore: update codegen manifests

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* fix: handle nil value

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* fix: linter issue

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* chore: update threshold to 1000

Signed-off-by: ShutingZhao <shuting@nirmata.com>

---------

Signed-off-by: ShutingZhao <shuting@nirmata.com>
2024-06-11 08:54:51 +00:00

53 lines
1.6 KiB
Go

package generator
import (
"context"
"errors"
"github.com/go-logr/logr"
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
"github.com/kyverno/kyverno/pkg/client/clientset/versioned"
configutils "github.com/kyverno/kyverno/pkg/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/metadata"
)
type UpdateRequestGenerator = Generator[*v1beta1.UpdateRequest]
type updaterequestsgenerator struct {
config configutils.Configuration
metaClient metadata.Interface
}
func NewUpdateRequestGenerator(config configutils.Configuration, metaClient metadata.Interface) UpdateRequestGenerator {
return &updaterequestsgenerator{
config: config,
metaClient: metaClient,
}
}
func (g *updaterequestsgenerator) Generate(ctx context.Context, client versioned.Interface, resource *v1beta1.UpdateRequest, log logr.Logger) (*v1beta1.UpdateRequest, error) {
objects, err := g.metaClient.Resource(
schema.GroupVersionResource{
Group: "kyverno.io",
Version: "v1beta1",
Resource: "updaterequests",
},
).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
count := len(objects.Items)
threshold := g.config.GetUpdateRequestThreshold()
if int64(count) >= threshold {
log.Error(errors.New("UpdateRequest creation skipped"),
"the number of updaterequests exceeds the threshold, please adjust updateRequestThreshold in the Kyverno configmap",
"current count", count, "threshold", threshold)
return nil, nil
}
created, err := client.KyvernoV1beta1().UpdateRequests(configutils.KyvernoNamespace()).Create(ctx, resource, metav1.CreateOptions{})
return created, err
}