|
94 | 94 |
|
95 | 95 | // ErrFieldNotFound indicates a field name does not exist on the struct. |
96 | 96 | ErrFieldNotFound = errors.New("field not found") |
| 97 | + |
| 98 | + // ErrMissingRequiredKey indicates that a required key is missing in a resource. |
| 99 | + ErrMissingRequiredKey = errors.New("missing required key") |
97 | 100 | ) |
98 | 101 |
|
99 | 102 | // Reconciler provides common functionality for all test framework reconcilers |
@@ -350,6 +353,82 @@ func (r *Reconciler) CheckSecretExists(ctx context.Context, instance client.Obje |
350 | 353 | return true |
351 | 354 | } |
352 | 355 |
|
| 356 | +// ValidateSecretWithKeys validates that a Secret exists and contains required keys |
| 357 | +func (r *Reconciler) ValidateSecretWithKeys( |
| 358 | + ctx context.Context, |
| 359 | + instance client.Object, |
| 360 | + secretName string, |
| 361 | + requiredKeys []string, |
| 362 | +) error { |
| 363 | + // When secret is not specified, skip it |
| 364 | + if secretName == "" { |
| 365 | + return nil |
| 366 | + } |
| 367 | + |
| 368 | + secret := &corev1.Secret{} |
| 369 | + err := r.Client.Get(ctx, client.ObjectKey{Namespace: instance.GetNamespace(), Name: secretName}, secret) |
| 370 | + if err != nil { |
| 371 | + return err |
| 372 | + } |
| 373 | + |
| 374 | + // Validate required keys |
| 375 | + for _, key := range requiredKeys { |
| 376 | + if _, ok := secret.Data[key]; !ok { |
| 377 | + return fmt.Errorf("%w '%s' in secret %s", ErrMissingRequiredKey, key, secretName) |
| 378 | + } |
| 379 | + } |
| 380 | + |
| 381 | + return nil |
| 382 | +} |
| 383 | + |
| 384 | +// ValidateConfigMapWithKeys validates that a ConfigMap exists and contains required keys |
| 385 | +func (r *Reconciler) ValidateConfigMapWithKeys( |
| 386 | + ctx context.Context, |
| 387 | + instance client.Object, |
| 388 | + configMapName string, |
| 389 | + requiredKeys []string, |
| 390 | +) error { |
| 391 | + // When config map is not specified, skip it |
| 392 | + if configMapName == "" { |
| 393 | + return nil |
| 394 | + } |
| 395 | + |
| 396 | + cm := &corev1.ConfigMap{} |
| 397 | + err := r.Client.Get(ctx, client.ObjectKey{Namespace: instance.GetNamespace(), Name: configMapName}, cm) |
| 398 | + if err != nil { |
| 399 | + return err |
| 400 | + } |
| 401 | + |
| 402 | + // Validate required keys |
| 403 | + for _, key := range requiredKeys { |
| 404 | + if _, ok := cm.Data[key]; !ok { |
| 405 | + return fmt.Errorf("%w '%s' in config map %s", ErrMissingRequiredKey, key, configMapName) |
| 406 | + } |
| 407 | + } |
| 408 | + |
| 409 | + return nil |
| 410 | +} |
| 411 | + |
| 412 | +// ValidateOpenstackInputs validates OpenStack configuration inputs |
| 413 | +func (r *Reconciler) ValidateOpenstackInputs( |
| 414 | + ctx context.Context, |
| 415 | + instance client.Object, |
| 416 | + openstackConfigMapName string, |
| 417 | + openstackConfigSecretName string, |
| 418 | +) error { |
| 419 | + err := r.ValidateConfigMapWithKeys(ctx, instance, openstackConfigMapName, []string{"clouds.yaml"}) |
| 420 | + if err != nil { |
| 421 | + return err |
| 422 | + } |
| 423 | + |
| 424 | + err = r.ValidateSecretWithKeys(ctx, instance, openstackConfigSecretName, []string{"secure.yaml"}) |
| 425 | + if err != nil { |
| 426 | + return err |
| 427 | + } |
| 428 | + |
| 429 | + return nil |
| 430 | +} |
| 431 | + |
353 | 432 | // GetStringHash returns a hash of the given string with the specified length |
354 | 433 | func GetStringHash(str string, hashLength int) string { |
355 | 434 | hash := sha256.New() |
|
0 commit comments