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
83 changes: 32 additions & 51 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,71 +1,52 @@
version: "1"
version: "2"
run:
timeout: 5m
allow-parallel-runners: true
issues:
# don't skip warning about doc comments
# don't exclude the default set of lint
exclude-use-default: false
# restore some of the defaults
# (fill in the rest as needed)
exclude-rules:
- path: "api/*"
linters:
- lll
- path: "internal/*"
linters:
- dupl
- lll
- path: ".*_test\\.go$"
linters:
- errcheck
linters:
disable-all: true
default: none
enable:
- copyloopvar
- dupl
- errcheck
- gci
- ginkgolinter
- goconst
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- prealloc
- revive
- staticcheck
- typecheck
- unconvert
- unparam
- unused
- wsl

linters-settings:
revive:
- wsl_v5
settings:
revive:
rules:
- name: comment-spacings
- name: import-shadowing
exclusions:
generated: lax
rules:
- name: comment-spacings
gci:
# Section configuration to compare against.
# Section names are case-insensitive and may contain parameters in ().
# The default order of sections is `standard > default > custom > blank > dot`,
# If `custom-order` is `true`, it follows the order of `sections` option.
# Default: ["standard", "default"]
sections:
- standard # Standard section: captures all standard packages.
- blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled.
- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/epam) # Custom section: groups all imports with the specified Prefix.
- prefix(github.com/epam/edp-codebase-operator) # Custom section: groups all imports with the specified Prefix.

# Skip generated files.
skip-generated: true

