Skip to content

Commit e77ba0f

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

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
40+
if se.Name != name {
41+
t.Errorf("Expected ServiceExport name to be %s, got: %s", name, se.Name)
42+
}
43+
44+
if se.Namespace != namespace {
45+
t.Errorf("Expected ServiceExport namespace to be %s, got: %s", namespace, se.Namespace)
46+
}
47+
48+
if se.Labels["app"] != "test" {
49+
t.Errorf("Expected ServiceExport label 'app' to be 'test', got: %s", se.Labels["app"])
50+
}
51+
}
52+
53+
func TestServiceExportList(t *testing.T) {
54+
// Test ServiceExportList creation
55+
seList := ServiceExportList()
56+
57+
if seList.Kind != "ServiceExportList" {
58+
t.Errorf("Expected ServiceExportList Kind to be 'ServiceExportList', got: %s", seList.Kind)
59+
}
60+
}

0 commit comments

Comments
 (0)