Skip to content

Commit

Permalink
Not list confgimaps in all namespaces
Browse files Browse the repository at this point in the history
This commit makes the changes to detect pac installation only in
default namespace i.e. pipelines-as-cdoe and openshift-pipelines
or other if provided by user through --pac-namespace flag

This makes the tkn pac create repo command usable for
non-admin users too

But on the other side, it will be a breaking change for admin users
who have pac installed in some other namespace and were
using the commands without pac-namespace flag

It will affect tkn pac create repo, tkn pac bootstrap and
tkn pac webhook add commands
  • Loading branch information
piyush-garg committed Aug 1, 2023
1 parent 1eb8efe commit 2c68a77
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 7 deletions.
37 changes: 30 additions & 7 deletions pkg/cmd/tknpac/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
kapierror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -27,6 +28,8 @@ const (

var providerTargets = []string{"github-app", "github-enterprise-app"}

var defaultNamespaces = []string{"openshift-pipelines", pacNS}

type bootstrapOpts struct {
forceInstallGosmee bool
providerType string
Expand Down Expand Up @@ -261,17 +264,37 @@ func DetectPacInstallation(ctx context.Context, wantedNS string, run *params.Run
return installed, "", fmt.Errorf("could not detect Pipelines as Code configmap in %s namespace : %w, please reinstall", wantedNS, err)
}

cms, err := run.Clients.Kube.CoreV1().ConfigMaps("").List(ctx, metav1.ListOptions{
LabelSelector: configMapPacLabel,
})
cm, err := getConfigMap(ctx, run)
if err == nil {
for _, cm := range cms.Items {
if cm.Name == infoConfigMap {
return installed, cm.Namespace, nil
return installed, cm.Namespace, nil
}
return installed, "", fmt.Errorf("could not detect Pipelines as Code configmap on the cluster, please specify the namespace in which pac is installed")
}

func getConfigMap(ctx context.Context, run *params.Run) (*corev1.ConfigMap, error) {
var (
err error
configMap *corev1.ConfigMap
)
for _, n := range defaultNamespaces {
configMap, err = run.Clients.Kube.CoreV1().ConfigMaps(n).Get(ctx, infoConfigMap, metav1.GetOptions{})
if err != nil {
if kapierror.IsNotFound(err) {
continue
}
if strings.Contains(err.Error(), fmt.Sprintf(`cannot get resource "configmaps" in API group "" in the namespace "%s"`, n)) {
continue
}
return nil, err
}
if configMap != nil {
break
}
}
return installed, "", fmt.Errorf("could not detect Pipelines as Code configmap on the cluster, please reinstall")
if configMap == nil {
return nil, fmt.Errorf("ConfigMap not found in default namespaces")
}
return configMap, nil
}

func addGithubAppFlag(cmd *cobra.Command, opts *bootstrapOpts) {
Expand Down
95 changes: 95 additions & 0 deletions pkg/cmd/tknpac/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package bootstrap
import (
"bytes"
"io"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"

"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
Expand Down Expand Up @@ -46,3 +48,96 @@ func TestInstall(t *testing.T) {
assert.Assert(t, err != nil)
assert.Equal(t, "=> Checking if Pipelines as Code is installed.\n", out.String())
}

func TestDetectPacInstallation(t *testing.T) {
testParams := []struct {
name string
namespace string
userProvidedNamespace string
configMap *corev1.ConfigMap
wantInstalled bool
wantNamespace string
wantError bool
errorMsg string
}{
{
name: "get configmap in pipeline-as-code namespace",
namespace: pacNS,
configMap: getConfigMapData(pacNS, "v0.17.2"),
wantNamespace: pacNS,
wantInstalled: true,
}, {
name: "get configmap in openshift-pipelines namespace",
namespace: "openshift-pipelines",
configMap: getConfigMapData("openshift-pipelines", "v0.17.2"),
wantNamespace: "openshift-pipelines",
wantInstalled: true,
}, {
name: "get configmap present in different namespace other than default namespaces",
namespace: "test",
userProvidedNamespace: "test",
configMap: getConfigMapData("test", "dev"),
wantNamespace: "test",
wantInstalled: true,
}, {
name: "configmap not in default namespace",
namespace: "test",
userProvidedNamespace: "",
configMap: getConfigMapData("test", "v0.17.2"),
wantError: true,
errorMsg: "could not detect Pipelines as Code configmap on the cluster, please specify the namespace in which pac is installed",
wantInstalled: false,
}, {
name: "configmap not in default namespace with user provided namespace",
namespace: "test",
userProvidedNamespace: "test1",
configMap: getConfigMapData("test", "v0.17.2"),
wantError: true,
errorMsg: "could not detect Pipelines as Code configmap in test1 namespace : configmaps \"pipelines-as-code-info\" not found, please reinstall",
wantInstalled: false,
},
}
for _, tp := range testParams {
t.Run(tp.name, func(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
cs, _ := testclient.SeedTestData(t, ctx, testclient.Data{})
logger, _ := logger.GetLogger()

run := &params.Run{
Clients: clients.Clients{
PipelineAsCode: cs.PipelineAsCode,
Log: logger,
Kube: cs.Kube,
},
Info: info.Info{},
}
if _, err := run.Clients.Kube.CoreV1().ConfigMaps(tp.namespace).Create(ctx, tp.configMap, metav1.CreateOptions{}); err != nil {
t.Errorf("failed to create configmap: %v", err)
}
installed, ns, err := DetectPacInstallation(ctx, tp.userProvidedNamespace, run)
if err != nil {
if !tp.wantError {
t.Errorf("Not expecting error but got: %v", err)
} else {
assert.Equal(t, err.Error(), tp.errorMsg)
}
} else {
assert.Equal(t, tp.wantInstalled, installed)
assert.Equal(t, tp.wantNamespace, ns)
}
})
}
}

func getConfigMapData(namespace, version string) *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: infoConfigMap,
Namespace: namespace,
},
Data: map[string]string{
"version": version,
},
}
}

0 comments on commit 2c68a77

Please sign in to comment.