From 23f707326986fbb960bf45fbcec3eccb6916b012 Mon Sep 17 00:00:00 2001 From: Andrew Minkin Date: Mon, 25 Sep 2023 18:54:59 +0600 Subject: [PATCH 1/8] EVEREST-203 Do not run registration for already registered k8s clusters --- pkg/install/operators.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/install/operators.go b/pkg/install/operators.go index 831781d1..960aeb4d 100644 --- a/pkg/install/operators.go +++ b/pkg/install/operators.go @@ -780,6 +780,16 @@ func (o *Operators) provisionMonitoring() error { // connectToEverest connects the k8s cluster to Everest. func (o *Operators) connectToEverest(ctx context.Context) (*client.KubernetesCluster, error) { + clusters, err := o.everestClient.ListKubernetesClusters(ctx) + if err != nil { + return nil, errors.Join(err, errors.New("could not list kubernetes clusters")) + } + for _, cluster := range clusters { + if cluster.Name == &o.config.Name { + // Cluster is already registered. Do nothing + return cluster, nil + } + } if err := o.prepareServiceAccount(); err != nil { return nil, errors.Join(err, errors.New("could not prepare a service account")) } From a0cf96a752d24badd7e04302f74d589f61e051e4 Mon Sep 17 00:00:00 2001 From: Andrew Minkin Date: Mon, 25 Sep 2023 19:01:47 +0600 Subject: [PATCH 2/8] EVEREST-203 Generated files --- pkg/install/deps.go | 3 ++ .../mock_everest_client_connector_test.go | 28 ++++++++++++++++++- pkg/install/operators.go | 4 +-- .../client/mock_kube_client_connector.go | 2 +- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/install/deps.go b/pkg/install/deps.go index 123f28d9..2b9fb49b 100644 --- a/pkg/install/deps.go +++ b/pkg/install/deps.go @@ -25,6 +25,9 @@ import ( //go:generate ../../bin/mockery --name=everestClientConnector --case=snake --inpackage --testonly type everestClientConnector interface { + ListKubernetesClusters( + ctx context.Context, + ) ([]client.KubernetesCluster, error) RegisterKubernetesCluster( ctx context.Context, body client.RegisterKubernetesClusterJSONRequestBody, diff --git a/pkg/install/mock_everest_client_connector_test.go b/pkg/install/mock_everest_client_connector_test.go index 259aba5f..d4ddf190 100644 --- a/pkg/install/mock_everest_client_connector_test.go +++ b/pkg/install/mock_everest_client_connector_test.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.4. DO NOT EDIT. +// Code generated by mockery v2.32.3. DO NOT EDIT. package install @@ -92,6 +92,32 @@ func (_m *mockEverestClientConnector) GetMonitoringInstance(ctx context.Context, return r0, r1 } +// ListKubernetesClusters provides a mock function with given fields: ctx +func (_m *mockEverestClientConnector) ListKubernetesClusters(ctx context.Context) ([]client.KubernetesCluster, error) { + ret := _m.Called(ctx) + + var r0 []client.KubernetesCluster + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) ([]client.KubernetesCluster, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) []client.KubernetesCluster); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]client.KubernetesCluster) + } + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // ListMonitoringInstances provides a mock function with given fields: ctx func (_m *mockEverestClientConnector) ListMonitoringInstances(ctx context.Context) ([]client.MonitoringInstanceBaseWithName, error) { ret := _m.Called(ctx) diff --git a/pkg/install/operators.go b/pkg/install/operators.go index 960aeb4d..1129805b 100644 --- a/pkg/install/operators.go +++ b/pkg/install/operators.go @@ -785,9 +785,9 @@ func (o *Operators) connectToEverest(ctx context.Context) (*client.KubernetesClu return nil, errors.Join(err, errors.New("could not list kubernetes clusters")) } for _, cluster := range clusters { - if cluster.Name == &o.config.Name { + if cluster.Name == o.config.Name { // Cluster is already registered. Do nothing - return cluster, nil + return &cluster, nil } } if err := o.prepareServiceAccount(); err != nil { diff --git a/pkg/kubernetes/client/mock_kube_client_connector.go b/pkg/kubernetes/client/mock_kube_client_connector.go index c3988f68..21963fe1 100644 --- a/pkg/kubernetes/client/mock_kube_client_connector.go +++ b/pkg/kubernetes/client/mock_kube_client_connector.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.4. DO NOT EDIT. +// Code generated by mockery v2.32.3. DO NOT EDIT. package client From 2c140631da29c06f508359b2848fdf382dacb4ad Mon Sep 17 00:00:00 2001 From: Andrew Minkin Date: Mon, 25 Sep 2023 19:05:02 +0600 Subject: [PATCH 3/8] EVEREST-203 Added integration test --- cli-tests/tests/flow/pg-operator.spec.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cli-tests/tests/flow/pg-operator.spec.ts b/cli-tests/tests/flow/pg-operator.spec.ts index 774b9f54..28eea39d 100644 --- a/cli-tests/tests/flow/pg-operator.spec.ts +++ b/cli-tests/tests/flow/pg-operator.spec.ts @@ -59,6 +59,23 @@ test.describe('Everest CLI install operators', async () => { await page.waitForTimeout(10_000); + await verifyClusterResources(); + await apiVerifyClusterExists(request, clusterName); + + await test.step('re-run everest install operators command', async () => { + const out = await cli.everestExecSkipWizard( + `install operators --operator.mongodb=false --operator.postgresql=true --operator.xtradb-cluster=true --backup.enable=0 --monitoring.enable=0 --name=${clusterName}`, + ); + + await out.assertSuccess(); + await out.outErrContainsNormalizedMany([ + 'percona-xtradb-cluster-operator operator has been installed', + 'everest-operator operator has been installed', + 'Connected Kubernetes cluster to Everest', + ]); + }); + await page.waitForTimeout(10_000); + await verifyClusterResources(); await apiVerifyClusterExists(request, clusterName); await cliDeleteCluster(cli, request, clusterName); From 0c79bca41bc19ce8630cb6ad73fd87c603ea3144 Mon Sep 17 00:00:00 2001 From: Andrew Minkin Date: Mon, 25 Sep 2023 19:38:28 +0600 Subject: [PATCH 4/8] EVEREST-203 Fixed test --- cli-tests/tests/flow/pg-operator.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli-tests/tests/flow/pg-operator.spec.ts b/cli-tests/tests/flow/pg-operator.spec.ts index 28eea39d..373116ee 100644 --- a/cli-tests/tests/flow/pg-operator.spec.ts +++ b/cli-tests/tests/flow/pg-operator.spec.ts @@ -71,6 +71,8 @@ test.describe('Everest CLI install operators', async () => { await out.outErrContainsNormalizedMany([ 'percona-xtradb-cluster-operator operator has been installed', 'everest-operator operator has been installed', + ]); + await out.outNotContains([ 'Connected Kubernetes cluster to Everest', ]); }); From 81b10532040c95c1ade3b130023054d9883f1a95 Mon Sep 17 00:00:00 2001 From: Andrew Minkin Date: Mon, 25 Sep 2023 19:46:34 +0600 Subject: [PATCH 5/8] EVEREST-203 fixed test --- cli-tests/tests/flow/pg-operator.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/cli-tests/tests/flow/pg-operator.spec.ts b/cli-tests/tests/flow/pg-operator.spec.ts index 373116ee..92d66b1d 100644 --- a/cli-tests/tests/flow/pg-operator.spec.ts +++ b/cli-tests/tests/flow/pg-operator.spec.ts @@ -38,7 +38,6 @@ test.describe('Everest CLI install operators', async () => { await out.outNotContains([ 'percona-server-mongodb-operator', - 'percona-xtradb-cluster-operator', ]); }); }; From b9849ce69157e0bf3db13fa0f85735d011518de7 Mon Sep 17 00:00:00 2001 From: Andrew Minkin Date: Mon, 25 Sep 2023 19:57:12 +0600 Subject: [PATCH 6/8] EVEREST-203 Better deletion handling --- pkg/install/operators.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/install/operators.go b/pkg/install/operators.go index 1129805b..3a2cfc1e 100644 --- a/pkg/install/operators.go +++ b/pkg/install/operators.go @@ -25,6 +25,8 @@ import ( "net/url" "time" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + "github.com/AlecAivazis/survey/v2" "github.com/operator-framework/api/pkg/operators/v1alpha1" "github.com/percona/percona-everest-backend/client" @@ -32,6 +34,7 @@ import ( "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "github.com/percona/percona-everest-cli/commands/common" @@ -786,6 +789,14 @@ func (o *Operators) connectToEverest(ctx context.Context) (*client.KubernetesClu } for _, cluster := range clusters { if cluster.Name == o.config.Name { + ns, err := o.kubeClient.GetNamespace(ctx, cluster.Namespace) + if err != nil && !k8serrors.IsNotFound(err) { + return nil, errors.Join(err, errors.New("could not get namespace from Kubernetes")) + } + + if ns.UID != types.UID(cluster.Uid) { + return nil, errors.New("namespace UID mismatch. It looks like you're trying to register a new kubernetes cluster using the existing name. Please unregister the existing kubernetes cluster first") + } // Cluster is already registered. Do nothing return &cluster, nil } From bbb9ae113958284bbbf252451cb61f55fe06267c Mon Sep 17 00:00:00 2001 From: Andrew Minkin Date: Mon, 25 Sep 2023 20:00:04 +0600 Subject: [PATCH 7/8] EVEREST-203 Shut up linter --- pkg/install/operators.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/install/operators.go b/pkg/install/operators.go index 3a2cfc1e..e8225f0f 100644 --- a/pkg/install/operators.go +++ b/pkg/install/operators.go @@ -25,8 +25,6 @@ import ( "net/url" "time" - k8serrors "k8s.io/apimachinery/pkg/api/errors" - "github.com/AlecAivazis/survey/v2" "github.com/operator-framework/api/pkg/operators/v1alpha1" "github.com/percona/percona-everest-backend/client" @@ -34,6 +32,7 @@ import ( "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" From 2654d429e9b9d6e056e37f5352059497736c03e6 Mon Sep 17 00:00:00 2001 From: Andrew Minkin Date: Tue, 26 Sep 2023 14:23:31 +0600 Subject: [PATCH 8/8] Update pkg/install/operators.go Co-authored-by: Michal Kralik --- pkg/install/operators.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/install/operators.go b/pkg/install/operators.go index e8225f0f..2e1af8a5 100644 --- a/pkg/install/operators.go +++ b/pkg/install/operators.go @@ -794,7 +794,7 @@ func (o *Operators) connectToEverest(ctx context.Context) (*client.KubernetesClu } if ns.UID != types.UID(cluster.Uid) { - return nil, errors.New("namespace UID mismatch. It looks like you're trying to register a new kubernetes cluster using the existing name. Please unregister the existing kubernetes cluster first") + return nil, errors.New("namespace UID mismatch. It looks like you're trying to register a new Kubernetes cluster using an existing name. Please unregister the existing Kubernetes cluster first") } // Cluster is already registered. Do nothing return &cluster, nil