-
Notifications
You must be signed in to change notification settings - Fork 762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: check for CT generateVap intent before generating vapbinding #3479
Changes from 1 commit
adef05d
62ac785
d4148f5
6868702
b0f26cb
70478ee
af0988f
34eb883
ebb02b5
2327de8
589312a
59cebbf
a1ab00b
24dd83d
d064c85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -476,28 +476,24 @@ func (r *ReconcileConstraintTemplate) handleUpdate( | |
logger.Error(err, "error adding template to watch registry") | ||
return reconcile.Result{}, err | ||
} | ||
isVAPapiEnabled := false | ||
isVapAPIEnabled := false | ||
var groupVersion *schema.GroupVersion | ||
if generateVap { | ||
isVAPapiEnabled, groupVersion = constraint.IsVapAPIEnabled() | ||
isVapAPIEnabled, groupVersion = constraint.IsVapAPIEnabled() | ||
} | ||
logger.Info("isVAPapiEnabled", "isVAPapiEnabled", isVAPapiEnabled) | ||
logger.Info("isVapAPIEnabled", "isVapAPIEnabled", isVapAPIEnabled) | ||
logger.Info("groupVersion", "groupVersion", groupVersion) | ||
if generateVap && (!isVAPapiEnabled || groupVersion == nil) { | ||
logger.V(1).Info("Warning: ValidatingAdmissionPolicy API is not enabled, ValidatingAdmissionPolicy resource cannot be generated for ConstraintTemplate", "name", ct.GetName()) | ||
createErr := &v1beta1.CreateCRDError{Code: ErrCreateCode, Message: "ValidatingAdmissionPolicy API is not enabled, ValidatingAdmissionPolicy resource cannot be generated for ConstraintTemplate"} | ||
status.Status.Errors = append(status.Status.Errors, createErr) | ||
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Warning: ValidatingAdmissionPolicy resource cannot be generated for ConstraintTemplate", status, errors.New("ValidatingAdmissionPolicy API is not enabled")) | ||
if generateVap && (!isVapAPIEnabled || groupVersion == nil) { | ||
logger.Error(constraint.ErrValidatingAdmissionPolicyAPIDisabled, "ValidatingAdmissionPolicy resource cannot be generated for ConstraintTemplate", "name", ct.GetName()) | ||
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "ValidatingAdmissionPolicy resource cannot be generated for ConstraintTemplate", status, constraint.ErrValidatingAdmissionPolicyAPIDisabled) | ||
return reconcile.Result{}, err | ||
} | ||
// generating vap resources | ||
if generateVap && isVAPapiEnabled && groupVersion != nil { | ||
if generateVap && isVapAPIEnabled && groupVersion != nil { | ||
currentVap, err := vapForVersion(groupVersion) | ||
if err != nil { | ||
logger.Error(err, "error getting vap object with respective groupVersion") | ||
createErr := &v1beta1.CreateCRDError{Code: ErrCreateCode, Message: err.Error()} | ||
status.Status.Errors = append(status.Status.Errors, createErr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when you omit this, you are overriding existing status errors. please restore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are not preserving statues errors here anyways - https://github.com/open-policy-agent/gatekeeper/blob/master/pkg/controller/constrainttemplate/constrainttemplate_controller.go#L404, this code I am removing isn't doing anything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the code that we have wihtout appending to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. ok then this has no impact on existing behavior. |
||
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Could not get VAP with correct group version", status, err) | ||
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Could not get VAP with runtime group version", status, err) | ||
return reconcile.Result{}, err | ||
} | ||
vapName := fmt.Sprintf("gatekeeper-%s", unversionedCT.GetName()) | ||
|
@@ -512,17 +508,13 @@ func (r *ReconcileConstraintTemplate) handleUpdate( | |
transformedVap, err := transform.TemplateToPolicyDefinition(unversionedCT) | ||
if err != nil { | ||
logger.Error(err, "transform to vap error", "vapName", vapName) | ||
createErr := &v1beta1.CreateCRDError{Code: ErrCreateCode, Message: err.Error()} | ||
status.Status.Errors = append(status.Status.Errors, createErr) | ||
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Could not transform to vap object", status, err) | ||
return reconcile.Result{}, err | ||
} | ||
|
||
newVap, err := getRunTimeVAP(groupVersion, transformedVap, currentVap) | ||
if err != nil { | ||
logger.Error(err, "getRunTimeVAP error", "vapName", vapName) | ||
createErr := &v1beta1.CreateCRDError{Code: ErrCreateCode, Message: err.Error()} | ||
status.Status.Errors = append(status.Status.Errors, createErr) | ||
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Could not get runtime vap object", status, err) | ||
return reconcile.Result{}, err | ||
} | ||
|
@@ -535,8 +527,6 @@ func (r *ReconcileConstraintTemplate) handleUpdate( | |
logger.Info("creating vap", "vapName", vapName) | ||
if err := r.Create(ctx, newVap); err != nil { | ||
logger.Info("creating vap error", "vapName", vapName, "error", err) | ||
createErr := &v1beta1.CreateCRDError{Code: ErrCreateCode, Message: err.Error()} | ||
status.Status.Errors = append(status.Status.Errors, createErr) | ||
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Could not create vap object", status, err) | ||
return reconcile.Result{}, err | ||
} | ||
|
@@ -547,21 +537,17 @@ func (r *ReconcileConstraintTemplate) handleUpdate( | |
} else if !reflect.DeepEqual(currentVap, newVap) { | ||
logger.Info("updating vap") | ||
if err := r.Update(ctx, newVap); err != nil { | ||
updateErr := &v1beta1.CreateCRDError{Code: ErrUpdateCode, Message: err.Error()} | ||
status.Status.Errors = append(status.Status.Errors, updateErr) | ||
err := r.reportErrorOnCTStatus(ctx, ErrUpdateCode, "Could not update vap object", status, err) | ||
return reconcile.Result{}, err | ||
} | ||
} | ||
} | ||
// do not generate vap resources | ||
// remove if exists | ||
if !generateVap && isVAPapiEnabled && groupVersion != nil { | ||
if !generateVap && isVapAPIEnabled && groupVersion != nil { | ||
currentVap, err := vapForVersion(groupVersion) | ||
if err != nil { | ||
logger.Error(err, "error getting vap object with respective groupVersion") | ||
createErr := &v1beta1.CreateCRDError{Code: ErrCreateCode, Message: err.Error()} | ||
status.Status.Errors = append(status.Status.Errors, createErr) | ||
err := r.reportErrorOnCTStatus(ctx, ErrCreateCode, "Could not get VAP with correct group version", status, err) | ||
return reconcile.Result{}, err | ||
} | ||
|
@@ -576,8 +562,6 @@ func (r *ReconcileConstraintTemplate) handleUpdate( | |
if currentVap != nil { | ||
logger.Info("deleting vap") | ||
if err := r.Delete(ctx, currentVap); err != nil { | ||
updateErr := &v1beta1.CreateCRDError{Code: ErrUpdateCode, Message: err.Error()} | ||
status.Status.Errors = append(status.Status.Errors, updateErr) | ||
err := r.reportErrorOnCTStatus(ctx, ErrUpdateCode, "Could not delete vap object", status, err) | ||
return reconcile.Result{}, err | ||
} | ||
|
@@ -714,8 +698,6 @@ func (r *ReconcileConstraintTemplate) triggerConstraintEvents(ctx context.Contex | |
cstrObjs, err := r.listObjects(ctx, gvk) | ||
if err != nil { | ||
logger.Error(err, "get all constraints listObjects") | ||
updateErr := &v1beta1.CreateCRDError{Code: ErrUpdateCode, Message: err.Error()} | ||
status.Status.Errors = append(status.Status.Errors, updateErr) | ||
err := r.reportErrorOnCTStatus(ctx, ErrUpdateCode, "Could not list all constraint objects", status, err) | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should be consistent everywhere in terms of logs and status reporting for these. if api is not enabled and other conditions for generateVap are not met, should they be considered errors or warnings? constraint and CT status reporting currently only has errors. @maxsmythe @sozercan thoughts? depending on this decision, please make all logs and status reporting consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's an error -- the user has signaled intent to trigger the generation pipeline but the pipeline is not generating.
If users don't care about the generation pipeline, they can interpret the severity of the error however they'd like.
If users would like to remove the error, they can change the expressed intent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm. then lets log error and report error as part of CT and Constraint status.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm, I am updating the PR.