1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-24 08:36:46 +00:00

chore: add dryrun as label (#11962)

* chore: add dryrun as label

Signed-off-by: Javier Solana <javier.solana@cabify.com>

* check request.Dryrun  to avoif SIGSEGV

Signed-off-by: Javier Solana <javier.solana@cabify.com>

* chore: add dryrun

Signed-off-by: Javier Solana <javier.solana@cabify.com>

* chore: add dryrun

Signed-off-by: Javier Solana <javier.solana@cabify.com>

* chore: update doc to add DryRun

Signed-off-by: Javier Solana <javier.solana@cabify.com>

* chore: update to add DryRun

Signed-off-by: Javier Solana <javier.solana@cabify.com>

* chore: update crds

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

---------

Signed-off-by: Javier Solana <javier.solana@cabify.com>
Signed-off-by: ShutingZhao <shuting@nirmata.com>
Co-authored-by: Javier Solana <javier.solana@cabify.com>
Co-authored-by: shuting <shuting@nirmata.com>
This commit is contained in:
Javier Solana 2025-03-07 16:30:22 +01:00 committed by GitHub
parent 29849e196f
commit b00b2c96c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 85 additions and 5 deletions

View file

@ -144,6 +144,11 @@ type RequestInfo struct {
// UserInfo is the userInfo carried in the admission request. // UserInfo is the userInfo carried in the admission request.
// +optional // +optional
AdmissionUserInfo authenticationv1.UserInfo `json:"userInfo"` AdmissionUserInfo authenticationv1.UserInfo `json:"userInfo"`
// DryRun indicates that modifications will definitely not be persisted for this request.
// Defaults to false.
// +optional
DryRun bool `json:"synchronize,omitempty"`
} }
// AdmissionRequestInfoObject stores the admission request and operation details // AdmissionRequestInfoObject stores the admission request and operation details

View file

@ -660,6 +660,11 @@ spec:
type: string type: string
nullable: true nullable: true
type: array type: array
synchronize:
description: |-
DryRun indicates that modifications will definitely not be persisted for this request.
Defaults to false.
type: boolean
userInfo: userInfo:
description: UserInfo is the userInfo carried in the admission description: UserInfo is the userInfo carried in the admission
request. request.

View file

@ -49,6 +49,11 @@ spec:
type: string type: string
nullable: true nullable: true
type: array type: array
synchronize:
description: |-
DryRun indicates that modifications will definitely not be persisted for this request.
Defaults to false.
type: boolean
userInfo: userInfo:
description: UserInfo is the userInfo carried in the admission request. description: UserInfo is the userInfo carried in the admission request.
properties: properties:

View file

@ -49,6 +49,11 @@ spec:
type: string type: string
nullable: true nullable: true
type: array type: array
synchronize:
description: |-
DryRun indicates that modifications will definitely not be persisted for this request.
Defaults to false.
type: boolean
userInfo: userInfo:
description: UserInfo is the userInfo carried in the admission request. description: UserInfo is the userInfo carried in the admission request.
properties: properties:

View file

@ -654,6 +654,11 @@ spec:
type: string type: string
nullable: true nullable: true
type: array type: array
synchronize:
description: |-
DryRun indicates that modifications will definitely not be persisted for this request.
Defaults to false.
type: boolean
userInfo: userInfo:
description: UserInfo is the userInfo carried in the admission description: UserInfo is the userInfo carried in the admission
request. request.

View file

@ -48304,6 +48304,11 @@ spec:
type: string type: string
nullable: true nullable: true
type: array type: array
synchronize:
description: |-
DryRun indicates that modifications will definitely not be persisted for this request.
Defaults to false.
type: boolean
userInfo: userInfo:
description: UserInfo is the userInfo carried in the admission description: UserInfo is the userInfo carried in the admission
request. request.

View file

@ -6946,6 +6946,19 @@ Kubernetes authentication/v1.UserInfo
<p>UserInfo is the userInfo carried in the admission request.</p> <p>UserInfo is the userInfo carried in the admission request.</p>
</td> </td>
</tr> </tr>
<tr>
<td>
<code>synchronize</code><br/>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>DryRun indicates that modifications will definitely not be persisted for this request.
Defaults to false.</p>
</td>
</tr>
</tbody> </tbody>
</table> </table>
<hr /> <hr />

View file

@ -2419,6 +2419,34 @@ Applicable only to policies that have validate.podSecurity subrule.</p>
</tr> </tr>
<tr>
<td><code>synchronize</code>
</br>
<span style="font-family: monospace">bool</span>
</td>
<td>
<p>DryRun indicates that modifications will definitely not be persisted for this request.
Defaults to false.</p>
</td>
</tr>
</tbody> </tbody>

View file

