Skip to content

Commit c5bbf36

Browse files
authored
Merge pull request #4895 from sbueringer/pr-fix-unhandled-errors
🐛 fix unhandled errors
2 parents ed520a9 + 4456b51 commit c5bbf36

File tree

12 files changed

+61
-17
lines changed

12 files changed

+61
-17
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ issues:
7474
# The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time.
7575
# If it is decided they will not be addressed they should be moved above this comment.
7676
- Subprocess launch(ed with variable|ing should be audited)
77-
- (G104|G307)
77+
- 'G307: Deferring unsafe method "Close" on type "\*os.File"'
7878
exclude-rules:
7979
- linters:
8080
- gosec

cmd/clusterctl/client/config/reader_viper_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func Test_viperReader_Get(t *testing.T) {
125125
g.Expect(err).NotTo(HaveOccurred())
126126
defer os.RemoveAll(dir)
127127

128-
os.Setenv("FOO", "foo")
128+
_ = os.Setenv("FOO", "foo")
129129

130130
configFile := filepath.Join(dir, "clusterctl.yaml")
131131
g.Expect(os.WriteFile(configFile, []byte("bar: bar"), 0600)).To(Succeed())
@@ -190,7 +190,7 @@ func Test_viperReader_GetWithoutDefaultConfig(t *testing.T) {
190190
g.Expect(err).NotTo(HaveOccurred())
191191
defer os.RemoveAll(dir)
192192

193-
os.Setenv("FOO_FOO", "bar")
193+
_ = os.Setenv("FOO_FOO", "bar")
194194

195195
v := newViperReader(injectConfigPaths([]string{dir}))
196196
g.Expect(v.Init("")).To(Succeed())
@@ -207,7 +207,7 @@ func Test_viperReader_Set(t *testing.T) {
207207
g.Expect(err).NotTo(HaveOccurred())
208208
defer os.RemoveAll(dir)
209209

210-
os.Setenv("FOO", "foo")
210+
_ = os.Setenv("FOO", "foo")
211211

212212
configFile := filepath.Join(dir, "clusterctl.yaml")
213213

cmd/clusterctl/cmd/config_provider.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,10 @@ func printComponents(c client.Components, output string) error {
187187
if _, err := os.Stdout.Write(yaml); err != nil {
188188
return errors.Wrap(err, "failed to write yaml to Stdout")
189189
}
190-
os.Stdout.WriteString("\n")
191-
return err
190+
if _, err := os.Stdout.WriteString("\n"); err != nil {
191+
return errors.Wrap(err, "failed to write trailing new line of yaml to Stdout")
192+
}
193+
return nil
192194
}
193195
return nil
194196
}

cmd/clusterctl/cmd/config_repositories.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,5 @@ func runGetRepositories(cfgFile string, out io.Writer) error {
110110
}
111111
fmt.Fprint(w, string(y))
112112
}
113-
w.Flush()
114-
return nil
113+
return w.Flush()
115114
}

cmd/clusterctl/cmd/generate_yaml_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func createTempFile(g *WithT, contents string) (string, func()) {
124124
g.Expect(os.WriteFile(templateFile, []byte(contents), 0600)).To(Succeed())
125125

126126
return templateFile, func() {
127-
os.RemoveAll(dir)
127+
// We don't want to fail if the deletion of the temp file fails, so we ignore the error here
128+
_ = os.RemoveAll(dir)
128129
}
129130
}

cmd/clusterctl/cmd/upgrade_plan.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ func runUpgradePlan() error {
116116
upgradeAvailable = true
117117
}
118118
}
119-
w.Flush()
119+
if err := w.Flush(); err != nil {
120+
return err
121+
}
120122
fmt.Println("")
121123

