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
7 changes: 7 additions & 0 deletions api/v1/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v1

const (
// GitLabCITemplateAnnotation is an annotation on a Codebase CR that specifies the ConfigMap
// name to use as the GitLab CI template. When absent, the operator falls back to "gitlab-ci-default".
GitLabCITemplateAnnotation = "app.edp.epam.com/gitlab-ci-template"
)
5 changes: 5 additions & 0 deletions api/v1/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ const (
// We can't use the branch name directly as a label value because it can contain special characters.
// XXH64 is used to generate the hash.
BranchHashLabel = "app.edp.epam.com/branch-hash"

// CITemplateLabel is a label on ConfigMaps that marks them as CI templates.
// The portal uses this label to discover available templates for a dropdown.
// Values: "gitlab" (extensible to other CI systems in the future).
CITemplateLabel = "app.edp.epam.com/ci-template"
)
5 changes: 5 additions & 0 deletions controllers/codebase/service/chain/put_gitlab_ci_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func (h *PutGitLabCIConfig) ServeRequest(ctx context.Context, codebase *codebase
return nil
}

// gitlabCIAlreadyExists is a fast-path optimization that checks whether
// .gitlab-ci.yml already exists in the local working directory, avoiding the
// expensive clone+checkout round-trip when the file was written by a prior
// reconciliation run. The manager's InjectGitLabCIConfig has its own os.Stat
// guard as a safety invariant, so this check is purely an optimization.
func (h *PutGitLabCIConfig) gitlabCIAlreadyExists(codebase *codebaseApi.Codebase) bool {
wd := util.GetWorkDir(codebase.Name, codebase.Namespace)
gitlabCIPath := filepath.Join(wd, gitlabci.GitLabCIFileName)
Expand Down
77 changes: 68 additions & 9 deletions controllers/codebase/service/chain/put_gitlab_ci_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"github.com/epam/edp-codebase-operator/v2/pkg/util"
)

const testGitLabCIConfigData = "variables:\n CODEBASE_NAME: \"{{.CodebaseName}}\"\ninclude:\n " +
"- component: $CI_SERVER_FQDN/kuberocketci/ci-java17-mvn/build@0.1.1"

func TestPutGitLabCIConfig_ServeRequest(t *testing.T) {
const defaultNs = "default"

Expand Down Expand Up @@ -134,12 +137,70 @@ func TestPutGitLabCIConfig_ServeRequest(t *testing.T) {
gitlabGitServerSecret,
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "gitlab-ci-java-maven",
Name: gitlabci.GitLabCIDefaultTemplate,
Namespace: defaultNs,
},
Data: map[string]string{
".gitlab-ci.yml": testGitLabCIConfigData,
},
},
},
gitClient: func(t *testing.T) gitproviderv2.Git {
mock := gitmocks.NewMockGit(t)

mock.On("Clone", testify.Anything, testify.Anything, testify.Anything).
Return(nil)
mock.On("GetCurrentBranchName", testify.Anything, testify.Anything).
Return("master", nil)
mock.On("Commit", testify.Anything, testify.Anything, "Add GitLab CI configuration").
Return(nil)
mock.On("Push", testify.Anything, testify.Anything, gitproviderv2.RefSpecPushAllBranches).
Return(nil)

return mock
},
setup: func(t *testing.T, wd string) {
require.NoError(t, os.MkdirAll(wd, 0755))
},
wantErr: require.NoError,
wantStatus: func(t *testing.T, codebase *codebaseApi.Codebase) {
require.Equal(t, util.ProjectGitLabCIPushedStatus, codebase.Status.Git)
},
},
{
name: "successfully inject GitLab CI config with annotation",
codebase: &codebaseApi.Codebase{
ObjectMeta: metav1.ObjectMeta{
Name: "java-app",
Namespace: defaultNs,
Annotations: map[string]string{
codebaseApi.GitLabCITemplateAnnotation: "my-custom-template",
},
},
Spec: codebaseApi.CodebaseSpec{
Strategy: codebaseApi.Clone,
CiTool: util.CIGitLab,
GitServer: gitlabGitServer.Name,
GitUrlPath: "/owner/java-repo",
Repository: &codebaseApi.Repository{Url: "https://gitlab.com/owner/java-repo.git"},
DefaultBranch: "master",
Lang: "java",
BuildTool: "maven",
},
Status: codebaseApi.CodebaseStatus{
Git: util.ProjectPushedStatus,
},
},
objects: []client.Object{
gitlabGitServer,
gitlabGitServerSecret,
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "my-custom-template",
Namespace: defaultNs,
},
Data: map[string]string{
".gitlab-ci.yml": "variables:\n CODEBASE_NAME: \"{{.CodebaseName}}\"\ninclude:\n " +
"- component: $CI_SERVER_FQDN/kuberocketci/ci-java17-mvn/build@0.1.1",
".gitlab-ci.yml": "custom: {{.CodebaseName}}",
},
},
},
Expand Down Expand Up @@ -398,12 +459,11 @@ func TestPutGitLabCIConfig_ServeRequest(t *testing.T) {
gitlabGitServerSecret,
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "gitlab-ci-java-maven",
Name: gitlabci.GitLabCIDefaultTemplate,
Namespace: defaultNs,
},
Data: map[string]string{
".gitlab-ci.yml": "variables:\n CODEBASE_NAME: \"{{.CodebaseName}}\"\ninclude:\n " +
"- component: $CI_SERVER_FQDN/kuberocketci/ci-java17-mvn/build@0.1.1",
".gitlab-ci.yml": testGitLabCIConfigData,
},
},
},
Expand Down Expand Up @@ -452,12 +512,11 @@ func TestPutGitLabCIConfig_ServeRequest(t *testing.T) {
gitlabGitServerSecret,
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "gitlab-ci-java-maven",
Name: gitlabci.GitLabCIDefaultTemplate,
Namespace: defaultNs,
},
Data: map[string]string{
".gitlab-ci.yml": "variables:\n CODEBASE_NAME: \"{{.CodebaseName}}\"\ninclude:\n " +
"- component: $CI_SERVER_FQDN/kuberocketci/ci-java17-mvn/build@0.1.1",
".gitlab-ci.yml": testGitLabCIConfigData,
},
},
},
Expand Down
Loading