forked from konflux-ci/qe-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
WIP #2
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
Open
psturc
wants to merge
1
commit into
main
Choose a base branch
from
health-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WIP #2
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,193 @@ | ||
| package prowjob | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
| "time" | ||
|
|
||
| "golang.org/x/exp/slices" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| var healthCheckConfig HealthCheckConfig | ||
|
|
||
| type HealthCheckConfig struct { | ||
| ExternalServices []Service `json:"externalServices"` | ||
| } | ||
|
|
||
| type HealthCheckStatus struct { | ||
| ExternalServices []Service `json:"externalServices"` | ||
| UnhealthyCriticalComponents []string `json:"unhealthyCriticalComponents"` | ||
| } | ||
|
|
||
| type Service struct { | ||
| Name string `json:"name"` | ||
| CriticalComponents []string `json:"criticalComponents"` | ||
| StatusPageURL string `json:"statusPageURL"` | ||
| CurrentStatus Summary `json:"currentStatus"` | ||
| } | ||
|
|
||
| // Summary is the Statuspage API component representation | ||
| type Summary struct { | ||
| Components []Component `json:"components"` | ||
| Incidents []Incident `json:"incidents"` | ||
| Status Status `json:"status"` | ||
| } | ||
|
|
||
| // Component is the Statuspage API component representation | ||
| type Component struct { | ||
| ID string `json:"id,omitempty"` | ||
| PageID string `json:"page_id,omitempty"` | ||
| GroupID string `json:"group_id,omitempty"` | ||
| CreatedAt time.Time `json:"created_at,omitempty"` | ||
| UpdatedAt time.Time `json:"updated_at,omitempty"` | ||
| Group bool `json:"group,omitempty"` | ||
| Name string `json:"name,omitempty"` | ||
| Description string `json:"description,omitempty"` | ||
| Position int32 `json:"position,omitempty"` | ||
| Status string `json:"status,omitempty"` | ||
| Showcase bool `json:"showcase,omitempty"` | ||
| OnlyShowIfDegraded bool `json:"only_show_if_degraded,omitempty"` | ||
| AutomationEmail string `json:"automation_email,omitempty"` | ||
| } | ||
|
|
||
| // Incident entity reflects one single incident | ||
| type Incident struct { | ||
| ID string `json:"id,omitempty"` | ||
| Name string `json:"name,omitempty"` | ||
| Status string `json:"status,omitempty"` | ||
| Message string `json:"message,omitempty"` | ||
| Visible int `json:"visible,omitempty"` | ||
| ComponentID int `json:"component_id,omitempty"` | ||
| ComponentStatus int `json:"component_status,omitempty"` | ||
| Notify bool `json:"notify,omitempty"` | ||
| Stickied bool `json:"stickied,omitempty"` | ||
| OccurredAt string `json:"occurred_at,omitempty"` | ||
| Template string `json:"template,omitempty"` | ||
| Vars []string `json:"vars,omitempty"` | ||
| CreatedAt string `json:"created_at,omitempty"` | ||
| UpdatedAt string `json:"updated_at,omitempty"` | ||
| DeletedAt string `json:"deleted_at,omitempty"` | ||
| IsResolved bool `json:"is_resolved,omitempty"` | ||
| Updates []IncidentUpdate `json:"incident_updates,omitempty"` | ||
| HumanStatus string `json:"human_status,omitempty"` | ||
| LatestUpdateID int `json:"latest_update_id,omitempty"` | ||
| LatestStatus int `json:"latest_status,omitempty"` | ||
| LatestHumanStatus string `json:"latest_human_status,omitempty"` | ||
| LatestIcon string `json:"latest_icon,omitempty"` | ||
| Permalink string `json:"permalink,omitempty"` | ||
| Duration int `json:"duration,omitempty"` | ||
| } | ||
|
|
||
| // IncidentUpdate entity reflects one single incident update | ||
| type IncidentUpdate struct { | ||
| ID string `json:"id,omitempty"` | ||
| Body string `json:"body,omitempty"` | ||
| IncidentID string `json:"incident_id,omitempty"` | ||
| ComponentID int `json:"component_id,omitempty"` | ||
| ComponentStatus int `json:"component_status,omitempty"` | ||
| Status string `json:"status,omitempty"` | ||
| Message string `json:"message,omitempty"` | ||
| UserID int `json:"user_id,omitempty"` | ||
| CreatedAt string `json:"created_at,omitempty"` | ||
| UpdatedAt string `json:"updated_at,omitempty"` | ||
| HumanStatus string `json:"human_status,omitempty"` | ||
| Permalink string `json:"permalink,omitempty"` | ||
| } | ||
|
|
||
| // Status entity contains the contents of API Response of a /status call. | ||
| type Status struct { | ||
| Indicator string `json:"indicator,omitempty"` | ||
| Description string `json:"description,omitempty"` | ||
| } | ||
|
|
||
| // healthCheckCmd represents the createReport command | ||
| var healthCheckCmd = &cobra.Command{ | ||
| Use: "health-check", | ||
| Short: "Perform a health check on dependant services", | ||
| PreRunE: func(cmd *cobra.Command, args []string) error { | ||
| viper.AddConfigPath("./config/health-check") | ||
| viper.SetConfigName("config") | ||
| viper.SetConfigType("yaml") | ||
| if err := viper.ReadInConfig(); err != nil { | ||
| return fmt.Errorf("err readinconfig: %+v", err) | ||
| } | ||
| if err := viper.Unmarshal(&healthCheckConfig); err != nil { | ||
| return fmt.Errorf("failed to parse config: %+v", err) | ||
| } | ||
| return nil | ||
| }, | ||
| SilenceUsage: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| status := &HealthCheckStatus{} | ||
| status.ExternalServices = healthCheckConfig.ExternalServices | ||
| for i, service := range status.ExternalServices { | ||
| r, err := http.Get(service.StatusPageURL) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get service %s status page: %+v", service.Name, err) | ||
| } | ||
| body, err := io.ReadAll(r.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read response body for a service %s: %+v", service.Name, err) | ||
| } | ||
| v := Summary{} | ||
| if err := json.Unmarshal(body, &v); err != nil { | ||
| return fmt.Errorf("failed to unmarshal response body from a service %s: %+v", service.Name, err) | ||
| } | ||
| status.ExternalServices[i].CurrentStatus = v | ||
|
|
||
| for _, c := range v.Components { | ||
| if c.Status == "major_outage" && isCriticalComponent(service, c) { | ||
| desc := fmt.Sprintf("%s: %s", service.Name, c.Name) | ||
| status.UnhealthyCriticalComponents = append(status.UnhealthyCriticalComponents, desc) | ||
| } | ||
| } | ||
|
|
||
| } | ||
| artifactDir := viper.GetString(artifactDirParamName) | ||
| if artifactDir == "" { | ||
| artifactDir = "./tmp" | ||
| klog.Warningf("path to artifact dir was not provided - using default %q\n", artifactDir) | ||
| } | ||
| if err := os.MkdirAll(artifactDir, 0o750); err != nil { | ||
| return fmt.Errorf("failed to create directory for results '%s': %+v", artifactDir, err) | ||
| } | ||
| o, err := json.MarshalIndent(status, "", " ") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal services status: %+v", err) | ||
| } | ||
| reportFilePath := artifactDir + "/services-status.json" | ||
| if err := os.WriteFile(reportFilePath, []byte(o), 0o600); err != nil { | ||
| return fmt.Errorf("failed to create file with the status of dependant services: %+v", err) | ||
| } | ||
| klog.Infof("health check report saved to %s", reportFilePath) | ||
|
|
||
| if viper.GetBool(failIfUnhealthyParamName) { | ||
| // for s := range status.Services.Github.Components { | ||
|
|
||
| // } | ||
| return fmt.Errorf("TESTING UNHEALTHY!") | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func isCriticalComponent(service Service, c Component) bool { | ||
| return slices.Contains(service.CriticalComponents, c.Name) | ||
| } | ||
|
|
||
| func init() { | ||
| healthCheckCmd.Flags().BoolVar(&failIfUnhealthy, failIfUnhealthyParamName, false, "Exit with non-zero code if health check fails") | ||
|
|
||
| _ = viper.BindPFlag(artifactDirParamName, healthCheckCmd.Flags().Lookup(artifactDirParamName)) | ||
| _ = viper.BindPFlag(failIfUnhealthyParamName, healthCheckCmd.Flags().Lookup(failIfUnhealthyParamName)) | ||
| // Bind environment variables to viper (in case the associated command's parameter is not provided) | ||
| _ = viper.BindEnv(artifactDirParamName, artifactDirEnv) | ||
| } | ||
This file contains hidden or 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 hidden or 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,19 @@ | ||
| externalServices: | ||
| - name: redhat | ||
| criticalComponents: | ||
| - registry.redhat.io | ||
| - registry.access.redhat.com | ||
| statusPageURL: https://status.redhat.com/api/v2/summary.json | ||
| - name: quay | ||
| criticalComponents: | ||
| - Registry | ||
| - API | ||
| - Build System | ||
| statusPageURL: https://status.quay.io/api/v2/summary.json | ||
| - name: github | ||
| criticalComponents: | ||
| - Git Operations | ||
| - API Requests | ||
| - Webhooks | ||
| - Pull Requests | ||
| statusPageURL: https://www.githubstatus.com/api/v2/summary.json |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove this if not required?