|
| 1 | +package cannotretrieveupdatessre |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + configv1 "github.com/openshift/api/config/v1" |
| 10 | + "github.com/openshift/configuration-anomaly-detection/pkg/investigations/investigation" |
| 11 | + k8sclient "github.com/openshift/configuration-anomaly-detection/pkg/k8s" |
| 12 | + "github.com/openshift/configuration-anomaly-detection/pkg/logging" |
| 13 | + "github.com/openshift/configuration-anomaly-detection/pkg/networkverifier" |
| 14 | + "github.com/openshift/configuration-anomaly-detection/pkg/notewriter" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + alertname = "CannotRetrieveUpdatesSRE" |
| 20 | + remediationName = "CannotRetrieveUpdatesSRE" |
| 21 | +) |
| 22 | + |
| 23 | +type Investigation struct{} |
| 24 | + |
| 25 | +// Run executes the investigation for the CannotRetrieveUpdatesSRE alert |
| 26 | +func (c *Investigation) Run(r *investigation.Resources) (investigation.InvestigationResult, error) { |
| 27 | + result := investigation.InvestigationResult{} |
| 28 | + notes := notewriter.New("CannotRetrieveUpdatesSRE", logging.RawLogger) |
| 29 | + k8scli, err := k8sclient.New(r.Cluster.ID(), r.OcmClient, remediationName) |
| 30 | + if err != nil { |
| 31 | + return result, fmt.Errorf("unable to initialize k8s cli: %w", err) |
| 32 | + } |
| 33 | + defer func() { |
| 34 | + logging.Infof("Cleaning up investigation resources for cluster %s", r.Cluster.ID()) |
| 35 | + deferErr := k8sclient.Cleanup(r.Cluster.ID(), r.OcmClient, remediationName) |
| 36 | + if deferErr != nil { |
| 37 | + logging.Error(deferErr) |
| 38 | + err = errors.Join(err, deferErr) |
| 39 | + } else { |
| 40 | + logging.Infof("Cleanup completed successfully for cluster %s", r.Cluster.ID()) |
| 41 | + } |
| 42 | + }() |
| 43 | + |
| 44 | + // Run network verifier |
| 45 | + verifierResult, failureReason, err := networkverifier.Run(r.Cluster, r.ClusterDeployment, r.AwsClient) |
| 46 | + if err != nil { |
| 47 | + notes.AppendWarning("NetworkVerifier failed to run:\n\t %s", err.Error()) |
| 48 | + } else { |
| 49 | + switch verifierResult { |
| 50 | + case networkverifier.Failure: |
| 51 | + result.ServiceLogPrepared = investigation.InvestigationStep{Performed: true, Labels: nil} |
| 52 | + notes.AppendWarning("NetworkVerifier found unreachable targets. \n \n Verify and send service log if necessary: \n osdctl servicelog post %s -t https://raw.githubusercontent.com/openshift/managed-notifications/master/osd/required_network_egresses_are_blocked.json -p URLS=%s", r.Cluster.ID(), failureReason) |
| 53 | + case networkverifier.Success: |
| 54 | + notes.AppendSuccess("Network verifier passed") |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Check ClusterVersion |
| 59 | + clusterVersion, note, err := checkClusterVersion(k8scli, r.Cluster.ID()) |
| 60 | + if err != nil { |
| 61 | + notes.AppendWarning("Failure checking ClusterVersion: %s", err.Error()) |
| 62 | + if note != "" { |
| 63 | + notes.AppendWarning(note) |
| 64 | + } |
| 65 | + } else { |
| 66 | + if note != "" { |
| 67 | + notes.AppendWarning(note) |
| 68 | + } |
| 69 | + if clusterVersion != "" { |
| 70 | + notes.AppendSuccess("ClusterVersion found: %s", clusterVersion) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + notes.AppendWarning("Alert escalated to on-call primary for review.") |
| 75 | + logging.Infof("Escalating incident with notes for cluster %s", r.Cluster.ID()) |
| 76 | + err = r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 77 | + if err != nil { |
| 78 | + logging.Errorf("Failed to escalate incident to PagerDuty: %v", err) |
| 79 | + return result, fmt.Errorf("failed to escalate incident: %w", err) |
| 80 | + } |
| 81 | + logging.Infof("Investigation completed and escalated successfully for cluster %s", r.Cluster.ID()) |
| 82 | + |
| 83 | + return result, nil |
| 84 | +} |
| 85 | + |
| 86 | +// checkClusterVersion retrieves the cluster version |
| 87 | +func checkClusterVersion(k8scli client.Client, clusterID string) (version string, note string, err error) { |
| 88 | + logging.Infof("Checking ClusterVersion for cluster %s", clusterID) |
| 89 | + clusterVersion := &configv1.ClusterVersion{} |
| 90 | + err = k8scli.Get(context.TODO(), client.ObjectKey{Name: "version"}, clusterVersion) |
| 91 | + if err != nil { |
| 92 | + return "", "Failed to get ClusterVersion: cluster access issues detected", fmt.Errorf("failed to get ClusterVersion: %w", err) |
| 93 | + } |
| 94 | + logging.Infof("ClusterVersion channel: %s", clusterVersion.Spec.Channel) |
| 95 | + logging.Infof("ClusterVersion found: %s", clusterVersion.Status.Desired.Version) |
| 96 | + |
| 97 | + for _, condition := range clusterVersion.Status.Conditions { |
| 98 | + if condition.Type == "RetrievedUpdates" { |
| 99 | + note, err = checkRetrievedUpdatesCondition(condition, clusterVersion.Status.Desired.Version, clusterVersion.Spec.Channel) |
| 100 | + if err != nil { |
| 101 | + return "", note, err |
| 102 | + } |
| 103 | + return clusterVersion.Status.Desired.Version, note, nil |
| 104 | + } |
| 105 | + } |
| 106 | + return clusterVersion.Status.Desired.Version, "", nil |
| 107 | +} |
| 108 | + |
| 109 | +func checkRetrievedUpdatesCondition(condition configv1.ClusterOperatorStatusCondition, currentVersion, channel string) (string, error) { |
| 110 | + if condition.Status == configv1.ConditionFalse { |
| 111 | + if (condition.Reason == "VersionNotFound" || condition.Reason == "RemoteFailed") && |
| 112 | + strings.Contains(strings.TrimSpace(condition.Message), "Unable to retrieve available updates") { |
| 113 | + logging.Warnf("Detected ClusterVersion issue: Reason=%s, Message=%s", condition.Reason, condition.Message) |
| 114 | + note := fmt.Sprintf("ClusterVersion issue detected: %s. Current version %s not found in channel %s", |
| 115 | + condition.Message, currentVersion, channel) |
| 116 | + return note, fmt.Errorf("clusterversion has undesirable state: %s", condition.Reason) |
| 117 | + } |
| 118 | + } |
| 119 | + return "", nil |
| 120 | +} |
| 121 | + |
| 122 | +func (i *Investigation) Name() string { |
| 123 | + return alertname |
| 124 | +} |
| 125 | + |
| 126 | +func (i *Investigation) Description() string { |
| 127 | + return fmt.Sprintf("Investigates '%s' alerts by running network verifier and checking ClusterVersion", alertname) |
| 128 | +} |
| 129 | + |
| 130 | +func (i *Investigation) ShouldInvestigateAlert(alert string) bool { |
| 131 | + return strings.Contains(alert, alertname) |
| 132 | +} |
| 133 | + |
| 134 | +func (i *Investigation) IsExperimental() bool { |
| 135 | + return true |
| 136 | +} |
0 commit comments