Skip to content

Commit

Permalink
HiveNamespace to use docID/clusterID for new Installs (#2992)
Browse files Browse the repository at this point in the history
HiveNamespace currently uses aro-<uuid>, this change is an effort
to unify UUIDs accross cluster doc instead of having multiple,
by pointing HiveNamespace to docID so this can be leveraged later.

More Details: https://redhat-internal.slack.com/archives/C02ULBRS68M/p1686806655273309
  • Loading branch information
SrinivasAtmakuri authored Feb 5, 2024
1 parent 2ac8d65 commit 6e8e4e1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pkg/cluster/adminupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestAdminUpdateSteps(t *testing.T) {
doc := baseClusterDoc()
doc.OpenShiftCluster.Properties.ProvisioningState = api.ProvisioningStateAdminUpdating
doc.OpenShiftCluster.Properties.MaintenanceTask = api.MaintenanceTaskEverything
doc.OpenShiftCluster.Properties.HiveProfile.Namespace = "some_namespace"
doc.OpenShiftCluster.Properties.HiveProfile.Namespace = "aro-00000000-0000-0000-0000-000000000000"
doc.OpenShiftCluster.Properties.HiveProfile.CreatedByHive = true
return doc, true
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/hive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (m *manager) hiveCreateNamespace(ctx context.Context) error {
return nil
}

namespace, err := m.hiveClusterManager.CreateNamespace(ctx)
namespace, err := m.hiveClusterManager.CreateNamespace(ctx, m.doc.ID)
if err != nil {
return err
}
Expand Down
25 changes: 13 additions & 12 deletions pkg/cluster/hive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
)

func TestHiveClusterDeploymentReady(t *testing.T) {
fakeNamespace := "fake-namespace"

fakeNamespace := "aro-00000000-0000-0000-0000-000000000000"
for _, tt := range []struct {
name string
mocks func(hiveMock *mock_hive.MockClusterManager, doc *api.OpenShiftClusterDocument)
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestHiveClusterDeploymentReady(t *testing.T) {
}

func TestHiveResetCorrelationData(t *testing.T) {
fakeNamespace := "fake-namespace"
fakeNamespace := "aro-00000000-0000-0000-0000-000000000000"

for _, tt := range []struct {
name string
Expand Down Expand Up @@ -115,6 +114,8 @@ func TestHiveResetCorrelationData(t *testing.T) {
}

func TestHiveCreateNamespace(t *testing.T) {
fakeNamespace := "aro-00000000-0000-0000-0000-000000000000"
fakeNewNamespace := "aro-11111111-1111-1111-1111-111111111111"
for _, tt := range []struct {
testName string
existingNamespaceName string
Expand All @@ -126,36 +127,36 @@ func TestHiveCreateNamespace(t *testing.T) {
{
testName: "creates namespace if it doesn't exist",
existingNamespaceName: "",
newNamespaceName: "new-namespace",
newNamespaceName: fakeNamespace,
clusterManagerMock: func(mockCtrl *gomock.Controller, namespaceName string) *mock_hive.MockClusterManager {
namespaceToReturn := &corev1.Namespace{}
namespaceToReturn.Name = namespaceName
mockClusterManager := mock_hive.NewMockClusterManager(mockCtrl)
mockClusterManager.EXPECT().CreateNamespace(gomock.Any()).Return(namespaceToReturn, nil)
mockClusterManager.EXPECT().CreateNamespace(gomock.Any(), gomock.Any()).Return(namespaceToReturn, nil)
return mockClusterManager
},
expectedNamespaceName: "new-namespace",
expectedNamespaceName: fakeNamespace,
wantErr: "",
},
{
testName: "doesn't create namespace if it already exists",
existingNamespaceName: "existing-namespace",
newNamespaceName: "new-namespace",
expectedNamespaceName: "existing-namespace",
existingNamespaceName: fakeNamespace,
newNamespaceName: fakeNewNamespace,
expectedNamespaceName: fakeNamespace,
clusterManagerMock: func(mockCtrl *gomock.Controller, namespaceName string) *mock_hive.MockClusterManager {
mockClusterManager := mock_hive.NewMockClusterManager(mockCtrl)
mockClusterManager.EXPECT().CreateNamespace(gomock.Any()).Times(0)
mockClusterManager.EXPECT().CreateNamespace(gomock.Any(), gomock.Any()).Times(0)
return mockClusterManager
},
},
{
testName: "returns error if cluster manager returns error",
existingNamespaceName: "",
newNamespaceName: "new-namespace",
newNamespaceName: fakeNamespace,
expectedNamespaceName: "",
clusterManagerMock: func(mockCtrl *gomock.Controller, namespaceName string) *mock_hive.MockClusterManager {
mockClusterManager := mock_hive.NewMockClusterManager(mockCtrl)
mockClusterManager.EXPECT().CreateNamespace(gomock.Any()).Return(nil, fmt.Errorf("cluster manager error"))
mockClusterManager.EXPECT().CreateNamespace(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("cluster manager error"))
return mockClusterManager
},
wantErr: "cluster manager error",
Expand Down
7 changes: 3 additions & 4 deletions pkg/hive/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import (
"github.com/Azure/ARO-RP/pkg/hive/failure"
"github.com/Azure/ARO-RP/pkg/util/dynamichelper"
utillog "github.com/Azure/ARO-RP/pkg/util/log"
"github.com/Azure/ARO-RP/pkg/util/uuid"
)

type ClusterManager interface {
CreateNamespace(ctx context.Context) (*corev1.Namespace, error)
CreateNamespace(ctx context.Context, docID string) (*corev1.Namespace, error)

// CreateOrUpdate reconciles the ClusterDocument and related secrets for an
// existing cluster. This may adopt the cluster (Create) or amend the
Expand Down Expand Up @@ -110,11 +109,11 @@ func NewFromConfig(log *logrus.Entry, _env env.Core, restConfig *rest.Config) (C
}, nil
}

func (hr *clusterManager) CreateNamespace(ctx context.Context) (*corev1.Namespace, error) {
func (hr *clusterManager) CreateNamespace(ctx context.Context, docID string) (*corev1.Namespace, error) {
var namespaceName string
var namespace *corev1.Namespace
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
namespaceName = "aro-" + uuid.DefaultGenerator.Generate()
namespaceName = "aro-" + docID
namespace = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
Expand Down
11 changes: 6 additions & 5 deletions pkg/hive/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestIsClusterDeploymentReady(t *testing.T) {
fakeNamespace := "fake-namespace"
fakeNamespace := "aro-00000000-0000-0000-0000-000000000000"
doc := &api.OpenShiftClusterDocument{
OpenShiftCluster: &api.OpenShiftCluster{
Properties: api.OpenShiftClusterProperties{
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestIsClusterDeploymentReady(t *testing.T) {
}

func TestIsClusterInstallationComplete(t *testing.T) {
fakeNamespace := "fake-namespace"
fakeNamespace := "aro-00000000-0000-0000-0000-000000000000"
doc := &api.OpenShiftClusterDocument{
OpenShiftCluster: &api.OpenShiftCluster{
Properties: api.OpenShiftClusterProperties{
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestIsClusterInstallationComplete(t *testing.T) {
}

func TestResetCorrelationData(t *testing.T) {
fakeNamespace := "fake-namespace"
fakeNamespace := "aro-00000000-0000-0000-0000-000000000000"
doc := &api.OpenShiftClusterDocument{
OpenShiftCluster: &api.OpenShiftCluster{
Properties: api.OpenShiftClusterProperties{
Expand Down Expand Up @@ -478,6 +478,7 @@ func TestResetCorrelationData(t *testing.T) {
}

func TestCreateNamespace(t *testing.T) {
const docID = "00000000-0000-0000-0000-000000000000"
for _, tc := range []struct {
name string
nsNames []string
Expand Down Expand Up @@ -519,7 +520,7 @@ func TestCreateNamespace(t *testing.T) {
uuid.DefaultGenerator = uuidfake.NewGenerator(tc.nsNames)
}

ns, err := c.CreateNamespace(context.Background())
ns, err := c.CreateNamespace(context.Background(), docID)
if err != nil && !tc.shouldFail {
t.Error(err)
}
Expand All @@ -538,7 +539,7 @@ func TestCreateNamespace(t *testing.T) {
}

func TestGetClusterDeployment(t *testing.T) {
fakeNamespace := "fake-namespace"
fakeNamespace := "aro-00000000-0000-0000-0000-000000000000"
doc := &api.OpenShiftClusterDocument{
OpenShiftCluster: &api.OpenShiftCluster{
Properties: api.OpenShiftClusterProperties{
Expand Down
8 changes: 4 additions & 4 deletions pkg/util/mocks/hive/hive.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/e2e/hive_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ var _ = Describe("Hive manager creates a namespace", func() {
})

It("Should be created successfully", func() {
const docID = "00000000-0000-0000-0000-000000000000"
var err error
ns, err = clients.HiveClusterManager.CreateNamespace(context.Background())
ns, err = clients.HiveClusterManager.CreateNamespace(context.Background(), docID)
Expect(err).NotTo(HaveOccurred())
Expect(ns).NotTo(BeNil())

Expand Down

0 comments on commit 6e8e4e1

Please sign in to comment.