Skip to content

Commit

Permalink
Add the image_tag_parameter on the service configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
psenderos committed Oct 14, 2024
1 parent 1ee1e92 commit 9e9b47c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 19 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ For each service you must provide the following values:
- `dockerfile`: (optional) the path to dockerfile to use for the service, relative to the `directory` of the service
- `scripts`: (optional) an object containing global scripts override. It is possibile to override only the bundle script
- `bundle` is ran right before the docker build, and receives in input the service name. It is executed instead of global bundle script. The path is relative to the service directory and it is executed in the service directory
- `image_tag_parameter`: (optional) it's possible to override the argo parameter for the image tag (default is `image.tag`)

If all your services use the same dockerfile, you can specify it inside the `docker` key, as `dockerfile`.
This must be a path relative to the root directory of your project.

### Complete configuration example

A complete configuration looks something like this:

```json
{
"default_environment": "dev",
Expand Down Expand Up @@ -131,6 +133,16 @@ A complete configuration looks something like this:
"directory": "./services/street-corners/",
"service_name": "street-corners",
"image_name": "street-corners"
},
"my-other-service": {
"directory": "./services/my-other-service/",
"service_name": "my-other-service",
"image_name": "my-other-service",
"dockerfile": "./my-other-service.Dockerfile",
"scripts": {
"bundle": "./my-other-service-bundle.sh"
},
"image_tag_parameter": "image.myservice.tag"
}
}
}
Expand Down
29 changes: 19 additions & 10 deletions argo/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ type resourceGroup string
type resourceKind string

const (
imageTagParameter parameterName = "image.tag"
defaultImageTag = "latest"
deploymentResource resourceKind = "Deployment"
statefulSetResource resourceKind = "StatefulSet"
appResourceGroup resourceGroup = "apps"
defaultImageTagParameter parameterName = "image.tag"
defaultImageTag = "latest"
deploymentResource resourceKind = "Deployment"
statefulSetResource resourceKind = "StatefulSet"
appResourceGroup resourceGroup = "apps"
)

type argoParameters struct {
Expand Down Expand Up @@ -60,7 +60,7 @@ type serviceInfo struct {
resourceKind resourceKind
}

func getServiceInfo(service config.ServiceName, env config.Environment) (*serviceInfo, error) {
func getServiceInfo(service config.ServiceName, env config.Environment, customImageTagParameter string) (*serviceInfo, error) {
cmd := exec.Command(
"argocd",
"--grpc-web",
Expand Down Expand Up @@ -93,6 +93,11 @@ func getServiceInfo(service config.ServiceName, env config.Environment) (*servic
parameters := response.Spec.Source.Helm.Parameters

imageTag := defaultImageTag
imageTagParameter := defaultImageTagParameter

if customImageTagParameter != "" {
imageTagParameter = parameterName(customImageTagParameter)
}

for _, param := range parameters {
if param.Name == imageTagParameter {
Expand Down Expand Up @@ -149,7 +154,11 @@ func restart(service config.ServiceName, env config.Environment, kind resourceKi
return nil
}

func deploy(service config.ServiceName, tag string, env config.Environment) error {
func deploy(service config.ServiceName, tag string, env config.Environment, customImageTagParameter string) error {
imageTagParameter := defaultImageTagParameter
if customImageTagParameter != "" {
imageTagParameter = parameterName(customImageTagParameter)
}
cmd := exec.Command(
"argocd",
"--grpc-web",
Expand All @@ -175,9 +184,9 @@ func deploy(service config.ServiceName, tag string, env config.Environment) erro
return nil
}

func Deploy(service config.ServiceName, tag string, env config.Environment) error {
func Deploy(service config.ServiceName, tag string, env config.Environment, imageTagParameter string) error {
log.Infof("retrieving current tag for service %s from argo...", service)
serviceInfo, err := getServiceInfo(service, env)
serviceInfo, err := getServiceInfo(service, env, imageTagParameter)
if err != nil {
return fmt.Errorf("failed to get current tag of service %s: %w", service, err)
}
Expand All @@ -193,7 +202,7 @@ func Deploy(service config.ServiceName, tag string, env config.Environment) erro
log.Infof("service %s restarted with tag %s", service, tag)
} else {
log.Infof("current tag and given tag differ, overriding...")
err = deploy(service, tag, env)
err = deploy(service, tag, env, imageTagParameter)
if err != nil {
return fmt.Errorf("deploy of service %s failed: %w", service, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var buildCmd = &cobra.Command{

if buildConfig.deploy {
for _, service := range services {
err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env)
err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env, config.GetImageTagParameter(service))
checkNoError(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/build_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var buildAllCmd = &cobra.Command{

if buildConfig.deploy {
for _, service := range config.GetAllServices() {
err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env)
err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env, config.GetImageTagParameter(service))
checkNoError(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var deployCmd = &cobra.Command{
continue
}
}
err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env)
err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env, config.GetImageTagParameter(service))
checkNoError(err)
}
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var deployAllCmd = &cobra.Command{
continue
}
}
err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env)
err = argo.Deploy(config.GetServiceName(service, baseConfig.env), actualTag, baseConfig.env, config.GetImageTagParameter(service))
checkNoError(err)
}
},
Expand Down
4 changes: 4 additions & 0 deletions config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func GetImageName(service Service) ImageName {
return Config.Services[service].ImageName
}

func GetImageTagParameter(service Service) string {
return Config.Services[service].ImageTagParameter
}

func GetAllServices() []Service {
res := make([]Service, 0, len(Config.Services))

Expand Down
11 changes: 6 additions & 5 deletions config/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ type ScriptsConfiguration struct {
}

type ServiceConfiguration struct {
Directory string `json:"directory"`
ServiceName ServiceName `json:"service_name"`
ImageName ImageName `json:"image_name"`
Scripts ScriptsConfiguration `json:"scripts"`
Dockerfile string `json:"dockerfile"`
Directory string `json:"directory"`
ServiceName ServiceName `json:"service_name"`
ImageName ImageName `json:"image_name"`
Scripts ScriptsConfiguration `json:"scripts"`
Dockerfile string `json:"dockerfile"`
ImageTagParameter string `json:"image_tag_parameter"`
}

type ArgoEnvironmentConfiguration struct {
Expand Down

0 comments on commit 9e9b47c

Please sign in to comment.