Skip to content

Commit

Permalink
upgrade procedure remove AMP_RELEASE
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Sep 2, 2021
1 parent 30e883a commit cd6d09b
Showing 1 changed file with 121 additions and 1 deletion.
122 changes: 121 additions & 1 deletion pkg/3scale/amp/operator/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"reflect"

appsv1alpha1 "github.com/3scale/3scale-operator/apis/apps/v1alpha1"
"github.com/3scale/3scale-operator/pkg/helper"
"github.com/3scale/3scale-operator/pkg/reconcilers"

"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
appsv1 "github.com/openshift/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand All @@ -30,7 +32,12 @@ func NewUpgradeApiManager(b *reconcilers.BaseReconciler, apiManager *appsv1alpha
}

func (u *UpgradeApiManager) Upgrade() (reconcile.Result, error) {
res, err := u.upgradeImages()
res, err := u.upgradeSystemAMPRelease()
if err != nil {
return res, fmt.Errorf("Upgrade: remove system AMP_RELEASE error: %w", err)
}

res, err = u.upgradeImages()
if err != nil {
return res, fmt.Errorf("Upgrading images: %w", err)
}
Expand All @@ -41,6 +48,119 @@ func (u *UpgradeApiManager) Upgrade() (reconcile.Result, error) {
return reconcile.Result{}, nil
}

func (u *UpgradeApiManager) upgradeSystemAMPRelease() (reconcile.Result, error) {
system, err := System(u.apiManager, u.Client())
if err != nil {
return reconcile.Result{}, err
}

res, err := u.deleteAMPReleaseSystemAppDC(system.AppDeploymentConfig())
if res.Requeue || err != nil {
return res, err
}

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

return u.deleteAMPReleaseConfigMap(system.EnvironmentConfigMap())
}

func (u *UpgradeApiManager) deleteAMPReleaseSystemAppDC(desired *appsv1.DeploymentConfig) (reconcile.Result, error) {
existing := &appsv1.DeploymentConfig{}
err := u.Client().Get(context.TODO(), types.NamespacedName{Name: desired.Name, Namespace: u.apiManager.Namespace}, existing)
if err != nil {
return reconcile.Result{}, err
}

if len(existing.Spec.Template.Spec.Containers) != 3 {
return reconcile.Result{}, fmt.Errorf("DeploymentConfig %s spec.template.spec.containers length is %d, should be 3",
existing.Name, len(existing.Spec.Template.Spec.Containers))
}

update := false

// regular pod containers
for idx := 0; idx < 3; idx++ {
container := &existing.Spec.Template.Spec.Containers[idx]
if envVarIdx := helper.FindEnvVar(container.Env, "AMP_RELEASE"); envVarIdx >= 0 {
// remove index
container.Env = append(container.Env[:envVarIdx], container.Env[envVarIdx+1:]...)
update = true
}
}

// Pre hook pod
// the ExecNewPod property is already a pointer
preHookPod := existing.Spec.Strategy.RollingParams.Pre.ExecNewPod
if envVarIdx := helper.FindEnvVar(preHookPod.Env, "AMP_RELEASE"); envVarIdx >= 0 {
// remove index
preHookPod.Env = append(preHookPod.Env[:envVarIdx], preHookPod.Env[envVarIdx+1:]...)
update = true
}

if update {
err = u.UpdateResource(existing)
if err != nil {
return reconcile.Result{}, err
}
}

return reconcile.Result{}, nil
}

func (u *UpgradeApiManager) deleteAMPReleaseSystemSidekiqDC(desired *appsv1.DeploymentConfig) (reconcile.Result, error) {
existing := &appsv1.DeploymentConfig{}
err := u.Client().Get(context.TODO(), types.NamespacedName{Name: desired.Name, Namespace: u.apiManager.Namespace}, existing)
if err != nil {
return reconcile.Result{}, err
}

update := false

// regular pod containers
container := &existing.Spec.Template.Spec.Containers[0]
if envVarIdx := helper.FindEnvVar(container.Env, "AMP_RELEASE"); envVarIdx >= 0 {
// remove index
container.Env = append(container.Env[:envVarIdx], container.Env[envVarIdx+1:]...)
update = true
}

if update {
err = u.UpdateResource(existing)
if err != nil {
return reconcile.Result{}, err
}
}

return reconcile.Result{}, nil
}

func (u *UpgradeApiManager) deleteAMPReleaseConfigMap(desired *v1.ConfigMap) (reconcile.Result, error) {
existing := &v1.ConfigMap{}
err := u.Client().Get(context.TODO(), types.NamespacedName{Name: desired.Name, Namespace: u.apiManager.Namespace}, existing)
if err != nil {
return reconcile.Result{}, err
}

update := false

if _, ok := existing.Data["AMP_RELEASE"]; ok {
delete(existing.Data, "AMP_RELEASE")
update = true
}

if update {
err = u.UpdateResource(existing)
if err != nil {
return reconcile.Result{}, err
}
}

return reconcile.Result{}, nil
}

func (u *UpgradeApiManager) upgradeImages() (reconcile.Result, error) {
res, err := u.upgradeAMPImageStreams()
if res.Requeue || err != nil {
Expand Down

0 comments on commit cd6d09b

Please sign in to comment.