Skip to content

Commit

Permalink
feat(deployment): deploy based on configured task name (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrntr committed Jul 22, 2020
1 parent b0a46ad commit 33280e0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
7 changes: 4 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ func (client *Clients) DeployPinnedTag(conf *viper.Viper, repoName string) {
log.LogAppErr(fmt.Sprintf("Couldn't fetch pinned tag while deploying pinned tag for %s", repoName), err)
return
}
jobID := utils.GetJobOfRepo(conf, repoName)
client.NomadClient.UpdateNomadJobTag(jobID, repoName, pinnedTag)
jobID := utils.GetRepoNomadJob(conf, repoName)
taskName := utils.GetRepoNomadTaskName(conf, repoName)
client.NomadClient.UpdateNomadJobTag(jobID, repoName, taskName, pinnedTag)
}

func (client *Clients) PopulateCaches(repoName string) {
Expand Down Expand Up @@ -228,7 +229,7 @@ func (client *Clients) updateDigestCache(repoName string, digest string) {
}

func (client *Clients) isPinnedTagDeployed(conf *viper.Viper, repoName string) (bool, error) {
jobID := utils.GetJobOfRepo(conf, repoName)
jobID := utils.GetRepoNomadJob(conf, repoName)
deployedTag, err := client.NomadClient.GetNomadJobTag(jobID, repoName)
if err != nil {
log.LogAppErr(fmt.Sprintf("Couldn't fetch nomad job tag while checking deployed tag for %s", repoName), err)
Expand Down
19 changes: 12 additions & 7 deletions client/nomad_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ func (client *NomadClient) GetNomadJobTag(jobID, imageName string) (string, erro
// Updates one image in a Nomad job, unless the Nomad jobspec is registrywatcher itself.
// Since the registrywatcher Nomad jobspec contains 2 images (UI and backend), it will update
// both images before it restarts itself.
func (client *NomadClient) UpdateNomadJobTag(jobID, imageName, desiredTag string) {
func (client *NomadClient) UpdateNomadJobTag(jobID, imageName, taskName, desiredTag string) {
_, registryDomain, registryPrefix, _ := utils.ExtractRegistryInfo(client.conf, imageName)
desiredFullImageName := utils.ConstructImageName(registryDomain, registryPrefix, imageName, desiredTag)
matchFound := false
log.LogAppInfo(fmt.Sprintf("Full image name to deploy %s", desiredFullImageName))
job, err := client.getNomadJob(jobID)
if err != nil {
log.LogAppErr(fmt.Sprintf("Couldn't find jobID %s", jobID), err)
return
}
for i, taskGroup := range job.TaskGroups {
for j, task := range taskGroup.Tasks {
fullImageName := getNomadJobImageFromTask(task)
arr := strings.Split(fullImageName, "/")
taskImageName := arr[len(arr)-1]
if taskImageName == imageName {
if task.Name == taskName {
matchFound = true
// to avoid unexpected issues we use the prefix from config
// its possible that what is deployed may be from a different registry
job.TaskGroups[i].Tasks[j].Config["image"] = desiredFullImageName
Expand All @@ -94,7 +94,8 @@ func (client *NomadClient) UpdateNomadJobTag(jobID, imageName, desiredTag string
}
// bootstrapping. the nomad job "registrywatcher" also contains
// a task/container of the ui image.
if taskImageName == "registrywatcher-ui" {
if taskName == "registrywatcher-ui" {
matchFound = true
uiFullImageName := utils.ConstructImageName(
registryDomain, registryPrefix, "registrywatcher-ui", desiredTag)
job.TaskGroups[i].Tasks[j].Config["image"] = uiFullImageName
Expand All @@ -108,7 +109,11 @@ func (client *NomadClient) UpdateNomadJobTag(jobID, imageName, desiredTag string
job.VaultToken = &vaultToken
}

go client.RestartNomadJob(&job, desiredTag)
if matchFound {
go client.RestartNomadJob(&job, desiredTag)
} else {
utils.PostSlackError(client.conf, fmt.Sprintf("Mapped task name %s not found in Nomad job %s. Please check deployment configuration.", taskName, *job.ID))
}
}

// Modify a flag that should not affect the operation of a Nomad jobspec
Expand Down
2 changes: 1 addition & 1 deletion client/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func SetUpClientTest(t *testing.T) *testEngine {
}

func (te *testEngine) RegisterJob() {
jobID := utils.GetJobOfRepo(te.Conf, te.TestRepoName)
jobID := utils.GetRepoNomadJob(te.Conf, te.TestRepoName)
tags, _ := te.Clients.DockerRegistryClient.GetAllTags(te.TestRepoName)
dockerImage := fmt.Sprintf("%s:%s", te.TestRepoName, tags[0])
job := testJob(jobID, dockerImage)
Expand Down
1 change: 1 addition & 0 deletions config/sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ registry_auth = "$YOUR_AUTH_STRING_HERE"
[repo_map.registrywatcher]
registry_name = "codefresh"
nomad_job_name = "registrywatcher"
nomad_task_name = "registrywatcher"
1 change: 1 addition & 0 deletions config/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ registry_auth = "YmxhaDpibGFoCg=="
[repo_map.testrepo]
registry_name = "localregistry"
nomad_job_name = "testrepo"
nomad_task_name = "testrepo"

# config: "basicauth.yml",
# username: "admin",
Expand Down
10 changes: 9 additions & 1 deletion utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,20 @@ func ConstructImageName(domain, prefix, repoName, tag string) string {
)
}

func GetJobOfRepo(conf *viper.Viper, repoName string) string {
// Get the Nomad job name config mapping for repoName
func GetRepoNomadJob(conf *viper.Viper, repoName string) string {
repoMap := conf.Get("repo_map").(map[string]interface{})
jobID := repoMap[repoName].(map[string]interface{})["nomad_job_name"].(string)
return jobID
}

// Get the Nomad task name config mapping for repoName
func GetRepoNomadTaskName(conf *viper.Viper, repoName string) string {
repoMap := conf.Get("repo_map").(map[string]interface{})
jobID := repoMap[repoName].(map[string]interface{})["nomad_task_name"].(string)
return jobID
}

const (
green = "#00FF00"
red = "#FF0000"
Expand Down

0 comments on commit 33280e0

Please sign in to comment.