-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from werf/feat-separate-file-for-user-contrib…
…uted-resource-status-rules feat: separate file for user-contributed resource status rules
- Loading branch information
Showing
7 changed files
with
170 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../pkg/tracker/generic/contrib_resource_status_rules.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
pkg/tracker/generic/contrib_resource_status_rules.schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters