Skip to content

Commit d0a017d

Browse files
committed
Try to detect if pac is installed via operator
When installing via openshift-pipelines "downstream" operator. PAC would get installed in openshift-pipelines. Strategy to detection is then : if user has a -n ns flag, try to see if pac is installed in ns try to detect pac in openshift-pipelines ns (downstream) try to detect pac in pipelines-as-code ns (upstream) The way to detect it is to check the configmap installed in the openshift-pipelines namespace. This makes this detection compatible with old way (via taskrun) and the new way via controller. As far goes for for permissions usually the folks who tries to do bootstrap will have to have anyway right access to that ns to be able to bootstrap so we should be good. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 0c5feed commit d0a017d

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

pkg/cmd/tknpac/bootstrap/bootstrap.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
const (
1515
pacNS = "pipelines-as-code"
1616
pacLabel = "eventlistener=pipelines-as-code-interceptor"
17+
openshiftpacNS = "openshift-pipelines"
1718
openShiftRouteGroup = "route.openshift.io"
1819
openShiftRouteVersion = "v1"
1920
openShiftRouteResource = "routes"
@@ -69,8 +70,17 @@ func install(ctx context.Context, run *params.Run, opts *bootstrapOpts) error {
6970
if !tektonInstalled {
7071
return errors.New("tekton API not found on the cluster. Please install Tekton first")
7172
}
72-
installed, _ := checkNS(ctx, run, opts)
73-
if !opts.forceInstall && installed {
73+
74+
// if we gt a ns back it means it has been detected in here so keep it as is.
75+
// or else just set the default to pacNS
76+
ns, err := detectPacInstallation(ctx, opts.targetNamespace, run)
77+
if ns != "" {
78+
opts.targetNamespace = ns
79+
} else if opts.targetNamespace == "" {
80+
opts.targetNamespace = pacNS
81+
}
82+
83+
if !opts.forceInstall && err == nil {
7484
// nolint:forbidigo
7585
fmt.Println("👌 Pipelines as Code is already installed.")
7686
} else if err := installPac(ctx, opts); err != nil {
@@ -141,7 +151,7 @@ func Command(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
141151
}
142152
cmd.AddCommand(GithubApp(run, ioStreams))
143153

144-
addCommonFlags(cmd, opts, ioStreams)
154+
addCommonFlags(cmd, ioStreams)
145155
addGithubAppFlag(cmd, opts)
146156

147157
cmd.PersistentFlags().BoolVar(&opts.forceInstall, "force-install", false, "whether we should force pac install even if it's already installed")
@@ -168,6 +178,12 @@ func GithubApp(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
168178
return err
169179
}
170180

181+
var err error
182+
opts.targetNamespace, err = detectPacInstallation(ctx, opts.targetNamespace, run)
183+
if err != nil {
184+
return err
185+
}
186+
171187
if b, _ := askYN(false, "", "Are you using Github Enterprise?"); b {
172188
opts.providerType = "github-enteprise-app"
173189
}
@@ -178,11 +194,40 @@ func GithubApp(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
178194
"commandType": "main",
179195
},
180196
}
181-
addCommonFlags(cmd, opts, ioStreams)
197+
addCommonFlags(cmd, ioStreams)
182198
addGithubAppFlag(cmd, opts)
199+
200+
cmd.PersistentFlags().StringVarP(&opts.targetNamespace, "namespace", "n", "", "target namespace where pac is installed")
183201
return cmd
184202
}
185203

204+
func detectPacInstallation(ctx context.Context, wantedNS string, run *params.Run) (string, error) {
205+
// detect which namespace pac is installed in
206+
// verify first if the targetNamespace actually exists
207+
if wantedNS != "" {
208+
installed, err := checkNS(ctx, run, wantedNS)
209+
if err != nil {
210+
return "", err
211+
}
212+
if !installed {
213+
return "", fmt.Errorf("PAC is not installed in namespace: %s", wantedNS)
214+
}
215+
return wantedNS, nil
216+
// if openshift pipelines ns is installed try it from there
217+
}
218+
219+
if installed, _ := checkNS(ctx, run, openshiftpacNS); installed {
220+
return openshiftpacNS, nil
221+
}
222+
223+
if installed, _ := checkNS(ctx, run, pacNS); installed {
224+
return pacNS, nil
225+
}
226+
227+
return "", fmt.Errorf("could not detect an installation of Pipelines as Code, " +
228+
"use the -n switch to specify a namespace")
229+
}
230+
186231
func addGithubAppFlag(cmd *cobra.Command, opts *bootstrapOpts) {
187232
cmd.PersistentFlags().StringVar(&opts.GithubOrganizationName, "github-organization-name", "", "Whether you want to target an organization instead of the current user")
188233
cmd.PersistentFlags().StringVar(&opts.GithubApplicationName, "github-application-name", "", "Github Application Name")
@@ -195,7 +240,6 @@ func addGithubAppFlag(cmd *cobra.Command, opts *bootstrapOpts) {
195240
fmt.Sprintf("target install type, choices are: %s ", strings.Join(providerTargets, ", ")))
196241
}
197242

198-
func addCommonFlags(cmd *cobra.Command, opts *bootstrapOpts, ioStreams *cli.IOStreams) {
243+
func addCommonFlags(cmd *cobra.Command, ioStreams *cli.IOStreams) {
199244
cmd.PersistentFlags().BoolP("no-color", "C", !ioStreams.ColorEnabled(), "disable coloring")
200-
cmd.PersistentFlags().StringVarP(&opts.targetNamespace, "namespace", "n", pacNS, "target namespace where pac is installed")
201245
}

pkg/cmd/tknpac/bootstrap/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func installPac(ctx context.Context, opts *bootstrapOpts) error {
5858

5959
if !opts.forceInstall {
6060
doinstall, err := askYN(true,
61-
"🕵️ Pipelines as Code doesn't seems to be installed",
61+
fmt.Sprintf("🕵️ Pipelines as Code doesn't seems to be installed in %s namespace", opts.targetNamespace),
6262
fmt.Sprintf("Do you want me to install Pipelines as Code %s?", latestversion))
6363
if err != nil {
6464
return err

pkg/cmd/tknpac/bootstrap/kubestuff.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
)
1212

13+
const configMapPacLabel = "app.kubernetes.io/part-of=pipelines-as-code"
14+
1315
// deleteSecret delete secret first if it exists
1416
func deleteSecret(ctx context.Context, run *params.Run, opts *bootstrapOpts) error {
1517
return run.Clients.Kube.CoreV1().Secrets(opts.targetNamespace).Delete(ctx, secretName, metav1.DeleteOptions{})
@@ -39,14 +41,26 @@ func createPacSecret(ctx context.Context, run *params.Run, opts *bootstrapOpts,
3941

4042
// checkSecret checks if the secret exists
4143
func checkSecret(ctx context.Context, run *params.Run, opts *bootstrapOpts) bool {
42-
secret, _ := run.Clients.Kube.CoreV1().Secrets(opts.targetNamespace).Get(ctx, secretName, metav1.GetOptions{})
43-
return secret.GetName() != ""
44+
_, err := run.Clients.Kube.CoreV1().Secrets(opts.targetNamespace).Get(ctx, secretName, metav1.GetOptions{})
45+
return err == nil
4446
}
4547

4648
// check if we have the namespace created
47-
func checkNS(ctx context.Context, run *params.Run, opts *bootstrapOpts) (bool, error) {
48-
ns, err := run.Clients.Kube.CoreV1().Namespaces().Get(ctx, opts.targetNamespace, metav1.GetOptions{})
49-
return ns.GetName() != "", err
49+
func checkNS(ctx context.Context, run *params.Run, targetNamespace string) (bool, error) {
50+
ns, err := run.Clients.Kube.CoreV1().Namespaces().Get(ctx, targetNamespace, metav1.GetOptions{})
51+
if err != nil {
52+
return false, err
53+
}
54+
55+
// check if there is a configmap with the pipelines-as-code label in targetNamespace
56+
cms, err := run.Clients.Kube.CoreV1().ConfigMaps(ns.GetName()).List(ctx, metav1.ListOptions{LabelSelector: configMapPacLabel})
57+
if err != nil {
58+
return false, err
59+
}
60+
if cms.Items == nil || len(cms.Items) == 0 {
61+
return false, nil
62+
}
63+
return true, nil
5064
}
5165

5266
func checkPipelinesInstalled(run *params.Run) (bool, error) {

pkg/cmd/tknpac/bootstrap/route.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"k8s.io/apimachinery/pkg/runtime/schema"
1313
)
1414

15-
// detectOpenShiftRoute detect the openshift route where the eventlistener is running
1615
func detectOpenShiftRoute(ctx context.Context, run *params.Run, opts *bootstrapOpts) (string, error) {
1716
gvr := schema.GroupVersionResource{
1817
Group: openShiftRouteGroup, Version: openShiftRouteVersion, Resource: openShiftRouteResource,

0 commit comments

Comments
 (0)