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