Skip to content

Commit

Permalink
Use some new golang 1.22 new features
Browse files Browse the repository at this point in the history
* use cmp.Or() when suitable.
* use for range number when suitable

Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>
  • Loading branch information
nunnatsa committed May 20, 2024
1 parent 47a2be3 commit 7faa2db
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 48 deletions.
2 changes: 1 addition & 1 deletion controllers/hyperconverged/dataimportschedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ = Describe("test data import schedule", func() {
It("should update the status and the variable if both are empty", func() {
regex := `(\d+) (\d+)/12 \* \* \*`

for i := 0; i < 1000; i++ { // testing random number - need some statistic confidence, so running this 1000 times
for range 1000 { // testing random number - need some statistic confidence, so running this 1000 times
dataImportSchedule = ""
hco := commontestutils.NewHco()
req := commontestutils.NewReq(hco)
Expand Down
17 changes: 8 additions & 9 deletions controllers/hyperconverged/hyperconverged_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hyperconverged

import (
"cmp"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -40,18 +41,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"

networkaddonsv1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1"
kubevirtcorev1 "kubevirt.io/api/core/v1"
aaqv1alpha1 "kubevirt.io/application-aware-quota/staging/src/kubevirt.io/application-aware-quota-api/pkg/apis/core/v1alpha1"
cdiv1beta1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
mtqv1alpha1 "kubevirt.io/managed-tenant-quota/staging/src/kubevirt.io/managed-tenant-quota-api/pkg/apis/core/v1alpha1"
sspv1beta2 "kubevirt.io/ssp-operator/api/v1beta2"

hcov1beta1 "github.com/kubevirt/hyperconverged-cluster-operator/api/v1beta1"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/alerts"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/common"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/operands"
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/monitoring/metrics"
hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util"
"github.com/kubevirt/hyperconverged-cluster-operator/version"
kubevirtcorev1 "kubevirt.io/api/core/v1"
aaqv1alpha1 "kubevirt.io/application-aware-quota/staging/src/kubevirt.io/application-aware-quota-api/pkg/apis/core/v1alpha1"
cdiv1beta1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
mtqv1alpha1 "kubevirt.io/managed-tenant-quota/staging/src/kubevirt.io/managed-tenant-quota-api/pkg/apis/core/v1alpha1"
sspv1beta2 "kubevirt.io/ssp-operator/api/v1beta2"
)

var (
Expand Down Expand Up @@ -107,10 +109,7 @@ func RegisterReconciler(mgr manager.Manager, ci hcoutil.ClusterInfo, upgradeable
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, ci hcoutil.ClusterInfo, upgradeableCond hcoutil.Condition) reconcile.Reconciler {

ownVersion := os.Getenv(hcoutil.HcoKvIoVersionName)
if ownVersion == "" {
ownVersion = version.Version
}
ownVersion := cmp.Or(os.Getenv(hcoutil.HcoKvIoVersionName), version.Version)

r := &ReconcileHyperConverged{
client: mgr.GetClient(),
Expand Down
16 changes: 4 additions & 12 deletions controllers/hyperconverged/hyperconverged_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hyperconverged

import (
"cmp"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -1390,10 +1391,7 @@ var _ = Describe("HyperconvergedController", func() {
cl := expected.initClient()
r := initReconciler(cl, nil)

r.ownVersion = os.Getenv(hcoutil.HcoKvIoVersionName)
if r.ownVersion == "" {
r.ownVersion = version.Version
}
r.ownVersion = cmp.Or(os.Getenv(hcoutil.HcoKvIoVersionName), version.Version)

_, err := r.Reconcile(context.TODO(), request)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -3124,10 +3122,7 @@ var _ = Describe("HyperconvergedController", func() {
})
r := initReconciler(cl, nil)

r.ownVersion = os.Getenv(hcoutil.HcoKvIoVersionName)
if r.ownVersion == "" {
r.ownVersion = version.Version
}
r.ownVersion = cmp.Or(os.Getenv(hcoutil.HcoKvIoVersionName), version.Version)

res, err := r.Reconcile(context.TODO(), request)
Expect(err).To(MatchError(apierrors.IsConflict, "conflict error"))
Expand All @@ -3142,10 +3137,7 @@ var _ = Describe("HyperconvergedController", func() {
cl.Status().(*commontestutils.HcoTestStatusWriter).InitiateErrors(apierrors.NewConflict(rs, "hco", errors.New("test error")))
r := initReconciler(cl, nil)

r.ownVersion = os.Getenv(hcoutil.HcoKvIoVersionName)
if r.ownVersion == "" {
r.ownVersion = version.Version
}
r.ownVersion = cmp.Or(os.Getenv(hcoutil.HcoKvIoVersionName), version.Version)

res, err := r.Reconcile(context.TODO(), request)
Expect(err).To(MatchError(apierrors.IsConflict, "conflict error"))
Expand Down
7 changes: 3 additions & 4 deletions controllers/hyperconverged/testUtils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hyperconverged

import (
"cmp"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -30,6 +31,7 @@ import (
sspv1beta2 "kubevirt.io/ssp-operator/api/v1beta2"

networkaddonsv1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1"

hcov1beta1 "github.com/kubevirt/hyperconverged-cluster-operator/api/v1beta1"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/alerts"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/common"
Expand Down Expand Up @@ -318,10 +320,7 @@ func doReconcile(cl client.Client, hco *hcov1beta1.HyperConverged, old *Reconcil
r := initReconciler(cl, old)

r.firstLoop = false
r.ownVersion = os.Getenv(hcoutil.HcoKvIoVersionName)
if r.ownVersion == "" {
r.ownVersion = version.Version
}
r.ownVersion = cmp.Or(os.Getenv(hcoutil.HcoKvIoVersionName), version.Version)

res, err := r.Reconcile(context.TODO(), request)
Expect(err).ToNot(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion controllers/operands/imageStream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ func validateImageStream(found *imagev1.ImageStream, hook *isHooks) {
"latest": false,
}

for i := 0; i < 3; i++ {
for i := range 3 {
tagName := found.Spec.Tags[i].Name
tag := getTagByName(found.Spec.Tags, tagName)
Expect(tag).ToNot(BeNil())
Expand Down
14 changes: 6 additions & 8 deletions controllers/operands/kubevirt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package operands

import (
"cmp"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -447,15 +448,12 @@ func getKVConfig(hc *hcov1beta1.HyperConverged) (*kubevirtcorev1.KubeVirtConfigu
}
}

var amd64MachineType string
// Remain backwards compatibility with the original env variable for a release before removing
if machineTypeEnvValue, _ := os.LookupEnv(machineTypeEnvName); machineTypeEnvValue != "" {
amd64MachineType = machineTypeEnvValue
} else if machineTypeEnvValue, ok := os.LookupEnv(amd64MachineTypeEnvName); ok {
amd64MachineType = machineTypeEnvValue
}
amd64MachineType := cmp.Or(
strings.TrimSpace(os.Getenv(machineTypeEnvName)),
strings.TrimSpace(os.Getenv(amd64MachineTypeEnvName)),
)

if amd64MachineType = strings.TrimSpace(amd64MachineType); amd64MachineType != "" {
if amd64MachineType != "" {
if config.ArchitectureConfiguration == nil {
config.ArchitectureConfiguration = &kubevirtcorev1.ArchConfiguration{}
}
Expand Down
11 changes: 7 additions & 4 deletions controllers/operands/kubevirtConsolePlugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ func NewKvUIPluginDeployment(hc *hcov1beta1.HyperConverged) *appsv1.Deployment {
ReadOnly: true,
}

volumeMounts := &deployment.Spec.Template.Spec.Containers[0].VolumeMounts
*volumeMounts = append(*volumeMounts, nginxVolumeMount)
deployment.Spec.Template.Spec.Containers[0].VolumeMounts = append(
deployment.Spec.Template.Spec.Containers[0].VolumeMounts,
nginxVolumeMount,
)

nginxVolume := corev1.Volume{
Name: nginxConfigMapName,
Expand All @@ -90,8 +92,9 @@ func NewKvUIPluginDeployment(hc *hcov1beta1.HyperConverged) *appsv1.Deployment {
},
},
}
volumes := &deployment.Spec.Template.Spec.Volumes
*volumes = append(*volumes, nginxVolume)

deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, nginxVolume)

return deployment
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/util/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ var _ = Describe("test clusterInfo", func() {
func(numMasterNodes, numWorkerNodes int, expectedIsControlPlaneHighlyAvailable, expectedIsInfrastructureHighlyAvailable bool) {

var nodesArray []client.Object
for i := 0; i < numMasterNodes; i++ {
for i := range numMasterNodes {
masterNode := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "master" + fmt.Sprint(i),
Expand All @@ -272,7 +272,7 @@ var _ = Describe("test clusterInfo", func() {
}
nodesArray = append(nodesArray, masterNode)
}
for i := 0; i < numWorkerNodes; i++ {
for i := range numWorkerNodes {
workerNode := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "worker" + fmt.Sprint(i),
Expand Down
8 changes: 2 additions & 6 deletions pkg/util/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"cmp"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -42,10 +43,5 @@ func ValidateManifestDir(dir string) error {
}

func GetManifestDirPath(envVarName string, defaultDir string) string {
filesLocation := os.Getenv(envVarName)
if filesLocation == "" {
return defaultDir
}

return filesLocation
return cmp.Or(os.Getenv(envVarName), defaultDir)
}
2 changes: 1 addition & 1 deletion tools/digester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func readOneDigest(ctx context.Context, image *Image, index int, wg *sync.WaitGr
}

func retryGetDigest(ctx context.Context, sys *types.SystemContext, imgRef types.ImageReference, attempts int, sleep time.Duration) (digest digest.Digest, err error) {
for i := 0; i < attempts; i++ {
for i := range attempts {
if i > 0 {
fmt.Println("retrying after error:", err)
jitter := time.Duration(rand.Int63n(int64(sleep)))
Expand Down

0 comments on commit 7faa2db

Please sign in to comment.