Skip to content

Commit 1337981

Browse files
authored
Merge pull request #748 from sm43/issue-538-prevent-re-installation-of-pac-from-cli
Prevent re installation of pac from cli
2 parents f510143 + 675b318 commit 1337981

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed

pkg/cli/webhook/webhook.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,12 @@ type response struct {
4141
}
4242

4343
func (w *Options) Install(ctx context.Context, providerType string) error {
44-
if w.RepositoryURL == "" {
45-
q := "Please enter the Git repository url containing the pipelines: "
46-
if err := prompt.SurveyAskOne(&survey.Input{Message: q}, &w.RepositoryURL,
47-
survey.WithValidator(survey.Required)); err != nil {
48-
return err
49-
}
50-
}
51-
5244
// figure out pac installation namespace
53-
installationNS, err := bootstrap.DetectPacInstallation(ctx, w.PACNamespace, w.Run)
54-
if err != nil {
45+
installed, installationNS, err := bootstrap.DetectPacInstallation(ctx, w.PACNamespace, w.Run)
46+
if !installed {
47+
return fmt.Errorf("pipelines as code not installed")
48+
}
49+
if installed && err != nil {
5550
return err
5651
}
5752

@@ -68,6 +63,14 @@ func (w *Options) Install(ctx context.Context, providerType string) error {
6863
w.ControllerURL, _ = bootstrap.DetectOpenShiftRoute(ctx, w.Run, w.PACNamespace)
6964
}
7065

66+
if w.RepositoryURL == "" {
67+
q := "Please enter the Git repository url containing the pipelines: "
68+
if err := prompt.SurveyAskOne(&survey.Input{Message: q}, &w.RepositoryURL,
69+
survey.WithValidator(survey.Required)); err != nil {
70+
return err
71+
}
72+
}
73+
7174
var webhookProvider Interface
7275
switch providerType {
7376
case "github":

pkg/cmd/tknpac/bootstrap/bootstrap.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1212
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
1313
"github.com/spf13/cobra"
14+
errors2 "k8s.io/apimachinery/pkg/api/errors"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416
)
1517

1618
const (
1719
pacNS = "pipelines-as-code"
18-
openshiftpacNS = "openshift-pipelines"
1920
openShiftRouteGroup = "route.openshift.io"
2021
openShiftRouteVersion = "v1"
2122
openShiftRouteResource = "routes"
@@ -44,6 +45,8 @@ type bootstrapOpts struct {
4445
forceGitHubApp bool
4546
}
4647

48+
const infoConfigMap = "pipelines-as-code-info"
49+
4750
const indexTmpl = `
4851
<html>
4952
<body>
@@ -73,14 +76,19 @@ func install(ctx context.Context, run *params.Run, opts *bootstrapOpts) error {
7376

7477
// if we gt a ns back it means it has been detected in here so keep it as is.
7578
// or else just set the default to pacNS
76-
ns, err := DetectPacInstallation(ctx, opts.targetNamespace, run)
79+
installed, ns, err := DetectPacInstallation(ctx, opts.targetNamespace, run)
80+
81+
// installed but there is error for missing resources
82+
if installed && err != nil && !opts.forceInstall {
83+
return err
84+
}
7785
if ns != "" {
7886
opts.targetNamespace = ns
7987
} else if opts.targetNamespace == "" {
8088
opts.targetNamespace = pacNS
8189
}
8290

83-
if !opts.forceInstall && err == nil {
91+
if !opts.forceInstall && err == nil && installed {
8492
fmt.Fprintln(opts.ioStreams.Out, "👌 Pipelines as Code is already installed.")
8593
} else if err := installPac(ctx, run, opts); err != nil {
8694
return err
@@ -187,10 +195,15 @@ func GithubApp(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
187195
}
188196

189197
var err error
190-
opts.targetNamespace, err = DetectPacInstallation(ctx, opts.targetNamespace, run)
198+
var installed bool
199+
installed, opts.targetNamespace, err = DetectPacInstallation(ctx, opts.targetNamespace, run)
191200
if err != nil {
192201
return err
193202
}
203+
// installed but there is error for missing resources
204+
if installed && err != nil && !opts.forceInstall {
205+
return err
206+
}
194207

195208
pacInfo, err := info.GetPACInfo(ctx, run, opts.targetNamespace)
196209
if err != nil {
@@ -221,31 +234,36 @@ func GithubApp(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
221234
return cmd
222235
}
223236

224-
func DetectPacInstallation(ctx context.Context, wantedNS string, run *params.Run) (string, error) {
225-
// detect which namespace pac is installed in
226-
// verify first if the targetNamespace actually exists
227-
if wantedNS != "" {
228-
installed, err := checkNS(ctx, run, wantedNS)
229-
if err != nil {
230-
return "", err
231-
}
232-
if !installed {
233-
return "", fmt.Errorf("PAC is not installed in namespace: %s", wantedNS)
234-
}
235-
return wantedNS, nil
236-
// if openshift pipelines ns is installed try it from there
237-
}
237+
// installed?
238+
// where?
238239

239-
if installed, _ := checkNS(ctx, run, openshiftpacNS); installed {
240-
return openshiftpacNS, nil
240+
func DetectPacInstallation(ctx context.Context, wantedNS string, run *params.Run) (bool, string, error) {
241+
var installed bool
242+
_, err := run.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories("").List(ctx, metav1.ListOptions{})
243+
if err != nil && errors2.IsNotFound(err) {
244+
return false, "", nil
241245
}
242246

243-
if installed, _ := checkNS(ctx, run, pacNS); installed {
244-
return pacNS, nil
247+
installed = true
248+
if wantedNS != "" {
249+
_, err := run.Clients.Kube.CoreV1().ConfigMaps(wantedNS).Get(ctx, infoConfigMap, metav1.GetOptions{})
250+
if err == nil {
251+
return installed, wantedNS, nil
252+
}
253+
return installed, "", fmt.Errorf("could not detect Pipelines as Code configmap in %s namespace : %w, please reinstall", wantedNS, err)
245254
}
246255

247-
return "", fmt.Errorf("could not detect an installation of Pipelines as Code, " +
248-
"use the -n switch to specify a namespace")
256+
cms, err := run.Clients.Kube.CoreV1().ConfigMaps("").List(ctx, metav1.ListOptions{
257+
LabelSelector: configMapPacLabel,
258+
})
259+
if err == nil {
260+
for _, cm := range cms.Items {
261+
if cm.Name == infoConfigMap {
262+
return installed, cm.Namespace, nil
263+
}
264+
}
265+
}
266+
return installed, "", fmt.Errorf("could not detect Pipelines as Code configmap on the cluster, please reinstall")
249267
}
250268

251269
func addGithubAppFlag(cmd *cobra.Command, opts *bootstrapOpts) {

pkg/cmd/tknpac/bootstrap/kubestuff.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,6 @@ func createPacSecret(ctx context.Context, run *params.Run, opts *bootstrapOpts,
3737
return nil
3838
}
3939

40-
// check if we have the namespace created
41-
func checkNS(ctx context.Context, run *params.Run, targetNamespace string) (bool, error) {
42-
ns, err := run.Clients.Kube.CoreV1().Namespaces().Get(ctx, targetNamespace, metav1.GetOptions{})
43-
if err != nil {
44-
return false, err
45-
}
46-
47-
// check if there is a configmap with the pipelines-as-code label in targetNamespace
48-
cms, err := run.Clients.Kube.CoreV1().ConfigMaps(ns.GetName()).List(ctx, metav1.ListOptions{LabelSelector: configMapPacLabel})
49-
if err != nil {
50-
return false, err
51-
}
52-
if cms.Items == nil || len(cms.Items) == 0 {
53-
return false, nil
54-
}
55-
return true, nil
56-
}
57-
5840
func checkPipelinesInstalled(run *params.Run) (bool, error) {
5941
return checkGroupInstalled(run, "tekton.dev")
6042
}

0 commit comments

Comments
 (0)