|
| 1 | +package insightsoperatordown |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + configv1 "github.com/openshift/api/config/v1" |
| 10 | + investigation "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 | + "github.com/openshift/configuration-anomaly-detection/pkg/ocm" |
| 16 | + "k8s.io/apimachinery/pkg/fields" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | +) |
| 19 | + |
| 20 | +type Investigation struct{} |
| 21 | + |
| 22 | +func (c *Investigation) Run(r *investigation.Resources) (investigation.InvestigationResult, error) { |
| 23 | + result := investigation.InvestigationResult{} |
| 24 | + notes := notewriter.New(r.Name, logging.RawLogger) |
| 25 | + |
| 26 | + user, err := ocm.GetCreatorFromCluster(r.OcmClient.GetConnection(), r.Cluster) |
| 27 | + if err != nil { |
| 28 | + notes.AppendWarning("encountered an issue when checking if the cluster owner is banned: %s", err) |
| 29 | + return result, r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 30 | + } |
| 31 | + |
| 32 | + if user.Banned() { |
| 33 | + notes.AppendWarning("User is banned: %s\nBan description: %s\nPlease open a proactive case, so that MCS can resolve the ban or organize a ownership transfer.", user.BanCode(), user.BanDescription()) |
| 34 | + } else { |
| 35 | + notes.AppendSuccess("User is not banned.") |
| 36 | + } |
| 37 | + |
| 38 | + // We continue with the next step OCPBUG22226 even if the user is banned. |
| 39 | + k8scli, err := k8sclient.New(r.Cluster.ID(), r.OcmClient, r.Name) |
| 40 | + if err != nil { |
| 41 | + return result, fmt.Errorf("unable to initialize k8s cli: %w", err) |
| 42 | + } |
| 43 | + defer func() { |
| 44 | + deferErr := k8sclient.Cleanup(r.Cluster.ID(), r.OcmClient, r.Name) |
| 45 | + if deferErr != nil { |
| 46 | + logging.Error(deferErr) |
| 47 | + err = errors.Join(err, deferErr) |
| 48 | + } |
| 49 | + }() |
| 50 | + |
| 51 | + coList := &configv1.ClusterOperatorList{} |
| 52 | + listOptions := &client.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{"metadata.name": "insights"})} |
| 53 | + err = k8scli.List(context.TODO(), coList, listOptions) |
| 54 | + if err != nil { |
| 55 | + return result, fmt.Errorf("unable to list insights clusteroperator: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + if len(coList.Items) != 1 { |
| 59 | + return result, fmt.Errorf("found %d clusteroperators, expected 1", len(coList.Items)) |
| 60 | + } |
| 61 | + co := coList.Items[0] |
| 62 | + |
| 63 | + if isOCPBUG22226(&co) { |
| 64 | + notes.AppendWarning("Found symptom of OCPBUGS-22226. Try deleting the insights operator pod to remediate.\n$ oc -n openshift-insights delete pods -l app=insights-operator --wait=false") |
| 65 | + return result, r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 66 | + } else { |
| 67 | + notes.AppendSuccess("Ruled out OCPBUGS-22226") |
| 68 | + } |
| 69 | + |
| 70 | + verifierResult, failureReason, err := networkverifier.Run(r.Cluster, r.ClusterDeployment, r.AwsClient) |
| 71 | + if err != nil { |
| 72 | + logging.Error("Network verifier ran into an error: %s", err.Error()) |
| 73 | + notes.AppendWarning("NetworkVerifier failed to run:\n\t %s", err.Error()) |
| 74 | + |
| 75 | + err = r.PdClient.AddNote(notes.String()) |
| 76 | + if err != nil { |
| 77 | + // We do not return as we want the alert to be escalated either no matter what. |
| 78 | + logging.Error("could not add failure reason incident notes") |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + switch verifierResult { |
| 83 | + case networkverifier.Failure: |
| 84 | + logging.Infof("Network verifier reported failure: %s", failureReason) |
| 85 | + // XXX: metrics.Inc(metrics.ServicelogPrepared, investigationName) |
| 86 | + result.ServiceLogPrepared = investigation.InvestigationStep{Performed: true, Labels: nil} |
| 87 | + 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) |
| 88 | + |
| 89 | + // In the future, we want to send a service log in this case |
| 90 | + err = r.PdClient.AddNote(notes.String()) |
| 91 | + if err != nil { |
| 92 | + logging.Error("could not add issues to incident notes") |
| 93 | + } |
| 94 | + case networkverifier.Success: |
| 95 | + notes.AppendSuccess("Network verifier passed") |
| 96 | + err = r.PdClient.AddNote(notes.String()) |
| 97 | + if err != nil { |
| 98 | + logging.Error("could not add passed message to incident notes") |
| 99 | + } |
| 100 | + } |
| 101 | + return result, r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 102 | +} |
| 103 | + |
| 104 | +func isOCPBUG22226(co *configv1.ClusterOperator) bool { |
| 105 | + symptomStatusString := "Failed to pull SCA certs" |
| 106 | + |
| 107 | + for _, condition := range co.Status.Conditions { |
| 108 | + if condition.Type == "SCAAvailable" && strings.Contains(condition.Message, symptomStatusString) { |
| 109 | + return true |
| 110 | + } |
| 111 | + } |
| 112 | + return false |
| 113 | +} |
| 114 | + |
| 115 | +func (c *Investigation) Name() string { |
| 116 | + return "insightsoperatordown" |
| 117 | +} |
| 118 | + |
| 119 | +func (c *Investigation) Description() string { |
| 120 | + return "Investigate insights operator down alert" |
| 121 | +} |
| 122 | + |
| 123 | +func (c *Investigation) ShouldInvestigateAlert(alert string) bool { |
| 124 | + return strings.Contains(alert, "InsightsOperatorDown") |
| 125 | +} |
| 126 | + |
| 127 | +func (c *Investigation) IsExperimental() bool { |
| 128 | + return false |
| 129 | +} |
| 130 | + |
| 131 | +func (c *Investigation) RequiresAwsClient() bool { |
| 132 | + return false |
| 133 | +} |
0 commit comments