# Enable custom order of sections.
# If `true`, make the section order the same as the order of `sections`.
# Default: false
custom-order: true
- linters:
- lll
path: api/*
- linters:
- lll
source: "\\+kubebuilder"
paths:
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ vet: ## Run go vet

.PHONY: lint
lint: golangci-lint ## Run go lint
${GOLANGCI_LINT} run -v -c .golangci.yaml ./...
${GOLANGCI_LINT} run -v ./...

.PHONY: lint
.PHONY: lint-fix
lint-fix: golangci-lint ## Run go lint fix
${GOLANGCI_LINT} run -v -c .golangci.yaml ./... --fix
${GOLANGCI_LINT} run -v ./... --fix

.PHONY: build
build: ## build operator's binary
Expand Down Expand Up @@ -133,7 +133,7 @@ helm-docs: helmdocs ## generate helm docs
GOLANGCI_LINT = ${CURRENT_DIR}/bin/golangci-lint
.PHONY: golangci-lint
golangci-lint: ## Download golangci-lint locally if necessary.
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
Expand All @@ -146,7 +146,7 @@ KUSTOMIZE_VERSION ?= v5.6.0
CONTROLLER_TOOLS_VERSION ?= v0.18.0
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
GOLANGCI_LINT_VERSION ?= v1.64.7
GOLANGCI_LINT_VERSION ?= v2.8.0
MOCKERY_VERSION ?= v3.6.2
HELMDOCS_VERSION ?= v1.14.2
GITCHGLOG_VERSION ?= v0.15.4
Expand Down
28 changes: 24 additions & 4 deletions controllers/cdstagedeploy/chain/process_trigger_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ func NewProcessTriggerTemplate(
}

func (h *ProcessTriggerTemplate) ServeRequest(ctx context.Context, stageDeploy *codebaseApi.CDStageDeploy) error {
log := ctrl.LoggerFrom(ctx).WithValues("stage", stageDeploy.Spec.Stage, "pipeline", stageDeploy.Spec.Pipeline, "status", stageDeploy.Status.Status)
log := ctrl.LoggerFrom(ctx).WithValues(
"stage",
stageDeploy.Spec.Stage,
"pipeline",
stageDeploy.Spec.Pipeline,
"status",
stageDeploy.Status.Status,
)

if skipPipelineRunCreation(stageDeploy) {
log.Info("Skip processing TriggerTemplate for auto-deploy.")
Expand All @@ -45,10 +52,19 @@ func (h *ProcessTriggerTemplate) ServeRequest(ctx context.Context, stageDeploy *

log.Info("Start processing TriggerTemplate for auto-deploy.")

pipeline, stage, rawResource, err := getResourcesForPipelineRun(ctx, stageDeploy, h.k8sClient, h.triggerTemplateManager)
pipeline, stage, rawResource, err := getResourcesForPipelineRun(
ctx,
stageDeploy,
h.k8sClient,
h.triggerTemplateManager,
)
if err != nil {
if errors.Is(err, tektoncd.ErrEmptyTriggerTemplateResources) {
log.Info("No resource templates found in the trigger template. Skip processing.", "triggertemplate", stage.Spec.TriggerTemplate)
log.Info(
"No resource templates found in the trigger template. Skip processing.",
"triggertemplate",
stage.Spec.TriggerTemplate,
)

stageDeploy.Status.Status = codebaseApi.CDStageDeployStatusCompleted

Expand Down Expand Up @@ -150,7 +166,11 @@ func getResourcesForPipelineRun(
return pipeline, nil, nil, fmt.Errorf("failed to get Stage: %w", err)
}

rawResource, err := triggerTemplateManager.GetRawResourceFromTriggerTemplate(ctx, stage.Spec.TriggerTemplate, stageDeploy.Namespace)
rawResource, err := triggerTemplateManager.GetRawResourceFromTriggerTemplate(
ctx,
stage.Spec.TriggerTemplate,
stageDeploy.Namespace,
)
if err != nil {
return pipeline, stage, nil, fmt.Errorf("failed to get raw resource from TriggerTemplate: %w", err)
}
Expand Down
5 changes: 4 additions & 1 deletion controllers/cdstagedeploy/chain/resolve_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func (r *ResolveStatus) ServeRequest(ctx context.Context, stageDeploy *codebaseA
return nil
}

func (r *ResolveStatus) getRunningPipelines(ctx context.Context, stageDeploy *codebaseApi.CDStageDeploy) (*tektonpipelineApi.PipelineRunList, error) {
func (r *ResolveStatus) getRunningPipelines(
ctx context.Context,
stageDeploy *codebaseApi.CDStageDeploy,
) (*tektonpipelineApi.PipelineRunList, error) {
log := ctrl.LoggerFrom(ctx)

pipelineRun := &tektonpipelineApi.PipelineRunList{}
Expand Down
11 changes: 9 additions & 2 deletions controllers/codebase/codebase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func (r *ReconcileCodebase) updateFinishStatus(ctx context.Context, c *codebaseA
// setFailureCount increments failure count and returns delay for next reconciliation.
func (r *ReconcileCodebase) setFailureCount(ctx context.Context, codebase *codebaseApi.Codebase) time.Duration {
const defaultTimeout = 10 * time.Second

timeout := util.GetTimeout(codebase.Status.FailureCount, defaultTimeout)

codebase.Status.FailureCount++
Expand All @@ -205,7 +206,10 @@ func (r *ReconcileCodebase) setFailureCount(ctx context.Context, codebase *codeb
return timeout
}

func (r *ReconcileCodebase) getChain(ctx context.Context, codebase *codebaseApi.Codebase) (cHand.CodebaseHandler, error) {
func (r *ReconcileCodebase) getChain(
ctx context.Context,
codebase *codebaseApi.Codebase,
) (cHand.CodebaseHandler, error) {
if r.chainGetter == nil {
r.chainGetter = func(cr *codebaseApi.Codebase) (cHand.CodebaseHandler, error) {
return chain.MakeChain(ctx, r.client), nil
Expand All @@ -215,7 +219,10 @@ func (r *ReconcileCodebase) getChain(ctx context.Context, codebase *codebaseApi.
return r.chainGetter(codebase)
}

func (r *ReconcileCodebase) tryToDeleteCodebase(ctx context.Context, codebase *codebaseApi.Codebase) (*reconcile.Result, error) {
func (r *ReconcileCodebase) tryToDeleteCodebase(
ctx context.Context,
codebase *codebaseApi.Codebase,
) (*reconcile.Result, error) {
log := ctrl.LoggerFrom(ctx)

if codebase.GetDeletionTimestamp().IsZero() {
Expand Down
7 changes: 6 additions & 1 deletion controllers/codebase/service/chain/checkout_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ func CheckoutBranch(
gitProviderFactory func(config gitproviderv2.Config) gitproviderv2.Git,
) error {
log := ctrl.LoggerFrom(ctx)
gitProvider := gitProviderFactory(gitproviderv2.NewConfigFromGitServerAndSecret(repoContext.GitServer, repoContext.GitServerSecret))
gitProvider := gitProviderFactory(
gitproviderv2.NewConfigFromGitServerAndSecret(
repoContext.GitServer,
repoContext.GitServerSecret,
),
)

currentBranchName, err := gitProvider.GetCurrentBranchName(ctx, repoContext.WorkDir)
if err != nil {
Expand Down
15 changes: 13 additions & 2 deletions controllers/codebase/service/chain/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ type GitRepositoryContext struct {
WorkDir string
}

func setIntermediateSuccessFields(ctx context.Context, c client.Client, cb *codebaseApi.Codebase, action codebaseApi.ActionType) error {
func setIntermediateSuccessFields(
ctx context.Context,
c client.Client,
cb *codebaseApi.Codebase,
action codebaseApi.ActionType,
) error {
// Set WebHookRef from WebHookID for backward compatibility.
webHookRef := cb.Status.WebHookRef
if webHookRef == "" && cb.Status.WebHookID != 0 {
Expand Down Expand Up @@ -81,6 +86,7 @@ func updateGitStatusWithPatch(
// Apply patch to status subresource
if err := c.Status().Patch(ctx, codebase, patch); err != nil {
setFailedFields(codebase, action, err.Error())

return fmt.Errorf("failed to patch git status to %s for codebase %s: %w",
gitStatus, codebase.Name, err)
}
Expand Down Expand Up @@ -113,7 +119,12 @@ func PrepareGitRepository(
return nil, err
}

gitProvider := gitProviderFactory(gitproviderv2.NewConfigFromGitServerAndSecret(gitRepoCtx.GitServer, gitRepoCtx.GitServerSecret))
gitProvider := gitProviderFactory(
gitproviderv2.NewConfigFromGitServerAndSecret(
gitRepoCtx.GitServer,
gitRepoCtx.GitServerSecret,
),
)

if !util.DoesDirectoryExist(gitRepoCtx.WorkDir) || util.IsDirectoryEmpty(gitRepoCtx.WorkDir) {
log.Info("Start cloning repository", "url", gitRepoCtx.RepoGitUrl)
Expand Down
3 changes: 3 additions & 0 deletions controllers/codebase/service/chain/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ func TestPrepareGitRepository(t *testing.T) {
m := gitMocks.NewMockGit(t)
m.On("Clone", testify.Anything, testify.Anything, testify.Anything).Return(nil)
m.On("GetCurrentBranchName", testify.Anything, testify.Anything).Return("main", nil)

return m
},
wantErr: require.NoError,
Expand Down Expand Up @@ -388,6 +389,7 @@ func TestPrepareGitRepository(t *testing.T) {
m := gitMocks.NewMockGit(t)
m.On("Clone", testify.Anything, testify.Anything, testify.Anything).
Return(assert.AnError)

return m
},
wantErr: func(t require.TestingT, err error, i ...interface{}) {
Expand Down Expand Up @@ -432,6 +434,7 @@ func TestPrepareGitRepository(t *testing.T) {
m.On("Clone", testify.Anything, testify.Anything, testify.Anything).Return(nil)
m.On("GetCurrentBranchName", testify.Anything, testify.Anything).
Return("", errors.New("failed to get current branch"))

return m
},
wantErr: func(t require.TestingT, err error, i ...interface{}) {
Expand Down
12 changes: 10 additions & 2 deletions controllers/codebase/service/chain/delete_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func (s *DeleteWebHook) ServeRequest(ctx context.Context, codebase *codebaseApi.
}

gitServer := &codebaseApi.GitServer{}
if err := s.client.Get(ctx, client.ObjectKey{Name: codebase.Spec.GitServer, Namespace: codebase.Namespace}, gitServer); err != nil {
if err := s.client.Get(
ctx,
client.ObjectKey{Name: codebase.Spec.GitServer, Namespace: codebase.Namespace},
gitServer,
); err != nil {
log.Error(err, "Failed to delete webhook: unable to get GitServer", "gitServer", codebase.Spec.GitServer)

return nil
Expand All @@ -59,7 +63,11 @@ func (s *DeleteWebHook) ServeRequest(ctx context.Context, codebase *codebaseApi.
return nil
}

gitProvider, err := gitprovider.NewProvider(gitServer, s.restyClient, string(secret.Data[util.GitServerSecretTokenField]))
gitProvider, err := gitprovider.NewProvider(
gitServer,
s.restyClient,
string(secret.Data[util.GitServerSecretTokenField]),
)
if err != nil {
log.Error(err, "Failed to delete webhook: unable to create git provider")

Expand Down
Loading