Skip to content

Commit f82ad97

Browse files
Fix implicit aliasing and other errors (#258)
1 parent cd726b2 commit f82ad97

File tree

6 files changed

+31
-18
lines changed

6 files changed

+31
-18
lines changed

api/v1beta1/conversion.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import (
2424

2525
// Convert_v1beta1_VCN_To_v1beta2_VCN converts v1beta1 VCN to v1beta2 VCN
2626
func Convert_v1beta1_VCN_To_v1beta2_VCN(in *VCN, out *v1beta2.VCN, s conversion.Scope) error {
27-
autoConvert_v1beta1_VCN_To_v1beta2_VCN(in, out, s)
27+
err := autoConvert_v1beta1_VCN_To_v1beta2_VCN(in, out, s)
28+
if err != nil {
29+
return err
30+
}
2831
if in.InternetGatewayId != nil {
2932
out.InternetGateway.Id = in.InternetGatewayId
3033
}
@@ -52,7 +55,10 @@ func Convert_v1beta1_VCN_To_v1beta2_VCN(in *VCN, out *v1beta2.VCN, s conversion.
5255

5356
// Convert_v1beta2_VCN_To_v1beta1_VCN converts v1beta2 VCN to v1beta1 VCN
5457
func Convert_v1beta2_VCN_To_v1beta1_VCN(in *v1beta2.VCN, out *VCN, s conversion.Scope) error {
55-
autoConvert_v1beta2_VCN_To_v1beta1_VCN(in, out, s)
58+
err := autoConvert_v1beta2_VCN_To_v1beta1_VCN(in, out, s)
59+
if err != nil {
60+
return err
61+
}
5662
if in.InternetGateway.Id != nil {
5763
out.InternetGatewayId = in.InternetGateway.Id
5864
}

api/v1beta1/ocicluster_conversion.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ func convertv1beta2NSGListTov1beta1NSGList(in []*v1beta2.NSG) ([]*NSG, error) {
8686

8787
func Convertv1beta1AdMapTov1beta2AdMap(in map[string]OCIAvailabilityDomain) (map[string]v1beta2.OCIAvailabilityDomain, error) {
8888
out := make(map[string]v1beta2.OCIAvailabilityDomain)
89-
for k, v := range in {
89+
for k, _ := range in {
9090
outV := &v1beta2.OCIAvailabilityDomain{}
91-
err := Convert_v1beta1_OCIAvailabilityDomain_To_v1beta2_OCIAvailabilityDomain(&v, outV, nil)
91+
inDomain := in[k]
92+
err := Convert_v1beta1_OCIAvailabilityDomain_To_v1beta2_OCIAvailabilityDomain(&inDomain, outV, nil)
9293
if err != nil {
9394
return nil, err
9495
}
@@ -99,9 +100,10 @@ func Convertv1beta1AdMapTov1beta2AdMap(in map[string]OCIAvailabilityDomain) (map
99100

100101
func Convertv1beta2AdMapTov1beta1AdMap(in map[string]v1beta2.OCIAvailabilityDomain) (map[string]OCIAvailabilityDomain, error) {
101102
out := make(map[string]OCIAvailabilityDomain)
102-
for k, v := range in {
103+
for k, _ := range in {
103104
outV := &OCIAvailabilityDomain{}
104-
err := Convert_v1beta2_OCIAvailabilityDomain_To_v1beta1_OCIAvailabilityDomain(&v, outV, nil)
105+
inDomain := in[k]
106+
err := Convert_v1beta2_OCIAvailabilityDomain_To_v1beta1_OCIAvailabilityDomain(&inDomain, outV, nil)
105107
if err != nil {
106108
return nil, err
107109
}

cloud/config/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package config
1818

1919
import (
2020
"os"
21+
"path/filepath"
2122
"strconv"
2223

2324
"github.com/oracle/oci-go-sdk/v65/common"
@@ -64,7 +65,7 @@ func FromDir(path string) (*AuthConfig, error) {
6465

6566
func getConfigFromFile(path string) (authConfig *AuthConfig, err error) {
6667
var f *os.File
67-
f, err = os.Open(path)
68+
f, err = os.Open(filepath.Clean(path))
6869
if err != nil {
6970
return nil, err
7071
}
@@ -168,7 +169,8 @@ func NewConfigurationProviderWithUserPrincipal(cfg *AuthConfig) (common.Configur
168169
}
169170

170171
func ReadFile(path string, key string) (string, error) {
171-
b, err := os.ReadFile(path + "/" + key)
172+
filePath := filepath.Join(path, key)
173+
b, err := os.ReadFile(filepath.Clean(filePath))
172174
if err != nil {
173175
return "", err
174176
}

cloud/scope/clients_mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func MockAuthConfig() (config.AuthConfig, error) {
9797
}
9898

9999
func generatePrivateKeyPEM() (string, error) {
100-
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
100+
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
101101
if err != nil {
102102
return "", err
103103
}

cloud/scope/machine.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ package scope
1818

1919
import (
2020
"context"
21+
"crypto/rand"
2122
"encoding/base64"
2223
"fmt"
23-
"math/rand"
24+
"math/big"
2425
"net/url"
2526
"strconv"
26-
"time"
2727

2828
"github.com/go-logr/logr"
2929
infrastructurev1beta2 "github.com/oracle/cluster-api-provider-oci/api/v1beta2"
@@ -195,10 +195,13 @@ func (m *MachineScope) GetOrCreateMachine(ctx context.Context) (*core.Instance,
195195
failureDomain := m.Machine.Spec.FailureDomain
196196
if failureDomain == nil {
197197
m.Logger.Info("Failure Domain is not set in the machine spec, setting it to a random value from 1 to 3")
198-
rand.Seed(time.Now().UnixNano())
199-
// rand.Intn(3) will produce a random number from 0(inclusive) to 3(exclusive)
200-
// ee add one to get a number from 1 to 3
201-
failureDomain = common.String(strconv.Itoa(rand.Intn(3) + 1))
198+
randomFaultDomain, err := rand.Int(rand.Reader, big.NewInt(3))
199+
if err != nil {
200+
m.Logger.Error(err, "Failed to generate random fault domain")
201+
return nil, err
202+
}
203+
// the random number generated is between zero and two, whereas we need a number between one and three
204+
failureDomain = common.String(strconv.Itoa(int(randomFaultDomain.Int64()) + 1))
202205
}
203206
failureDomainIndex, err := strconv.Atoi(*failureDomain)
204207
if err != nil {

cloud/scope/machine_pool.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,9 @@ func (m *MachinePoolScope) FindInstancePool(ctx context.Context) (*core.Instance
530530
}
531531

532532
var instancePoolSummary *core.InstancePoolSummary
533-
for _, i := range instancePoolSummaries {
534-
if m.IsResourceCreatedByClusterAPI(i.FreeformTags) {
535-
instancePoolSummary = &i
533+
for i, summary := range instancePoolSummaries {
534+
if m.IsResourceCreatedByClusterAPI(summary.FreeformTags) {
535+
instancePoolSummary = &instancePoolSummaries[i]
536536
break
537537
}
538538
}

0 commit comments

Comments
 (0)