refactor: replace some functions with general getLabel function

This commit is contained in:
Moritz 2023-01-31 14:19:32 +01:00
parent 6e2f05bdab
commit 1516d343d8
1 changed files with 18 additions and 51 deletions

View File

@ -75,9 +75,9 @@ var UpgradeAll = cli.Command{
}
for _, stackInfo := range stacks {
stackName := stackInfo.Name
recipeName := getRecipe(cl, stackName)
chaos := getChaos(cl, stackName)
updatesEnabled := getUpdateActivation(cl, stackName)
recipeName := getLabel(cl, stackName, "recipe")
chaos := getBoolLabel(cl, stackName, "chaos")
updatesEnabled := getBoolLabel(cl, stackName, "autoupdate")
if recipeName != "" && updatesEnabled && (!chaos || internal.Force) {
upgrade(cl, stackName, recipeName)
} else {
@ -88,8 +88,8 @@ var UpgradeAll = cli.Command{
},
}
// Read recipe from docker label
func getRecipe(cl *dockerclient.Client, stackName string) string {
// Read docker label in the format coop-cloud.${STACK_NAME}.${LABEL}
func getLabel(cl *dockerclient.Client, stackName string, label string) string {
filter := filters.NewArgs()
filter.Add("label", fmt.Sprintf("%s=%s", convert.LabelNamespace, stackName))
@ -98,58 +98,25 @@ func getRecipe(cl *dockerclient.Client, stackName string) string {
logrus.Fatal(err)
}
for _, service := range services {
labelKey := fmt.Sprintf("coop-cloud.%s.recipe", stackName)
if recipeName, ok := service.Spec.Labels[labelKey]; ok {
return recipeName
labelKey := fmt.Sprintf("coop-cloud.%s.%s", stackName, label)
if labelValue, ok := service.Spec.Labels[labelKey]; ok {
return labelValue
}
}
logrus.Debugf("no recipe name found for %s", stackName)
logrus.Debugf("no %s label found for %s", label, stackName)
return ""
}
// Read chaos deployment from docker label
func getChaos(cl *dockerclient.Client, stackName string) bool {
filter := filters.NewArgs()
filter.Add("label", fmt.Sprintf("%s=%s", convert.LabelNamespace, stackName))
services, err := cl.ServiceList(context.Background(), types.ServiceListOptions{Filters: filter})
if err != nil {
logrus.Fatal(err)
}
for _, service := range services {
labelKey := fmt.Sprintf("coop-cloud.%s.chaos", stackName)
if chaosLabel, ok := service.Spec.Labels[labelKey]; ok {
chaos, err := strconv.ParseBool(chaosLabel)
if err != nil {
logrus.Fatal(err)
}
return chaos
// Read boolean docker label
func getBoolLabel(cl *dockerclient.Client, stackName string, label string) bool {
lableValue := getLabel(cl, stackName, label)
if lableValue != "" {
value, err := strconv.ParseBool(lableValue)
if err != nil {
logrus.Fatal(err)
}
return value
}
logrus.Debugf("no chaos label found for %s", stackName)
return false
}
// Read chaos deployment from docker label
func getUpdateActivation(cl *dockerclient.Client, stackName string) bool {
filter := filters.NewArgs()
filter.Add("label", fmt.Sprintf("%s=%s", convert.LabelNamespace, stackName))
services, err := cl.ServiceList(context.Background(), types.ServiceListOptions{Filters: filter})
if err != nil {
logrus.Fatal(err)
}
for _, service := range services {
labelKey := fmt.Sprintf("coop-cloud.%s.autoupdate", stackName)
if chaosLabel, ok := service.Spec.Labels[labelKey]; ok {
chaos, err := strconv.ParseBool(chaosLabel)
if err != nil {
logrus.Fatal(err)
}
return chaos
}
}
logrus.Debugf("no autoupdate label found for %s", stackName)
return false
}
@ -288,7 +255,7 @@ func createDeployConfig(recipeName string, stackName string, env config.AppEnv)
if err != nil {
logrus.Fatal(err)
}
config.ExposeAllEnv(compose, env)
config.ExposeAllEnv(stackName, compose, env)
// after the upgrade the deployment won't be in chaos state anymore
config.SetChaosLabel(compose, stackName, false)
config.SetRecipeLabel(compose, stackName, recipeName)