|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" |
| 10 | + sourcev1 "github.com/fluxcd/source-controller/api/v1beta2" |
| 11 | + "github.com/mitchellh/go-wordwrap" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | +) |
| 15 | + |
| 16 | +type HelmReleaseCheck struct { |
| 17 | + Name string |
| 18 | + Namespace string |
| 19 | +} |
| 20 | + |
| 21 | +func (d *HelmReleaseCheck) GetName() string { |
| 22 | + return d.Name |
| 23 | +} |
| 24 | + |
| 25 | +func (d *HelmReleaseCheck) Run() *Result { |
| 26 | + result := &Result{} |
| 27 | + |
| 28 | + hr := &helmv2.HelmRelease{} |
| 29 | + |
| 30 | + err := KubeClient.Get(context.Background(), client.ObjectKey{ |
| 31 | + Namespace: d.Namespace, |
| 32 | + Name: d.Name, |
| 33 | + }, hr) |
| 34 | + |
| 35 | + if err != nil { |
| 36 | + result.IsError = true |
| 37 | + return result |
| 38 | + } |
| 39 | + |
| 40 | + // define a slice of handler including a regexp and a function |
| 41 | + // if we find a match we process the event by an appropriate function |
| 42 | + // this is assumed to stay branchless in the future which enables easy extensibility |
| 43 | + // the order of processing is important as we prioritize the status messages |
| 44 | + type handler struct { |
| 45 | + regex *regexp.Regexp |
| 46 | + fn func(res *Result, matches []string) |
| 47 | + } |
| 48 | + |
| 49 | + handlers := []handler{ |
| 50 | + { |
| 51 | + regex: regexp.MustCompile("Helm test failed: pod (?P<podName>.*) failed"), |
| 52 | + fn: handeHelmTestError, |
| 53 | + }, |
| 54 | + { |
| 55 | + regex: regexp.MustCompile("(install retries exhausted|upgrade retries exhausted|Helm install failed|Helm upgrade failed).*"), |
| 56 | + fn: func(res *Result, matches []string) { |
| 57 | + res.Status = prettify(matches[0]) |
| 58 | + res.IsError = true |
| 59 | + res.IsOkay = false |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + regex: regexp.MustCompile("^HelmChart '(?P<namespace>.*)/(?P<name>.*)' is not ready$"), |
| 64 | + fn: handleHelmChartError, |
| 65 | + }, |
| 66 | + { |
| 67 | + regex: regexp.MustCompile("Release reconciliation succeeded"), |
| 68 | + fn: func(res *Result, matches []string) { |
| 69 | + res.Status = prettify(matches[0]) |
| 70 | + res.IsError = false |
| 71 | + res.IsOkay = true |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + // iterate over status conditions in the helm releases |
| 77 | + // here all useful information about potential errors should be found |
| 78 | + for _, curHandler := range handlers { |
| 79 | + for _, condition := range hr.GetConditions() { |
| 80 | + matches := curHandler.regex.FindStringSubmatch(condition.Message) |
| 81 | + if matches != nil { |
| 82 | + curHandler.fn(result, matches) |
| 83 | + return result |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return result |
| 89 | +} |
| 90 | + |
| 91 | +// handeHelmTestError ... |
| 92 | +func handeHelmTestError(res *Result, matches []string) { |
| 93 | + |
| 94 | + // It seems controller-runtime does not allow to access the logs. |
| 95 | + // Use kubectl directly for the moment. |
| 96 | + test := KubeClientGo.CoreV1().Pods("garden").GetLogs(matches[1], &corev1.PodLogOptions{}) |
| 97 | + logs, err := test.Do(context.Background()).Raw() |
| 98 | + log := string(logs) |
| 99 | + if err != nil { |
| 100 | + log = fmt.Sprintf("couldn't get pod logs: %s", err) |
| 101 | + } |
| 102 | + |
| 103 | + // Do some easy formatting for the moment. |
| 104 | + // We should definitely look for some package doing the job in the end. |
| 105 | + const replacement = "\n > " |
| 106 | + var replacer = strings.NewReplacer( |
| 107 | + "\r\n", replacement, |
| 108 | + "\r", replacement, |
| 109 | + "\n", replacement, |
| 110 | + "\v", replacement, |
| 111 | + "\f", replacement, |
| 112 | + "\u0085", replacement, |
| 113 | + "\u2028", replacement, |
| 114 | + "\u2029", replacement, |
| 115 | + ) |
| 116 | + |
| 117 | + newline := "\n > " |
| 118 | + res.Status = matches[0] + newline + replacer.Replace(wordwrap.WrapString(strings.TrimSpace(log), 100)) |
| 119 | + |
| 120 | + res.IsError = true |
| 121 | + res.IsOkay = false |
| 122 | +} |
| 123 | + |
| 124 | +func handleHelmChartError(res *Result, matches []string) { |
| 125 | + namespace := matches[1] |
| 126 | + name := matches[2] |
| 127 | + |
| 128 | + hc := &sourcev1.HelmChart{} |
| 129 | + |
| 130 | + err := KubeClient.Get(context.Background(), client.ObjectKey{ |
| 131 | + Namespace: namespace, |
| 132 | + Name: name, |
| 133 | + }, hc) |
| 134 | + |
| 135 | + status := matches[0] |
| 136 | + |
| 137 | + if err != nil { |
| 138 | + status = status + ": " + err.Error() |
| 139 | + } else { |
| 140 | + hcReadyMessage := getMessage(hc.Status.Conditions, "Ready") |
| 141 | + status = status + ": " + hcReadyMessage |
| 142 | + } |
| 143 | + |
| 144 | + res.Status = prettify(status) |
| 145 | + res.IsError = true |
| 146 | + res.IsOkay = false |
| 147 | +} |
| 148 | + |
| 149 | +func prettify(message string) string { |
| 150 | + newline := "\n > " |
| 151 | + return strings.Replace(message, ": ", newline, -1) |
| 152 | +} |
0 commit comments