Skip to content
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

Fix status for invalid object #107

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1568,24 +1568,27 @@ func (r *ConfigurationPolicyReconciler) handleSingleObj(
if !exists && obj.shouldExist {
// it is a musthave and it does not exist, so it must be created
if strings.EqualFold(string(remediation), string(policyv1.Enforce)) {
// object is missing, so send noncompliant event
_ = createStatus("", obj.gvr.Resource, compliantObject, obj.namespaced, obj.policy,
obj.index, false, true)
obj.policy.Status.ComplianceState = policyv1.NonCompliant
statusStr := convertPolicyStatusToString(obj.policy)
objLog.Info("Sending an update policy status event", "policy", obj.policy.Name, "status", statusStr)
r.Recorder.Event(obj.policy, eventWarning, fmt.Sprintf(eventFmtStr, obj.policy.GetName(), obj.name),
statusStr)
// update parent policy status
r.addForUpdate(obj.policy, true)

var uid string
statusUpdateNeeded, uid, err = r.enforceByCreatingOrDeleting(obj)

if err != nil {
// violation created for handling error
objLog.Error(err, "Could not handle missing musthave object")
} else {
// object is missing and will be created, so send noncompliant "does not exist" event first
// (this check has already happened, but we send the event here to avoid the status flipping on an
// error)
_ = createStatus("", obj.gvr.Resource, compliantObject, obj.namespaced, obj.policy,
obj.index, false, true)
obj.policy.Status.ComplianceState = policyv1.NonCompliant
statusStr := convertPolicyStatusToString(obj.policy)
objLog.Info("Sending a noncompliant status event (object missing)", "policy", obj.policy.Name, "status",
statusStr)
r.Recorder.Event(obj.policy, eventWarning, fmt.Sprintf(eventFmtStr, obj.policy.GetName(), obj.name),
statusStr)
// update parent policy status
r.addForUpdate(obj.policy, true)

created := true
creationInfo = &policyv1.ObjectProperties{
CreatedByPolicy: &created,
Expand Down Expand Up @@ -1638,7 +1641,7 @@ func (r *ConfigurationPolicyReconciler) handleSingleObj(
throwSpecViolation, msg, pErr, triedUpdate, updatedObj := r.checkAndUpdateResource(obj, compType, mdCompType,
remediation)

if triedUpdate {
if triedUpdate && !strings.Contains(msg, "Error validating the object") {
// object has a mismatch and needs an update to be enforced, throw violation for mismatch
_ = createStatus("", obj.gvr.Resource, compliantObject, obj.namespaced, obj.policy, obj.index,
false, true)
Expand Down
35 changes: 35 additions & 0 deletions test/e2e/case23_invalid_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package e2e

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -39,6 +40,23 @@ var _ = Describe("Test an objectDefinition with an invalid field", Ordered, func
g.Expect(utils.GetStatusMessage(managedPlc)).To(Equal(expectedMsg))
}, defaultTimeoutSeconds, 1).Should(Succeed())

By("Verifying events do not continue to be created after the first violation for created objects")
startTime := metav1.NewTime(time.Now())

Consistently(func() interface{} {
compPlcEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
policyName,
"",
"unknown field \"invalid\" in io.k8s.api.core.v1.ConfigMap",
defaultTimeoutSeconds)

if len(compPlcEvents) == 0 {
return false
}

return startTime.After(compPlcEvents[len(compPlcEvents)-1].LastTimestamp.Time)
}, defaultTimeoutSeconds, 1).Should(BeTrue())

By("Verifying the message is correct when the " + configMapName + " ConfigMap already exists")
configmap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -57,6 +75,23 @@ var _ = Describe("Test an objectDefinition with an invalid field", Ordered, func

return utils.GetStatusMessage(managedPlc)
}, defaultTimeoutSeconds, 1).Should(Equal(expectedMsg))

By("Verifying events do not continue to be created after the first violation for existing objects")
alreadyExistsStartTime := metav1.NewTime(time.Now())

Consistently(func() interface{} {
compPlcEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
policyName,
"",
"unknown field \"invalid\" in io.k8s.api.core.v1.ConfigMap",
defaultTimeoutSeconds)

if len(compPlcEvents) == 0 {
return false
}

return alreadyExistsStartTime.After(compPlcEvents[len(compPlcEvents)-1].LastTimestamp.Time)
}, defaultTimeoutSeconds, 1).Should(BeTrue())
})

AfterAll(func() {
Expand Down