Skip to content

Commit 9800cf2

Browse files
committed
K8SPSMDB-1466 improve MCS error handling to prevent operator crashes
1 parent 4fd2556 commit 9800cf2

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

pkg/mcs/register.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mcs
22

33
import (
4-
"github.com/pkg/errors"
54
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
65
"k8s.io/apimachinery/pkg/runtime"
76
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -36,7 +35,10 @@ func addKnownTypes(scheme *runtime.Scheme) error {
3635
func Register(dc *discovery.DiscoveryClient) error {
3736
resources, err := dc.ServerPreferredResources()
3837
if err != nil {
39-
return errors.Wrap(err, "get api groups and resources")
38+
// MCS is optional functionality - if discovery fails for any reason,
39+
// mark it as unavailable and continue without crashing the operator
40+
available = false
41+
return nil
4042
}
4143

4244
outer:

pkg/mcs/register_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package mcs
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsAvailable(t *testing.T) {
8+
// Test IsAvailable function
9+
available = true
10+
if !IsAvailable() {
11+
t.Error("Expected IsAvailable to return true when available is true")
12+
}
13+
14+
available = false
15+
if IsAvailable() {
16+
t.Error("Expected IsAvailable to return false when available is false")
17+
}
18+
}
19+
20+
func TestMCSSchemeGroupVersion(t *testing.T) {
21+
// Test that MCSSchemeGroupVersion is initialized correctly
22+
if MCSSchemeGroupVersion.Group != "" {
23+
t.Errorf("Expected MCSSchemeGroupVersion.Group to be empty initially, got: %s", MCSSchemeGroupVersion.Group)
24+
}
25+
if MCSSchemeGroupVersion.Version != "" {
26+
t.Errorf("Expected MCSSchemeGroupVersion.Version to be empty initially, got: %s", MCSSchemeGroupVersion.Version)
27+
}
28+
}
29+
30+
func TestServiceExport(t *testing.T) {
31+
// Test ServiceExport creation
32+
namespace := "test-namespace"
33+
name := "test-service"
34+
labels := map[string]string{
35+
"app": "test",
36+
}
37+
38+
se := ServiceExport(namespace, name, labels)
39+
if se == nil {
40+
t.Error("Expected ServiceExport to be created, got nil")
41+
}
42+
43+
if se.Name != name {
44+
t.Errorf("Expected ServiceExport name to be %s, got: %s", name, se.Name)
45+
}
46+
47+
if se.Namespace != namespace {
48+
t.Errorf("Expected ServiceExport namespace to be %s, got: %s", namespace, se.Namespace)
49+
}
50+
51+
if se.Labels["app"] != "test" {
52+
t.Errorf("Expected ServiceExport label 'app' to be 'test', got: %s", se.Labels["app"])
53+
}
54+
}
55+
56+
func TestServiceExportList(t *testing.T) {
57+
// Test ServiceExportList creation
58+
seList := ServiceExportList()
59+
if seList == nil {
60+
t.Error("Expected ServiceExportList to be created, got nil")
61+
}
62+
63+
if seList.Kind != "ServiceExportList" {
64+
t.Errorf("Expected ServiceExportList Kind to be 'ServiceExportList', got: %s", seList.Kind)
65+
}
66+
}

0 commit comments

Comments
 (0)