Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[serverless-1.33] fix: PaC build #783

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions pkg/pipelines/tekton/pac/pac.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ const (
routePacLabel = "pipelines-as-code/route=controller"
)

// Namespaces where PaC is commonly installed.
var usualSuspects = []string{"pipelines-as-code", "openshift-pipelines"}

// DetectPACInstallation checks whether PAC is installed on the cluster
// Taken and slightly modified from https://github.com/openshift-pipelines/pipelines-as-code/blob/6a7f043f9bb51d04ab729505b26446695595df1f/pkg/cmd/tknpac/bootstrap/bootstrap.go
func DetectPACInstallation(ctx context.Context, wantedNamespace string) (bool, string, error) {
matejvasek marked this conversation as resolved.
Show resolved Hide resolved
func DetectPACInstallation(ctx context.Context) (bool, string, error) {
var installed bool

clientPac, _, err := NewTektonPacClientAndResolvedNamespace("")
clientPac, cns, err := NewTektonPacClientAndResolvedNamespace("")
if err != nil {
return false, "", err
}
Expand All @@ -36,20 +39,21 @@ func DetectPACInstallation(ctx context.Context, wantedNamespace string) (bool, s
return false, "", err
}

_, err = clientPac.Repositories("").List(ctx, metav1.ListOptions{})
_, err = clientPac.Repositories(cns).List(ctx, metav1.ListOptions{})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rationale: unprivileged user most likely cannot scan all the namespaces, but they most likely can scan current namespace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

if err != nil && k8serrors.IsNotFound(err) {
return false, "", nil
}

installed = true
if wantedNamespace != "" {
_, err := clientK8s.CoreV1().ConfigMaps(wantedNamespace).Get(ctx, infoConfigMap, metav1.GetOptions{})
if err == nil {
return installed, wantedNamespace, nil

for _, suspectedNS := range usualSuspects {
_, e := clientK8s.CoreV1().ConfigMaps(suspectedNS).Get(ctx, infoConfigMap, metav1.GetOptions{})
if e == nil {
return installed, suspectedNS, nil
}
return installed, "", fmt.Errorf("could not detect Pipelines as Code configmap in %s namespace : %w, please reinstall", wantedNamespace, err)
}

// Search all namespaces if the usual suspects do not contain the desired configmap.
cms, err := clientK8s.CoreV1().ConfigMaps("").List(ctx, metav1.ListOptions{
LabelSelector: configMapPacLabel,
})
Expand Down
13 changes: 9 additions & 4 deletions pkg/pipelines/tekton/pipelines_pac_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (pp *PipelinesProvider) createLocalPACResources(ctx context.Context, f fn.F
// also creates PVC for the function source code
func (pp *PipelinesProvider) createClusterPACResources(ctx context.Context, f fn.Function, metadata pipelines.PacMetadata) error {
// figure out pac installation namespace
installed, _, err := pac.DetectPACInstallation(ctx, "")
installed, _, err := pac.DetectPACInstallation(ctx)
if !installed {
errMsg := ""
if err != nil {
Expand All @@ -154,7 +154,12 @@ func (pp *PipelinesProvider) createClusterPACResources(ctx context.Context, f fn
labels = pp.decorator.UpdateLabels(f, labels)
}

registry, err := docker.GetRegistry(f.Image)
img := f.Deploy.Image
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rationale: f.Image appears to be empty , f.Deploy.Image appears to contain the desired value instead.

Copy link
Collaborator Author

@matejvasek matejvasek Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caused error for all users not only unprivileged ones!

if img == "" {
img = f.Image
}

registry, err := docker.GetRegistry(img)
if err != nil {
return fmt.Errorf("problem in resolving image registry name: %w", err)
}
Expand All @@ -163,7 +168,7 @@ func (pp *PipelinesProvider) createClusterPACResources(ctx context.Context, f fn
registry = authn.DefaultAuthKey
}

creds, err := pp.credentialsProvider(ctx, f.Image)
creds, err := pp.credentialsProvider(ctx, img)
if err != nil {
return err
}
Expand Down Expand Up @@ -199,7 +204,7 @@ func (pp *PipelinesProvider) createClusterPACResources(ctx context.Context, f fn
func (pp *PipelinesProvider) createRemotePACResources(ctx context.Context, f fn.Function, metadata pipelines.PacMetadata) error {

// figure out pac installation namespace
installed, installationNS, err := pac.DetectPACInstallation(ctx, "")
installed, installationNS, err := pac.DetectPACInstallation(ctx)
if !installed {
errMsg := ""
if err != nil {
Expand Down
Loading