@@ -12,6 +12,7 @@ import (
1212 "time"
1313
1414 "github.com/pkg/errors"
15+ embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
1516 downstreamtypes "github.com/replicatedhq/kots/pkg/api/downstream/types"
1617 "github.com/replicatedhq/kots/pkg/app"
1718 apptypes "github.com/replicatedhq/kots/pkg/app/types"
@@ -53,6 +54,7 @@ import (
5354 "k8s.io/client-go/kubernetes"
5455 "k8s.io/client-go/kubernetes/scheme"
5556 "k8s.io/client-go/tools/cache"
57+ kbclient "sigs.k8s.io/controller-runtime/pkg/client"
5658)
5759
5860var (
@@ -933,7 +935,7 @@ func (o *Operator) reconcileDeployment(cm *corev1.ConfigMap) (finalError error)
933935 if cm .Data ["requires-cluster-upgrade" ] == "true" {
934936 // wait for cluster upgrade even if the embedded cluster version doesn't match yet
935937 // in order to continuously report progress to the user
936- if err := o .waitForClusterUpgrade (cm .Data ["app-slug" ]); err != nil {
938+ if err := o .waitForClusterUpgrade (cm .Data ["app-id" ], cm . Data [ "app- slug" ]); err != nil {
937939 return errors .Wrap (err , "failed to wait for cluster upgrade" )
938940 }
939941 }
@@ -1035,21 +1037,31 @@ func (o *Operator) reconcileDeployment(cm *corev1.ConfigMap) (finalError error)
10351037 return nil
10361038}
10371039
1038- func (o * Operator ) waitForClusterUpgrade (appSlug string ) error {
1039- kbClient , err := k8sutil .GetKubeClient (context .Background ())
1040+ func (o * Operator ) waitForClusterUpgrade (appID string , appSlug string ) error {
1041+ ctx := context .Background ()
1042+
1043+ kbClient , err := k8sutil .GetKubeClient (ctx )
10401044 if err != nil {
10411045 return errors .Wrap (err , "failed to get kube client" )
10421046 }
1043- logger .Infof ("waiting for cluster upgrade to finish" )
1047+ logger .Infof ("Waiting for cluster upgrade to finish" )
10441048 for {
1045- ins , err := embeddedcluster .GetCurrentInstallation (context . Background () , kbClient )
1049+ ins , err := embeddedcluster .GetCurrentInstallation (ctx , kbClient )
10461050 if err != nil {
10471051 return errors .Wrap (err , "failed to wait for embedded cluster installation" )
10481052 }
1049- if embeddedcluster .InstallationSucceeded (context .Background (), ins ) {
1053+ if embeddedcluster .InstallationSucceeded (ctx , ins ) {
1054+ logger .Infof ("Cluster upgrade succeeded" )
1055+ if err := o .notifyClusterUpgradeSucceeded (ctx , kbClient , ins , appID ); err != nil {
1056+ logger .Errorf ("Failed to notify upgrade succeeded: %v" , err )
1057+ }
10501058 return nil
10511059 }
1052- if embeddedcluster .InstallationFailed (context .Background (), ins ) {
1060+ if embeddedcluster .InstallationFailed (ctx , ins ) {
1061+ logger .Infof ("Cluster upgrade failed" )
1062+ if err := o .notifyClusterUpgradeFailed (ctx , kbClient , ins , appID ); err != nil {
1063+ logger .Errorf ("Failed to notify upgrade failed: %v" , err )
1064+ }
10531065 if err := upgradeservicetask .SetStatusUpgradeFailed (appSlug , ins .Status .Reason ); err != nil {
10541066 return errors .Wrap (err , "failed to set task status to failed" )
10551067 }
@@ -1061,3 +1073,53 @@ func (o *Operator) waitForClusterUpgrade(appSlug string) error {
10611073 time .Sleep (5 * time .Second )
10621074 }
10631075}
1076+
1077+ // notifyClusterUpgradeSucceeded sends a metrics event to the api that the upgrade succeeded.
1078+ func (o * Operator ) notifyClusterUpgradeSucceeded (ctx context.Context , kbClient kbclient.Client , ins * embeddedclusterv1beta1.Installation , appID string ) error {
1079+ if ins .Spec .AirGap {
1080+ return nil
1081+ }
1082+
1083+ license , err := o .store .GetLatestLicenseForApp (appID )
1084+ if err != nil {
1085+ return errors .Wrapf (err , "failed to get latest license for app %s" , appID )
1086+ }
1087+
1088+ prev , err := embeddedcluster .GetPreviousInstallation (ctx , kbClient )
1089+ if err != nil {
1090+ return errors .Wrap (err , "failed to get previous installation" )
1091+ } else if prev == nil {
1092+ return errors .New ("previous installation not found" )
1093+ }
1094+
1095+ err = embeddedcluster .NotifyUpgradeSucceeded (ctx , license .Spec .Endpoint , ins , prev )
1096+ if err != nil {
1097+ return errors .Wrap (err , "failed to send event" )
1098+ }
1099+ return nil
1100+ }
1101+
1102+ // notifyClusterUpgradeFailed sends a metrics event to the api that the upgrade failed.
1103+ func (o * Operator ) notifyClusterUpgradeFailed (ctx context.Context , kbClient kbclient.Client , ins * embeddedclusterv1beta1.Installation , appID string ) error {
1104+ if ins .Spec .AirGap {
1105+ return nil
1106+ }
1107+
1108+ license , err := o .store .GetLatestLicenseForApp (appID )
1109+ if err != nil {
1110+ return errors .Wrapf (err , "failed to get latest license for app %s" , appID )
1111+ }
1112+
1113+ prev , err := embeddedcluster .GetPreviousInstallation (ctx , kbClient )
1114+ if err != nil {
1115+ return errors .Wrap (err , "failed to get previous installation" )
1116+ } else if prev == nil {
1117+ return errors .New ("previous installation not found" )
1118+ }
1119+
1120+ err = embeddedcluster .NotifyUpgradeFailed (ctx , license .Spec .Endpoint , ins , prev , ins .Status .Reason )
1121+ if err != nil {
1122+ return errors .Wrap (err , "failed to send event" )
1123+ }
1124+ return nil
1125+ }
0 commit comments