diff --git a/internal/controller/validatorconfig_controller.go b/internal/controller/validatorconfig_controller.go index c5997127..f7e321a0 100644 --- a/internal/controller/validatorconfig_controller.go +++ b/internal/controller/validatorconfig_controller.go @@ -20,9 +20,11 @@ import ( "context" "crypto/sha256" "encoding/base64" + "errors" "fmt" "net/http" "os" + "strconv" "strings" "sync" "time" @@ -383,27 +385,39 @@ func isConditionTrue(vc *v1alpha1.ValidatorConfig, chartName string, conditionTy return vc.Status.Conditions[idx], vc.Status.Conditions[idx].Status == corev1.ConditionTrue } -func (r *ValidatorConfigReconciler) emitFinalizeCleanup() { - grpcEnabled := os.Getenv("CLEANUP_GRPC_ENABLED") +func (r *ValidatorConfigReconciler) emitFinalizeCleanup() error { + grpcEnabled := os.Getenv("CLEANUP_GRPC_SERVER_ENABLED") if grpcEnabled != "true" { r.Log.V(0).Info("Cleanup job gRPC server is not enabled") - return + return nil } host := os.Getenv("CLEANUP_GRPC_SERVER_HOST") + if host == "" { + err := errors.New("CLEANUP_GRPC_SERVER_HOST is empty") + r.Log.Error(err, "CLEANUP_GRPC_SERVER_HOST is invalid, skipping emition of finalize cleanup event") + return err + } + port := os.Getenv("CLEANUP_GRPC_SERVER_PORT") + _, err := strconv.Atoi(port) + if err != nil { + r.Log.Error(err, "CLEANUP_GRPC_SERVER_PORT is invalid, skipping emition of finalize cleanup event") + return err + } url := fmt.Sprintf("https://%s:%s", host, port) - client := cleanupv1connect.NewCleanupServiceClient( http.DefaultClient, url, ) - _, err := client.FinalizeCleanup( + _, err = client.FinalizeCleanup( context.Background(), connect.NewRequest(&cleanv1.FinalizeCleanupRequest{}), ) if err != nil { r.Log.Error(err, "FinalizeCleanup request failed", "url", url) + return err } + return nil } diff --git a/internal/controller/validatorconfig_controller_test.go b/internal/controller/validatorconfig_controller_test.go index ce4d3579..80abf440 100644 --- a/internal/controller/validatorconfig_controller_test.go +++ b/internal/controller/validatorconfig_controller_test.go @@ -210,3 +210,66 @@ func TestConfigureHelmBasicAuth(t *testing.T) { } } } + +func TestEmitFinalizeCleanup(t *testing.T) { + cs := []struct { + name string + reconciler ValidatorConfigReconciler + env map[string]string + expected error + }{ + { + name: "CLEANUP_GRPC_SERVER_ENABLED is empty", + reconciler: ValidatorConfigReconciler{}, + env: map[string]string{}, + expected: errors.New("CLEANUP_GRPC_SERVER_HOST is empty"), + }, + { + name: "CLEANUP_GRPC_SERVER_ENABLED is disabled", + reconciler: ValidatorConfigReconciler{}, + env: map[string]string{"CLEANUP_GRPC_SERVER_ENABLED": "false"}, + expected: nil, + }, + { + name: "CLEANUP_GRPC_SERVER_HOST is empty", + reconciler: ValidatorConfigReconciler{}, + env: map[string]string{ + "CLEANUP_GRPC_SERVER_ENABLED": "true", + "CLEANUP_GRPC_SERVER_PORT": "3005", + }, + expected: errors.New("CLEANUP_GRPC_SERVER_HOST is empty"), + }, + { + name: "CLEANUP_GRPC_SERVER_PORT is empty", + reconciler: ValidatorConfigReconciler{}, + env: map[string]string{ + "CLEANUP_GRPC_SERVER_ENABLED": "true", + "CLEANUP_GRPC_SERVER_HOST": "localhost", + }, + expected: errors.New("strconv.Atoi: parsing \"\": invalid syntax"), + }, + { + name: "CLEANUP_GRPC_SERVER_PORT is invalid", + reconciler: ValidatorConfigReconciler{}, + env: map[string]string{ + "CLEANUP_GRPC_SERVER_ENABLED": "true", + "CLEANUP_GRPC_SERVER_HOST": "localhost", + "CLEANUP_GRPC_SERVER_PORT": "abcd", + }, + expected: errors.New("strconv.Atoi: parsing \"abcd\": invalid syntax"), + }, + } + for _, c := range cs { + t.Log(c.name) + + os.Clearenv() + for k, v := range c.env { + os.Setenv(k, v) + } + + err := c.reconciler.emitFinalizeCleanup() + if err != nil && !reflect.DeepEqual(err.Error(), c.expected.Error()) { + t.Errorf("expected (%v), got (%v)", c.expected, err) + } + } +}