Skip to content

Commit

Permalink
fix sonar issues (stolostron#901)
Browse files Browse the repository at this point in the history
Signed-off-by: ldpliu <[email protected]>
  • Loading branch information
ldpliu authored Apr 29, 2024
1 parent c8c17db commit 8fb9c6f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion manager/pkg/nonk8sapi/authentication/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func createClient(clusterAPICABundle []byte) (*http.Client, error) {
/* #nosec G402*/
tlsConfig := &tls.Config{
//nolint:gosec
InsecureSkipVerify: true,
InsecureSkipVerify: true, // #nosec G402
}

if clusterAPICABundle != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

var errOptimisticConcurrencyUpdateFailed = errors.New("zero rows were affected by an optimistic concurrency update")

const failedQueryMsg = "failed to query table spec.%s - %w"

// PostgreSQL abstracts PostgreSQL client.
type PostgreSQL struct {
conn *pgxpool.Pool
Expand Down Expand Up @@ -130,7 +132,7 @@ func (p *PostgreSQL) GetObjectsBundle(ctx context.Context, tableName string, cre
payload->'metadata'->'labels'->'global-hub.open-cluster-management.io/global-resource' IS NOT NULL`,
tableName))
if err != nil {
return nil, fmt.Errorf("failed to query table spec.%s - %w", tableName, err)
return nil, fmt.Errorf(failedQueryMsg, tableName, err)
}

defer rows.Close()
Expand Down Expand Up @@ -168,7 +170,7 @@ func (p *PostgreSQL) GetUpdatedManagedClusterLabelsBundles(ctx context.Context,
from spec.%[1]s WHERE updated_at::timestamp > timestamp '%[2]s') AND leaf_hub_name <> ''`, tableName,
timestamp.Format(time.RFC3339Nano)))
if err != nil {
return nil, fmt.Errorf("failed to query table spec.%s - %w", tableName, err)
return nil, fmt.Errorf(failedQueryMsg, tableName, err)
}

defer rows.Close()
Expand Down Expand Up @@ -215,7 +217,7 @@ func (p *PostgreSQL) GetEntriesWithDeletedLabels(ctx context.Context,
rows, err := p.conn.Query(ctx, fmt.Sprintf(`SELECT leaf_hub_name,managed_cluster_name,deleted_label_keys,version
FROM spec.%s WHERE deleted_label_keys != '[]' AND leaf_hub_name <> ''`, tableName))
if err != nil {
return nil, fmt.Errorf("failed to query table spec.%s - %w", tableName, err)
return nil, fmt.Errorf(failedQueryMsg, tableName, err)
}

defer rows.Close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ const (
func (r *genericSpecToDBReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
reqLogger := r.log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
instanceUID, instance, err := r.processCR(ctx, request, reqLogger)
const failedMsg = "Reconciliation failed"
if err != nil {
reqLogger.Error(err, "Reconciliation failed")
reqLogger.Error(err, failedMsg)
return ctrl.Result{Requeue: true, RequeueAfter: requeuePeriodSeconds * time.Second}, err
}

Expand All @@ -63,16 +64,15 @@ func (r *genericSpecToDBReconciler) Reconcile(ctx context.Context, request ctrl.

instanceInTheDatabase, err := r.processInstanceInTheDatabase(ctx, instance, instanceUID, reqLogger)
if err != nil {
reqLogger.Error(err, "Reconciliation failed")
reqLogger.Error(err, failedMsg)
return ctrl.Result{Requeue: true, RequeueAfter: requeuePeriodSeconds * time.Second}, err
}

if !r.areEqual(instance, instanceInTheDatabase) {
reqLogger.Info("Mismatch between hub and the database, updating the database")

if err := r.specDB.UpdateSpecObject(ctx, r.tableName, instanceUID, &instance); err != nil {
reqLogger.Error(err, "Reconciliation failed")

reqLogger.Error(err, failedMsg)
return ctrl.Result{}, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/pgx_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func GetPostgresConfig(URI string, cert []byte) (*pgx.ConnConfig, error) {
config.TLSConfig = &tls.Config{
RootCAs: caCertPool,
//nolint:gosec
InsecureSkipVerify: true,
InsecureSkipVerify: true, // #nosec G402
}
}
return config, nil
Expand Down
10 changes: 6 additions & 4 deletions pkg/utils/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const faildGetMsg = "Failed to get %v/%v, err:%v"

// LabelsField presents a "f:labels" field subfield of metadataField.
type LabelsField struct {
Labels map[string]struct{} `json:"f:labels"`
Expand Down Expand Up @@ -94,7 +96,7 @@ func AddAnnotation(
if errors.IsNotFound(err) {
return nil
}
klog.Errorf("Failed to get %v/%v, err:%v", namespace, name, err)
klog.Errorf(faildGetMsg, namespace, name, err)
return err
}
if HasItem(obj.GetAnnotations(), key, value) {
Expand Down Expand Up @@ -130,7 +132,7 @@ func DeleteAnnotation(
if errors.IsNotFound(err) {
return nil
}
klog.Errorf("Failed to get %v/%v, err:%v", namespace, name, err)
klog.Errorf(faildGetMsg, namespace, name, err)
return err
}
if !HasItemKey(obj.GetAnnotations(), key) {
Expand Down Expand Up @@ -162,7 +164,7 @@ func AddLabel(
if errors.IsNotFound(err) {
return nil
}
klog.Errorf("Failed to get %v/%v, err:%v", namespace, name, err)
klog.Errorf(faildGetMsg, namespace, name, err)
return err
}
if HasItem(obj.GetLabels(), labelKey, labelValue) {
Expand Down Expand Up @@ -198,7 +200,7 @@ func DeleteLabel(
if errors.IsNotFound(err) {
return nil
}
klog.Errorf("Failed to get %v/%v, err:%v", namespace, name, err)
klog.Errorf(faildGetMsg, namespace, name, err)
return err
}

Expand Down

0 comments on commit 8fb9c6f

Please sign in to comment.