2022-10-12 08:52:42 +02:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2023-06-21 15:35:43 +02:00
|
|
|
"context"
|
|
|
|
|
2022-10-12 08:52:42 +02:00
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2022-10-14 06:23:42 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/tls"
|
2022-10-12 08:52:42 +02:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
appsv1informers "k8s.io/client-go/informers/apps/v1"
|
|
|
|
appsv1listers "k8s.io/client-go/listers/apps/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Runtime interface {
|
|
|
|
IsDebug() bool
|
2023-06-21 15:35:43 +02:00
|
|
|
IsReady(ctx context.Context) bool
|
|
|
|
IsLive(ctx context.Context) bool
|
2022-10-12 08:52:42 +02:00
|
|
|
IsRollingUpdate() bool
|
|
|
|
IsGoingDown() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type runtime struct {
|
|
|
|
serverIP string
|
|
|
|
deploymentLister appsv1listers.DeploymentLister
|
2022-10-14 06:23:42 +02:00
|
|
|
certValidator tls.CertValidator
|
2022-10-12 08:52:42 +02:00
|
|
|
logger logr.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRuntime(
|
|
|
|
logger logr.Logger,
|
|
|
|
serverIP string,
|
|
|
|
deploymentInformer appsv1informers.DeploymentInformer,
|
2022-10-14 06:23:42 +02:00
|
|
|
certValidator tls.CertValidator,
|
2022-10-12 08:52:42 +02:00
|
|
|
) Runtime {
|
|
|
|
return &runtime{
|
2022-10-14 06:23:42 +02:00
|
|
|
logger: logger,
|
2022-10-12 08:52:42 +02:00
|
|
|
serverIP: serverIP,
|
|
|
|
deploymentLister: deploymentInformer.Lister(),
|
2022-10-14 06:23:42 +02:00
|
|
|
certValidator: certValidator,
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *runtime) IsDebug() bool {
|
|
|
|
return c.serverIP != ""
|
|
|
|
}
|
|
|
|
|
2023-06-21 15:35:43 +02:00
|
|
|
func (c *runtime) IsLive(context.Context) bool {
|
2022-11-03 10:19:38 +00:00
|
|
|
return true
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 15:35:43 +02:00
|
|
|
func (c *runtime) IsReady(ctx context.Context) bool {
|
|
|
|
return c.validateCertificates(ctx)
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *runtime) IsRollingUpdate() bool {
|
|
|
|
if c.IsDebug() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
deployment, err := c.getDeployment()
|
|
|
|
if err != nil {
|
2022-10-17 03:21:43 +02:00
|
|
|
c.logger.Error(err, "failed to get deployment")
|
2022-10-12 08:52:42 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
var replicas int32 = 1
|
|
|
|
if deployment.Spec.Replicas != nil {
|
|
|
|
replicas = *deployment.Spec.Replicas
|
|
|
|
}
|
|
|
|
nonTerminatedReplicas := deployment.Status.Replicas
|
|
|
|
if nonTerminatedReplicas > replicas {
|
|
|
|
c.logger.Info("detect Kyverno is in rolling update, won't trigger the update again")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *runtime) IsGoingDown() bool {
|
|
|
|
if c.IsDebug() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
deployment, err := c.getDeployment()
|
|
|
|
if err != nil {
|
|
|
|
return apierrors.IsNotFound(err)
|
|
|
|
}
|
2022-10-19 12:06:48 +02:00
|
|
|
if deployment.GetDeletionTimestamp() != nil {
|
|
|
|
return true
|
|
|
|
}
|
2022-10-12 08:52:42 +02:00
|
|
|
if deployment.Spec.Replicas != nil {
|
|
|
|
return *deployment.Spec.Replicas == 0
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *runtime) getDeployment() (*appsv1.Deployment, error) {
|
2022-10-17 03:21:43 +02:00
|
|
|
return c.deploymentLister.Deployments(config.KyvernoNamespace()).Get(config.KyvernoDeploymentName())
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 15:35:43 +02:00
|
|
|
func (c *runtime) validateCertificates(ctx context.Context) bool {
|
|
|
|
validity, err := c.certValidator.ValidateCert(ctx)
|
2022-10-14 06:23:42 +02:00
|
|
|
if err != nil {
|
|
|
|
c.logger.Error(err, "failed to validate certificates")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return validity
|
|
|
|
}
|