Skip to content
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
1 change: 1 addition & 0 deletions api/v1/codebase_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type CodebaseSpec struct {

// A name of tool which should be used as CI.
// +optional
// +kubebuilder:validation:Enum=tekton;gitlab
// +kubebuilder:default:=tekton
CiTool string `json:"ciTool"`

Expand Down
7 changes: 7 additions & 0 deletions api/v1/git_server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ type GitServerSpec struct {
// +optional
// +kubebuilder:example:=`https://webhook-url.com`
WebhookUrl string `json:"webhookUrl,omitempty"`

// TektonDisabled disables creation of Tekton EventListener and associated
// Ingress/Route resources for this GitServer.
// Use this when the git provider's native CI (e.g. GitLab CI, GitHub Actions)
// is used instead of Tekton, or when webhook endpoints are managed externally.
// +optional
TektonDisabled bool `json:"tektonDisabled,omitempty"`
}

// GitServerStatus defines the observed state of GitServer.
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/v2.edp.epam.com_codebases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ spec:
ciTool:
default: tekton
description: A name of tool which should be used as CI.
enum:
- tekton
- gitlab
type: string
cloneRepositoryCredentials:
description: CloneRepositoryCredentials contains reference to secret
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/v2.edp.epam.com_gitservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ spec:
sshPort:
format: int32
type: integer
tektonDisabled:
description: |-
TektonDisabled disables creation of Tekton EventListener and associated
Ingress/Route resources for this GitServer.
Use this when the git provider's native CI (e.g. GitLab CI, GitHub Actions)
is used instead of Tekton, or when webhook endpoints are managed externally.
type: boolean
webhookUrl:
description: |-
WebhookUrl is a URL for webhook that will be created in the git provider.
Expand Down
6 changes: 6 additions & 0 deletions controllers/codebasebranch/codebasebranch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ func (r *ReconcileCodebaseBranch) setDefaultValues(
return false, nil
}

// GitLab CI codebases use .gitlab-ci.yml for pipeline configuration,
// so there is no need to generate Tekton pipeline names.
if codebase.Spec.CiTool == util.CIGitLab {
return false, nil
}

gitServer := &codebaseApi.GitServer{}
if err := r.client.Get(ctx, types.NamespacedName{
Name: codebase.Spec.GitServer,
Expand Down
107 changes: 107 additions & 0 deletions controllers/codebasebranch/codebasebranch_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ func TestReconcileCodebaseBranch_Reconcile_ShouldSetPipelines(t *testing.T) {
BuildTool: "go",
Type: "application",
GitServer: "test-gs",
CiTool: util.CITekton,
Versioning: codebaseApi.Versioning{
Type: codebaseApi.VersioningTypDefault,
},
Expand Down Expand Up @@ -609,6 +610,7 @@ func TestReconcileCodebaseBranch_Reconcile_FailedToSetPipelines_GitServerNotFoun
BuildTool: "go",
Type: "application",
GitServer: "test-gs",
CiTool: util.CITekton,
Versioning: codebaseApi.Versioning{
Type: codebaseApi.VersioningTypDefault,
},
Expand All @@ -634,3 +636,108 @@ func TestReconcileCodebaseBranch_Reconcile_FailedToSetPipelines_GitServerNotFoun
require.Error(t, err)
require.Contains(t, err.Error(), "failed to get GitServer")
}

func TestReconcileCodebaseBranch_Reconcile_ShouldSkipPipelinesForGitLab(t *testing.T) {
cb := &codebaseApi.CodebaseBranch{
ObjectMeta: metaV1.ObjectMeta{
Name: "test-branch",
Namespace: "default",
},
Spec: codebaseApi.CodebaseBranchSpec{
CodebaseName: "test-codebase",
BranchName: "test-branch",
},
}
c := &codebaseApi.Codebase{
ObjectMeta: metaV1.ObjectMeta{
Name: "test-codebase",
Namespace: "default",
},
Spec: codebaseApi.CodebaseSpec{
Lang: "go",
Framework: "gin",
BuildTool: "go",
Type: "application",
GitServer: "test-gs",
CiTool: util.CIGitLab,
Versioning: codebaseApi.Versioning{
Type: codebaseApi.VersioningTypDefault,
},
},
}
gs := &codebaseApi.GitServer{
ObjectMeta: metaV1.ObjectMeta{
Name: "test-gs",
Namespace: "default",
},
Spec: codebaseApi.GitServerSpec{
GitProvider: codebaseApi.GitProviderGithub,
},
}

scheme := runtime.NewScheme()
require.NoError(t, codebaseApi.AddToScheme(scheme))

fakeCl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(c, cb, gs).WithStatusSubresource(cb).Build()

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: "test-branch",
Namespace: "default",
},
}

controller := NewReconcileCodebaseBranch(fakeCl, scheme, logr.Discard())

_, err := controller.Reconcile(context.Background(), req)

require.NoError(t, err)

updatedCb := &codebaseApi.CodebaseBranch{}
err = fakeCl.Get(
context.Background(),
types.NamespacedName{
Name: "test-branch",
Namespace: "default",
},
updatedCb,
)

require.NoError(t, err)
assert.Nil(t, updatedCb.Spec.Pipelines)
}

func TestSetDefaultValues_ShouldSkipForGitLab(t *testing.T) {
cb := &codebaseApi.CodebaseBranch{
ObjectMeta: metaV1.ObjectMeta{
Name: "test-branch",
Namespace: "default",
},
Spec: codebaseApi.CodebaseBranchSpec{
CodebaseName: "test-codebase",
BranchName: "test-branch",
},
}
c := &codebaseApi.Codebase{
ObjectMeta: metaV1.ObjectMeta{
Name: "test-codebase",
Namespace: "default",
},
Spec: codebaseApi.CodebaseSpec{
CiTool: util.CIGitLab,
},
}

scheme := runtime.NewScheme()
require.NoError(t, codebaseApi.AddToScheme(scheme))

fakeCl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(c, cb).Build()

r := NewReconcileCodebaseBranch(fakeCl, scheme, logr.Discard())

changed, err := r.setDefaultValues(context.Background(), cb, c)

require.NoError(t, err)
assert.False(t, changed)
assert.Nil(t, cb.Spec.Pipelines)
}
7 changes: 6 additions & 1 deletion controllers/gitserver/create_event_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func NewCreateEventListener(k8sClient client.Client) *CreateEventListener {
func (h *CreateEventListener) ServeRequest(ctx context.Context, gitServer *codebaseApi.GitServer) error {
log := ctrl.LoggerFrom(ctx)

if gitServer.Spec.TektonDisabled {
log.Info("Skip creating EventListener because Tekton is disabled")
return nil
}

if gitServer.Spec.WebhookUrl != "" {
log.Info("Skip creating EventListener because webhook URL is set")
return nil
Expand All @@ -53,7 +58,7 @@ func (h *CreateEventListener) createEventListener(ctx context.Context, gitServer

// Use Unstructured to avoid direct dependency on "knative.dev/pkg/apis/duck/v1" because EventListener relies on it.
// This dependency can conflict with the operator's dependencies.
// https://github.com/tektoncd/triggers/blob/v0.27.0/pkg/apis/triggers/v1beta1/event_listener_types.go#L86
// https://github.com/tektoncd/triggers/blob/v0.34.0/pkg/apis/triggers/v1beta1/event_listener_types.go#L86
el := tektoncd.NewEventListenerUnstructured()
elName := generateEventListenerName(gitServer.Name)

Expand Down
59 changes: 59 additions & 0 deletions controllers/gitserver/create_event_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,65 @@ func TestCreateEventListener_ServeRequest(t *testing.T) {
wantErr require.ErrorAssertionFunc
want func(t *testing.T, k8sClient client.Client)
}{
{
name: "skip creating event listener because tekton is disabled",
gitServer: &codebaseApi.GitServer{
ObjectMeta: controllerruntime.ObjectMeta{
Name: "test-git-server",
Namespace: "default",
},
Spec: codebaseApi.GitServerSpec{
TektonDisabled: true,
},
},
k8sClient: func(t *testing.T) client.Client {
return fake.NewClientBuilder().WithScheme(scheme).Build()
},
wantErr: require.NoError,
want: func(t *testing.T, k8sClient client.Client) {
el := tektoncd.NewEventListenerUnstructured()
err := k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: "default",
Name: generateEventListenerName("test-git-server"),
}, el)
require.Error(t, err)
require.True(t, k8sErrors.IsNotFound(err))

i := &networkingv1.Ingress{}
err = k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: "default",
Name: GenerateIngressName("test-git-server"),
}, i)
require.Error(t, err)
require.True(t, k8sErrors.IsNotFound(err))
},
},
{
name: "skip creating event listener because tekton is disabled even with webhook URL set",
gitServer: &codebaseApi.GitServer{
ObjectMeta: controllerruntime.ObjectMeta{
Name: "test-git-server",
Namespace: "default",
},
Spec: codebaseApi.GitServerSpec{
TektonDisabled: true,
WebhookUrl: "https://external-webhook",
},
},
k8sClient: func(t *testing.T) client.Client {
return fake.NewClientBuilder().WithScheme(scheme).Build()
},
wantErr: require.NoError,
want: func(t *testing.T, k8sClient client.Client) {
el := tektoncd.NewEventListenerUnstructured()
err := k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: "default",
Name: generateEventListenerName("test-git-server"),
}, el)
require.Error(t, err)
require.True(t, k8sErrors.IsNotFound(err))
},
},
{
name: "skip creating event listener because webhook URL is set",
gitServer: &codebaseApi.GitServer{
Expand Down
3 changes: 3 additions & 0 deletions deploy-templates/crds/v2.edp.epam.com_codebases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ spec:
ciTool:
default: tekton
description: A name of tool which should be used as CI.
enum:
- tekton
- gitlab
type: string
cloneRepositoryCredentials:
description: CloneRepositoryCredentials contains reference to secret
Expand Down
7 changes: 7 additions & 0 deletions deploy-templates/crds/v2.edp.epam.com_gitservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ spec:
sshPort:
format: int32
type: integer
tektonDisabled:
description: |-
TektonDisabled disables creation of Tekton EventListener and associated
Ingress/Route resources for this GitServer.
Use this when the git provider's native CI (e.g. GitLab CI, GitHub Actions)
is used instead of Tekton, or when webhook endpoints are managed externally.
type: boolean
webhookUrl:
description: |-
WebhookUrl is a URL for webhook that will be created in the git provider.
Expand Down
13 changes: 12 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -796,10 +796,11 @@ Selected branch will become a default branch for a new codebase (e.g. master, ma
<td>false</td>
</tr><tr>
<td><b>ciTool</b></td>
<td>string</td>
<td>enum</td>
<td>
A name of tool which should be used as CI.<br/>
<br/>
<i>Enum</i>: tekton, gitlab<br/>
<i>Default</i>: tekton<br/>
</td>
<td>false</td>
Expand Down Expand Up @@ -1276,6 +1277,16 @@ For Gerrit provider, only id_rsa key is required and used.<br/>
SkipWebhookSSLVerification is a flag to skip webhook tls verification.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>tektonDisabled</b></td>
<td>boolean</td>
<td>
TektonDisabled disables creation of Tekton EventListener and associated
Ingress/Route resources for this GitServer.
Use this when the git provider's native CI (e.g. GitLab CI, GitHub Actions)
is used instead of Tekton, or when webhook endpoints are managed externally.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>webhookUrl</b></td>
<td>string</td>
Expand Down
2 changes: 1 addition & 1 deletion pkg/tektoncd/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

const (
triggersGroup = "triggers.tekton.dev"
triggersAPIVersion = "v1alpha1"
triggersAPIVersion = "v1beta1"
triggersKind = "EventListener"
)

Expand Down