Skip to content

Commit

Permalink
metering labels: upgrade path
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed May 28, 2020
1 parent 8020223 commit 7e1f772
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
96 changes: 96 additions & 0 deletions pkg/3scale/amp/operator/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (u *UpgradeApiManager) Upgrade() (reconcile.Result, error) {
return res, err
}

res, err = u.upgradePodTemplateLabels()
if res.Requeue || err != nil {
return res, err
}

return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -614,6 +619,97 @@ func (u *UpgradeApiManager) upgradeSystemMasterApicastSecret() (reconcile.Result
return reconcile.Result{}, nil
}

func (u *UpgradeApiManager) upgradePodTemplateLabels() (reconcile.Result, error) {
apicast, err := Apicast(u.apiManager)
if err != nil {
return reconcile.Result{}, err
}
backend, err := Backend(u.apiManager, u.Client())
if err != nil {
return reconcile.Result{}, err
}
zync, err := Zync(u.apiManager, u.Client())
if err != nil {
return reconcile.Result{}, err
}
memcached, err := Memcached(u.apiManager)
if err != nil {
return reconcile.Result{}, err
}
system, err := System(u.apiManager, u.Client())
if err != nil {
return reconcile.Result{}, err
}

deploymentConfigs := []*appsv1.DeploymentConfig{
apicast.StagingDeploymentConfig(),
apicast.ProductionDeploymentConfig(),
backend.ListenerDeploymentConfig(),
backend.WorkerDeploymentConfig(),
backend.CronDeploymentConfig(),
zync.DeploymentConfig(),
zync.QueDeploymentConfig(),
zync.DatabaseDeploymentConfig(),
memcached.DeploymentConfig(),
system.AppDeploymentConfig(),
system.SidekiqDeploymentConfig(),
system.SphinxDeploymentConfig(),
}

if !u.apiManager.IsExternalDatabaseEnabled() {
redis, err := Redis(u.apiManager)
if err != nil {
return reconcile.Result{}, err
}
deploymentConfigs = append(deploymentConfigs, redis.SystemDeploymentConfig())
deploymentConfigs = append(deploymentConfigs, redis.BackendDeploymentConfig())
}

if !u.apiManager.IsSystemPostgreSQLEnabled() {
systemPostgreSQL, err := SystemPostgreSQL(u.apiManager, u.Client())
if err != nil {
return reconcile.Result{}, err
}
deploymentConfigs = append(deploymentConfigs, systemPostgreSQL.DeploymentConfig())
} else {
systemMySQL, err := SystemMySQL(u.apiManager, u.Client())
if err != nil {
return reconcile.Result{}, err
}
deploymentConfigs = append(deploymentConfigs, systemMySQL.DeploymentConfig())
}

for _, desired := range deploymentConfigs {
err = u.ensurePodTemplateLabels(desired)
if err != nil {
return reconcile.Result{}, err
}
}

return reconcile.Result{}, nil
}

func (u *UpgradeApiManager) ensurePodTemplateLabels(desired *appsv1.DeploymentConfig) error {
u.Logger().V(1).Info(fmt.Sprintf("ensurePodTemplateLabels object %s", common.ObjectInfo(desired)))
existing := &appsv1.DeploymentConfig{}
err := u.Client().Get(context.TODO(), types.NamespacedName{Name: desired.Name, Namespace: u.apiManager.Namespace}, existing)
if err != nil {
return err
}

if !reflect.DeepEqual(existing.Spec.Template.Labels, desired.Spec.Template.Labels) {
diff := cmp.Diff(existing.Spec.Template.Labels, desired.Spec.Template.Labels)
u.Logger().V(1).Info(fmt.Sprintf("DC %s template lables changed: %s", desired.Name, diff))
existing.Spec.Template.Labels = desired.Spec.Template.Labels
err = u.UpdateResource(existing)
if err != nil {
return err
}
}

return nil
}

func (u *UpgradeApiManager) Logger() logr.Logger {
return u.logger
}
6 changes: 6 additions & 0 deletions pkg/apis/apps/v1alpha1/apimanager_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,9 @@ func (apimanager *APIManager) IsExternalDatabaseEnabled() bool {
func (apimanager *APIManager) IsPDBEnabled() bool {
return apimanager.Spec.PodDisruptionBudget != nil && apimanager.Spec.PodDisruptionBudget.Enabled
}

func (apimanager *APIManager) IsSystemPostgreSQLEnabled() bool {
return !apimanager.IsExternalDatabaseEnabled() &&
apimanager.Spec.System.DatabaseSpec != nil &&
apimanager.Spec.System.DatabaseSpec.PostgreSQL != nil
}

0 comments on commit 7e1f772

Please sign in to comment.