Skip to content

Commit

Permalink
check if deployment operator already exists (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed May 19, 2024
1 parent da9e0a9 commit c34318c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/plural/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ func (p *Plural) doInstallOperator(url, token, values string) error {
if err != nil {
return err
}
alreadyExists, err := console.IsAlreadyAgentInstalled(p.Kube.GetClient())
if err != nil {
return err
}
if alreadyExists && !confirm("the deployment operator is already installed. Do you want to replace it", "PLURAL_INSTALL_AGENT_CONFIRM_IF_EXISTS") {
utils.Success("deployment operator is already installed, skip installation\n")
return nil
}

err = p.Kube.CreateNamespace(console.OperatorNamespace, false)
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
Expand Down
22 changes: 22 additions & 0 deletions pkg/console/agent.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package console

import (
"context"
"fmt"
"strings"
"time"

"github.com/pkg/errors"
Expand All @@ -11,6 +13,8 @@ import (
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/storage/driver"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

const (
Expand All @@ -20,6 +24,24 @@ const (
OperatorNamespace = "plrl-deploy-operator"
)

func IsAlreadyAgentInstalled(k8sClient *kubernetes.Clientset) (bool, error) {
dl, err := k8sClient.AppsV1().Deployments("").List(context.Background(), metav1.ListOptions{
LabelSelector: "app.kubernetes.io/name=deployment-operator",
})
if err != nil {
return false, err
}
for _, deployment := range dl.Items {
for _, container := range deployment.Spec.Template.Spec.Containers {
if strings.Contains(container.Image, "pluralsh/deployment-operator") {
return true, nil
}
}
}

return false, nil
}

func InstallAgent(url, token, namespace, version string, values map[string]interface{}) error {
settings := cli.New()
vals := map[string]interface{}{
Expand Down

0 comments on commit c34318c

Please sign in to comment.