Skip to content
Draft
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
26 changes: 26 additions & 0 deletions cmd/pipelines-as-code-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import (
"log"
"os"
"strings"
"time"

"github.com/openshift-pipelines/pipelines-as-code/pkg/adapter"
"github.com/openshift-pipelines/pipelines-as-code/pkg/generated/injection/informers/pipelinesascode/v1alpha1/repository"
"github.com/openshift-pipelines/pipelines-as-code/pkg/informer/transform"
"github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
evadapter "knative.dev/eventing/pkg/adapter/v2"
"knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/logging"
"knative.dev/pkg/signals"
Expand All @@ -31,11 +36,32 @@ func main() {
log.Fatal("failed to init clients : ", err)
}

// Set up injection context for informers, same way as the watcher does.
cfg := injection.ParseAndGetRESTConfigOrDie()
ctx = controller.WithResyncPeriod(ctx, 10*time.Minute)
ctx, informers := injection.Default.SetupInformers(ctx, cfg)

// Register the informer and set the cache transform before starting informers.
// SetTransform must happen before the informer starts. Trim cached objects
// the same way the watcher does to keep the cache footprint small.
repoInformer := repository.Get(ctx)
if err := repoInformer.Informer().SetTransform(transform.RepositoryForCache); err != nil {
log.Fatal("failed to set transform on repository informer: ", err)
}
run.RepositoryLister = repoInformer.Lister()

kinteract, err := kubeinteraction.NewKubernetesInteraction(run)
if err != nil {
log.Fatal("failed to init kinit client : ", err)
}

// Start all informers and wait for cache sync before processing webhooks.
// controller.StartInformers starts each informer in its own goroutine and waits
// for all caches to sync, ensuring RepositoryLister has an up-to-date view.
if err := controller.StartInformers(ctx.Done(), informers...); err != nil {
log.Fatalf("failed to start and sync informers: %v", err)
}

loggerConfiguratorOpt := evadapter.WithLoggerConfiguratorConfigMapName(logging.ConfigMapName())
loggerConfigurator := evadapter.NewLoggerConfiguratorFromConfigMap(PACControllerLogKey, loggerConfiguratorOpt)
copts := []evadapter.ConfiguratorOption{
Expand Down
2 changes: 1 addition & 1 deletion config/201-controller-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ rules:
verbs: ["get", "update"]
- apiGroups: ["pipelinesascode.tekton.dev"]
resources: ["repositories"]
verbs: ["get", "create", "list"]
verbs: ["get", "create", "list", "watch"]
Comment thread
theakshaypant marked this conversation as resolved.
- apiGroups: ["tekton.dev"]
resources: ["pipelineruns"]
verbs: ["get", "list", "create", "patch"]
Expand Down
20 changes: 10 additions & 10 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/adapter/v2"
"knative.dev/pkg/logging"
"knative.dev/pkg/system"
Expand Down Expand Up @@ -201,15 +200,6 @@ func (l listener) handleEvent(ctx context.Context) http.HandlerFunc {
event := info.NewEvent()
pacInfo := l.run.Info.GetPacOpts()

globalRepo, err := l.run.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(l.run.Info.Kube.Namespace).Get(
ctx, l.run.Info.Controller.GlobalRepository, metav1.GetOptions{},
)
if err == nil && globalRepo != nil {
l.logger.Infof("detected global repository settings named %s in namespace %s", l.run.Info.Controller.GlobalRepository, l.run.Info.Kube.Namespace)
} else {
globalRepo = &v1alpha1.Repository{}
}

detected, configuring, err := github.ConfigureRepository(ctx, l.run, request, string(payload), &pacInfo, l.logger)
if detected {
if configuring && err == nil {
Expand Down Expand Up @@ -255,6 +245,16 @@ func (l listener) handleEvent(ctx context.Context) http.HandlerFunc {
trace.WithSpanKind(trace.SpanKindServer),
)

globalRepo, err := l.run.GetRepository(ctx, l.run.Info.Kube.Namespace, l.run.Info.Controller.GlobalRepository)
if err == nil && globalRepo != nil {
l.logger.Infof("detected global repository settings named %s in namespace %s", l.run.Info.Controller.GlobalRepository, l.run.Info.Kube.Namespace)
} else {
if err != nil {
logger.Infof("global repository lookup failed: %v, continuing with empty global config", err)
}
globalRepo = &v1alpha1.Repository{}
}

s := sinker{
run: l.run,
vcx: gitProvider,
Expand Down
5 changes: 1 addition & 4 deletions pkg/gitclient/client_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.41.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// SetupAuthenticatedClient sets up the authenticated VCS client with proper token scoping.
Expand All @@ -40,9 +39,7 @@ func SetupAuthenticatedClient(
run.Info.Kube.Namespace != "" &&
run.Info.Controller.GlobalRepository != "" {
var err error
if globalRepo, err = run.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(run.Info.Kube.Namespace).Get(
ctx, run.Info.Controller.GlobalRepository, metav1.GetOptions{},
); err != nil {
if globalRepo, err = run.GetRepository(ctx, run.Info.Kube.Namespace, run.Info.Controller.GlobalRepository); err != nil {
logger.Errorf("cannot get global repository: %v", err)
}
}
Expand Down
51 changes: 37 additions & 14 deletions pkg/matcher/repo_runinfo_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ import (
var ErrRepositoryNameConflict = errors.New("multiple repositories exist with the given name")

func MatchEventURLRepo(ctx context.Context, cs *params.Run, event *info.Event, ns string) (*apipac.Repository, error) {
repositories, err := cs.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(ns).List(
ctx, metav1.ListOptions{},
)
repoItems, err := cs.ListRepositories(ctx, ns)
if err != nil {
return nil, err
}
sort.RepositorySortByCreationOldestTime(repositories.Items)
for _, repo := range repositories.Items {
repo.Spec.URL = strings.TrimSuffix(repo.Spec.URL, "/")
if repo.Spec.URL == event.URL {

sort.RepositorySortByCreationOldestTime(repoItems)
eventURL := strings.TrimSuffix(event.URL, "/")

for _, repo := range repoItems {
repoURL := strings.TrimSuffix(repo.Spec.URL, "/")
if repoURL == eventURL {
return &repo, nil
}
}
Expand All @@ -37,20 +38,42 @@ func MatchEventURLRepo(ctx context.Context, cs *params.Run, event *info.Event, n
// GetRepoByName get a repo by name anywhere on a cluster.
// Parameter 'ns' may optionally be supplied in case of a naming conflict.
func GetRepoByName(ctx context.Context, cs *params.Run, repoName, ns string) (*apipac.Repository, error) {
repositories, err := cs.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(ns).List(
ctx, metav1.ListOptions{
FieldSelector: "metadata.name==" + repoName,
},
)
// No namespace: the direct API path filters by name server-side, which the
// lister cannot do, so keep it as a dedicated branch.
if cs.RepositoryLister == nil {
repositories, err := cs.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(ns).List(
ctx, metav1.ListOptions{
FieldSelector: "metadata.name==" + repoName,
},
)
if err != nil {
return nil, err
}
return repoByUniqueName(repositories.Items)
}

// Use the lister with the provided namespace (empty means all namespaces).
allRepos, err := cs.ListRepositories(ctx, ns)
if err != nil {
return nil, err
}
var matching []apipac.Repository
for _, repo := range allRepos {
if repo.Name == repoName {
matching = append(matching, repo)
}
}
return repoByUniqueName(matching)
}

switch len(repositories.Items) {
// repoByUniqueName returns the sole repository in repos, nil when there are
// none, or ErrRepositoryNameConflict when more than one share the name.
func repoByUniqueName(repos []apipac.Repository) (*apipac.Repository, error) {
switch len(repos) {
case 0:
return nil, nil
case 1:
return &repositories.Items[0], nil
return &repos[0], nil
default:
return nil, ErrRepositoryNameConflict
}
Expand Down
Loading
Loading