|
5 | 5 | "context" |
6 | 6 | "crypto/tls" |
7 | 7 | "encoding/json" |
| 8 | + "errors" |
8 | 9 | "fmt" |
9 | 10 | "net" |
10 | 11 | "net/http" |
@@ -41,6 +42,10 @@ func bash(ctx context.Context, script string) (string, error) { |
41 | 42 |
|
42 | 43 | if err != nil { |
43 | 44 | logger.V(1).Info("Failed to run", "command", script, "stderr", stderr, "error", err) |
| 45 | + var exitErr *exec.ExitError |
| 46 | + if errors.As(err, &exitErr) { |
| 47 | + exitErr.Stderr = stderrBuf.Bytes() |
| 48 | + } |
44 | 49 | } |
45 | 50 | logger.V(1).Info("Output", "command", script, "output", stdout) |
46 | 51 |
|
@@ -81,79 +86,151 @@ func CatalogReportsConditionWithoutReason(ctx context.Context, catalogUserName, |
81 | 86 | func ensureCatalogPortForward(ctx context.Context) (string, error) { |
82 | 87 | sc := scenarioCtx(ctx) |
83 | 88 | if sc.catalogAddr != "" { |
84 | | - return sc.catalogAddr, nil |
| 89 | + if catalogPortForwardAlive(sc.catalogAddr) { |
| 90 | + return sc.catalogAddr, nil |
| 91 | + } |
| 92 | + logger.V(1).Info("Catalog port-forward is dead, re-establishing", "addr", sc.catalogAddr) |
| 93 | + resetCatalogPortForward(ctx) |
| 94 | + } |
| 95 | + |
| 96 | + ns := componentNamespaces["catalogd"] |
| 97 | + target, err := catalogdLeaderPod(ctx, ns) |
| 98 | + port := int32(443) |
| 99 | + if err != nil { |
| 100 | + logger.V(1).Info("Could not resolve catalogd leader pod, falling back to service", "error", err) |
| 101 | + target = "service/catalogd-service" |
| 102 | + } else { |
| 103 | + port = 8443 |
85 | 104 | } |
86 | 105 |
|
87 | | - addr, cleanup, err := portForward(ctx, componentNamespaces["catalogd"], "service/catalogd-service", 443) |
| 106 | + addr, cleanup, err := portForward(ctx, ns, target, port) |
88 | 107 | if err != nil { |
89 | | - return "", fmt.Errorf("failed to start catalog port-forward: %w", err) |
| 108 | + return "", fmt.Errorf("failed to start catalog port-forward to %s: %w", target, err) |
90 | 109 | } |
91 | 110 | sc.catalogAddr = addr |
92 | 111 | sc.catalogCleanup = cleanup |
93 | 112 |
|
94 | 113 | waitFor(ctx, func() bool { |
95 | | - client := &http.Client{ |
96 | | - Timeout: 3 * time.Second, |
97 | | - Transport: &http.Transport{ |
98 | | - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec |
99 | | - DialContext: (&net.Dialer{Timeout: 2 * time.Second}).DialContext, |
100 | | - }, |
101 | | - } |
102 | | - resp, err := client.Get(fmt.Sprintf("https://%s/", addr)) |
103 | | - if err != nil { |
104 | | - return false |
105 | | - } |
106 | | - resp.Body.Close() |
107 | | - return true |
| 114 | + return catalogPortForwardAlive(addr) |
108 | 115 | }) |
109 | 116 | return addr, nil |
110 | 117 | } |
111 | 118 |
|
| 119 | +func catalogdLeaderPod(ctx context.Context, ns string) (string, error) { |
| 120 | + holder, err := k8sClient(ctx, "get", "lease", "catalogd-operator-lock", "-n", ns, |
| 121 | + "-o", "jsonpath={.spec.holderIdentity}") |
| 122 | + if err != nil { |
| 123 | + return "", fmt.Errorf("failed to get catalogd leader lease: %w", err) |
| 124 | + } |
| 125 | + holder = strings.TrimSpace(holder) |
| 126 | + podName := holder |
| 127 | + if idx := strings.LastIndex(holder, "_"); idx >= 0 { |
| 128 | + podName = holder[:idx] |
| 129 | + } |
| 130 | + if podName == "" { |
| 131 | + return "", fmt.Errorf("catalogd leader lease has empty holderIdentity") |
| 132 | + } |
| 133 | + logger.Info("Resolved catalogd leader pod", "holder", holder, "pod", podName) |
| 134 | + return fmt.Sprintf("pod/%s", podName), nil |
| 135 | +} |
| 136 | + |
| 137 | +func catalogPortForwardAlive(addr string) bool { |
| 138 | + client := &http.Client{ |
| 139 | + Timeout: 3 * time.Second, |
| 140 | + Transport: &http.Transport{ |
| 141 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec |
| 142 | + DialContext: (&net.Dialer{Timeout: 2 * time.Second}).DialContext, |
| 143 | + }, |
| 144 | + } |
| 145 | + resp, err := client.Get(fmt.Sprintf("https://%s/", addr)) |
| 146 | + if err != nil { |
| 147 | + return false |
| 148 | + } |
| 149 | + resp.Body.Close() |
| 150 | + return true |
| 151 | +} |
| 152 | + |
| 153 | +// resetCatalogPortForward tears down the cached port-forward so the next |
| 154 | +// call to ensureCatalogPortForward establishes a fresh connection. With |
| 155 | +// CatalogdHA, non-leader pods return 404 (empty local cache); resetting |
| 156 | +// lets the next retry potentially reach the leader pod. |
| 157 | +func resetCatalogPortForward(ctx context.Context) { |
| 158 | + sc := scenarioCtx(ctx) |
| 159 | + if sc.catalogCleanup != nil { |
| 160 | + sc.catalogCleanup() |
| 161 | + } |
| 162 | + sc.catalogAddr = "" |
| 163 | + sc.catalogCleanup = nil |
| 164 | +} |
| 165 | + |
112 | 166 | func catalogCurlJq(ctx context.Context, catalogName, jqFilter string) (string, error) { |
113 | 167 | addr, err := ensureCatalogPortForward(ctx) |
114 | 168 | if err != nil { |
115 | 169 | return "", err |
116 | 170 | } |
| 171 | + // pipefail: propagate curl exit code through the pipe (e.g. HTTP 404 from a non-leader catalogd pod). |
| 172 | + // -sS: silent but show errors on stderr. -k: skip TLS verification for the port-forward. |
| 173 | + // --compressed: request gzip and stream-decompress (catalogd uses gzhttp); saves network for the large operatorhubio catalog. |
| 174 | + // --fail: exit 22 on HTTP errors so non-JSON error bodies don't reach jq. |
117 | 175 | script := fmt.Sprintf( |
118 | | - `curl -s -k https://%s/catalogs/%s/api/v1/all | jq -s '%s'`, |
| 176 | + `set -o pipefail; curl -sS -k --compressed --fail https://%s/catalogs/%s/api/v1/all | jq '%s'`, |
119 | 177 | addr, catalogName, jqFilter, |
120 | 178 | ) |
121 | | - return bash(ctx, script) |
| 179 | + out, err := bash(ctx, script) |
| 180 | + if err != nil { |
| 181 | + resetCatalogPortForward(ctx) |
| 182 | + } |
| 183 | + return out, err |
122 | 184 | } |
123 | 185 |
|
124 | 186 | func CatalogContainsSomePackages(ctx context.Context, catalogName string) error { |
125 | | - out, err := catalogCurlJq(ctx, catalogName, |
126 | | - `.[] | select(.schema == "olm.package") | .name`) |
127 | | - if err != nil { |
128 | | - return err |
129 | | - } |
130 | | - if strings.TrimSpace(out) == "" { |
131 | | - return fmt.Errorf("catalog %q contains no packages", catalogName) |
132 | | - } |
| 187 | + waitFor(ctx, func() bool { |
| 188 | + out, err := catalogCurlJq(ctx, catalogName, |
| 189 | + `objects | select(.schema == "olm.package") | .name`) |
| 190 | + if err != nil { |
| 191 | + logger.Info("Catalog query failed, retrying", "catalog", catalogName, "error", err, "stderr", stderrOutput(err)) |
| 192 | + return false |
| 193 | + } |
| 194 | + if strings.TrimSpace(out) == "" { |
| 195 | + logger.Info("Catalog returned no packages, retrying", "catalog", catalogName) |
| 196 | + return false |
| 197 | + } |
| 198 | + return true |
| 199 | + }) |
133 | 200 | return nil |
134 | 201 | } |
135 | 202 |
|
136 | 203 | func PackageHasSomeChannels(ctx context.Context, packageName, catalogName string) error { |
137 | | - out, err := catalogCurlJq(ctx, catalogName, |
138 | | - fmt.Sprintf(`.[] | select(.schema == "olm.channel") | select(.package == "%s") | .name`, packageName)) |
139 | | - if err != nil { |
140 | | - return err |
141 | | - } |
142 | | - if strings.TrimSpace(out) == "" { |
143 | | - return fmt.Errorf("package %q in catalog %q has no channels", packageName, catalogName) |
144 | | - } |
| 204 | + waitFor(ctx, func() bool { |
| 205 | + out, err := catalogCurlJq(ctx, catalogName, |
| 206 | + fmt.Sprintf(`objects | select(.schema == "olm.channel") | select(.package == "%s") | .name`, packageName)) |
| 207 | + if err != nil { |
| 208 | + logger.Info("Catalog query failed, retrying", "catalog", catalogName, "package", packageName, "error", err, "stderr", stderrOutput(err)) |
| 209 | + return false |
| 210 | + } |
| 211 | + if strings.TrimSpace(out) == "" { |
| 212 | + logger.Info("Package has no channels, retrying", "catalog", catalogName, "package", packageName) |
| 213 | + return false |
| 214 | + } |
| 215 | + return true |
| 216 | + }) |
145 | 217 | return nil |
146 | 218 | } |
147 | 219 |
|
148 | 220 | func PackageHasSomeBundles(ctx context.Context, packageName, catalogName string) error { |
149 | | - out, err := catalogCurlJq(ctx, catalogName, |
150 | | - fmt.Sprintf(`.[] | select(.schema == "olm.bundle") | select(.package == "%s") | .name`, packageName)) |
151 | | - if err != nil { |
152 | | - return err |
153 | | - } |
154 | | - if strings.TrimSpace(out) == "" { |
155 | | - return fmt.Errorf("package %q in catalog %q has no bundles", packageName, catalogName) |
156 | | - } |
| 221 | + waitFor(ctx, func() bool { |
| 222 | + out, err := catalogCurlJq(ctx, catalogName, |
| 223 | + fmt.Sprintf(`objects | select(.schema == "olm.bundle") | select(.package == "%s") | .name`, packageName)) |
| 224 | + if err != nil { |
| 225 | + logger.Info("Catalog query failed, retrying", "catalog", catalogName, "package", packageName, "error", err, "stderr", stderrOutput(err)) |
| 226 | + return false |
| 227 | + } |
| 228 | + if strings.TrimSpace(out) == "" { |
| 229 | + logger.Info("Package has no bundles, retrying", "catalog", catalogName, "package", packageName) |
| 230 | + return false |
| 231 | + } |
| 232 | + return true |
| 233 | + }) |
157 | 234 | return nil |
158 | 235 | } |
159 | 236 |
|
|
0 commit comments