|
| 1 | +package cannotretrieveupdatesre |
| 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 | + "k8s.io/apimachinery/pkg/fields" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + alertname = "CannotRetrieveUpdatesSRE" |
| 21 | + remediationName = "cannotretrieveupdatesre" |
| 22 | +) |
| 23 | + |
| 24 | +type Investigation struct { |
| 25 | + kclient client.Client |
| 26 | + notes *notewriter.NoteWriter |
| 27 | +} |
| 28 | + |
| 29 | +// Run executes the investigation for the CannotRetrieveUpdatesSRE alert |
| 30 | +func (i *Investigation) Run(r *investigation.Resources) (investigation.InvestigationResult, error) { |
| 31 | + result := investigation.InvestigationResult{} |
| 32 | + |
| 33 | + // Setup |
| 34 | + err := i.setup(r) |
| 35 | + if err != nil { |
| 36 | + return result, fmt.Errorf("failed to setup investigation: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + defer func(r *investigation.Resources) { |
| 40 | + logging.Infof("Cleaning up investigation resources for cluster %s", r.Cluster.ID()) |
| 41 | + if cleanupErr := k8sclient.Cleanup(r.Cluster.ID(), r.OcmClient, remediationName); cleanupErr != nil { |
| 42 | + logging.Errorf("Failed to cleanup Kubernetes client: %v", cleanupErr) |
| 43 | + } else { |
| 44 | + logging.Infof("Cleanup completed successfully for cluster %s", r.Cluster.ID()) |
| 45 | + } |
| 46 | + }(r) |
| 47 | + |
| 48 | + if err := i.checkClusterValidity(r); err != nil { |
| 49 | + logging.Errorf("Cluster validation failed: %v", err) |
| 50 | + return result, r.PdClient.EscalateIncidentWithNote(i.notes.String()) |
| 51 | + } |
| 52 | + |
| 53 | + if err := i.runNetworkVerifier(r, &result); err != nil { |
| 54 | + logging.Errorf("Network verification failed: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + if err := i.checkClusterVersion(r); err != nil { |
| 58 | + logging.Errorf("ClusterVersion check failed: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + i.notes.AppendWarning("Alert escalated to on-call primary for review.") |
| 62 | + logging.Infof("Escalating incident with notes for cluster %s", r.Cluster.ID()) |
| 63 | + err = r.PdClient.EscalateIncidentWithNote(i.notes.String()) |
| 64 | + if err != nil { |
| 65 | + logging.Errorf("Failed to escalate incident to PagerDuty: %v", err) |
| 66 | + return result, fmt.Errorf("failed to escalate incident: %w", err) |
| 67 | + } |
| 68 | + logging.Infof("Investigation completed and escalated successfully for cluster %s", r.Cluster.ID()) |
| 69 | + |
| 70 | + return result, nil |
| 71 | +} |
| 72 | + |
| 73 | +func (i *Investigation) checkClusterValidity(r *investigation.Resources) error { |
| 74 | + if r.Cluster == nil || r.Cluster.ID() == "" { |
| 75 | + errMsg := "invalid cluster configuration: cluster or cluster ID is missing" |
| 76 | + i.notes.AppendWarning(errMsg) |
| 77 | + return errors.New(errMsg) |
| 78 | + } |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +func (i *Investigation) runNetworkVerifier(r *investigation.Resources, result *investigation.InvestigationResult) error { |
| 83 | + logging.Infof("Running network verification for cluster %s", r.Cluster.ID()) |
| 84 | + verifierResult, failureReason, err := networkverifier.Run(r.Cluster, r.ClusterDeployment, r.AwsClient) |
| 85 | + if err != nil { |
| 86 | + i.notes.AppendWarning("Network verifier encountered an error: %v", err) |
| 87 | + return fmt.Errorf("network verifier failed: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + logging.Infof("Network verification completed with result: %v", verifierResult) |
| 91 | + switch verifierResult { |
| 92 | + case networkverifier.Success: |
| 93 | + i.notes.AppendSuccess("Network verifier passed") |
| 94 | + case networkverifier.Failure: |
| 95 | + logging.Infof("Network verifier reported failure: %s", failureReason) |
| 96 | + result.ServiceLogPrepared = investigation.InvestigationStep{Performed: true, Labels: nil} |
| 97 | + i.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", |
| 98 | + r.Cluster.ID(), failureReason) |
| 99 | + return errors.New("network verification failed: " + failureReason) |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func (i *Investigation) checkClusterVersion(r *investigation.Resources) error { |
| 105 | + logging.Infof("Checking ClusterVersion for cluster %s", r.Cluster.ID()) |
| 106 | + cvList := &configv1.ClusterVersionList{} |
| 107 | + listOptions := &client.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{"metadata.name": "version"})} |
| 108 | + err := i.kclient.List(context.TODO(), cvList, listOptions) |
| 109 | + if err != nil { |
| 110 | + i.notes.AppendWarning("Failed to list ClusterVersion: %v\nThis may indicate cluster access issues", err) |
| 111 | + return fmt.Errorf("failed to list ClusterVersion: %w", err) |
| 112 | + } |
| 113 | + if len(cvList.Items) != 1 { |
| 114 | + errMsg := fmt.Sprintf("found %d ClusterVersions, expected 1", len(cvList.Items)) |
| 115 | + logging.Warnf(errMsg) |
| 116 | + i.notes.AppendWarning(errMsg) |
| 117 | + return errors.New(errMsg) |
| 118 | + } |
| 119 | + |
| 120 | + versionCv := cvList.Items[0] |
| 121 | + logging.Infof("ClusterVersion found: %s", versionCv.Status.Desired.Version) |
| 122 | + for _, condition := range versionCv.Status.Conditions { |
| 123 | + logging.Debugf("Checking ClusterVersion condition: Type=%s, Status=%s, Reason=%s, Message=%s", |
| 124 | + condition.Type, condition.Status, condition.Reason, condition.Message) |
| 125 | + if condition.Type == "RetrievedUpdates" && |
| 126 | + condition.Status == "False" && |
| 127 | + condition.Reason == "VersionNotFound" && |
| 128 | + strings.Contains(condition.Message, "Unable to retrieve available updates") { |
| 129 | + i.notes.AppendWarning("ClusterVersion error detected: %s\nThis indicates the current version %s is not found in the specified channel %s", |
| 130 | + condition.Message, versionCv.Status.Desired.Version, versionCv.Spec.Channel) |
| 131 | + return errors.New("clusterversion validation failed: VersionNotFound") |
| 132 | + } |
| 133 | + } |
| 134 | + fmt.Printf("Cluster version: %s\n", versionCv.Status.Desired.Version) |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// setup initializes the investigation resources |
| 139 | +func (i *Investigation) setup(r *investigation.Resources) error { |
| 140 | + logging.Infof("Setting up investigation '%s' for cluster %s with remediation name %s", |
| 141 | + i.Name(), r.Cluster.ID(), r.Name) |
| 142 | + |
| 143 | + k, err := k8sclient.New(r.Cluster.ID(), r.OcmClient, remediationName) |
| 144 | + if err != nil { |
| 145 | + logging.Errorf("Failed to initialize Kubernetes client: %v", err) |
| 146 | + return fmt.Errorf("failed to initialize kubernetes client: %w", err) |
| 147 | + } |
| 148 | + i.kclient = k |
| 149 | + i.notes = notewriter.New(r.Name, logging.RawLogger) |
| 150 | + |
| 151 | + logging.Infof("Successfully set up Kubernetes client and notewriter for remediation %s", r.Name) |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func (i *Investigation) Name() string { |
| 156 | + return alertname |
| 157 | +} |
| 158 | + |
| 159 | +func (i *Investigation) Description() string { |
| 160 | + return fmt.Sprintf("Investigates '%s' alerts by running network verifier and checking ClusterVersion", alertname) |
| 161 | +} |
| 162 | + |
| 163 | +func (i *Investigation) ShouldInvestigateAlert(alert string) bool { |
| 164 | + return strings.Contains(alert, alertname) |
| 165 | +} |
| 166 | + |
| 167 | +func (i *Investigation) IsExperimental() bool { |
| 168 | + return true |
| 169 | +} |
0 commit comments