Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
EVEREST-203 Do not run registration for already registered k8s cluste…
Browse files Browse the repository at this point in the history
…rs (#151)

* EVEREST-203 Do not run registration for already registered k8s clusters

* EVEREST-203 Generated files

* EVEREST-203 Added integration test

* EVEREST-203 Fixed test

* EVEREST-203 fixed test

* EVEREST-203 Better deletion handling

* EVEREST-203 Shut up linter

* Update pkg/install/operators.go

Co-authored-by: Michal Kralik <[email protected]>

---------

Co-authored-by: Michal Kralik <[email protected]>
  • Loading branch information
gen1us2k and Michal Kralik committed Sep 28, 2023
1 parent ea97202 commit 6da6b55
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
20 changes: 19 additions & 1 deletion cli-tests/tests/flow/pg-operator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ test.describe('Everest CLI install operators', async () => {

await out.outNotContains([
'percona-server-mongodb-operator',
'percona-xtradb-cluster-operator',
]);
});
};
Expand All @@ -59,6 +58,25 @@ 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',
]);
await out.outNotContains([
'Connected Kubernetes cluster to Everest',
]);
});
await page.waitForTimeout(10_000);

await verifyClusterResources();
await apiVerifyClusterExists(request, clusterName);
await cliDeleteCluster(cli, request, clusterName);
Expand Down
3 changes: 3 additions & 0 deletions pkg/install/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 27 additions & 1 deletion pkg/install/mock_everest_client_connector_test.go

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

20 changes: 20 additions & 0 deletions pkg/install/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ 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"

"github.com/percona/percona-everest-cli/commands/common"
Expand Down Expand Up @@ -780,6 +782,24 @@ 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 {
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 an existing name. Please unregister the existing Kubernetes cluster first")
}
// 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"))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/client/mock_kube_client_connector.go

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

0 comments on commit 6da6b55

Please sign in to comment.