Skip to content

Commit 3b86d1a

Browse files
authored
hack/ci,*: check all returned errors (#912)
**Description of the change:** Check all returned errors in functions used by the `operator-sdk`. **Motivation for the change:** We should make sure that all errors are checked in general. Some of these issues don't _really_ affect the code much (specifically the deferred `file.Close()` calls), but some of these could cause difficult to diagnose problems. To find all unchecked errors, the [errcheck](https://github.com/kisielk/errcheck) program can be used. There is a PR to add that to our travis CI, but currently it segfaults when run under travis, so that will need more work.
1 parent c440cef commit 3b86d1a

File tree

21 files changed

+175
-52
lines changed

21 files changed

+175
-52
lines changed

commands/operator-sdk/cmd/add/api.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ Example:
5454
}
5555

5656
apiCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes APIVersion that has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)")
57-
apiCmd.MarkFlagRequired("api-version")
57+
if err := apiCmd.MarkFlagRequired("api-version"); err != nil {
58+
log.Fatalf("Failed to mark `api-version` flag for `add api` subcommand as required")
59+
}
5860
apiCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes resource Kind name. (e.g AppService)")
59-
apiCmd.MarkFlagRequired("kind")
61+
if err := apiCmd.MarkFlagRequired("kind"); err != nil {
62+
log.Fatalf("Failed to mark `kind` flag for `add api` subcommand as required")
63+
}
6064

6165
return apiCmd
6266
}

commands/operator-sdk/cmd/add/controller.go

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

