Skip to content
util.go 1.06 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
package util

import (
	version "github.com/hashicorp/go-version"
Timothee Gosselin's avatar
Timothee Gosselin committed
	appsv1alpha1 "k8s.libre.sh/apps/nextcloud/api/v1alpha1"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

//. TOFIX get install status and desired status, if status =! nil ??
func GetAppPhase(status appsv1alpha1.NextcloudStatus, desiredVersion string) (appsv1alpha1.Phase, error) {
Timothee Gosselin's avatar
Timothee Gosselin committed
	// get nextcloud installed version in status
	if len(status.Version) == 0 {
		return appsv1alpha1.PhaseInstalling, nil
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
	v1, err := version.NewVersion(status.Version)
	// get nextcloud desired version in spec
	v2, err := version.NewVersion(desiredVersion)
	if err != nil {
		return appsv1alpha1.PhaseFailed, err
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
	// compare current version from status with desired from spec
	c := v1.Compare(v2)
	switch {
	// current version is greater than desired, no downgrade
	case c == 1:
		return appsv1alpha1.PhaseFailed, nil
Timothee Gosselin's avatar
Timothee Gosselin committed
	// current version lower than desired, upgrade job
	case c == -1:
		return appsv1alpha1.PhaseUpgrading, nil
Timothee Gosselin's avatar
Timothee Gosselin committed
	// current version and desired are the same, normal start
	case c == 0:
		return appsv1alpha1.PhaseCreating, nil
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
	return appsv1alpha1.PhaseNone, nil
Timothee Gosselin's avatar
Timothee Gosselin committed
}