|
17 | 17 | package cert |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "crypto/x509" |
21 | | - "encoding/pem" |
22 | 20 | "fmt" |
23 | 21 | "os" |
24 | 22 | "strings" |
25 | | - "time" |
26 | 23 |
|
27 | 24 | "github.com/ai-dynamo/grove/operator/internal/constants" |
28 | 25 | authorizationwebhook "github.com/ai-dynamo/grove/operator/internal/webhook/admission/pcs/authorization" |
@@ -54,19 +51,15 @@ func ManageWebhookCerts(mgr ctrl.Manager, certDir string, secretName string, aut |
54 | 51 |
|
55 | 52 | logger := ctrl.Log.WithName("cert-management") |
56 | 53 |
|
57 | | - // If cert-manager is enabled, wait for it to provide certificates |
58 | 54 | if certManagerEnabled { |
59 | 55 | logger.Info("Using cert-manager for certificate management", |
60 | 56 | "secretName", secretName, "certDir", certDir) |
61 | | - go waitForExternalCerts(logger, certDir, certsReadyCh) |
62 | 57 | return nil |
63 | 58 | } |
64 | 59 |
|
65 | | - // If auto-provision is disabled, wait for externally provided certificates |
66 | 60 | if !autoProvision { |
67 | 61 | logger.Info("Using externally provided certificates", |
68 | 62 | "certDir", certDir, "secretName", secretName) |
69 | | - go waitForExternalCerts(logger, certDir, certsReadyCh) |
70 | 63 | return nil |
71 | 64 | } |
72 | 65 |
|
@@ -142,82 +135,3 @@ func getOperatorNamespace() (string, error) { |
142 | 135 | } |
143 | 136 | return namespace, nil |
144 | 137 | } |
145 | | - |
146 | | -// certsExist checks if both certificate and key files exist and have content in the specified directory |
147 | | -func certsExist(certDir string) bool { |
148 | | - certPath := fmt.Sprintf("%s/tls.crt", certDir) |
149 | | - keyPath := fmt.Sprintf("%s/tls.key", certDir) |
150 | | - return fileExistsWithContent(certPath) && fileExistsWithContent(keyPath) |
151 | | -} |
152 | | - |
153 | | -// waitForExternalCerts waits for externally managed certificates to be available |
154 | | -// in the specified directory. This is used when cert-manager is enabled. |
155 | | -func waitForExternalCerts(logger logr.Logger, certDir string, certsReadyCh chan struct{}) { |
156 | | - const ( |
157 | | - maxRetries = 30 |
158 | | - retryInterval = 2 * time.Second |
159 | | - certFileName = "tls.crt" |
160 | | - keyFileName = "tls.key" |
161 | | - ) |
162 | | - |
163 | | - certPath := fmt.Sprintf("%s/%s", certDir, certFileName) |
164 | | - keyPath := fmt.Sprintf("%s/%s", certDir, keyFileName) |
165 | | - |
166 | | - for i := 0; i < maxRetries; i++ { |
167 | | - // Check if both certificate and key files exist |
168 | | - certExists := fileExists(certPath) |
169 | | - keyExists := fileExists(keyPath) |
170 | | - |
171 | | - if certExists && keyExists { |
172 | | - logger.Info("External certificates found and ready", |
173 | | - "certPath", certPath, "keyPath", keyPath) |
174 | | - close(certsReadyCh) |
175 | | - return |
176 | | - } |
177 | | - |
178 | | - if i < maxRetries-1 { |
179 | | - logger.Info("Waiting for external certificates to be mounted", |
180 | | - "attempt", i+1, "maxRetries", maxRetries, |
181 | | - "certExists", certExists, "keyExists", keyExists) |
182 | | - time.Sleep(retryInterval) |
183 | | - } |
184 | | - } |
185 | | - |
186 | | - logger.Error(fmt.Errorf("timeout waiting for external certificates"), |
187 | | - "Failed to find certificates after maximum retries", |
188 | | - "certPath", certPath, "keyPath", keyPath) |
189 | | - // Don't close the channel - this will cause the readiness check to fail |
190 | | -} |
191 | | - |
192 | | -// fileExists checks if a file exists and is not a directory |
193 | | -func fileExists(path string) bool { |
194 | | - info, err := os.Stat(path) |
195 | | - if err != nil { |
196 | | - return false |
197 | | - } |
198 | | - return !info.IsDir() |
199 | | -} |
200 | | - |
201 | | -// fileExistsWithContent checks if a file exists and contains valid PEM data |
202 | | -func fileExistsWithContent(path string) bool { |
203 | | - data, err := os.ReadFile(path) |
204 | | - if err != nil || len(data) == 0 { |
205 | | - return false |
206 | | - } |
207 | | - |
208 | | - // Try to decode PEM block to verify it's valid |
209 | | - block, _ := pem.Decode(data) |
210 | | - if block == nil || len(block.Bytes) == 0 { |
211 | | - return false |
212 | | - } |
213 | | - |
214 | | - // For certificate files, try to parse as X.509 |
215 | | - // For key files, just having valid PEM is enough |
216 | | - if strings.Contains(path, "tls.crt") || strings.Contains(path, "ca.crt") { |
217 | | - _, err := x509.ParseCertificate(block.Bytes) |
218 | | - return err == nil |
219 | | - } |
220 | | - |
221 | | - // For key files (tls.key), valid PEM block is sufficient |
222 | | - return true |
223 | | -} |
0 commit comments