Skip to content

Commit

Permalink
feat: Make CI request timeouts configurable (#30)
Browse files Browse the repository at this point in the history
Change-Id: If6375c142459085f21fd9026656964d9dc83d940
  • Loading branch information
mykysha committed Dec 8, 2023
1 parent ec1adcc commit 0193999
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
22 changes: 13 additions & 9 deletions controllers/codebasebranch/chain/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package factory

import (
"strings"
"time"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -20,7 +21,7 @@ import (

var log = ctrl.Log.WithName("codebase_branch_factory")

func createJenkinsDefChain(c client.Client) handler.CodebaseBranchHandler {
func createJenkinsDefChain(requestRequeueDelay time.Duration, c client.Client) handler.CodebaseBranchHandler {
log.Info("chain is selected", "type", "jenkins chain")

return chain.CheckCommitHashExists{
Expand All @@ -30,7 +31,8 @@ func createJenkinsDefChain(c client.Client) handler.CodebaseBranchHandler {
TriggerJob: trigger_job.TriggerJob{
Client: c,
Service: &service.CodebaseBranchServiceProvider{
Client: c,
TimeoutDuration: requestRequeueDelay,
Client: c,
},
Next: put_codebase_image_stream.PutCodebaseImageStream{
Client: c,
Expand All @@ -41,7 +43,7 @@ func createJenkinsDefChain(c client.Client) handler.CodebaseBranchHandler {
}
}

func createTektonDefChain(c client.Client) handler.CodebaseBranchHandler {
func createTektonDefChain(requestRequeueDelay time.Duration, c client.Client) handler.CodebaseBranchHandler {
log.Info("chain is selected", "type", "tekton chain")

return put_branch_in_git.PutBranchInGit{
Expand All @@ -52,12 +54,13 @@ func createTektonDefChain(c client.Client) handler.CodebaseBranchHandler {
Next: &clean_tmp_directory.CleanTempDirectory{},
},
Service: &service.CodebaseBranchServiceProvider{
Client: c,
TimeoutDuration: requestRequeueDelay,
Client: c,
},
}
}

func GetDeletionChain(ciType string, c client.Client) handler.CodebaseBranchHandler {
func GetDeletionChain(ciType string, requestRequeueDelay time.Duration, c client.Client) handler.CodebaseBranchHandler {
if strings.EqualFold(ciType, util.CITekton) {
return empty.MakeChain("no deletion chain for tekton", false)
}
Expand All @@ -66,16 +69,17 @@ func GetDeletionChain(ciType string, c client.Client) handler.CodebaseBranchHand
TriggerJob: trigger_job.TriggerJob{
Client: c,
Service: &service.CodebaseBranchServiceProvider{
Client: c,
TimeoutDuration: requestRequeueDelay,
Client: c,
},
},
}
}

func GetChain(ciType string, c client.Client) handler.CodebaseBranchHandler {
func GetChain(ciType string, requestRequeueDelay time.Duration, c client.Client) handler.CodebaseBranchHandler {
if strings.EqualFold(ciType, util.CITekton) {
return createTektonDefChain(c)
return createTektonDefChain(requestRequeueDelay, c)
}

return createJenkinsDefChain(c)
return createJenkinsDefChain(requestRequeueDelay, c)
}
30 changes: 16 additions & 14 deletions controllers/codebasebranch/codebasebranch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ import (
"github.com/epam/edp-codebase-operator/v2/pkg/util"
)

func NewReconcileCodebaseBranch(c client.Client, scheme *runtime.Scheme, log logr.Logger) *ReconcileCodebaseBranch {
const (
codebaseBranchOperatorFinalizerName = "codebase.branch.operator.finalizer.name"
errorStatus = "error"
)

func NewReconcileCodebaseBranch(c client.Client, scheme *runtime.Scheme, log logr.Logger, ciRequestDelay time.Duration) *ReconcileCodebaseBranch {
return &ReconcileCodebaseBranch{
client: c,
scheme: scheme,
log: log.WithName("codebase-branch"),
ciRequestDelay: ciRequestDelay,
client: c,
scheme: scheme,
log: log.WithName("codebase-branch"),
}
}

type ReconcileCodebaseBranch struct {
client client.Client
scheme *runtime.Scheme
log logr.Logger
ciRequestDelay time.Duration
client client.Client
scheme *runtime.Scheme
log logr.Logger
}

const (
codebaseBranchOperatorFinalizerName = "codebase.branch.operator.finalizer.name"
errorStatus = "error"
)

func (r *ReconcileCodebaseBranch) SetupWithManager(mgr ctrl.Manager, maxConcurrentReconciles int) error {
p := predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
Expand Down Expand Up @@ -133,7 +135,7 @@ func (r *ReconcileCodebaseBranch) Reconcile(ctx context.Context, request reconci
log.Error(err, "set labels failed")
}

result, err := r.tryToDeleteCodebaseBranch(ctx, cb, factory.GetDeletionChain(c.Spec.CiTool, r.client))
result, err := r.tryToDeleteCodebaseBranch(ctx, cb, factory.GetDeletionChain(c.Spec.CiTool, r.ciRequestDelay, r.client))
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to remove codebasebranch %v: %w", cb.Name, err)
}
Expand All @@ -150,7 +152,7 @@ func (r *ReconcileCodebaseBranch) Reconcile(ctx context.Context, request reconci
cb.Status.Build = &buildNumber
}

cbChain := factory.GetChain(c.Spec.CiTool, r.client)
cbChain := factory.GetChain(c.Spec.CiTool, r.ciRequestDelay, r.client)
if err := cbChain.ServeRequest(ctx, cb); err != nil {
const defaultPostponeTime = 5 * time.Second

Expand Down
8 changes: 4 additions & 4 deletions controllers/codebasebranch/service/codebasebranch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var log = ctrl.Log.WithName("codebase_branch_service")

const (
jenkinsJobSuccessStatus = "blue"
defaultTimeoutDuration = 5 * time.Second
defaultRetryCount = 50
)

Expand All @@ -35,7 +34,8 @@ type CodebaseBranchService interface {
}

type CodebaseBranchServiceProvider struct {
Client client.Client
TimeoutDuration time.Duration
Client client.Client
}

var ErrJobFailed = errors.New("deletion job failed")
Expand Down Expand Up @@ -63,7 +63,7 @@ func (s *CodebaseBranchServiceProvider) TriggerDeletionJob(cb *codebaseApi.Codeb

rj := fmt.Sprintf("%v/job/Delete-release-%v", cb.Spec.CodebaseName, cb.Spec.CodebaseName)

js, err := jc.GetJobStatus(rj, defaultTimeoutDuration, defaultRetryCount)
js, err := jc.GetJobStatus(rj, s.TimeoutDuration, defaultRetryCount)
if err != nil {
return fmt.Errorf("failed to get deletion job status: %w", err)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func (s *CodebaseBranchServiceProvider) TriggerReleaseJob(cb *codebaseApi.Codeba

rj := fmt.Sprintf("%v/job/Create-release-%v", cb.Spec.CodebaseName, cb.Spec.CodebaseName)

js, err := jc.GetJobStatus(rj, defaultTimeoutDuration, defaultRetryCount)
js, err := jc.GetJobStatus(rj, s.TimeoutDuration, defaultRetryCount)
if err != nil {
return fmt.Errorf("failed to fetch Jenkins job status: %w", err)
}
Expand Down
30 changes: 29 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"context"
"flag"
"fmt"
"os"
"strconv"
"time"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand Down Expand Up @@ -52,6 +54,8 @@ const (
codebaseOperatorLock = "edp-codebase-operator-lock"
codebaseBranchMaxConcurrentReconcilesEnv = "CODEBASE_BRANCH_MAX_CONCURRENT_RECONCILES"
logFailCtrlCreateMessage = "failed to create controller"

defaultCIRequestDelay = 5 * time.Second
)

func main() {
Expand Down Expand Up @@ -141,7 +145,17 @@ func main() {
os.Exit(1)
}

cbCtrl := codebasebranch.NewReconcileCodebaseBranch(mgr.GetClient(), mgr.GetScheme(), ctrlLog)
ciRequestDelay, err := getCIRequestDelay("CI_REQUEST_DELAY_SEC")
if err != nil {
setupLog.Error(err, "failed to read CI Request Delay from env vars", "controller", "codebase-branch")
setupLog.Info("Using default CI Request Delay")

ciRequestDelay = defaultCIRequestDelay
}

setupLog.Info("Request delay set", "CI Request delay", ciRequestDelay)

cbCtrl := codebasebranch.NewReconcileCodebaseBranch(mgr.GetClient(), mgr.GetScheme(), ctrlLog, ciRequestDelay)
if err = cbCtrl.SetupWithManager(mgr,
getMaxConcurrentReconciles(codebaseBranchMaxConcurrentReconcilesEnv)); err != nil {
setupLog.Error(err, logFailCtrlCreateMessage, "controller", "codebase-branch")
Expand Down Expand Up @@ -212,3 +226,17 @@ func getMaxConcurrentReconciles(envVar string) int {

return int(n)
}

func getCIRequestDelay(envVar string) (time.Duration, error) {
value, exists := os.LookupEnv(envVar)
if !exists {
return defaultCIRequestDelay, nil
}

intValue, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("failed to parse delay: %w", err)
}

return time.Duration(intValue) * time.Second, nil
}

0 comments on commit 0193999

Please sign in to comment.