Skip to content

Commit

Permalink
code cleanup and optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
sharmapulkit04 committed Aug 23, 2021
1 parent 95c29d4 commit 1c8f389
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions api/v1alpha2/jenkins_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/lictenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -26,11 +26,11 @@ import (
"os"
"time"

//"github.com/jenkinsci/kubernetes-operator/pkg/configuration/base/resources"

"github.com/jenkinsci/kubernetes-operator/pkg/log"
"github.com/jenkinsci/kubernetes-operator/pkg/plugins"

"golang.org/x/mod/semver"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -198,10 +198,16 @@ func (in *PluginDataManager) ManagePluginData(sig chan bool) {
var isInit bool // checks if the operator
var retryInterval time.Duration
for {
isCached := in.fetchPluginData()
var isCached bool
err := in.fetchPluginData()
if err == nil {
isCached = true
} else {
jenkinslog.Info("Cache plugin data", "failed to fetch plugin data", err)
}
if !isInit {
sig <- isCached // sending signal to main to continue
isInit = false
isInit = true
}

in.IsCached = in.IsCached || isCached
Expand All @@ -215,49 +221,42 @@ func (in *PluginDataManager) ManagePluginData(sig chan bool) {
}

// Downloads extracts and reads the JSON data in every 12 hours
func (in *PluginDataManager) fetchPluginData() bool {
func (in *PluginDataManager) fetchPluginData() error {
jenkinslog.Info("Initializing/Updating the plugin data cache")
var err error
for in.Attempts = 0; in.Attempts < 5; in.Attempts++ {
err = in.download()
if err == nil {
break
}
jenkinslog.V(1).Info("Cache Plugin Data", "failed to download file", err)
jenkinslog.V(log.VDebug).Info("Cache Plugin Data", "failed to download file", err)
}

if err != nil {
jenkinslog.Info("Cache Plugin Data", "failed to download file", err)
return false
return err
}

for in.Attempts = 0; in.Attempts < 5; in.Attempts++ {
err = in.extract()
if err == nil {
break
}
jenkinslog.V(1).Info("Cache Plugin Data", "failed to extract file", err)
jenkinslog.V(log.VDebug).Info("Cache Plugin Data", "failed to extract file", err)
}

if err != nil {
jenkinslog.Info("Cache Plugin Data", "failed to extract file", err)
return false
return err
}

for in.Attempts = 0; in.Attempts < 5; in.Attempts++ {
err = in.cache()
if err == nil {
break
}
jenkinslog.V(1).Info("Cache Plugin Data", "failed to read plugin data file", err)
}

if err != nil {
jenkinslog.Info("Cache Plugin Data", "failed to read plugin data file", err)
return false
jenkinslog.V(log.VDebug).Info("Cache Plugin Data", "failed to read plugin data file", err)
}

return true
return err
}

func (in *PluginDataManager) download() error {
Expand All @@ -278,11 +277,7 @@ func (in *PluginDataManager) download() error {
defer resp.Body.Close()

_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}

return nil
return err
}

func (in *PluginDataManager) extract() error {
Expand Down Expand Up @@ -320,10 +315,7 @@ func (in *PluginDataManager) cache() error {
return err
}
err = json.Unmarshal(byteValue, &in.PluginDataCache)
if err != nil {
return err
}
return nil
return err
}

// returns a semantic version that can be used for comparison, allowed versioning format vMAJOR.MINOR.PATCH or MAJOR.MINOR.PATCH
Expand Down

0 comments on commit 1c8f389

Please sign in to comment.