-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: Move gitlab configuration to the separate chain handler (#225) #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
controllers/codebase/service/chain/put_gitlab_ci_config.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package chain | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1" | ||
| "github.com/epam/edp-codebase-operator/v2/pkg/git" | ||
| gitlabci "github.com/epam/edp-codebase-operator/v2/pkg/gitlab" | ||
| "github.com/epam/edp-codebase-operator/v2/pkg/util" | ||
| ) | ||
|
|
||
| type PutGitLabCIConfig struct { | ||
| client client.Client | ||
| git git.Git | ||
| gitlabCIManager gitlabci.Manager | ||
| } | ||
|
|
||
| func NewPutGitLabCIConfig(c client.Client, g git.Git, m gitlabci.Manager) *PutGitLabCIConfig { | ||
| return &PutGitLabCIConfig{client: c, git: g, gitlabCIManager: m} | ||
| } | ||
|
|
||
| func (h *PutGitLabCIConfig) ServeRequest(ctx context.Context, codebase *codebaseApi.Codebase) error { | ||
| log := ctrl.LoggerFrom(ctx) | ||
|
|
||
| // Skip if not GitLab CI | ||
| if codebase.Spec.CiTool != util.CIGitLab { | ||
| log.Info("Skip GitLab CI config injection, not using GitLab CI") | ||
| return nil | ||
| } | ||
|
|
||
| // Skip if already pushed (this stage or later) | ||
| if codebase.Status.Git == util.ProjectGitLabCIPushedStatus || | ||
| codebase.Status.Git == util.ProjectTemplatesPushedStatus { | ||
| log.Info("Skip GitLab CI config, already pushed in previous run") | ||
| return nil | ||
| } | ||
|
|
||
| // Skip if already exists | ||
| if h.gitlabCIAlreadyExists(codebase) { | ||
| log.Info("Skip GitLab CI config, already exists in repository") | ||
| return nil | ||
| } | ||
|
|
||
| log.Info("Start pushing GitLab CI config") | ||
|
|
||
| if err := h.tryToPushGitLabCIConfig(ctx, codebase); err != nil { | ||
| setFailedFields(codebase, codebaseApi.RepositoryProvisioning, err.Error()) | ||
| return fmt.Errorf("failed to push GitLab CI config for %v codebase: %w", codebase.Name, err) | ||
| } | ||
|
|
||
| log.Info("End pushing GitLab CI config") | ||
|
|
||
| // Set status to mark this stage complete | ||
| codebase.Status.Git = util.ProjectGitLabCIPushedStatus | ||
| if err := h.client.Status().Update(ctx, codebase); err != nil { | ||
| setFailedFields(codebase, codebaseApi.RepositoryProvisioning, err.Error()) | ||
| return fmt.Errorf("failed to set git status %s for codebase %s: %w", | ||
| util.ProjectGitLabCIPushedStatus, codebase.Name, err) | ||
| } | ||
|
|
||
| log.Info("GitLab CI status has been set successfully") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (h *PutGitLabCIConfig) gitlabCIAlreadyExists(codebase *codebaseApi.Codebase) bool { | ||
| wd := util.GetWorkDir(codebase.Name, codebase.Namespace) | ||
| gitlabCIPath := filepath.Join(wd, gitlabci.GitLabCIFileName) | ||
|
|
||
| // Check if file exists locally first | ||
| if _, err := os.Stat(gitlabCIPath); err == nil { | ||
| return true | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| func (h *PutGitLabCIConfig) tryToPushGitLabCIConfig(ctx context.Context, codebase *codebaseApi.Codebase) error { | ||
| log := ctrl.LoggerFrom(ctx) | ||
|
|
||
| gitServer := &codebaseApi.GitServer{} | ||
| if err := h.client.Get(ctx, client.ObjectKey{Name: codebase.Spec.GitServer, Namespace: codebase.Namespace}, gitServer); err != nil { | ||
| return fmt.Errorf("failed to get GitServer: %w", err) | ||
| } | ||
|
|
||
| gitServerSecret := &corev1.Secret{} | ||
| if err := h.client.Get(ctx, client.ObjectKey{Name: gitServer.Spec.NameSshKeySecret, Namespace: codebase.Namespace}, gitServerSecret); err != nil { | ||
| return fmt.Errorf("failed to get GitServer secret: %w", err) | ||
| } | ||
|
|
||
| privateSSHKey := string(gitServerSecret.Data[util.PrivateSShKeyName]) | ||
| repoSshUrl := util.GetSSHUrl(gitServer, codebase.Spec.GetProjectID()) | ||
| wd := util.GetWorkDir(codebase.Name, codebase.Namespace) | ||
|
|
||
| // Ensure we have the repository locally | ||
| if !util.DoesDirectoryExist(wd) || util.IsDirectoryEmpty(wd) { | ||
| log.Info("Start cloning repository", "url", repoSshUrl) | ||
|
|
||
| if err := h.git.CloneRepositoryBySsh(ctx, privateSSHKey, gitServer.Spec.GitUser, repoSshUrl, wd, gitServer.Spec.SshPort); err != nil { | ||
SergK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return fmt.Errorf("failed to clone git repository: %w", err) | ||
| } | ||
|
|
||
| log.Info("Repository has been cloned", "url", repoSshUrl) | ||
| } | ||
|
|
||
| ru, err := util.GetRepoUrl(codebase) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to build repo url: %w", err) | ||
| } | ||
|
|
||
| log.Info("Start checkout default branch", "branch", codebase.Spec.DefaultBranch, "repo", ru) | ||
|
|
||
| err = CheckoutBranch(ru, wd, codebase.Spec.DefaultBranch, h.git, codebase, h.client) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to checkout default branch %v: %w", codebase.Spec.DefaultBranch, err) | ||
| } | ||
|
|
||
| log.Info("Default branch has been checked out") | ||
| log.Info("Start injecting GitLab CI config") | ||
|
|
||
| // Inject the GitLab CI configuration | ||
| err = h.gitlabCIManager.InjectGitLabCIConfig(ctx, codebase, wd) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to inject GitLab CI config: %w", err) | ||
| } | ||
|
|
||
| log.Info("GitLab CI config has been injected") | ||
| log.Info("Start committing changes") | ||
|
|
||
| err = h.git.CommitChanges(wd, "Add GitLab CI configuration") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to commit changes: %w", err) | ||
| } | ||
|
|
||
| log.Info("Changes have been committed") | ||
| log.Info("Start pushing changes") | ||
|
|
||
| err = h.git.PushChanges(privateSSHKey, gitServer.Spec.GitUser, wd, gitServer.Spec.SshPort, "--all") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to push changes: %w", err) | ||
| } | ||
|
|
||
| log.Info("GitLab CI config has been pushed successfully") | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.