Skip to content

Commit

Permalink
HAWNG-401: Fixes AST scan warnings
Browse files Browse the repository at this point in the history
* Fixes error suppression, ensuring they are observed & handled
  • Loading branch information
phantomjinx committed Jan 17, 2024
1 parent f1c359f commit 427cb6a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
9 changes: 7 additions & 2 deletions pkg/capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,19 @@ func APICapabilities(ctx context.Context, apiClient kclient.Interface, configCli
// Update the api spec version
var openShiftSemVer *semver.Version
openShiftSemVer, err = semver.NewVersion(update.Version)
if err != nil {
return nil, errs.Wrap(err, fmt.Sprintf("Error parsing OpenShift cluster semantic version %s", update.Version))
}

apiSpec.Version = openShiftSemVer.String()

// Update whether this is OpenShift 4.3+
constraint43, _ := semver.NewConstraint(">= 4.3")
apiSpec.IsOpenShift43Plus = constraint43.Check(openShiftSemVer)
constraint43, err := semver.NewConstraint(">= 4.3")
if err != nil {
return nil, errs.Wrap(err, fmt.Sprintf("Error parsing OpenShift cluster semantic version %s", update.Version))
}

apiSpec.IsOpenShift43Plus = constraint43.Check(openShiftSemVer)
break
}
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/controller/hawtio/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,20 @@ func createCertValidationCronJob(name, namespace, schedule, serviceAccountName s
return cronjob
}

func updateExpirationPeriod(cronJob *batchv1.CronJob, newPeriod int) bool {
func updateExpirationPeriod(cronJob *batchv1.CronJob, newPeriod int) (bool, error) {
arguments := cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args
for i, arg := range arguments {
if arg == "--cert-expiration-period" {
period, _ := strconv.Atoi(arguments[i+1])
period, err := strconv.Atoi(arguments[i+1])
if err != nil {
return false, err
}
if period == newPeriod {
return false
return false, nil
}
cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args[i+1] = strconv.Itoa(newPeriod)
return true
return true, nil
}
}
return false
return false, nil
}
10 changes: 9 additions & 1 deletion pkg/controller/hawtio/hawtio_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,12 @@ func (r *ReconcileHawtio) Reconcile(ctx context.Context, request reconcile.Reque
cronJob.Spec.Schedule = hawtio.Spec.Auth.ClientCertCheckSchedule
update = true
}
if update || updateExpirationPeriod(cronJob, hawtio.Spec.Auth.ClientCertExpirationPeriod) {
updateExp, err := updateExpirationPeriod(cronJob, hawtio.Spec.Auth.ClientCertExpirationPeriod)
if err != nil {
log.Error(err, "CronJob haven't been updated")
}

if update || updateExp {
err = r.client.Update(ctx, cronJob)

if err != nil {
Expand All @@ -606,6 +611,9 @@ func (r *ReconcileHawtio) Reconcile(ctx context.Context, request reconcile.Reque
//if cronjob exists and ClientCertRotate is disabled, cronjob has to be deleted
} else {
err = r.client.Delete(ctx, cronJob)
if err != nil {
log.Error(err, "CronJob could not be deleted")
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func MatchPatterns(patterns []string, target string) bool {
// Match provides simple string pattern match that only supports wildcard '*'.
func Match(pattern, str string) bool {
var b strings.Builder
b.WriteRune('^')
_, err := b.WriteRune('^')
if err != nil {
return false
}
for _, c := range pattern {
var s string
switch c {
Expand All @@ -67,9 +70,15 @@ func Match(pattern, str string) bool {
default:
s = string(c)
}
b.WriteString(s)
_, err := b.WriteString(s)
if err != nil {
return false
}
}
_, err = b.WriteRune('$')
if err != nil {
return false
}
b.WriteRune('$')

match, err := regexp.MatchString(b.String(), str)
if err != nil {
Expand Down

0 comments on commit 427cb6a

Please sign in to comment.