diff --git a/pkg/controller/add_cluster.go b/pkg/controller/add_cluster.go index 26032082..b3660026 100644 --- a/pkg/controller/add_cluster.go +++ b/pkg/controller/add_cluster.go @@ -35,6 +35,7 @@ import ( corev1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" + pkgerrors "github.com/pkg/errors" "go.uber.org/zap" ) @@ -53,7 +54,7 @@ func (c *Controller) EnsurePlacement(cluster *myspec.M3DBCluster) error { // Get placement plClient := c.adminClient.placementClientForCluster(cluster) _, err := plClient.Get() - if err == m3admin.ErrNotFound { + if pkgerrors.Cause(err) == m3admin.ErrNotFound { placementInitRequest := &admin.PlacementInitRequest{ NumShards: cluster.Spec.NumberOfShards, ReplicationFactor: cluster.Spec.ReplicationFactor, diff --git a/pkg/controller/add_cluster_test.go b/pkg/controller/add_cluster_test.go index 19cd68e6..6ced10d1 100644 --- a/pkg/controller/add_cluster_test.go +++ b/pkg/controller/add_cluster_test.go @@ -22,12 +22,18 @@ package controller import ( "archive/zip" + "errors" "strings" "testing" + "github.com/m3db/m3db-operator/pkg/m3admin" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "github.com/golang/mock/gomock" "github.com/kubernetes/utils/pointer" + pkgerrors "github.com/pkg/errors" "github.com/rakyll/statik/fs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -55,6 +61,35 @@ func registerValidConfigMap() error { return nil } +func TestEnsurePlacement(t *testing.T) { + cluster := getFixture("cluster-3-zones.yaml", t) + deps := newTestDeps(t, &testOpts{ + crdObjects: []runtime.Object{cluster}, + }) + k8sops, err := newFakeK8sops() + require.NoError(t, err) + + placementMock := deps.placementClient + defer deps.cleanup() + + controller := deps.newController() + controller.k8sclient = k8sops + + placementMock.EXPECT().Get().Return(nil, pkgerrors.WithMessage(m3admin.ErrNotFound, "foo")) + placementMock.EXPECT().Init(gomock.Any()) + + err = controller.EnsurePlacement(cluster) + assert.NoError(t, err) + + placementMock.EXPECT().Get() + err = controller.EnsurePlacement(cluster) + assert.NoError(t, err) + + placementMock.EXPECT().Get().Return(nil, errors.New("placement client not available")) + err = controller.EnsurePlacement(cluster) + assert.Error(t, err) +} + func TestEnsureService_Base(t *testing.T) { cluster := getFixture("cluster-simple.yaml", t) k8sops, err := newFakeK8sops() diff --git a/pkg/controller/update_cluster.go b/pkg/controller/update_cluster.go index 2635c105..6aaea5d4 100644 --- a/pkg/controller/update_cluster.go +++ b/pkg/controller/update_cluster.go @@ -47,6 +47,7 @@ import ( klabels "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/util/runtime" + pkgerrors "github.com/pkg/errors" "go.uber.org/zap" ) @@ -109,7 +110,7 @@ func (c *Controller) pruneNamespaces(cluster *myspec.M3DBCluster, registry *dbns continue } - if err == m3admin.ErrNotFound { + if pkgerrors.Cause(err) == m3admin.ErrNotFound { c.logger.Info("namespace has already been deleted", zap.String("namespace", ns)) continue } @@ -165,7 +166,7 @@ func (c *Controller) validatePlacementWithStatus(cluster *myspec.M3DBCluster) (* return cluster, nil } - if err != m3admin.ErrNotFound { + if pkgerrors.Cause(err) != m3admin.ErrNotFound { err := fmt.Errorf("error from m3admin placement get: %v", err) c.logger.Error(err.Error()) runtime.HandleError(err) diff --git a/pkg/controller/update_cluster_test.go b/pkg/controller/update_cluster_test.go index f071ca5a..745ed5fb 100644 --- a/pkg/controller/update_cluster_test.go +++ b/pkg/controller/update_cluster_test.go @@ -47,6 +47,7 @@ import ( "k8s.io/apimachinery/pkg/util/clock" "github.com/golang/mock/gomock" + pkgerrors "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -91,7 +92,7 @@ func TestReconcileNamespaces(t *testing.T) { assert.NoError(t, err) } -func TestCleanupNamespaces(t *testing.T) { +func TestPruneNamespaces(t *testing.T) { cluster := getFixture("cluster-simple.yaml", t) cluster.Spec.Namespaces = []myspec.Namespace{} @@ -111,7 +112,7 @@ func TestCleanupNamespaces(t *testing.T) { err := controller.pruneNamespaces(cluster, registry) assert.NoError(t, err) - nsMock.EXPECT().Delete("foo").Return(m3admin.ErrNotFound) + nsMock.EXPECT().Delete("foo").Return(pkgerrors.WithMessage(m3admin.ErrNotFound, "foo")) err = controller.pruneNamespaces(cluster, registry) assert.NoError(t, err) @@ -542,7 +543,6 @@ func podWithName(name string) *corev1.Pod { } func TestValidatePlacementWithStatus(t *testing.T) { - cluster := getFixture("cluster-3-zones.yaml", t) deps := newTestDeps(t, &testOpts{ crdObjects: []runtime.Object{cluster}, @@ -552,7 +552,6 @@ func TestValidatePlacementWithStatus(t *testing.T) { defer deps.cleanup() controller := deps.newController() - //idProvider := deps.idProvider placementMock.EXPECT().Get().AnyTimes() @@ -562,6 +561,26 @@ func TestValidatePlacementWithStatus(t *testing.T) { require.NotNil(t, clusterReturn) } +func TestValidatePlacementWithStatus_ErrNotFound(t *testing.T) { + cluster := getFixture("cluster-3-zones.yaml", t) + deps := newTestDeps(t, &testOpts{ + crdObjects: []runtime.Object{cluster}, + }) + + placementMock := deps.placementClient + defer deps.cleanup() + + controller := deps.newController() + + placementMock.EXPECT().Get().Return(nil, pkgerrors.Wrap(m3admin.ErrNotFound, "foo")) + placementMock.EXPECT().Init(gomock.Any()) + + clusterReturn, err := controller.validatePlacementWithStatus(cluster) + + require.NoError(t, err) + require.NotNil(t, clusterReturn) +} + func TestSortPodID(t *testing.T) { for _, test := range []struct { podIDs []podID