From 33280e0c6b39e0c4c26ebfd8156afc501206fa75 Mon Sep 17 00:00:00 2001 From: kr Date: Wed, 22 Jul 2020 09:23:22 +0800 Subject: [PATCH] feat(deployment): deploy based on configured task name (#7) --- client/client.go | 7 ++++--- client/nomad_client.go | 19 ++++++++++++------- client/setup.go | 2 +- config/sample.toml | 1 + config/test.toml | 1 + utils/helpers.go | 10 +++++++++- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/client/client.go b/client/client.go index ecff54b..08dc83a 100644 --- a/client/client.go +++ b/client/client.go @@ -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) { @@ -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) diff --git a/client/nomad_client.go b/client/nomad_client.go index 3a1b5f3..f975375 100644 --- a/client/nomad_client.go +++ b/client/nomad_client.go @@ -71,9 +71,11 @@ 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) @@ -81,10 +83,8 @@ func (client *NomadClient) UpdateNomadJobTag(jobID, imageName, desiredTag string } 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 @@ -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 @@ -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 diff --git a/client/setup.go b/client/setup.go index 163407b..e0165d3 100644 --- a/client/setup.go +++ b/client/setup.go @@ -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) diff --git a/config/sample.toml b/config/sample.toml index a07bc99..f1c9e0c 100644 --- a/config/sample.toml +++ b/config/sample.toml @@ -31,3 +31,4 @@ registry_auth = "$YOUR_AUTH_STRING_HERE" [repo_map.registrywatcher] registry_name = "codefresh" nomad_job_name = "registrywatcher" +nomad_task_name = "registrywatcher" diff --git a/config/test.toml b/config/test.toml index 4dd85d8..8eb50da 100644 --- a/config/test.toml +++ b/config/test.toml @@ -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", diff --git a/utils/helpers.go b/utils/helpers.go index e85cd55..1f7eb00 100644 --- a/utils/helpers.go +++ b/utils/helpers.go @@ -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"