Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into weekly-summary
Browse files Browse the repository at this point in the history
  • Loading branch information
dzsak committed Dec 7, 2023
2 parents 80bed82 + d5921b1 commit ba5c4b8
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 186 deletions.
56 changes: 21 additions & 35 deletions cmd/agent/imagebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func dockerfileImageBuild(
}

var pods *corev1.PodList
err = wait.PollImmediate(100*time.Millisecond, 20*time.Second, func() (done bool, err error) {
err = wait.PollImmediate(1*time.Second, 20*time.Second, func() (done bool, err error) {
pods, err = kubeEnv.Client.CoreV1().Pods("infrastructure").List(context.TODO(), meta_v1.ListOptions{
LabelSelector: fmt.Sprintf("job-name=%s", jobName),
})
Expand All @@ -225,35 +225,26 @@ func dockerfileImageBuild(
return false, nil
}

if pods.Items[0].Status.Phase == corev1.PodFailed {
return false, fmt.Errorf("pod failed")
for _, containerStatus := range pods.Items[0].Status.ContainerStatuses {
if containerStatus.State.Waiting != nil {
streamImageBuildEvent(messages, trigger.TriggeredBy, buildId, "running", fmt.Sprintf("%s: %s\n", pods.Items[0].Name, containerStatus.State.Waiting.Reason))
}
}

if pods.Items[0].Status.Phase != corev1.PodRunning {
if pods.Items[0].Status.Phase == corev1.PodPending {
return false, nil
}
return true, nil
})
if err != nil {
logrus.Errorf("poll: %s", err)
streamImageBuildEvent(messages, trigger.TriggeredBy, buildId, "error", err.Error())
logrus.Errorf("cannot get pods: %s", err)
streamImageBuildEvent(messages, trigger.TriggeredBy, buildId, "error", "")
return
}

pod := pods.Items[0]
done := streamLogs(kubeEnv, messages, pod.Name, pod.Spec.InitContainers[0].Name, trigger.TriggeredBy, buildId)
if !done {
streamImageBuildEvent(messages, trigger.TriggeredBy, buildId, "notBuilt", "")
return
}

done = streamLogs(kubeEnv, messages, pod.Name, pod.Spec.Containers[0].Name, trigger.TriggeredBy, buildId)

if done {
streamImageBuildEvent(messages, trigger.TriggeredBy, buildId, "success", "")
} else {
streamImageBuildEvent(messages, trigger.TriggeredBy, buildId, "notBuilt", "")
}
streamLogs(kubeEnv, messages, pod.Name, pod.Spec.InitContainers[0].Name, trigger.TriggeredBy, buildId)
streamLogs(kubeEnv, messages, pod.Name, pod.Spec.Containers[0].Name, trigger.TriggeredBy, buildId)
}

func generateJob(trigger dx.ImageBuildRequest, name, sourceUrl string) *batchv1.Job {
Expand Down Expand Up @@ -331,7 +322,7 @@ func streamLogs(kubeEnv *agent.KubeEnv,
messages chan *streaming.WSMessage,
pod, container string,
userLogin, imageBuildId string,
) bool {
) {
count := int64(100)
logsReq := kubeEnv.Client.CoreV1().Pods("infrastructure").GetLogs(pod, &corev1.PodLogOptions{
Container: container,
Expand All @@ -343,7 +334,7 @@ func streamLogs(kubeEnv *agent.KubeEnv,
if err != nil {
logrus.Errorf("could not stream pod logs: %v", err)
streamImageBuildEvent(messages, userLogin, imageBuildId, "error", "")
return false
return
}
defer podLogs.Close()

Expand All @@ -358,26 +349,21 @@ func streamLogs(kubeEnv *agent.KubeEnv,
}

logrus.Errorf("cannot stream build logs: %s", err)
streamImageBuildEvent(messages, userLogin, imageBuildId, "error", sb.String())
return false
streamImageBuildEvent(messages, userLogin, imageBuildId, "error", "")
break
}

// if first || sb.Len() > 300 {
streamImageBuildEvent(messages, userLogin, imageBuildId, "running", sb.String())
sb.Reset()
// first = false
// }

if strings.Contains(sb.String(), "error") {
streamImageBuildEvent(messages, userLogin, imageBuildId, "error", sb.String())
return false
if strings.Contains(sb.String(), "Error") {
streamImageBuildEvent(messages, userLogin, imageBuildId, "notBuilt", sb.String())
break
}

if strings.Contains(sb.String(), "pushed") {
if strings.Contains(sb.String(), "Pushed") {
streamImageBuildEvent(messages, userLogin, imageBuildId, "success", sb.String())
break
}
}

return true
streamImageBuildEvent(messages, userLogin, imageBuildId, "running", sb.String())
sb.Reset()
}
}
76 changes: 43 additions & 33 deletions pkg/dashboard/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,48 @@ func envs(w http.ResponseWriter, r *http.Request) {

envs := []*api.GitopsEnv{}
for _, env := range envsFromDB {
repo, err := gitRepoCache.InstanceForRead(env.InfraRepo)
if err != nil {
if strings.Contains(err.Error(), "repository not found") ||
strings.Contains(err.Error(), "repo name is mandatory") {
envs = append(envs, &api.GitopsEnv{
Name: env.Name,
})
continue
} else {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}

var stackConfig *dx.StackConfig
if env.RepoPerEnv {
stackConfig, err = stackYaml(repo, "stack.yaml")
if err != nil && !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
gitRepoCache.PerformAction(env.InfraRepo, func(repo *git.Repository) {
stackConfig, err = stackYaml(repo, "stack.yaml")
})
if err != nil {
if strings.Contains(err.Error(), "repository not found") ||
strings.Contains(err.Error(), "repo name is mandatory") {
envs = append(envs, &api.GitopsEnv{
Name: env.Name,
})
continue
} else if !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
} else {
stackConfig, err = stackYaml(repo, filepath.Join(env.Name, "stack.yaml"))
if err != nil && !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from %s repo for env %s: %s", env.InfraRepo, env.Name, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
gitRepoCache.PerformAction(env.InfraRepo, func(repo *git.Repository) {
stackConfig, err = stackYaml(repo, filepath.Join(env.Name, "stack.yaml"))
})
if err != nil {
if strings.Contains(err.Error(), "repository not found") ||
strings.Contains(err.Error(), "repo name is mandatory") {
envs = append(envs, &api.GitopsEnv{
Name: env.Name,
})
continue
} else if !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from %s repo for env %s: %s", env.InfraRepo, env.Name, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
}

Expand Down Expand Up @@ -362,14 +375,11 @@ func deploymentTemplateForApp(w http.ResponseWriter, r *http.Request) {
installationToken, _, _ := tokenManager.Token()
gitRepoCache, _ := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)

repo, err := gitRepoCache.InstanceForRead(fmt.Sprintf("%s/%s", owner, repoName))
if err != nil {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

appChart, err := getChartForApp(repo, env, configName)
var appChart *dx.Chart
var err error
gitRepoCache.PerformAction(fmt.Sprintf("%s/%s", owner, repoName), func(repo *git.Repository) {
appChart, err = getChartForApp(repo, env, configName)
})
if err != nil {
logrus.Errorf("cannot get manifest chart: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down
28 changes: 16 additions & 12 deletions pkg/dashboard/server/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,32 @@ func commits(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
gitRepoCache, _ := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)

repo, err := gitRepoCache.InstanceForRead(repoName)
if err != nil {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

var err error
if branch == "" {
branch, err = helper.HeadBranch(repo)
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
branch, err = helper.HeadBranch(repo)
})
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
logrus.Errorf("cannot get head branch: %s", err)
return
}
}

hash := helper.BranchHeadHash(repo, branch)
var hash plumbing.Hash
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
hash = helper.BranchHeadHash(repo, branch)
})

if hashString != "head" {
hash = plumbing.NewHash(hashString)
}

commitWalker, err := repo.Log(&git.LogOptions{
From: hash,
var commitWalker object.CommitIter
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
commitWalker, err = repo.Log(&git.LogOptions{
From: hash,
})
})
if err != nil {
logrus.Errorf("cannot walk commits: %s", err)
Expand Down Expand Up @@ -98,7 +100,9 @@ func commits(w http.ResponseWriter, r *http.Request) {
return
}

commits, err = decorateCommitsWithGimletArtifacts(commits, dao, repo, owner, repoName)
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
commits, err = decorateCommitsWithGimletArtifacts(commits, dao, repo, owner, repoName)
})
if err != nil {
logrus.Warnf("cannot get deplyotargets: %s", err)
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/dashboard/server/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,12 @@ func updateGitopsCommitStatuses(
return err
}

repo, err := gitopsRepoCache.InstanceForRead(repoName)
if err != nil {
return err
}

var commitWalker object.CommitIter
hash := plumbing.NewHash(eventHash)
commitWalker, err := repo.Log(&git.LogOptions{
From: hash,
gitopsRepoCache.PerformAction(repoName, func(repo *git.Repository) {
commitWalker, err = repo.Log(&git.LogOptions{
From: hash,
})
})
if err != nil {
return err
Expand Down
Loading

0 comments on commit ba5c4b8

Please sign in to comment.