@@ -2,9 +2,7 @@ package extensions
22
33import (
44 "context"
5- "fmt"
65 "log/slog"
7- "runtime/debug"
86
97 "github.com/pkg/errors"
108 ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
@@ -76,7 +74,7 @@ func Upgrade(ctx context.Context, kcli client.Client, hcli helm.Client, prev *ec
7674 return nil
7775}
7876
79- func handleExtensionInstall (ctx context.Context , kcli client.Client , hcli helm.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart ) ( finalErr error ) {
77+ func handleExtensionInstall (ctx context.Context , kcli client.Client , hcli helm.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart ) error {
8078 return handleExtension (ctx , kcli , in , ext , actionInstall , func () error {
8179 exists , err := hcli .ReleaseExists (ctx , ext .TargetNS , ext .Name )
8280 if err != nil {
@@ -93,7 +91,7 @@ func handleExtensionInstall(ctx context.Context, kcli client.Client, hcli helm.C
9391 })
9492}
9593
96- func handleExtensionUpgrade (ctx context.Context , kcli client.Client , hcli helm.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart ) ( finalErr error ) {
94+ func handleExtensionUpgrade (ctx context.Context , kcli client.Client , hcli helm.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart ) error {
9795 return handleExtension (ctx , kcli , in , ext , actionUpgrade , func () error {
9896 if err := upgrade (ctx , hcli , ext ); err != nil {
9997 return errors .Wrap (err , "upgrade" )
@@ -109,7 +107,7 @@ func handleExtensionNoop(ctx context.Context, kcli client.Client, in *ecv1beta1.
109107 })
110108}
111109
112- func handleExtensionUninstall (ctx context.Context , kcli client.Client , hcli helm.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart ) ( finalErr error ) {
110+ func handleExtensionUninstall (ctx context.Context , kcli client.Client , hcli helm.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart ) error {
113111 return handleExtension (ctx , kcli , in , ext , actionUninstall , func () error {
114112 exists , err := hcli .ReleaseExists (ctx , ext .TargetNS , ext .Name )
115113 if err != nil {
@@ -126,7 +124,7 @@ func handleExtensionUninstall(ctx context.Context, kcli client.Client, hcli helm
126124 })
127125}
128126
129- func handleExtension (ctx context.Context , kcli client.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart , action helmAction , processFn func () error ) ( finalErr error ) {
127+ func handleExtension (ctx context.Context , kcli client.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart , action helmAction , processFn func () error ) error {
130128 slogArgs := slogArgs (ext , action )
131129
132130 processed , err := extensionAlreadyProcessed (ctx , kcli , in , ext )
@@ -146,26 +144,19 @@ func handleExtension(ctx context.Context, kcli client.Client, in *ecv1beta1.Inst
146144 }
147145 }
148146
149- defer func () {
150- if r := recover (); r != nil {
151- actionIng , _ := formatAction (action )
152- finalErr = fmt .Errorf ("%s %s recovered from panic: %v: %s" , actionIng , ext .Name , r , string (debug .Stack ()))
153- }
154-
155- err := markExtensionProcessed (ctx , kcli , in , ext , action , finalErr )
156- if err != nil {
157- if finalErr == nil {
158- finalErr = errors .Wrap (err , "mark extension as processed" )
159- } else {
160- slog .Error ("Failed to mark extension as processed" , append (slogArgs , "error" , err )... )
161- }
147+ err = processFn ()
148+ if err != nil {
149+ if err := markExtensionAsFailed (ctx , kcli , in , ext , action , err ); err != nil {
150+ slog .Error ("Failed to mark extension as failed" , append (slogArgs , "error" , err )... )
162151 }
163- }()
164-
165- if err := processFn (); err != nil {
166152 return errors .Wrap (err , "process extension" )
167153 }
168154
155+ err = markExtensionAsProcessed (ctx , kcli , in , ext , action )
156+ if err != nil {
157+ return errors .Wrap (err , "mark extension as processed" )
158+ }
159+
169160 slog .Info ("Extension is ready!" , slogArgs ... )
170161
171162 return nil
@@ -187,23 +178,20 @@ func markExtensionAsProcessing(ctx context.Context, kcli client.Client, in *ecv1
187178 return nil
188179}
189180
190- func markExtensionProcessed (ctx context.Context , kcli client.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart , action helmAction , finalErr error ) error {
181+ func markExtensionAsProcessed (ctx context.Context , kcli client.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart , action helmAction ) error {
191182 _ , actionEd := formatAction (action )
192-
193- status := metav1 .ConditionTrue
194- reason := actionEd
195- message := ""
196-
197- if finalErr != nil {
198- status = metav1 .ConditionFalse
199- reason = string (action ) + "Failed"
200- message = helpers .CleanErrorMessage (finalErr )
183+ if err := setCondition (ctx , kcli , in , conditionName (ext ), metav1 .ConditionTrue , actionEd , "" ); err != nil {
184+ return errors .Wrap (err , "failed to set condition status" )
201185 }
186+ return nil
187+ }
202188
203- if err := setCondition (ctx , kcli , in , conditionName (ext ), status , reason , message ); err != nil {
189+ func markExtensionAsFailed (ctx context.Context , kcli client.Client , in * ecv1beta1.Installation , ext ecv1beta1.Chart , action helmAction , finalErr error ) error {
190+ reason := string (action ) + "Failed"
191+ message := helpers .CleanErrorMessage (finalErr )
192+ if err := setCondition (ctx , kcli , in , conditionName (ext ), metav1 .ConditionFalse , reason , message ); err != nil {
204193 return errors .Wrap (err , "failed to set condition status" )
205194 }
206-
207195 return nil
208196}
209197
0 commit comments