Skip to content

Commit

Permalink
Merge pull request #4895 from sbueringer/pr-fix-unhandled-errors
Browse files Browse the repository at this point in the history
🐛  fix unhandled errors
  • Loading branch information
k8s-ci-robot authored Jul 15, 2021
2 parents ed520a9 + 4456b51 commit c5bbf36
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ issues:
# The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time.
# If it is decided they will not be addressed they should be moved above this comment.
- Subprocess launch(ed with variable|ing should be audited)
- (G104|G307)
- 'G307: Deferring unsafe method "Close" on type "\*os.File"'
exclude-rules:
- linters:
- gosec
Expand Down
6 changes: 3 additions & 3 deletions cmd/clusterctl/client/config/reader_viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func Test_viperReader_Get(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(dir)

os.Setenv("FOO", "foo")
_ = os.Setenv("FOO", "foo")

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

os.Setenv("FOO_FOO", "bar")
_ = os.Setenv("FOO_FOO", "bar")

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

os.Setenv("FOO", "foo")
_ = os.Setenv("FOO", "foo")

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

Expand Down
6 changes: 4 additions & 2 deletions cmd/clusterctl/cmd/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ func printComponents(c client.Components, output string) error {
if _, err := os.Stdout.Write(yaml); err != nil {
return errors.Wrap(err, "failed to write yaml to Stdout")
}
os.Stdout.WriteString("\n")
return err
if _, err := os.Stdout.WriteString("\n"); err != nil {
return errors.Wrap(err, "failed to write trailing new line of yaml to Stdout")
}
return nil
}
return nil
}
3 changes: 1 addition & 2 deletions cmd/clusterctl/cmd/config_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,5 @@ func runGetRepositories(cfgFile string, out io.Writer) error {
}
fmt.Fprint(w, string(y))
}
w.Flush()
return nil
return w.Flush()
}
3 changes: 2 additions & 1 deletion cmd/clusterctl/cmd/generate_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func createTempFile(g *WithT, contents string) (string, func()) {
g.Expect(os.WriteFile(templateFile, []byte(contents), 0600)).To(Succeed())

return templateFile, func() {
os.RemoveAll(dir)
// We don't want to fail if the deletion of the temp file fails, so we ignore the error here
_ = os.RemoveAll(dir)
}
}
4 changes: 3 additions & 1 deletion cmd/clusterctl/cmd/upgrade_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func runUpgradePlan() error {
upgradeAvailable = true
}
}
w.Flush()
if err := w.Flush(); err != nil {
return err
}
fmt.Println("")

if upgradeAvailable {
Expand Down
4 changes: 3 additions & 1 deletion cmd/clusterctl/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func printVariablesOutput(template client.Template, options client.GetClusterTem
for _, v := range optionalVariables {
fmt.Fprintf(w, " - %s\t(defaults to %s)\n", v, *variableMap[v])
}
w.Flush()
if err := w.Flush(); err != nil {
return err
}
}

fmt.Println()
Expand Down
3 changes: 2 additions & 1 deletion cmd/clusterctl/cmd/version_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ func generateTempVersionFilePath(g *WithT) (string, func()) {
tmpVersionFile := filepath.Join(dir, "clusterctl", "state.yaml")

return tmpVersionFile, func() {
os.RemoveAll(dir)
// We don't want to fail if the deletion of the temp file fails, so we ignore the error here
_ = os.RemoveAll(dir)
}
}
6 changes: 5 additions & 1 deletion controllers/machinedeployment_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ func (r *MachineDeploymentReconciler) getNewMachineSet(ctx context.Context, d *c

// new MachineSet does not exist, create one.
newMSTemplate := *d.Spec.Template.DeepCopy()
machineTemplateSpecHash := fmt.Sprintf("%d", mdutil.ComputeHash(&newMSTemplate))
hash, err := mdutil.ComputeSpewHash(&newMSTemplate)
if err != nil {
return nil, err
}
machineTemplateSpecHash := fmt.Sprintf("%d", hash)
newMSTemplate.Labels = mdutil.CloneAndAddLabel(d.Spec.Template.Labels,
mdutil.DefaultMachineDeploymentUniqueLabelKey, machineTemplateSpecHash)

Expand Down
35 changes: 33 additions & 2 deletions controllers/mdutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey, labelVal
// DeepHashObject writes specified object to hash using the spew library
// which follows pointers and prints actual values of the nested objects
// ensuring the hash does not change when a pointer changes.
// Deprecated: Please use controllers/mdutil SpewHashObject(hasher, objectToWrite).
func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
hasher.Reset()
printer := spew.ConfigState{
Expand All @@ -701,16 +702,46 @@ func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
DisableMethods: true,
SpewKeys: true,
}
printer.Fprintf(hasher, "%#v", objectToWrite)
// We ignore the returned error because there is no way to return the error without
// breaking API compatibility. Please use SpewHashObject instead.
_, _ = printer.Fprintf(hasher, "%#v", objectToWrite)
}

// ComputeHash computes the has of a MachineTemplateSpec.
// SpewHashObject writes specified object to hash using the spew library
// which follows pointers and prints actual values of the nested objects
// ensuring the hash does not change when a pointer changes.
func SpewHashObject(hasher hash.Hash, objectToWrite interface{}) error {
hasher.Reset()
printer := spew.ConfigState{
Indent: " ",
SortKeys: true,
DisableMethods: true,
SpewKeys: true,
}

if _, err := printer.Fprintf(hasher, "%#v", objectToWrite); err != nil {
return fmt.Errorf("failed to write object to hasher")
}
return nil
}

// ComputeHash computes the hash of a MachineTemplateSpec using the spew library.
// Deprecated: Please use controllers/mdutil ComputeSpewHash(template).
func ComputeHash(template *clusterv1.MachineTemplateSpec) uint32 {
machineTemplateSpecHasher := fnv.New32a()
DeepHashObject(machineTemplateSpecHasher, *template)
return machineTemplateSpecHasher.Sum32()
}

// ComputeSpewHash computes the hash of a MachineTemplateSpec using the spew library.
func ComputeSpewHash(template *clusterv1.MachineTemplateSpec) (uint32, error) {
machineTemplateSpecHasher := fnv.New32a()
if err := SpewHashObject(machineTemplateSpecHasher, *template); err != nil {
return 0, err
}
return machineTemplateSpecHasher.Sum32(), nil
}

// GetDeletingMachineCount gets the number of machines that are in the process of being deleted
// in a machineList.
func GetDeletingMachineCount(machineList *clusterv1.MachineList) int32 {
Expand Down
2 changes: 1 addition & 1 deletion controllers/remote/cluster_cache_healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestClusterCacheHealthCheck(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())
l, err := net.ListenTCP("tcp", addr)
g.Expect(err).NotTo(HaveOccurred())
l.Close()
g.Expect(l.Close()).To(Succeed())

config := rest.CopyConfig(env.Config)
config.Host = fmt.Sprintf("http://127.0.0.1:%d", l.Addr().(*net.TCPAddr).Port)
Expand Down
4 changes: 3 additions & 1 deletion internal/envtest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ func (e *Environment) WaitForWebhooks() {
klog.V(2).Infof("Webhook port is not ready, will retry in %v: %s", timeout, err)
continue
}
conn.Close()
if err := conn.Close(); err != nil {
klog.V(2).Infof("Closing connection when testing if webhook port is ready failed: %v", err)
}
klog.V(2).Info("Webhook port is now open. Continuing with tests...")
return
}
Expand Down

0 comments on commit c5bbf36

Please sign in to comment.