From e77ba0f9531433ceb27955898c4ca9a799943965 Mon Sep 17 00:00:00 2001 From: Viacheslav Sarzhan Date: Fri, 12 Sep 2025 20:29:22 +0300 Subject: [PATCH] K8SPSMDB-1466 improve MCS error handling to prevent operator crashes --- pkg/mcs/register.go | 6 ++-- pkg/mcs/register_test.go | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 pkg/mcs/register_test.go diff --git a/pkg/mcs/register.go b/pkg/mcs/register.go index 402ae7886b..0137298b43 100644 --- a/pkg/mcs/register.go +++ b/pkg/mcs/register.go @@ -1,7 +1,6 @@ package mcs import ( - "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -36,7 +35,10 @@ func addKnownTypes(scheme *runtime.Scheme) error { func Register(dc *discovery.DiscoveryClient) error { resources, err := dc.ServerPreferredResources() if err != nil { - return errors.Wrap(err, "get api groups and resources") + // MCS is optional functionality - if discovery fails for any reason, + // mark it as unavailable and continue without crashing the operator + available = false + return nil } outer: diff --git a/pkg/mcs/register_test.go b/pkg/mcs/register_test.go new file mode 100644 index 0000000000..3628d84f40 --- /dev/null +++ b/pkg/mcs/register_test.go @@ -0,0 +1,60 @@ +package mcs + +import ( + "testing" +) + +func TestIsAvailable(t *testing.T) { + // Test IsAvailable function + available = true + if !IsAvailable() { + t.Error("Expected IsAvailable to return true when available is true") + } + + available = false + if IsAvailable() { + t.Error("Expected IsAvailable to return false when available is false") + } +} + +func TestMCSSchemeGroupVersion(t *testing.T) { + // Test that MCSSchemeGroupVersion is initialized correctly + if MCSSchemeGroupVersion.Group != "" { + t.Errorf("Expected MCSSchemeGroupVersion.Group to be empty initially, got: %s", MCSSchemeGroupVersion.Group) + } + if MCSSchemeGroupVersion.Version != "" { + t.Errorf("Expected MCSSchemeGroupVersion.Version to be empty initially, got: %s", MCSSchemeGroupVersion.Version) + } +} + +func TestServiceExport(t *testing.T) { + // Test ServiceExport creation + namespace := "test-namespace" + name := "test-service" + labels := map[string]string{ + "app": "test", + } + + se := ServiceExport(namespace, name, labels) + + if se.Name != name { + t.Errorf("Expected ServiceExport name to be %s, got: %s", name, se.Name) + } + + if se.Namespace != namespace { + t.Errorf("Expected ServiceExport namespace to be %s, got: %s", namespace, se.Namespace) + } + + if se.Labels["app"] != "test" { + t.Errorf("Expected ServiceExport label 'app' to be 'test', got: %s", se.Labels["app"]) + } +} + +func TestServiceExportList(t *testing.T) { + // Test ServiceExportList creation + seList := ServiceExportList() + + if seList.Kind != "ServiceExportList" { + t.Errorf("Expected ServiceExportList Kind to be 'ServiceExportList', got: %s", seList.Kind) + } +}