5
\$\begingroup\$

Im fairly new to Golang and I appreciate any feedback about how to make the code cleaner.

The code is called by go-routine and should update status (fail/success etc) of several installation process.

package avss
import (
 "context"
 "sync"
 "helm.sh/helm/v3/pkg/release"
 "sigs.k8s.io/controller-runtime/pkg/client"
 avmv1alpha1 "github.vmp/avs/api/v1alpha1"
)
func GetUpdateStatus(ctx context.Context, log logr.Logger, cr client.Object, status *avmv1alpha1.ComponentStatusWithAggregation, statusClient client.StatusClient) func(chartStatus *avmv1alpha1.InstallStatus) error {
 var conditionMutex sync.Mutex
 return func(chartStatus *avmv1alpha1.InstallStatus) error {
 // prevent status update by subroutines at the same time, lock the access to prevent concurrent modification
 conditionMutex.Lock()
 defer conditionMutex.Unlock()
 status.ChartStatus = updateStatus(chartStatus, status.ChartStatus)
 status.Status = overallStatus(status.ChartStatus)
 err := statusClient.Status().Update(ctx, cr)
 return err
 }
}
func overallStatus(chartsStatus []avmv1alpha1.InstallStatus) string {
 overallStatus := ""
 for _, status := range chartsStatus {
 switch status.ReleaseStatus {
 case release.StatusFailed.String():
 overallStatus = "error"
 case release.StatusDeployed.String():
 if overallStatus == "" {
 overallStatus = "installed"
 }
 default:
 if overallStatus != release.StatusFailed.String() {
 overallStatus = "reconcile"
 }
 }
 }
 return overallStatus
}
func updateStatus(newStatus *avmv1alpha1.ChartStatus, cStatus []avmv1alpha1.InstallStatus) []avmv1alpha1.ChartStatus {
 found := false
 for i, status := range cStatus {
 if status.Name == newStatus.Name {
 found = true
 cStatus[i] = *newStatus
 }
 }
 if !found {
 cStatus = append(cStatus, *newStatus)
 }
 return cStatus
}

https://play.golang.org/p/9C8xGV2116O

The code is working! however, I guess it can be improved as this is my first Go program which should be run in production :-)

If the code is perfect, 🀩 please let me know

200_success
145k22 gold badges190 silver badges478 bronze badges
asked May 12, 2021 at 14:25
\$\endgroup\$
1
  • \$\begingroup\$ I think it is proper.. changes that could be made are ("installed" or "reconcile" -> if this value is being used elsewhere I would suggest you define them as constants in a common go file like params.go) \$\endgroup\$ Commented May 20, 2021 at 13:43

1 Answer 1

2
\$\begingroup\$

Overall this looks like a pretty sensible file. Some minor points you might want to consider follow.

Imports

logr is not imported in the file. I presume this is https://github.com/go-logr/logr. It's passed into the first function but not used. Perhaps remove it from the signature?

Discrete strings

overallStatus returns one of four strings. You could define these strings as constants like Siva mentioned. A step further is to introduce a type that groups these strings together. This

  • ensures the function returns one of the strings defined, and
  • makes it clear what strings can be returned from the function by looking at the signature.
type status string
const (
 statusUnknown status = ""
 statusError status = "error"
 statusInstalled status = "installed"
 statusReconcile status = "reconcile"
)
answered May 21, 2021 at 15:14
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.