122124
if upgradeAvailable {

cmd/clusterctl/cmd/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ func printVariablesOutput(template client.Template, options client.GetClusterTem
129129
for _, v := range optionalVariables {
130130
fmt.Fprintf(w, " - %s\t(defaults to %s)\n", v, *variableMap[v])
131131
}
132-
w.Flush()
132+
if err := w.Flush(); err != nil {
133+
return err
134+
}
133135
}
134136

135137
fmt.Println()

cmd/clusterctl/cmd/version_checker_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ func generateTempVersionFilePath(g *WithT) (string, func()) {
380380
tmpVersionFile := filepath.Join(dir, "clusterctl", "state.yaml")
381381

382382
return tmpVersionFile, func() {
383-
os.RemoveAll(dir)
383+
// We don't want to fail if the deletion of the temp file fails, so we ignore the error here
384+
_ = os.RemoveAll(dir)
384385
}
385386
}

controllers/machinedeployment_sync.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ func (r *MachineDeploymentReconciler) getNewMachineSet(ctx context.Context, d *c
137137

138138
// new MachineSet does not exist, create one.
139139
newMSTemplate := *d.Spec.Template.DeepCopy()
140-
machineTemplateSpecHash := fmt.Sprintf("%d", mdutil.ComputeHash(&newMSTemplate))
140+
hash, err := mdutil.ComputeSpewHash(&newMSTemplate)
141+
if err != nil {
142+
return nil, err
143+
}
144+
machineTemplateSpecHash := fmt.Sprintf("%d", hash)
141145
newMSTemplate.Labels = mdutil.CloneAndAddLabel(d.Spec.Template.Labels,
142146
mdutil.DefaultMachineDeploymentUniqueLabelKey, machineTemplateSpecHash)
143147

controllers/mdutil/util.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey, labelVal
693693
// DeepHashObject writes specified object to hash using the spew library
694694
// which follows pointers and prints actual values of the nested objects
695695
// ensuring the hash does not change when a pointer changes.
696+
// Deprecated: Please use controllers/mdutil SpewHashObject(hasher, objectToWrite).
696697
func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
697698
hasher.Reset()
698699
printer := spew.ConfigState{
@@ -701,16 +702,46 @@ func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
701702
DisableMethods: true,
702703
SpewKeys: true,
703704
}
704-
printer.Fprintf(hasher, "%#v", objectToWrite)
705+
// We ignore the returned error because there is no way to return the error without
706+
// breaking API compatibility. Please use SpewHashObject instead.
707+
_, _ = printer.Fprintf(hasher, "%#v", objectToWrite)
705708
}
706709

707-
// ComputeHash computes the has of a MachineTemplateSpec.
710+
// SpewHashObject writes specified object to hash using the spew library
711+
// which follows pointers and prints actual values of the nested objects
712+
// ensuring the hash does not change when a pointer changes.
713+
func SpewHashObject(hasher hash.Hash, objectToWrite interface{}) error {
714+
hasher.Reset()
715+
printer := spew.ConfigState{
716+
Indent: " ",
717+
SortKeys: true,
718+
DisableMethods: true,
719+
SpewKeys: true,
720+
}
721+
722+
if _, err := printer.Fprintf(hasher, "%#v", objectToWrite); err != nil {
723+
return fmt.Errorf("failed to write object to hasher")
724+
}
725+
return nil
726+
}
727+
728+
// ComputeHash computes the hash of a MachineTemplateSpec using the spew library.
729+
// Deprecated: Please use controllers/mdutil ComputeSpewHash(template).
708730
func ComputeHash(template *clusterv1.MachineTemplateSpec) uint32 {
709731
machineTemplateSpecHasher := fnv.New32a()
710732
DeepHashObject(machineTemplateSpecHasher, *template)
711733
return machineTemplateSpecHasher.Sum32()
712734
}
713735

736+
// ComputeSpewHash computes the hash of a MachineTemplateSpec using the spew library.
737+
func ComputeSpewHash(template *clusterv1.MachineTemplateSpec) (uint32, error) {
738+
machineTemplateSpecHasher := fnv.New32a()
739+
if err := SpewHashObject(machineTemplateSpecHasher, *template); err != nil {
740+
return 0, err
741+
}
742+
return machineTemplateSpecHasher.Sum32(), nil
743+
}
744+
714745
// GetDeletingMachineCount gets the number of machines that are in the process of being deleted
715746
// in a machineList.
716747
func GetDeletingMachineCount(machineList *clusterv1.MachineList) int32 {

0 commit comments

Comments
 (0)