|
| 1 | +// Package upgradeconfigsyncfailureover4hr contains functionality for the UpgradeConfigSyncFailureOver4HrSRE investigation |
| 2 | +package upgradeconfigsyncfailureover4hr |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + v1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1" |
| 12 | + "github.com/openshift/configuration-anomaly-detection/pkg/investigations/investigation" |
| 13 | + k8sclient "github.com/openshift/configuration-anomaly-detection/pkg/k8s" |
| 14 | + "github.com/openshift/configuration-anomaly-detection/pkg/logging" |
| 15 | + "github.com/openshift/configuration-anomaly-detection/pkg/notewriter" |
| 16 | + ocm "github.com/openshift/configuration-anomaly-detection/pkg/ocm" |
| 17 | + corev1 "k8s.io/api/core/v1" |
| 18 | + "k8s.io/apimachinery/pkg/types" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | +) |
| 21 | + |
| 22 | +type Investigation struct{} |
| 23 | + |
| 24 | +const ( |
| 25 | + alertname = "UpgradeConfigSyncFailureOver4HrSRE" |
| 26 | + remediationName = "upgradeconfigsyncfailureover4hr" |
| 27 | +) |
| 28 | + |
| 29 | +func (c *Investigation) Run(r *investigation.Resources) (investigation.InvestigationResult, error) { |
| 30 | + result := investigation.InvestigationResult{} |
| 31 | + notes := notewriter.New("UpgradeConfigSyncFailureOver4Hr", logging.RawLogger) |
| 32 | + k8scli, err := k8sclient.New(r.Cluster.ID(), r.OcmClient, remediationName) |
| 33 | + if err != nil { |
| 34 | + return result, fmt.Errorf("unable to initialize k8s cli: %w", err) |
| 35 | + } |
| 36 | + defer func() { |
| 37 | + deferErr := k8sclient.Cleanup(r.Cluster.ID(), r.OcmClient, remediationName) |
| 38 | + if deferErr != nil { |
| 39 | + logging.Error(deferErr) |
| 40 | + err = errors.Join(err, deferErr) |
| 41 | + } |
| 42 | + }() |
| 43 | + logging.Infof("Checking if user is Banned.") |
| 44 | + userBannedStatus, userBannedNotes, err := ocm.CheckIfUserBanned(r.OcmClient, r.Cluster) |
| 45 | + if err != nil { |
| 46 | + notes.AppendWarning("encountered an issue when checking if the cluster owner is banned: %s\nPlease investigate.", err) |
| 47 | + return result, r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 48 | + } |
| 49 | + if userBannedStatus { |
| 50 | + notes.AppendWarning(userBannedNotes) |
| 51 | + } else { |
| 52 | + notes.AppendSuccess("User is not banned.") |
| 53 | + } |
| 54 | + user, err := ocm.GetCreatorFromCluster(r.OcmClient.GetConnection(), r.Cluster) |
| 55 | + logging.Infof("User ID is: %v", user.ID()) |
| 56 | + clusterSecretToken, note, err := getClusterPullSecret(k8scli) |
| 57 | + if err != nil { |
| 58 | + notes.AppendWarning("Failre getting ClusterSecret: %s", err) |
| 59 | + return result, r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 60 | + } |
| 61 | + if note != "" { |
| 62 | + notes.AppendWarning(note) |
| 63 | + } |
| 64 | + registryCredential, err := ocm.GetOCMPullSecret(r.OcmClient.GetConnection(), user.ID()) |
| 65 | + if err != nil { |
| 66 | + notes.AppendWarning("Error getting OCMPullSecret: %s", err) |
| 67 | + return result, r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 68 | + } |
| 69 | + if clusterSecretToken == registryCredential { |
| 70 | + notes.AppendSuccess("Pull Secret matches on cluster and in OCM. Please continue investigation.") |
| 71 | + } else { |
| 72 | + notes.AppendWarning("Pull secret does not match on cluster and in OCM.") |
| 73 | + } |
| 74 | + return result, r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 75 | +} |
| 76 | + |
| 77 | +func getClusterPullSecret(k8scli client.Client) (secretToken string, note string, err error) { |
| 78 | + secret := &corev1.Secret{} |
| 79 | + err = k8scli.Get(context.TODO(), types.NamespacedName{ |
| 80 | + Namespace: "openshift-config", |
| 81 | + Name: "pull-secret", |
| 82 | + }, secret) |
| 83 | + if err != nil { |
| 84 | + return "", "", err |
| 85 | + } |
| 86 | + if secret.Data == nil { |
| 87 | + return "", "Cluster pull secret Data is empty.", err |
| 88 | + } |
| 89 | + secretValue, exists := secret.Data[".dockerconfigjson"] |
| 90 | + if !exists { |
| 91 | + return "", "Cluster pull secret does not contain the necessary .dockerconfigjson", err |
| 92 | + } |
| 93 | + |
| 94 | + dockerConfigJson, err := v1.UnmarshalAccessToken(secretValue) |
| 95 | + if err != nil { |
| 96 | + return "", "", err |
| 97 | + } |
| 98 | + _, exists = dockerConfigJson.Auths()["cloud.openshift.com"] |
| 99 | + if !exists { |
| 100 | + return "", "cloud.openshift.com value not found in clusterPullSecret. This means there is an issue with the pull secret on the cluster.", err |
| 101 | + } |
| 102 | + |
| 103 | + value, err := base64.StdEncoding.DecodeString(dockerConfigJson.Auths()["registry.connect.redhat.com"].Auth()) |
| 104 | + if err != nil { |
| 105 | + return "", "", err |
| 106 | + } |
| 107 | + _, splitValue, _ := strings.Cut(string(value), ":") |
| 108 | + return splitValue, "", nil |
| 109 | +} |
| 110 | + |
| 111 | +func (c *Investigation) Name() string { |
| 112 | + return "UpgradeConfigSyncFailureOver4hr" |
| 113 | +} |
| 114 | + |
| 115 | +func (c *Investigation) Description() string { |
| 116 | + return "Investigates the UpgradeConfigSyncFailureOver4hr alert" |
| 117 | +} |
| 118 | + |
| 119 | +func (c *Investigation) ShouldInvestigateAlert(alert string) bool { |
| 120 | + return strings.Contains(alert, "UpgradeConfigSyncFailureOver4HrSRE") |
| 121 | +} |
| 122 | + |
| 123 | +func (c *Investigation) IsExperimental() bool { |
| 124 | + return false |
| 125 | +} |
0 commit comments