Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVEREST-1102 | Fix GetDBNamespaces implementation #454

Merged
merged 11 commits into from
Jun 28, 2024
4 changes: 2 additions & 2 deletions api/backup_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (e *EverestServer) ListBackupStorages(ctx echo.Context) error {

// CreateBackupStorage creates a new backup storage object.
func (e *EverestServer) CreateBackupStorage(ctx echo.Context) error { //nolint:funlen
namespaces, err := e.kubeClient.GetDBNamespaces(ctx.Request().Context(), e.kubeClient.Namespace())
namespaces, err := e.kubeClient.GetDBNamespaces(ctx.Request().Context())
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{
Expand Down Expand Up @@ -268,7 +268,7 @@ func (e *EverestServer) UpdateBackupStorage(ctx echo.Context, backupStorageName
})
}

namespaces, err := e.kubeClient.GetDBNamespaces(ctx.Request().Context(), e.kubeClient.Namespace())
namespaces, err := e.kubeClient.GetDBNamespaces(ctx.Request().Context())
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{
Expand Down
2 changes: 1 addition & 1 deletion api/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// ListNamespaces returns the current version information.
func (e *EverestServer) ListNamespaces(ctx echo.Context) error {
namespaces, err := e.kubeClient.GetDBNamespaces(ctx.Request().Context(), e.kubeClient.Namespace())
namespaces, err := e.kubeClient.GetDBNamespaces(ctx.Request().Context())
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{
Expand Down
2 changes: 1 addition & 1 deletion api/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (e *EverestServer) collectMetrics(ctx context.Context, url string) error {
return err
}

namespaces, err := e.kubeClient.GetDBNamespaces(ctx, e.kubeClient.Namespace())
namespaces, err := e.kubeClient.GetDBNamespaces(ctx)
if err != nil {
e.l.Error(errors.Join(err, errors.New("failed to get watched namespaces")))
return err
Expand Down
2 changes: 1 addition & 1 deletion deploy/quickstart-k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rules:
resources: ["databaseclusters", "databaseclusterbackups", "databaseclusterrestores", "backupstorages", "monitoringconfigs"]
verbs: ["*"]
- apiGroups: [""]
resources: ["secrets", "configmaps"]
resources: ["secrets", "configmaps", "namespaces"]
verbs: ["*"]
- apiGroups: [""]
resources: ["nodes", "pods", "persistentvolumes"]
Expand Down
2 changes: 2 additions & 0 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (

// SystemNamespace is the namespace where everest is installed.
SystemNamespace = "everest-system"
// MonitoringNamespace is the namespace where monitoring configs are created.
MonitoringNamespace = "everest-monitoring"
// PerconaEverestDeploymentName stores the name of everest API Server deployment.
PerconaEverestDeploymentName = "percona-everest"
// PerconaEverestOperatorDeploymentName stores the name of everest operator deployment.
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/backup_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (k *Kubernetes) IsBackupStorageUsed(ctx context.Context, namespace, backupS

namespaces := nsList
if len(nsList) == 0 {
namespaces, err = k.GetDBNamespaces(ctx, namespace)
namespaces, err = k.GetDBNamespaces(ctx)
if err != nil {
return false, err
}
Expand Down
34 changes: 16 additions & 18 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,29 +1020,27 @@ func (k *Kubernetes) DeleteEverest(ctx context.Context, namespace string, versio
}

// GetDBNamespaces returns a list of namespaces that are monitored by the Everest operator.
func (k *Kubernetes) GetDBNamespaces(ctx context.Context, namespace string) ([]string, error) {
deployment, err := k.GetDeployment(ctx, EverestOperatorDeploymentName, namespace)
func (k *Kubernetes) GetDBNamespaces(ctx context.Context) ([]string, error) {
// List all namespaces managed by everest.
namespaceList, err := k.ListNamespaces(ctx, metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
common.KubernetesManagedByLabel: common.Everest,
},
}),
})
if err != nil {
// If the operator is not found, we assume that no namespaces are being watched.
if apierrors.IsNotFound(err) {
return []string{}, nil
}
return nil, err
return nil, errors.Join(err, errors.New("failed to get watched namespaces"))
}

for _, container := range deployment.Spec.Template.Spec.Containers {
if container.Name != everestOperatorContainerName {
internalNs := []string{common.SystemNamespace, common.MonitoringNamespace}
result := make([]string, 0, len(namespaceList.Items))
for _, ns := range namespaceList.Items {
if slices.Contains(internalNs, ns.GetName()) {
continue
}
for _, envVar := range container.Env {
if envVar.Name != EverestDBNamespacesEnvVar {
continue
}
return strings.Split(envVar.Value, ","), nil
}
result = append(result, ns.GetName())
}

return nil, errors.New("failed to get watched namespaces")
return result, nil
}

// WaitForRollout waits for rollout of a provided deployment in the provided namespace.
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/kubernetes_interface.go

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

18 changes: 9 additions & 9 deletions pkg/kubernetes/mock_kubernetes_connector.go

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

2 changes: 1 addition & 1 deletion pkg/kubernetes/monitoring_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (k *Kubernetes) IsMonitoringConfigUsed(ctx context.Context, namespace, moni

namespaces := nsList
if len(nsList) == 0 {
namespaces, err = k.GetDBNamespaces(ctx, k.Namespace())
namespaces, err = k.GetDBNamespaces(ctx)
if err != nil {
return false, err
}
Expand Down
18 changes: 3 additions & 15 deletions pkg/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/AlecAivazis/survey/v2"
"go.uber.org/zap"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -190,7 +189,7 @@ func (u *Uninstall) confirmForce() (bool, error) {

func (u *Uninstall) getDBs(ctx context.Context) (map[string]*everestv1alpha1.DatabaseClusterList, error) {
allDBs := make(map[string]*everestv1alpha1.DatabaseClusterList)
namespaces, err := u.kubeClient.GetDBNamespaces(ctx, common.SystemNamespace)
namespaces, err := u.kubeClient.GetDBNamespaces(ctx)
if err != nil {
// If the system namespace doesn't exist, we assume there are no DBs.
if k8serrors.IsNotFound(err) {
Expand Down Expand Up @@ -315,20 +314,9 @@ func (u *Uninstall) deleteNamespaces(ctx context.Context, namespaces []string) e

func (u *Uninstall) deleteDBNamespaces(ctx context.Context) error {
u.l.Info("Trying to delete database namespaces")
// List all namespaces managed by everest.
namespaceList, err := u.kubeClient.ListNamespaces(ctx, metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
common.KubernetesManagedByLabel: common.Everest,
},
}),
})
namespaces, err := u.kubeClient.GetDBNamespaces(ctx)
if err != nil {
return err
}
namespaces := make([]string, 0, len(namespaceList.Items))
for _, item := range namespaceList.Items {
namespaces = append(namespaces, item.Name)
return errors.Join(err, errors.New("failed to deleteDBNamespaces"))
}
if len(namespaces) == 0 {
u.l.Info("No database namespaces found")
Expand Down
4 changes: 2 additions & 2 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (u *Upgrade) Run(ctx context.Context) error {

// ensureManagedByLabelOnDBNamespaces ensures that all database namespaces have the managed-by label set.
func (u *Upgrade) ensureManagedByLabelOnDBNamespaces(ctx context.Context) error {
dbNamespaces, err := u.kubeClient.GetDBNamespaces(ctx, common.SystemNamespace)
dbNamespaces, err := u.kubeClient.GetDBNamespaces(ctx)
if err != nil {
u.l.Error(err)
return errors.Join(err, errors.New("could not retrieve database namespaces"))
Expand Down Expand Up @@ -471,7 +471,7 @@ func (u *Upgrade) checkRequirements(ctx context.Context, supVer *supportedVersio
u.l.Debug("cli version is empty")
}

nss, err := u.kubeClient.GetDBNamespaces(ctx, common.SystemNamespace)
nss, err := u.kubeClient.GetDBNamespaces(ctx)
if err != nil {
return err
}
Expand Down
Loading