Skip to content

Commit

Permalink
chore(lint): Linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hrak committed May 16, 2024
1 parent 0c0afe9 commit 1146199
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 53 deletions.
15 changes: 11 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ linters-settings:
limitations under the License.
gocyclo:
min-complexity: 15

revive:
rules:
- name: dot-imports
arguments:
# dot import should be ONLY allowed for ginkgo testing packages
allowedPackages:
- "github.com/onsi/ginkgo/v2"
- "github.com/onsi/gomega"
linters:
enable:
- gosec
Expand All @@ -32,12 +39,12 @@ linters:

run:
issues-exit-code: 1
skip-dirs:
- pkg/mocks
- test

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-dirs:
- pkg/mocks
- test
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
Expand Down
6 changes: 3 additions & 3 deletions api/v1beta2/cloudstackmachine_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package v1beta2_test

import (
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
capcv1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -43,7 +43,7 @@ var _ = Describe("CloudStackMachineConfig_CompressUserdata", func() {
Name: "is false when uncompressed user data is true",
Machine: capcv1.CloudStackMachine{
Spec: capcv1.CloudStackMachineSpec{
UncompressedUserData: pointer.Bool(true),
UncompressedUserData: pointer.To(true),
},
},
Expect: false,
Expand All @@ -52,7 +52,7 @@ var _ = Describe("CloudStackMachineConfig_CompressUserdata", func() {
Name: "Is false when uncompressed user data is false",
Machine: capcv1.CloudStackMachine{
Spec: capcv1.CloudStackMachineSpec{
UncompressedUserData: pointer.Bool(false),
UncompressedUserData: pointer.To(false),
},
},
Expect: true,
Expand Down
18 changes: 9 additions & 9 deletions controllers/cloudstackfailuredomain_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3"
Expand Down Expand Up @@ -117,17 +117,17 @@ var _ = Describe("CloudStackFailureDomainReconciler", func() {

},
// should delete - simulate owner is kubeadmcontrolplane
Entry("Should delete machine if spec.replicas > 1", true, pointer.Int32(2), pointer.Int32(2), pointer.Int32(2), pointer.Bool(true), true),
Entry("Should delete machine if spec.replicas > 1", true, pointer.To(int32(2)), pointer.To(int32(2)), pointer.To(int32(2)), pointer.To(true), true),
// should delete - simulate owner is etcdadmcluster
Entry("Should delete machine if status.readyReplica does not exist", true, pointer.Int32(2), pointer.Int32(2), nil, pointer.Bool(true), true),
Entry("Should delete machine if status.readyReplica does not exist", true, pointer.To(int32(2)), pointer.To(int32(2)), nil, pointer.To(true), true),
// should delete - simulate owner is machineset
Entry("Should delete machine if status.ready does not exist", true, pointer.Int32(2), pointer.Int32(2), pointer.Int32(2), nil, true),
Entry("Should delete machine if status.ready does not exist", true, pointer.To(int32(2)), pointer.To(int32(2)), pointer.To(int32(2)), nil, true),
// should not delete if condition not met
Entry("Should not delete machine if cluster control plane not ready", false, pointer.Int32(2), pointer.Int32(2), pointer.Int32(2), pointer.Bool(true), false),
Entry("Should not delete machine if status.replicas < spec.replicas", false, pointer.Int32(2), pointer.Int32(1), pointer.Int32(1), pointer.Bool(true), true),
Entry("Should not delete machine if spec.replicas < 2", false, pointer.Int32(1), pointer.Int32(1), pointer.Int32(1), pointer.Bool(true), true),
Entry("Should not delete machine if status.ready is false", false, pointer.Int32(2), pointer.Int32(2), pointer.Int32(2), pointer.Bool(false), true),
Entry("Should not delete machine if status.readyReplicas <> status.replicas", false, pointer.Int32(2), pointer.Int32(2), pointer.Int32(1), pointer.Bool(true), true),
Entry("Should not delete machine if cluster control plane not ready", false, pointer.To(int32(2)), pointer.To(int32(2)), pointer.To(int32(2)), pointer.To(true), false),
Entry("Should not delete machine if status.replicas < spec.replicas", false, pointer.To(int32(2)), pointer.To(int32(1)), pointer.To(int32(1)), pointer.To(true), true),
Entry("Should not delete machine if spec.replicas < 2", false, pointer.To(int32(1)), pointer.To(int32(1)), pointer.To(int32(1)), pointer.To(true), true),
Entry("Should not delete machine if status.ready is false", false, pointer.To(int32(2)), pointer.To(int32(2)), pointer.To(int32(2)), pointer.To(false), true),
Entry("Should not delete machine if status.readyReplicas <> status.replicas", false, pointer.To(int32(2)), pointer.To(int32(2)), pointer.To(int32(1)), pointer.To(true), true),
)
})
})
Expand Down
6 changes: 3 additions & 3 deletions controllers/cloudstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"regexp"
"time"

"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -327,8 +327,8 @@ func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.R
// ResolveVMInstanceDetails can get InstanceID by CS machine name
err := r.CSClient.ResolveVMInstanceDetails(r.ReconciliationSubject)
if err != nil {
r.ReconciliationSubject.Status.Status = pointer.String(metav1.StatusFailure)
r.ReconciliationSubject.Status.Reason = pointer.String(err.Error() +
r.ReconciliationSubject.Status.Status = pointer.To(metav1.StatusFailure)
r.ReconciliationSubject.Status.Reason = pointer.To(err.Error() +
fmt.Sprintf(" If this VM has already been deleted, please remove the finalizer named %s from object %s",
"cloudstackmachine.infrastructure.cluster.x-k8s.io", r.ReconciliationSubject.Name))
// Cloudstack VM may be not found or more than one found by name
Expand Down
4 changes: 2 additions & 2 deletions controllers/cloudstackmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -121,7 +121,7 @@ var _ = Describe("CloudStackMachineReconciler", func() {
})

It("Should call ResolveVMInstanceDetails when CS machine without instanceID deleted", func() {
instanceID := pointer.String("instance-id-123")
instanceID := pointer.To("instance-id-123")
// Mock a call to GetOrCreateVMInstance and set the machine to running.
mockCloudClient.EXPECT().GetOrCreateVMInstance(
gomock.Any(), gomock.Any(), gomock.Any(),
Expand Down
8 changes: 3 additions & 5 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ import (
"k8s.io/client-go/tools/record"
"sigs.k8s.io/cluster-api-provider-cloudstack/test/fakes"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/go-logr/logr"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -154,7 +152,7 @@ var _ = BeforeSuite(func() {
Ω(flag.Lookup("v").Value.Set("1")).Should(Succeed())
flag.Parse()

logger = klogr.New()
logger = klog.Background()
})

// A mock fo the CloudClient interface used in controller utils.
Expand Down
8 changes: 2 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package main
import (
"context"
"fmt"
"os"

"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
"os"

flag "github.com/spf13/pflag"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -67,8 +65,6 @@ var (
)

func init() {
klog.InitFlags(nil)

utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme))
utilruntime.Must(infrav1b1.AddToScheme(scheme))
Expand Down Expand Up @@ -188,7 +184,7 @@ func main() {
os.Exit(1)
}

ctrl.SetLogger(klogr.New())
ctrl.SetLogger(klog.Background())

tlsOptionOverrides, err := flags.GetTLSOptionOverrideFuncs(tlsOptions)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/cloud/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
)

Expand All @@ -42,9 +42,9 @@ type VMIface interface {

// Set infrastructure spec and status from the CloudStack API's virtual machine metrics type.
func setMachineDataFromVMMetrics(vmResponse *cloudstack.VirtualMachinesMetric, csMachine *infrav1.CloudStackMachine) {
csMachine.Spec.ProviderID = pointer.String(fmt.Sprintf("cloudstack:///%s", vmResponse.Id))
csMachine.Spec.ProviderID = pointer.To(fmt.Sprintf("cloudstack:///%s", vmResponse.Id))
// InstanceID is later used as required parameter to destroy VM.
csMachine.Spec.InstanceID = pointer.String(vmResponse.Id)
csMachine.Spec.InstanceID = pointer.To(vmResponse.Id)
csMachine.Status.Addresses = []corev1.NodeAddress{{Type: corev1.NodeInternalIP, Address: vmResponse.Ipaddress}}
newInstanceState := vmResponse.State
if newInstanceState != csMachine.Status.InstanceState || (newInstanceState != "" && csMachine.Status.InstanceStateLastUpdated.IsZero()) {
Expand Down Expand Up @@ -351,14 +351,14 @@ func (c *client) DeployVM(
return err
}

csMachine.Spec.InstanceID = pointer.String(vm.Id)
csMachine.Spec.InstanceID = pointer.To(vm.Id)
csMachine.Status.InstanceState = vm.State

return fmt.Errorf("incomplete vm deployment (vm_id=%v): %w", vm.Id, err)
}

csMachine.Spec.InstanceID = pointer.String(deployVMResp.Id)
csMachine.Status.Status = pointer.String(metav1.StatusSuccess)
csMachine.Spec.InstanceID = pointer.To(deployVMResp.Id)
csMachine.Status.Status = pointer.To(metav1.StatusSuccess)

return nil
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/cloud/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

. "github.com/onsi/gomega"
"github.com/pkg/errors"
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
)

var _ = Describe("Instance", func() {
Expand Down Expand Up @@ -89,8 +89,8 @@ var _ = Describe("Instance", func() {
vmsResp := &cloudstack.VirtualMachinesMetric{Id: *dummies.CSMachine1.Spec.InstanceID}
vms.EXPECT().GetVirtualMachinesMetricByID(*dummies.CSMachine1.Spec.InstanceID).Return(vmsResp, 1, nil)
Ω(client.ResolveVMInstanceDetails(dummies.CSMachine1)).Should(Succeed())
Ω(dummies.CSMachine1.Spec.ProviderID).Should(Equal(pointer.String("cloudstack:///" + vmsResp.Id)))
Ω(dummies.CSMachine1.Spec.InstanceID).Should(Equal(pointer.String(vmsResp.Id)))
Ω(dummies.CSMachine1.Spec.ProviderID).Should(Equal(pointer.To("cloudstack:///" + vmsResp.Id)))
Ω(dummies.CSMachine1.Spec.InstanceID).Should(Equal(pointer.To(vmsResp.Id)))
})

It("handles an unknown error when fetching by name", func() {
Expand All @@ -115,8 +115,8 @@ var _ = Describe("Instance", func() {

Ω(client.ResolveVMInstanceDetails(dummies.CSMachine1)).Should(Succeed())
Ω(dummies.CSMachine1.Spec.ProviderID).Should(Equal(
pointer.String(fmt.Sprintf("cloudstack:///%s", *dummies.CSMachine1.Spec.InstanceID))))
Ω(dummies.CSMachine1.Spec.InstanceID).Should(Equal(pointer.String(*dummies.CSMachine1.Spec.InstanceID)))
pointer.To(fmt.Sprintf("cloudstack:///%s", *dummies.CSMachine1.Spec.InstanceID))))
Ω(dummies.CSMachine1.Spec.InstanceID).Should(Equal(pointer.To(*dummies.CSMachine1.Spec.InstanceID)))
})
})

Expand Down Expand Up @@ -670,7 +670,7 @@ var _ = Describe("Instance", func() {
dummies.CSMachine1.Spec.Template.ID = ""
dummies.CSMachine1.Spec.Offering.Name = "offering"
dummies.CSMachine1.Spec.Template.Name = "template"
dummies.CSMachine1.Spec.UncompressedUserData = pointer.Bool(true)
dummies.CSMachine1.Spec.UncompressedUserData = pointer.To(true)

vms.EXPECT().
GetVirtualMachinesMetricByID(*dummies.CSMachine1.Spec.InstanceID).
Expand Down
6 changes: 3 additions & 3 deletions test/dummies/v1beta1/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/smallfish/simpleyaml"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
capcv1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -199,7 +199,7 @@ func SetDummyCSMachineVars() {
Kind: "Secret",
Name: "IdentitySecret",
},
InstanceID: pointer.String("Instance1"),
InstanceID: pointer.To("Instance1"),
Template: capcv1.CloudStackResourceIdentifier{
Name: GetYamlVal("CLOUDSTACK_TEMPLATE_NAME"),
},
Expand Down Expand Up @@ -344,7 +344,7 @@ func SetClusterSpecToNet(net *capcv1.Network) {

func SetDummyCAPIMachineVars() {
CAPIMachine = &capiv1.Machine{
Spec: capiv1.MachineSpec{FailureDomain: pointer.String(Zone1.ID)},
Spec: capiv1.MachineSpec{FailureDomain: pointer.To(Zone1.ID)},
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/dummies/v1beta2/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/smallfish/simpleyaml"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
"sigs.k8s.io/cluster-api-provider-cloudstack/test/fakes"
Expand Down Expand Up @@ -228,7 +228,7 @@ func SetDummyCSMachineVars() {
},
Spec: infrav1.CloudStackMachineSpec{
Name: "test-machine-1",
InstanceID: pointer.String("Instance1"),
InstanceID: pointer.To("Instance1"),
FailureDomainName: GetYamlVal("CLOUDSTACK_FD1_NAME"),
Template: infrav1.CloudStackResourceIdentifier{
Name: GetYamlVal("CLOUDSTACK_TEMPLATE_NAME"),
Expand Down Expand Up @@ -451,7 +451,7 @@ func SetDummyCAPIMachineVars() {
},
Spec: clusterv1.MachineSpec{
ClusterName: ClusterName,
FailureDomain: pointer.String("fd1"),
FailureDomain: pointer.To("fd1"),
InfrastructureRef: corev1.ObjectReference{
APIVersion: infrav1.GroupVersion.String(),
Kind: "CloudStackMachine",
Expand Down
6 changes: 3 additions & 3 deletions test/dummies/v1beta3/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/smallfish/simpleyaml"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
"sigs.k8s.io/cluster-api-provider-cloudstack/test/fakes"
Expand Down Expand Up @@ -224,7 +224,7 @@ func SetDummyCSMachineVars() {
},
Spec: infrav1.CloudStackMachineSpec{
Name: "test-machine-1",
InstanceID: pointer.String("Instance1"),
InstanceID: pointer.To("Instance1"),
FailureDomainName: GetYamlVal("CLOUDSTACK_FD1_NAME"),
Template: infrav1.CloudStackResourceIdentifier{
Name: GetYamlVal("CLOUDSTACK_TEMPLATE_NAME"),
Expand Down Expand Up @@ -447,7 +447,7 @@ func SetDummyCAPIMachineVars() {
},
Spec: clusterv1.MachineSpec{
ClusterName: ClusterName,
FailureDomain: pointer.String("fd1"),
FailureDomain: pointer.To("fd1"),
InfrastructureRef: corev1.ObjectReference{
APIVersion: infrav1.GroupVersion.String(),
Kind: "CloudStackMachine",
Expand Down

0 comments on commit 1146199

Please sign in to comment.