2626
func NewControllerCmd() *cobra.Command {
27-
apiCmd := &cobra.Command{
27+
controllerCmd := &cobra.Command{
2828
Use: "controller",
2929
Short: "Adds a new controller pkg",
3030
Long: `operator-sdk add controller --kind=<kind> --api-version=<group/version> creates a new
@@ -47,12 +47,16 @@ Example:
4747
Run: controllerRun,
4848
}
4949

50-
apiCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes APIVersion that has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)")
51-
apiCmd.MarkFlagRequired("api-version")
52-
apiCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes resource Kind name. (e.g AppService)")
53-
apiCmd.MarkFlagRequired("kind")
50+
controllerCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes APIVersion that has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)")
51+
if err := controllerCmd.MarkFlagRequired("api-version"); err != nil {
52+
log.Fatalf("Failed to mark `api-version` flag for `add controller` subcommand as required")
53+
}
54+
controllerCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes resource Kind name. (e.g AppService)")
55+
if err := controllerCmd.MarkFlagRequired("kind"); err != nil {
56+
log.Fatalf("Failed to mark `kind` flag for `add controller` subcommand as required")
57+
}
5458

55-
return apiCmd
59+
return controllerCmd
5660
}
5761

5862
func controllerRun(cmd *cobra.Command, args []string) {

commands/operator-sdk/cmd/add/crd.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ Generated CR filename: <project-name>/deploy/crds/<group>_<version>_<kind>_cr.y
4343
Run: crdFunc,
4444
}
4545
crdCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes apiVersion and has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)")
46-
crdCmd.MarkFlagRequired("api-version")
46+
if err := crdCmd.MarkFlagRequired("api-version"); err != nil {
47+
log.Fatalf("Failed to mark `api-version` flag for `add crd` subcommand as required")
48+
}
4749
crdCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes CustomResourceDefintion kind. (e.g AppService)")
48-
crdCmd.MarkFlagRequired("kind")
50+
if err := crdCmd.MarkFlagRequired("kind"); err != nil {
51+
log.Fatalf("Failed to mark `kind` flag for `add crd` subcommand as required")
52+
}
4953
return crdCmd
5054
}
5155

commands/operator-sdk/cmd/test/cluster.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"strings"
2121
"time"
2222

23+
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
2324
k8sInternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
2425
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2526
"github.com/operator-framework/operator-sdk/pkg/scaffold"
@@ -168,7 +169,11 @@ func testClusterFunc(cmd *cobra.Command, args []string) error {
168169
if err != nil {
169170
return fmt.Errorf("test failed and failed to get error logs")
170171
}
171-
defer readCloser.Close()
172+
defer func() {
173+
if err := readCloser.Close(); err != nil && !fileutil.IsClosedError(err) {
174+
log.Errorf("Failed to close pod log reader: (%v)", err)
175+
}
176+
}()
172177
buf := new(bytes.Buffer)
173178
_, err = buf.ReadFrom(readCloser)
174179
if err != nil {

commands/operator-sdk/cmd/test/local.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ func replaceImage(manifestPath, image string) error {
240240
foundDeployment = true
241241
scheme := runtime.NewScheme()
242242
// scheme for client go
243-
cgoscheme.AddToScheme(scheme)
243+
if err := cgoscheme.AddToScheme(scheme); err != nil {
244+
log.Fatalf("Failed to add client-go scheme to runtime client: (%v)", err)
245+
}
244246
dynamicDecoder := serializer.NewCodecFactory(scheme).UniversalDeserializer()
245247

246248
obj, _, err := dynamicDecoder.Decode(yamlSpec, nil, nil)

internal/util/yamlutil/manifest.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ func GenerateCombinedNamespacedManifest() (*os.File, error) {
5858
if err != nil {
5959
return nil, err
6060
}
61-
defer file.Close()
61+
defer func() {
62+
if err := file.Close(); err != nil && !fileutil.IsClosedError(err) {
63+
log.Errorf("Failed to close file %s: (%v)", file.Name(), err)
64+
}
65+
}()
6266

6367
sa, err := ioutil.ReadFile(filepath.Join(scaffold.DeployDir, scaffold.ServiceAccountYamlFile))
6468
if err != nil {
@@ -98,7 +102,11 @@ func GenerateCombinedGlobalManifest() (*os.File, error) {
98102
if err != nil {
99103
return nil, err
100104
}
101-
defer file.Close()
105+
defer func() {
106+
if err := file.Close(); err != nil && !fileutil.IsClosedError(err) {
107+
log.Errorf("Failed to close file %s: (%v)", file.Name(), err)
108+
}
109+
}()
102110

103111
files, err := ioutil.ReadDir(scaffold.CrdsDir)
104112
if err != nil {

pkg/ansible/controller/reconcile.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
133133
if err != nil {
134134
return reconcileResult, err
135135
}
136-
defer os.Remove(kc.Name())
136+
defer func() {
137+
if err := os.Remove(kc.Name()); err != nil {
138+
logger.Error(err, "Failed to remove generated kubeconfig file")
139+
}
140+
}()
137141
result, err := r.Runner.Run(ident, u, kc.Name())
138142
if err != nil {
139143
return reconcileResult, err

pkg/ansible/controller/reconcile_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,13 @@ func TestReconcile(t *testing.T) {
501501
if tc.ExpectedObject != nil {
502502
actualObject := &unstructured.Unstructured{}
503503
actualObject.SetGroupVersionKind(tc.ExpectedObject.GroupVersionKind())
504-
tc.Client.Get(context.TODO(), types.NamespacedName{
504+
err := tc.Client.Get(context.TODO(), types.NamespacedName{
505505
Name: tc.ExpectedObject.GetName(),
506506
Namespace: tc.ExpectedObject.GetNamespace(),
507507
}, actualObject)
508+
if err != nil {
509+
t.Fatalf("Failed to get object: (%v)", err)
510+
}
508511
if !reflect.DeepEqual(actualObject.GetAnnotations(), tc.ExpectedObject.GetAnnotations()) {
509512
t.Fatalf("Annotations are not the same\nexpected: %v\nactual: %v", tc.ExpectedObject.GetAnnotations(), actualObject.GetAnnotations())
510513
}

pkg/ansible/controller/status/types.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ func NewAnsibleResultFromMap(sm map[string]interface{}) *AnsibleResult {
7878
}
7979
if v, ok := sm["completion"]; ok {
8080
s := v.(string)
81-
a.TimeOfCompletion.UnmarshalJSON([]byte(s))
81+
if err := a.TimeOfCompletion.UnmarshalJSON([]byte(s)); err != nil {
82+
log.Error(err, "Failed to unmarshal time of completion for ansible result")
83+
}
8284
}
8385
return a
8486
}
@@ -189,6 +191,8 @@ func (status *Status) GetJSONMap() map[string]interface{} {
189191
log.Error(err, "Unable to marshal json")
190192
return status.CustomStatus
191193
}
192-
json.Unmarshal(b, &status.CustomStatus)
194+
if err := json.Unmarshal(b, &status.CustomStatus); err != nil {
195+
log.Error(err, "Unable to unmarshal json")
196+
}
193197
return status.CustomStatus
194198
}

pkg/ansible/proxy/kubeconfig/kubeconfig.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ import (
2323
"net/url"
2424
"os"
2525

26+
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
27+
2628
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
2730
)
2831

32+
var log = logf.Log.WithName("kubeconfig")
33+
2934
// kubectl, as of 1.10.5, only does basic auth if the username is present in
3035
// the URL. The python client used by ansible, as of 6.0.0, only does basic
3136
// auth if the username and password are provided under the "user" key within
@@ -80,7 +85,9 @@ func Create(ownerRef metav1.OwnerReference, proxyURL string, namespace string) (
8085
var parsed bytes.Buffer
8186

8287
t := template.Must(template.New("kubeconfig").Parse(kubeConfigTemplate))
83-
t.Execute(&parsed, v)
88+
if err := t.Execute(&parsed, v); err != nil {
89+
return nil, err
90+
}
8491

8592
file, err := ioutil.TempFile("", "kubeconfig")
8693
if err != nil {
@@ -89,7 +96,11 @@ func Create(ownerRef metav1.OwnerReference, proxyURL string, namespace string) (
8996
// multiple calls to close file will not hurt anything,
9097
// but we don't want to lose the error because we are
9198
// writing to the file, so we will call close twice.
92-
defer file.Close()
99+
defer func() {
100+
if err := file.Close(); err != nil && !fileutil.IsClosedError(err) {
101+
log.Error(err, "Failed to close generated kubeconfig file")
102+
}
103+
}()
93104

94105
if _, err := file.WriteString(parsed.String()); err != nil {
95106
return nil, err

0 commit comments

Comments
 (0)