Skip to content

Commit

Permalink
Use enactments matching state for policy conditions calculation (#306)
Browse files Browse the repository at this point in the history
Signed-off-by: Quique Llorente <[email protected]>
  • Loading branch information
qinqon authored and kubevirt-bot committed Jan 22, 2020
1 parent 261514b commit 5d140c7
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,80 @@ import (
nmstatev1alpha1 "github.com/nmstate/kubernetes-nmstate/pkg/apis/nmstate/v1alpha1"
)

type CountByConditionType map[nmstatev1alpha1.ConditionType]int
type CountByConditionStatus map[corev1.ConditionStatus]int

func Count(enactments nmstatev1alpha1.NodeNetworkConfigurationEnactmentList) CountByConditionType {
trueConditionsCount := CountByConditionType{}
for _, enactment := range enactments.Items {
for _, conditionType := range nmstatev1alpha1.NodeNetworkConfigurationEnactmentConditionTypes {
type ConditionCount map[nmstatev1alpha1.ConditionType]CountByConditionStatus

func Count(enactments nmstatev1alpha1.NodeNetworkConfigurationEnactmentList) ConditionCount {
conditionCount := ConditionCount{}
for _, conditionType := range nmstatev1alpha1.NodeNetworkConfigurationEnactmentConditionTypes {
conditionCount[conditionType] = CountByConditionStatus{}
for _, enactment := range enactments.Items {
condition := enactment.Status.Conditions.Find(conditionType)
if condition != nil {
if condition.Status == corev1.ConditionTrue {
trueConditionsCount[conditionType] += 1
}
conditionCount[conditionType][condition.Status] += 1
} else {
conditionCount[conditionType][corev1.ConditionUnknown] += 1
}
}
}
return trueConditionsCount
return conditionCount
}

func (c CountByConditionType) Failed() int {
func (c ConditionCount) failed() CountByConditionStatus {
return c[nmstatev1alpha1.NodeNetworkConfigurationEnactmentConditionFailing]
}
func (c CountByConditionType) Progressing() int {
func (c ConditionCount) progressing() CountByConditionStatus {
return c[nmstatev1alpha1.NodeNetworkConfigurationEnactmentConditionProgressing]
}
func (c CountByConditionType) Available() int {
func (c ConditionCount) available() CountByConditionStatus {
return c[nmstatev1alpha1.NodeNetworkConfigurationEnactmentConditionAvailable]
}
func (c ConditionCount) matching() CountByConditionStatus {
return c[nmstatev1alpha1.NodeNetworkConfigurationEnactmentConditionMatching]
}

func (c CountByConditionStatus) true() int {
return c[corev1.ConditionTrue]
}

func (c CountByConditionStatus) false() int {
return c[corev1.ConditionFalse]
}

func (c CountByConditionStatus) unknown() int {
return c[corev1.ConditionUnknown]
}

func (c ConditionCount) Failed() int {
return c.failed().true()
}
func (c ConditionCount) NotFailed() int {
return c.failed().false()
}
func (c ConditionCount) Progressing() int {
return c.progressing().true()
}
func (c ConditionCount) NotProgressing() int {
return c.progressing().false()
}
func (c ConditionCount) Available() int {
return c.available().true()
}
func (c ConditionCount) NotAvailable() int {
return c.available().false()
}
func (c ConditionCount) Matching() int {
return c.matching().true()
}
func (c ConditionCount) NotMatching() int {
return c.matching().false()
}

func (c ConditionCount) String() string {
return fmt.Sprintf("{failed: %s, progressing: %s, available: %s, matching: %s}", c.failed(), c.progressing(), c.available(), c.matching())
}

func (c CountByConditionType) String() string {
return fmt.Sprintf("{failed: %d, progressing: %d, available: %d}", c.Failed(), c.Progressing(), c.Available())
func (c CountByConditionStatus) String() string {
return fmt.Sprintf("{true: %d, false: %d, unknown: %d}", c.true(), c.false(), c.unknown())
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,39 @@ func Update(cli client.Client, policyKey types.NamespacedName) error {
}

nodes := corev1.NodeList{}
nodeSelectorFilter := client.MatchingLabels(policy.Spec.NodeSelector)
err = cli.List(context.TODO(), &nodes, nodeSelectorFilter)
err = cli.List(context.TODO(), &nodes)
if err != nil {
return errors.Wrap(err, "getting nodes failed")
}
numberOfNodesAffected := len(nodes.Items)
numberOfReadyNodes := 0
for _, node := range nodes.Items {
for _, condition := range node.Status.Conditions {
if condition.Type == corev1.NodeReady &&
condition.Status == corev1.ConditionTrue {
numberOfReadyNodes += 1
}
}
}

// Let's get conditions with true status count
enactmentsCount := enactmentconditions.Count(enactments)

logger.Info(fmt.Sprintf("enactments count: %s", enactmentsCount.String()))
numberOfFinishedEnactments := enactmentsCount.Available() + enactmentsCount.Failed() + enactmentsCount.NotMatching()

if numberOfNodesAffected == 0 {
message := "Policy does not match any node"
setPolicyNotMatching(&policy.Status.Conditions, message)
} else if enactmentsCount.Failed()+enactmentsCount.Available() == numberOfNodesAffected {
if enactmentsCount.Failed() > 0 {
message := fmt.Sprintf("%d/%d nodes failed to configure", enactmentsCount.Failed(), numberOfNodesAffected)
logger.Info(fmt.Sprintf("enactments count: %s", enactmentsCount))
if numberOfFinishedEnactments < numberOfReadyNodes {
setPolicyProgressing(&policy.Status.Conditions, fmt.Sprintf("Policy is progressing %d/%d nodes finished", numberOfFinishedEnactments, numberOfReadyNodes))
} else {
if enactmentsCount.Matching() == 0 {
message := "Policy does not match any node"
setPolicyNotMatching(&policy.Status.Conditions, message)
} else if enactmentsCount.Failed() > 0 {
message := fmt.Sprintf("%d/%d nodes failed to configure", enactmentsCount.Failed(), enactmentsCount.Matching())
setPolicyFailedToConfigure(&policy.Status.Conditions, message)
} else {
message := fmt.Sprintf("%d/%d nodes successfully configured", enactmentsCount.Available(), numberOfNodesAffected)
message := fmt.Sprintf("%d/%d nodes successfully configured", enactmentsCount.Available(), enactmentsCount.Available())
setPolicySuccess(&policy.Status.Conditions, message)
}
} else {
setPolicyProgressing(&policy.Status.Conditions, fmt.Sprintf("Policy is progresssing at %d nodes: %s", numberOfNodesAffected, enactmentsCount))
}

err = cli.Status().Update(context.TODO(), policy)
Expand Down
Loading

0 comments on commit 5d140c7

Please sign in to comment.