Skip to content

Commit

Permalink
feat: Skip Ingress creation if GitServer.spec.WebhookUrl is set (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmotso committed Jul 17, 2024
1 parent f382e8a commit 5e37f47
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/v1/git_server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type GitServerSpec struct {
SkipWebhookSSLVerification bool `json:"skipWebhookSSLVerification"`

// WebhookUrl is a URL for webhook that will be created in the git provider.
// If it is not set, a webhook will be created from Ingress with the name "event-listener".
// If not set, a new EventListener and Ingress will be created and used for webhooks.
// +optional
// +kubebuilder:example:=`https://webhook-url.com`
WebhookUrl string `json:"webhookUrl,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/v2.edp.epam.com_gitservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ spec:
webhookUrl:
description: |-
WebhookUrl is a URL for webhook that will be created in the git provider.
If it is not set, a webhook will be created from Ingress with the name "event-listener".
If not set, a new EventListener and Ingress will be created and used for webhooks.
example: https://webhook-url.com
type: string
required:
Expand Down
10 changes: 10 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ rules:
- patch
- update
- watch
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs:
- create
- get
- list
- watch
- apiGroups:
- route.openshift.io
resources:
- routes
verbs:
- create
- get
- list
- watch
Expand Down
7 changes: 7 additions & 0 deletions controllers/gitserver/create_event_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func NewCreateEventListener(k8sClient client.Client) *CreateEventListener {
}

func (h *CreateEventListener) ServeRequest(ctx context.Context, gitServer *codebaseApi.GitServer) error {
log := ctrl.LoggerFrom(ctx)

if gitServer.Spec.WebhookUrl != "" {
log.Info("Skip creating EventListener because webhook URL is set")
return nil
}

if err := h.createEventListener(ctx, gitServer); err != nil {
return err
}
Expand Down
30 changes: 30 additions & 0 deletions controllers/gitserver/create_event_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -35,6 +36,35 @@ func TestCreateEventListener_ServeRequest(t *testing.T) {
wantErr require.ErrorAssertionFunc
want func(t *testing.T, k8sClient client.Client)
}{
{
name: "skip creating event listener because webhook URL is set",
gitServer: &codebaseApi.GitServer{
Spec: codebaseApi.GitServerSpec{
WebhookUrl: "https://test-webhook-url",
},
},
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: "create event listener success k8s",
gitServer: &codebaseApi.GitServer{
Expand Down
2 changes: 1 addition & 1 deletion deploy-templates/crds/v2.edp.epam.com_gitservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ spec:
webhookUrl:
description: |-
WebhookUrl is a URL for webhook that will be created in the git provider.
If it is not set, a webhook will be created from Ingress with the name "event-listener".
If not set, a new EventListener and Ingress will be created and used for webhooks.
example: https://webhook-url.com
type: string
required:
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ GitServerSpec defines the desired state of GitServer.
<td>string</td>
<td>
WebhookUrl is a URL for webhook that will be created in the git provider.
If it is not set, a webhook will be created from Ingress with the name "event-listener".<br/>
If not set, a new EventListener and Ingress will be created and used for webhooks.<br/>
</td>
<td>false</td>
</tr></tbody>
Expand Down

0 comments on commit 5e37f47

Please sign in to comment.