fix: golangci linting errors

This commit is contained in:
Ansh Garhewal 2024-07-21 19:05:18 +05:30
parent 87ec9f8c2a
commit b2f6f482ca
No known key found for this signature in database
10 changed files with 54 additions and 25 deletions

View File

@ -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")
}
}

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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,

View File

@ -16,5 +16,5 @@ func randomString(n int) string {
}
func init() {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
}

View File

@ -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"

View File

@ -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.

View File

@ -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)
}

View File

@ -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)
}
}
}