From b2f6f482cad8351cff353882c877423070a23ade Mon Sep 17 00:00:00 2001 From: Ansh Garhewal Date: Sun, 21 Jul 2024 19:05:18 +0530 Subject: [PATCH] fix: golangci linting errors --- api/v1alpha2/jenkins_webhook.go | 41 +++++++++++++++---- pkg/client/jenkins.go | 7 ++-- pkg/client/script.go | 6 ++- pkg/client/token.go | 5 ++- .../base/resources/deployment.go | 4 +- pkg/configuration/base/resources/random.go | 2 +- pkg/configuration/base/validate.go | 2 +- pkg/configuration/configuration.go | 4 +- pkg/groovy/groovy.go | 4 +- pkg/notifications/sender.go | 4 +- 10 files changed, 54 insertions(+), 25 deletions(-) diff --git a/api/v1alpha2/jenkins_webhook.go b/api/v1alpha2/jenkins_webhook.go index 47a3efbd..d811d4d7 100644 --- a/api/v1alpha2/jenkins_webhook.go +++ b/api/v1alpha2/jenkins_webhook.go @@ -21,7 +21,6 @@ import ( "encoding/json" "errors" "io" - "io/ioutil" "net/http" "os" "time" @@ -271,7 +270,7 @@ func (in *SecurityValidator) download() error { } defer func() { if err := out.Close(); err != nil { - jenkinslog.V(log.VDebug).Info("Failed to close file", "error", err) + jenkinslog.V(log.VDebug).Info("Failed to close SecurityValidator.download io", "error", err) } }() @@ -290,10 +289,10 @@ func (in *SecurityValidator) download() error { return err } - defer response.Body.Close() + defer httpResponseCloser(response) if err := out.Close(); err != nil { - jenkinslog.V(log.VDebug).Info("Failed to send file", err) + jenkinslog.V(log.VDebug).Info("Failed to send file", "error", err.Error()) } _, err = io.Copy(out, response.Body) @@ -306,18 +305,32 @@ func (in *SecurityValidator) extract() error { if err != nil { return err } - defer reader.Close() + defer func() { + if err := reader.Close(); err != nil { + log.Log.Error(err, "failed to close SecurityValidator.extract.reader ") + } + }() + archive, err := gzip.NewReader(reader) if err != nil { return err } - defer archive.Close() + defer func() { + if err := archive.Close(); err != nil { + log.Log.Error(err, "failed to close SecurityValidator.extract.archive ") + } + }() writer, err := os.Create(PluginDataFile) if err != nil { return err } - defer writer.Close() + + defer func() { + if err := writer.Close(); err != nil { + log.Log.Error(err, "failed to close SecurityValidator.extract.writer") + } + }() _, err = io.Copy(writer, archive) return err @@ -329,8 +342,12 @@ func (in *SecurityValidator) cache() error { if err != nil { return err } - defer jsonFile.Close() - byteValue, err := ioutil.ReadAll(jsonFile) + defer func() { + if err := jsonFile.Close(); err != nil { + log.Log.Error(err, "failed to close SecurityValidator.cache.jsonFile") + } + }() + byteValue, err := io.ReadAll(jsonFile) if err != nil { return err } @@ -356,3 +373,9 @@ func compareVersions(firstVersion string, lastVersion string, pluginVersion stri } return true } + +func httpResponseCloser(response *http.Response) { + if err := response.Body.Close(); err != nil { + log.Log.Error(err, "failed to close http response body") + } +} diff --git a/pkg/client/jenkins.go b/pkg/client/jenkins.go index 3216b5be..807e1d92 100644 --- a/pkg/client/jenkins.go +++ b/pkg/client/jenkins.go @@ -144,9 +144,10 @@ func NewBearerTokenAuthorization(url, token string) (Jenkins, error) { } func newClient(url, userName, passwordOrToken string) (Jenkins, error) { - if strings.HasSuffix(url, "/") { - url = url[:len(url)-1] - } + // if strings.HasSuffix(url, "/") { + // url = url[:len(url)-1] + url = strings.TrimSuffix(url, "/") + // } jenkinsClient := &jenkins{} jenkinsClient.Server = url diff --git a/pkg/client/script.go b/pkg/client/script.go index 108cf783..ae6f10ef 100644 --- a/pkg/client/script.go +++ b/pkg/client/script.go @@ -10,6 +10,7 @@ import ( "time" "github.com/bndr/gojenkins" + "github.com/jenkinsci/kubernetes-operator/pkg/log" "github.com/pkg/errors" ) @@ -49,8 +50,9 @@ func (jenkins *jenkins) executeScript(script string, verifier string) (string, e if err != nil { return "", errors.Wrapf(err, "couldn't execute groovy script, logs '%s'", output) } - defer r.Body.Close() - + if err := r.Body.Close(); err != nil { + log.Log.Error(err, "failed to close jenkins.executeScript.Requester") + } if r.StatusCode != http.StatusOK { return output, errors.Errorf("invalid status code '%d', logs '%s'", r.StatusCode, output) } diff --git a/pkg/client/token.go b/pkg/client/token.go index 5cb401fb..161e7c80 100644 --- a/pkg/client/token.go +++ b/pkg/client/token.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" + "github.com/jenkinsci/kubernetes-operator/pkg/log" "github.com/pkg/errors" ) @@ -40,7 +41,9 @@ func (jenkins *jenkins) GenerateToken(userName, tokenName string) (*UserToken, e return nil, errors.Wrap(err, "couldn't generate API token") } defer r.Body.Close() - + if err := r.Body.Close(); err != nil { + log.Log.Error(err, "failed to close jenkins.GenerateToken.Requester") + } if r.StatusCode == http.StatusOK { if token.raw.Status == "ok" { return token, nil diff --git a/pkg/configuration/base/resources/deployment.go b/pkg/configuration/base/resources/deployment.go index fe0b1c0c..609b206c 100644 --- a/pkg/configuration/base/resources/deployment.go +++ b/pkg/configuration/base/resources/deployment.go @@ -8,7 +8,7 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/pointer" + "k8s.io/utils/ptr" ) // NewJenkinsMasterPod builds Jenkins Master Kubernetes Pod resource. @@ -24,7 +24,7 @@ func NewJenkinsDeployment(objectMeta metav1.ObjectMeta, jenkins *v1alpha2.Jenkin Labels: objectMeta.Labels, }, Spec: appsv1.DeploymentSpec{ - Replicas: pointer.Int32Ptr(1), + Replicas: ptr.To(int32(1)), Strategy: appsv1.DeploymentStrategy{Type: appsv1.RollingUpdateDeploymentStrategyType}, Template: corev1.PodTemplateSpec{ ObjectMeta: objectMeta, diff --git a/pkg/configuration/base/resources/random.go b/pkg/configuration/base/resources/random.go index 00ec62b2..d6a4ca5e 100644 --- a/pkg/configuration/base/resources/random.go +++ b/pkg/configuration/base/resources/random.go @@ -16,5 +16,5 @@ func randomString(n int) string { } func init() { - rand.Seed(time.Now().UnixNano()) + rand.New(rand.NewSource(time.Now().UnixNano())) } diff --git a/pkg/configuration/base/validate.go b/pkg/configuration/base/validate.go index ae3bf622..3f396690 100644 --- a/pkg/configuration/base/validate.go +++ b/pkg/configuration/base/validate.go @@ -11,7 +11,7 @@ import ( "github.com/jenkinsci/kubernetes-operator/pkg/constants" "github.com/jenkinsci/kubernetes-operator/pkg/plugins" - docker "github.com/docker/distribution/reference" + docker "github.com/distribution/reference" stackerr "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index 1f651b83..40790f51 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -155,7 +155,7 @@ func (c *Configuration) Exec(podName, containerName string, command []string) (s return stdout, stderr, stackerr.Wrap(err, "pod exec error while creating Executor") } - err = exec.Stream(remotecommand.StreamOptions{ + err = exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{ Stdin: nil, Stdout: &stdout, Stderr: &stderr, @@ -165,7 +165,7 @@ func (c *Configuration) Exec(podName, containerName string, command []string) (s return stdout, stderr, stackerr.Wrapf(err, "pod exec error operation on stream: stdout '%s' stderr '%s'", stdout.String(), stderr.String()) } - return + return bytes.Buffer{}, bytes.Buffer{}, nil } // GetJenkinsMasterContainer returns the Jenkins master container from the CR. diff --git a/pkg/groovy/groovy.go b/pkg/groovy/groovy.go index ae4f2249..1250c0eb 100644 --- a/pkg/groovy/groovy.go +++ b/pkg/groovy/groovy.go @@ -59,7 +59,7 @@ func (g *Groovy) EnsureSingle(source, name, hash, groovyScript string) (requeue return true, err } - var appliedGroovyScripts []v1alpha2.AppliedGroovyScript + appliedGroovyScripts := []v1alpha2.AppliedGroovyScript{} for _, ags := range g.jenkins.Status.AppliedGroovyScripts { if g.configurationType == ags.ConfigurationType && ags.Source == source && ags.Name == name { @@ -182,7 +182,7 @@ func (g *Groovy) isGroovyScriptAlreadyApplied(source, name, hash string) bool { func (g *Groovy) calculateHash(data map[string]string) (string, error) { hash := sha256.New() - var keys []string + keys := []string{} for key := range data { keys = append(keys, key) } diff --git a/pkg/notifications/sender.go b/pkg/notifications/sender.go index 6e167b3c..05cfbfde 100644 --- a/pkg/notifications/sender.go +++ b/pkg/notifications/sender.go @@ -64,7 +64,7 @@ func Listen(events chan event.Event, k8sEvent k8sevent.Recorder, k8sClient k8scl continue // skip the event } - go func(notificationConfig v1alpha2.Notification) { + go func(notificationConfig v1alpha2.Notification, e event.Event) { err = provider.Send(e) if err != nil { wrapped := errors.WithMessage(err, @@ -75,7 +75,7 @@ func Listen(events chan event.Event, k8sEvent k8sevent.Recorder, k8sClient k8scl logger.Error(nil, fmt.Sprintf("%s", wrapped)) } } - }(notificationConfig) + }(notificationConfig, e) } } }