Skip to content

Commit

Permalink
Merge pull request #317 from werf/feat-separate-file-for-user-contrib…
Browse files Browse the repository at this point in the history
…uted-resource-status-rules

feat: separate file for user-contributed resource status rules
  • Loading branch information
ilya-lesikov authored Dec 18, 2023
2 parents f0cf8e3 + 0f41256 commit e4b47e6
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 12 deletions.
1 change: 1 addition & 0 deletions contrib/resource_status_rules.yaml
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/samber/lo v1.38.1
github.com/spf13/cobra v1.6.1
github.com/werf/logboek v0.5.5
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/crypto v0.7.0
k8s.io/api v0.26.2
k8s.io/apimachinery v0.26.2
Expand Down Expand Up @@ -52,6 +53,8 @@ require (
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/werf/logboek v0.5.5 h1:RmtTejHJOyw0fub4pIfKsb7OTzD90ZOUyuBAXqYqJpU=
github.com/werf/logboek v0.5.5/go.mod h1:Gez5J4bxekyr6MxTmIJyId1F61rpO+0/V4vjCIEIZmk=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xlab/treeprint v1.1.0 h1:G/1DjNkPpfZCFt9CSh6b5/nY4VimlbHF3Rh4obvtzDk=
github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
Expand Down
73 changes: 73 additions & 0 deletions pkg/tracker/generic/contrib_resource_status_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package generic

import (
_ "embed"
"encoding/json"
"fmt"

"github.com/xeipuuv/gojsonschema"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/yaml"
)

//go:embed contrib_resource_status_rules.schema.json
var contribResourceStatusRulesSchema string

//go:embed contrib_resource_status_rules.yaml
var contribResourceStatusRules string

type ContribResourceStatusRules struct {
Rules []struct {
ResourceGroup *string `yaml:"resourceGroup"`
ResourceKind *string `yaml:"resourceKind"`
JSONPath string `yaml:"jsonPath"`
HumanJSONPath string `yaml:"humanJsonPath"`
Conditions struct {
Ready []string `yaml:"ready"`
Progressing []string `yaml:"progressing"`
Failed []string `yaml:"failed"`
} `yaml:"conditions"`
} `yaml:"rules"`
}

func buildContribResourceStatusRules() {
rulesJsonByte, err := yaml.ToJSON([]byte(contribResourceStatusRules))
if err != nil {
panic(fmt.Sprintf("convert rules yaml file to json: %s", err))
}
rulesJson := string(rulesJsonByte)

schemaLoader := gojsonschema.NewStringLoader(contribResourceStatusRulesSchema)
documentLoader := gojsonschema.NewStringLoader(rulesJson)

if result, err := gojsonschema.Validate(schemaLoader, documentLoader); err != nil {
panic(fmt.Sprintf("validate rules file: %s", err))
} else if !result.Valid() {
msg := "Rules file is not valid:\n"
for _, err := range result.Errors() {
msg += fmt.Sprintf("- %s\n", err)
}
panic(msg)
}

rules := &ContribResourceStatusRules{}
if err := json.Unmarshal(rulesJsonByte, rules); err != nil {
panic(fmt.Sprintf("unmarshal rules file: %s", err))
}

for _, rule := range rules.Rules {
var groupKind *schema.GroupKind
if rule.ResourceGroup != nil && rule.ResourceKind != nil {
groupKind = &schema.GroupKind{Group: *rule.ResourceGroup, Kind: *rule.ResourceKind}
}

ResourceStatusJSONPathConditions = append(ResourceStatusJSONPathConditions, &ResourceStatusJSONPathCondition{
GroupKind: groupKind,
JSONPath: rule.JSONPath,
HumanPath: rule.HumanJSONPath,
ReadyValues: casify(rule.Conditions.Ready...),
PendingValues: casify(rule.Conditions.Progressing...),
FailedValues: casify(rule.Conditions.Failed...),
})
}
}
74 changes: 74 additions & 0 deletions pkg/tracker/generic/contrib_resource_status_rules.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"$id": "http://werf.io/contrib_resource_status_rules.schema.json",
"title": "Contrib resource status rules schema",
"type": "object",
"required": [
"rules"
],
"properties": {
"rules": {
"type": "array",
"items": {
"type": "object",
"dependencies": {
"resourceGroup": ["resourceKind"],
"resourceKind": ["resourceGroup"]
},
"required": [
"jsonPath",
"humanJsonPath",
"conditions"
],
"properties": {
"resourceGroup": {
"type": "string",
"minLength": 1
},
"resourceKind": {
"type": "string",
"minLength": 1
},
"jsonPath": {
"type": "string",
"minLength": 1
},
"humanJsonPath": {
"type": "string",
"minLength": 1
},
"conditions": {
"type": "object",
"required": [
"ready",
"progressing"
],
"properties": {
"ready": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
},
"progressing": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
},
"failed": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
}
}
}
}
}
}
}
}
15 changes: 15 additions & 0 deletions pkg/tracker/generic/contrib_resource_status_rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
rules:
- resourceGroup: "acid.zalan.do"
resourceKind: "postgresql"
jsonPath: "$.status.PostgresClusterStatus"
humanJsonPath: "status.PostgresClusterStatus"
conditions:
ready:
- "Running"
progressing:
- "Creating"
- "Updating"
failed:
- "CreateFailed"
- "UpdateFailed"
- "DeleteFailed"
13 changes: 1 addition & 12 deletions pkg/tracker/generic/resource_state_json_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,11 @@ type ResourceStatusJSONPathCondition struct {
}

func initResourceStatusJSONPathsByPriority() {
buildResourceSpecificConditions()
buildContribResourceStatusRules()
buildUniversalConditions()
buildLowPriorityConditions()
}

func buildResourceSpecificConditions() {
ResourceStatusJSONPathConditions = append(ResourceStatusJSONPathConditions, &ResourceStatusJSONPathCondition{
GroupKind: &schema.GroupKind{Group: "acid.zalan.do", Kind: "postgresql"},
JSONPath: `$.status.PostgresClusterStatus`,
HumanPath: "status.PostgresClusterStatus",
ReadyValues: casify("Running"),
PendingValues: casify("Creating", "Updating"),
FailedValues: casify("CreateFailed", "UpdateFailed", "SyncFailed"),
})
}

func buildUniversalConditions() {
readyValuesByPriority := []string{
"ready",
Expand Down

0 comments on commit e4b47e6

Please sign in to comment.