@ -100,7 +100,7 @@ func (e *engine) Validate(
response = response.WithPolicyResponse(policyResponse) response = response.WithPolicyResponse(policyResponse)
} }
response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now())) response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now()))
e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), response) e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), policyContext.AdmissionInfo(), response)
return response return response
} }
@ -118,7 +118,7 @@ func (e *engine) Mutate(
WithPolicyResponse(policyResponse) WithPolicyResponse(policyResponse)
} }
response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now())) response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now()))
e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), response) e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), policyContext.AdmissionInfo(), response)
return response return response
} }
@ -134,7 +134,7 @@ func (e *engine) Generate(
response = response.WithPolicyResponse(policyResponse) response = response.WithPolicyResponse(policyResponse)
} }
response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now())) response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now()))
e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), response) e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), policyContext.AdmissionInfo(), response)
return response return response
} }
@ -153,7 +153,7 @@ func (e *engine) VerifyAndPatchImages(
WithPatchedResource(patchedResource), innerIvm WithPatchedResource(patchedResource), innerIvm
} }
response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now())) response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now()))
e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), response) e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), policyContext.AdmissionInfo(), response)
return response, ivm return response, ivm
} }
@ -169,7 +169,7 @@ func (e *engine) ApplyBackgroundChecks(
response = response.WithPolicyResponse(policyResponse) response = response.WithPolicyResponse(policyResponse)
} }
response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now())) response = response.WithStats(engineapi.NewExecutionStats(startTime, time.Now()))
e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), response) e.reportMetrics(ctx, logger, policyContext.Operation(), policyContext.AdmissionOperation(), policyContext.AdmissionInfo(), response)
return response return response
} }

View file

@ -2,10 +2,12 @@ package engine
import ( import (
"context" "context"
"strconv"
"strings" "strings"
"github.com/go-logr/logr" "github.com/go-logr/logr"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1" kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
kyvernov2 "github.com/kyverno/kyverno/api/kyverno/v2"
engineapi "github.com/kyverno/kyverno/pkg/engine/api" engineapi "github.com/kyverno/kyverno/pkg/engine/api"
"github.com/kyverno/kyverno/pkg/metrics" "github.com/kyverno/kyverno/pkg/metrics"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
@ -17,6 +19,7 @@ func (e *engine) reportMetrics(
logger logr.Logger, logger logr.Logger,
operation kyvernov1.AdmissionOperation, operation kyvernov1.AdmissionOperation,
admissionOperation bool, admissionOperation bool,
admissionInfo kyvernov2.RequestInfo,
response engineapi.EngineResponse, response engineapi.EngineResponse,
) { ) {
if e.resultCounter == nil && e.durationHistogram == nil { if e.resultCounter == nil && e.durationHistogram == nil {
@ -71,6 +74,7 @@ func (e *engine) reportMetrics(
attribute.String("rule_result", string(ruleResult)), attribute.String("rule_result", string(ruleResult)),
attribute.String("rule_type", string(ruleType)), attribute.String("rule_type", string(ruleType)),
attribute.String("rule_execution_cause", string(executionCause)), attribute.String("rule_execution_cause", string(executionCause)),
attribute.String("dry_run", strconv.FormatBool(admissionInfo.DryRun)),
} }
e.resultCounter.Add(ctx, 1, metric.WithAttributes(commonLabels...)) e.resultCounter.Add(ctx, 1, metric.WithAttributes(commonLabels...))
} }
@ -88,6 +92,7 @@ func (e *engine) reportMetrics(
attribute.String("rule_result", string(ruleResult)), attribute.String("rule_result", string(ruleResult)),
attribute.String("rule_type", string(ruleType)), attribute.String("rule_type", string(ruleType)),
attribute.String("rule_execution_cause", string(executionCause)), attribute.String("rule_execution_cause", string(executionCause)),
attribute.String("dry_run", strconv.FormatBool(admissionInfo.DryRun)),
} }
e.durationHistogram.Record(ctx, rule.Stats().ProcessingTime().Seconds(), metric.WithAttributes(commonLabels...)) e.durationHistogram.Record(ctx, rule.Stats().ProcessingTime().Seconds(), metric.WithAttributes(commonLabels...))
} }

View file

@ -34,5 +34,9 @@ func (b *policyContextBuilder) Build(request admissionv1.AdmissionRequest, roles
Roles: roles, Roles: roles,
ClusterRoles: clusterRoles, ClusterRoles: clusterRoles,
} }
if request.DryRun != nil {
userRequestInfo.DryRun = *request.DryRun
}
return engine.NewPolicyContextFromAdmissionRequest(b.jp, request, userRequestInfo, gvk, b.configuration) return engine.NewPolicyContextFromAdmissionRequest(b.jp, request, userRequestInfo, gvk, b.configuration)
} }