@@ -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
1618const (
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+
4750const 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
251269func addGithubAppFlag (cmd * cobra.Command , opts * bootstrapOpts ) {
0 commit comments