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

feat: refactor/improve generic tracker status ruleset #315

Merged
merged 10 commits into from
Dec 18, 2023
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.20

require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/chanced/caps v1.0.1
github.com/dominikbraun/graph v0.23.0
github.com/fluxcd/flagger v1.29.0
github.com/gookit/color v1.5.2
github.com/samber/lo v1.38.1
github.com/spf13/cobra v1.6.1
github.com/werf/logboek v0.5.5
golang.org/x/crypto v0.7.0
golang.org/x/text v0.8.0
k8s.io/api v0.26.2
k8s.io/apimachinery v0.26.2
k8s.io/cli-runtime v0.26.2
Expand Down Expand Up @@ -60,6 +60,7 @@ require (
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/avelino/slugify v0.0.0-20180501145920-855f152bd774/go.mod h1:5wi5YYOp
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/chanced/caps v1.0.1 h1:B9LhH2mmAcNjn1argkddu9fRdU+VMrfomRoeXL1DYhg=
github.com/chanced/caps v1.0.1/go.mod h1:SJhRzeYLKJ3OmzyQXhdZ7Etj7lqqWoPtQ1zcSJRtQjs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
49 changes: 24 additions & 25 deletions pkg/tracker/generic/ready_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,46 @@ package generic
import (
"fmt"

"github.com/samber/lo"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/werf/kubedog/pkg/tracker/indicators"
"github.com/werf/kubedog/pkg/utils"
)

func NewResourceStatusIndicator(object *unstructured.Unstructured) (indicator *indicators.StringEqualConditionIndicator, humanJSONPath string, err error) {
var matchedJSONPath *ResourceStatusJSONPath
for _, readyJSONPath := range ResourceStatusJSONPathsByPriority {
result, found, err := utils.JSONPath(readyJSONPath.JSONPath, object.UnstructuredContent())
if err != nil {
return nil, "", fmt.Errorf("jsonpath error: %w", err)
} else if !found {
groupKind := object.GroupVersionKind().GroupKind()

var matchedCondition *ResourceStatusJSONPathCondition
for _, condition := range ResourceStatusJSONPathConditions {
if condition.GroupKind != nil && *condition.GroupKind != groupKind {
continue
}

var resultIsValidValue bool
for _, validValue := range append(readyJSONPath.PendingValues, readyJSONPath.ReadyValue, readyJSONPath.FailedValue) {
if result == validValue {
resultIsValidValue = true
break
}
}
if !resultIsValidValue {
currentValue, found, err := utils.JSONPath(condition.JSONPath, object.UnstructuredContent())
if err != nil {
return nil, "", fmt.Errorf("jsonpath error: %w", err)
} else if !found {
continue
}

path := readyJSONPath
matchedJSONPath = &path
matchedJSONPath.CurrentValue = result
knownValues := lo.Union(condition.ReadyValues, condition.PendingValues, condition.FailedValues)

break
if lo.Contains(knownValues, currentValue) {
matchedCondition = condition
matchedCondition.CurrentValue = currentValue
break
}
}

if matchedJSONPath == nil {
if matchedCondition == nil {
return nil, "", nil
}

return &indicators.StringEqualConditionIndicator{
Value: matchedJSONPath.CurrentValue,
TargetValue: matchedJSONPath.ReadyValue,
FailedValue: matchedJSONPath.FailedValue,
}, matchedJSONPath.HumanPath, nil
indicator = &indicators.StringEqualConditionIndicator{
Value: matchedCondition.CurrentValue,
}
indicator.SetReady(lo.Contains(matchedCondition.ReadyValues, matchedCondition.CurrentValue))
indicator.SetFailed(lo.Contains(matchedCondition.FailedValues, matchedCondition.CurrentValue))

return indicator, matchedCondition.HumanPath, nil
}
Loading