Skip to content

Commit c34318c

Browse files
authored
check if deployment operator already exists (#511)
1 parent da9e0a9 commit c34318c

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

cmd/plural/cd.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ func (p *Plural) doInstallOperator(url, token, values string) error {
117117
if err != nil {
118118
return err
119119
}
120+
alreadyExists, err := console.IsAlreadyAgentInstalled(p.Kube.GetClient())
121+
if err != nil {
122+
return err
123+
}
124+
if alreadyExists && !confirm("the deployment operator is already installed. Do you want to replace it", "PLURAL_INSTALL_AGENT_CONFIRM_IF_EXISTS") {
125+
utils.Success("deployment operator is already installed, skip installation\n")
126+
return nil
127+
}
128+
120129
err = p.Kube.CreateNamespace(console.OperatorNamespace, false)
121130
if err != nil && !apierrors.IsAlreadyExists(err) {
122131
return err

pkg/console/agent.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package console
22

33
import (
4+
"context"
45
"fmt"
6+
"strings"
57
"time"
68

79
"github.com/pkg/errors"
@@ -11,6 +13,8 @@ import (
1113
"helm.sh/helm/v3/pkg/chart/loader"
1214
"helm.sh/helm/v3/pkg/cli"
1315
"helm.sh/helm/v3/pkg/storage/driver"
16+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17+
"k8s.io/client-go/kubernetes"
1418
)
1519

1620
const (
@@ -20,6 +24,24 @@ const (
2024
OperatorNamespace = "plrl-deploy-operator"
2125
)
2226

27+
func IsAlreadyAgentInstalled(k8sClient *kubernetes.Clientset) (bool, error) {
28+
dl, err := k8sClient.AppsV1().Deployments("").List(context.Background(), metav1.ListOptions{
29+
LabelSelector: "app.kubernetes.io/name=deployment-operator",
30+
})
31+
if err != nil {
32+
return false, err
33+
}
34+
for _, deployment := range dl.Items {
35+
for _, container := range deployment.Spec.Template.Spec.Containers {
36+
if strings.Contains(container.Image, "pluralsh/deployment-operator") {
37+
return true, nil
38+
}
39+
}
40+
}
41+
42+
return false, nil
43+
}
44+
2345
func InstallAgent(url, token, namespace, version string, values map[string]interface{}) error {
2446
settings := cli.New()
2547
vals := map[string]interface{}{

0 commit comments

Comments
 (0)