Skip to content

Commit

Permalink
Add errcheck to test suite to ensure all errors produced are checked
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelasp committed Aug 5, 2022
1 parent c577834 commit 9d13654
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 9 deletions.
13 changes: 13 additions & 0 deletions build/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,16 @@ if [ -n "${ERRS}" ]; then
fi
echo "PASS"
echo

echo -n "Checking errcheck: "
go install github.com/kisielk/errcheck@latest 2>/dev/null
ERRS=$(errcheck -exclude errcheck_excludes.txt -ignoretests ${TARGETS} 2>&1 || true)
if [ -n "${ERRS}" ]; then
echo "FAIL"
echo "${ERRS}"
echo
exit 1
fi
echo "PASS"
echo

4 changes: 3 additions & 1 deletion cmd/echo/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func process(w http.ResponseWriter, r *http.Request, b []byte) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("failed to compress data: %v", err)
}
zw.Close()
if err := zw.Close(); err != nil {
return nil, fmt.Errorf("failed to close gzip writer: %v", err)
}
w.Header().Set("Content-Encoding", "gzip")
return compressed.Bytes(), nil
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/echo/app/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ func generateInsecureCertAndKey(organization string, validFrom time.Time, validF
klog.Fatalf("Failed to create certificate: %s", err)
}
var certBytes bytes.Buffer
pem.Encode(&certBytes, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err := pem.Encode(&certBytes, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
klog.Fatalf("Failed to encode certificate: %v", err)
}

var keyBytes bytes.Buffer
pb := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}
pem.Encode(&keyBytes, pb)
if err := pem.Encode(&keyBytes, pb); err != nil {
klog.Fatalf("Failed to encode RSA Key: %v", err)
}

return certBytes.Bytes(), keyBytes.Bytes(), nil
}
4 changes: 3 additions & 1 deletion cmd/glbc/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ func getFlagPage(w http.ResponseWriter, r *http.Request) {
Version: version.Version,
Verbosity: flag.Lookup("v").Value.String(),
}
flagPageTemplate.Execute(w, s)
if err := flagPageTemplate.Execute(w, s); err != nil {
klog.Errorf("Unable to apply flag page template: %v", err)
}
}

var flagPageTemplate = template.Must(template.New("").Parse(`GCE Ingress Controller "GLBC"
Expand Down
5 changes: 4 additions & 1 deletion cmd/glbc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ func runControllers(ctx *ingctx.ControllerContext) {
lbc := controller.NewLoadBalancerController(ctx, stopCh)
if ctx.EnableASMConfigMap {
ctx.ASMConfigController.RegisterInformer(ctx.ConfigMapInformer, func() {
lbc.Stop(false) // We want to trigger a restart, don't have to clean up all the resources.
// We want to trigger a restart, don't have to clean up all the resources.
if err := lbc.Stop(false); err != nil {
klog.Errorf("Failed to stop the load balancer controller: %v", err)
}
})
}

Expand Down
11 changes: 11 additions & 0 deletions errcheck_excludes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fmt.Fprintf
fmt.Fprint
(net/http.ResponseWriter).Write
(*net/http.Server).Shutdown
(*flag.FlagSet).Parse
(*os.File).Close
(io.Closer).Close
(flag.Value).Set

// We sometimes call this with a function that has no chance to error
k8s.io/apimachinery/pkg/util/wait.PollUntil
4 changes: 3 additions & 1 deletion pkg/composite/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ func populateApiServices() {
}

var result map[string]interface{}
json.Unmarshal([]byte(byteValue), &result)
if err := json.Unmarshal([]byte(byteValue), &result); err != nil {
panic(err)
}

// Queue of ApiService names for BFS
typesQueue := []string{}
Expand Down
4 changes: 3 additions & 1 deletion pkg/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ var Scheme = runtime.NewScheme()

func init() {
// Register external types for Scheme
v1.AddToScheme(Scheme)
if err := v1.AddToScheme(Scheme); err != nil {
panic(err)
}
}

// IsRfc1918Addr returns true if the address supplied is an RFC1918 address
Expand Down
4 changes: 3 additions & 1 deletion pkg/firewalls/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ func (fwc *FirewallController) sync(key string) error {

// If there are no more ingresses, then delete the firewall rule.
if len(gceIngresses) == 0 {
fwc.firewallPool.GC()
if err := fwc.firewallPool.GC(); err != nil {
klog.Errorf("Could not garbage collect firewall pool, got error: %v", err)
}
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/loadbalancers/address_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ func (am *addressManager) TearDownAddressIPIfNetworkTierMismatch() error {
return utils.NewNetworkTierErr(fmt.Sprintf("User specific address IP (%v)", am.name), string(am.networkTier), addr.NetworkTier)
}
klog.V(3).Infof("Deleting IP address %v because has wrong network tier", am.targetIP)
am.svc.DeleteRegionAddress(addr.Name, am.targetIP)
if err := am.svc.DeleteRegionAddress(addr.Name, am.targetIP); err != nil {
klog.Errorf("Unable to delete region address %s on target ip %s, err: %v", addr.Name, am.targetIP, err)
}
}
return nil
}

0 comments on commit 9d13654

Please sign in to comment.