Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszsek committed Jan 19, 2019
1 parent 02c04af commit 7281032
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
29 changes: 14 additions & 15 deletions pkg/controller/jenkins/configuration/base/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func New(client client.Client, scheme *runtime.Scheme, logger logr.Logger,
func (r *ReconcileJenkinsBaseConfiguration) Reconcile() (reconcile.Result, jenkinsclient.Jenkins, error) {
metaObject := resources.NewResourceObjectMeta(r.jenkins)

err := r.ensureResourcesReuiredForJenkinsPod(metaObject)
err := r.ensureResourcesRequiredForJenkinsPod(metaObject)
if err != nil {
return reconcile.Result{}, nil, err
}
Expand Down Expand Up @@ -91,20 +91,14 @@ func (r *ReconcileJenkinsBaseConfiguration) Reconcile() (reconcile.Result, jenki
}
if !ok {
r.logger.V(log.VWarn).Info("Please correct Jenkins CR (spec.master.plugins)")
currentJenkinsMasterPod, err := r.getJenkinsMasterPod(metaObject)
if err != nil {
return reconcile.Result{}, nil, err
}
if err := r.k8sClient.Delete(context.TODO(), currentJenkinsMasterPod); err != nil {
return reconcile.Result{}, nil, err
}
return reconcile.Result{Requeue: true}, nil, r.restartJenkinsMasterPod(metaObject)
}

result, err = r.ensureBaseConfiguration(jenkinsClient)
return result, jenkinsClient, err
}

func (r *ReconcileJenkinsBaseConfiguration) ensureResourcesReuiredForJenkinsPod(metaObject metav1.ObjectMeta) error {
func (r *ReconcileJenkinsBaseConfiguration) ensureResourcesRequiredForJenkinsPod(metaObject metav1.ObjectMeta) error {
if err := r.createOperatorCredentialsSecret(metaObject); err != nil {
return err
}
Expand Down Expand Up @@ -133,7 +127,7 @@ func (r *ReconcileJenkinsBaseConfiguration) ensureResourcesReuiredForJenkinsPod(
if err := r.createRBAC(metaObject); err != nil {
return err
}
r.logger.V(log.VDebug).Info("User configuration config map is present")
r.logger.V(log.VDebug).Info("Service account, role and role binding are present")

if err := r.createService(metaObject); err != nil {
return err
Expand Down Expand Up @@ -339,16 +333,21 @@ func (r *ReconcileJenkinsBaseConfiguration) ensureJenkinsMasterPod(meta metav1.O
}

if currentJenkinsMasterPod != nil && recreatePod && currentJenkinsMasterPod.ObjectMeta.DeletionTimestamp == nil {
r.logger.Info(fmt.Sprintf("Terminating Jenkins Master Pod %s/%s", currentJenkinsMasterPod.Namespace, currentJenkinsMasterPod.Name))
if err := r.k8sClient.Delete(context.TODO(), currentJenkinsMasterPod); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{Requeue: true}, nil
return reconcile.Result{Requeue: true}, r.restartJenkinsMasterPod(meta)
}

return reconcile.Result{}, nil
}

func (r *ReconcileJenkinsBaseConfiguration) restartJenkinsMasterPod(meta metav1.ObjectMeta) error {
currentJenkinsMasterPod, err := r.getJenkinsMasterPod(meta)
r.logger.Info(fmt.Sprintf("Terminating Jenkins Master Pod %s/%s", currentJenkinsMasterPod.Namespace, currentJenkinsMasterPod.Name))
if err != nil {
return err
}
return r.k8sClient.Delete(context.TODO(), currentJenkinsMasterPod)
}

func (r *ReconcileJenkinsBaseConfiguration) waitForJenkins(meta metav1.ObjectMeta) (reconcile.Result, error) {
jenkinsMasterPodStatus, err := r.getJenkinsMasterPod(meta)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def Jenkins jenkins = Jenkins.getInstance()
def seedViewName = 'seed-jobs'
def nonSeedViewName = 'non-seed-jobs'
def jenkinsViewName = "jenkins"
def jenkinsViewName = "` + constants.OperatorName + `"
if (jenkins.getView(seedViewName) == null) {
def seedView = new ListView(seedViewName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const installPluginsCommand = "install-plugins.sh"

// bash scripts installs single jenkins plugin with concrete version
// bash scripts installs single jenkins plugin with specific version
const installPluginsBashFmt = `#!/bin/bash -eu
# Resolve dependencies and download plugins given on the command line
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/jenkins/jenkins_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type ReconcileJenkins struct {
// Reconcile it's a main reconciliation loop which maintain desired state based on Jenkins.Spec
func (r *ReconcileJenkins) Reconcile(request reconcile.Request) (reconcile.Result, error) {
logger := r.buildLogger(request.Name)
logger.Info("Reconciling Jenkins")
logger.V(log.VDebug).Info("Reconciling Jenkins")

result, err := r.reconcile(request, logger)
if err != nil && errors.IsConflict(err) {
Expand Down Expand Up @@ -201,6 +201,7 @@ func (r *ReconcileJenkins) setDefaults(jenkins *virtuslabv1alpha1.Jenkins, logge
}
if len(jenkins.Spec.Backup) == 0 {
logger.Info("Setting default backup strategy: " + virtuslabv1alpha1.JenkinsBackupTypeNoBackup)
logger.V(log.VWarn).Info("Backup is disable !!! Please configure backup in '.spec.backup'")
changed = true
jenkins.Spec.Backup = virtuslabv1alpha1.JenkinsBackupTypeNoBackup
}
Expand Down
43 changes: 25 additions & 18 deletions pkg/controller/jenkins/plugins/base_plugins.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package plugins

// BasePlugins returns map of plugins to install by operator
func BasePlugins() (plugins map[string][]string) {
plugins = map[string][]string{}

for rootPluginName, dependentPlugins := range BasePluginsMap {
plugins[rootPluginName] = []string{}
for _, pluginName := range dependentPlugins {
plugins[rootPluginName] = append(plugins[rootPluginName], pluginName.String())
}
}

return
}
const (
// ApacheComponentsClientPlugin is apache-httpcomponents-client-4-api Jenkins plugin with version
ApacheComponentsClientPlugin = "apache-httpcomponents-client-4-api:4.5.5-3.0"
// Jackson2ADIPlugin is jackson2-api-httpcomponents-client-4-api Jenkins plugin with version
Jackson2ADIPlugin = "jackson2-api:2.9.8"
)

// BasePluginsMap contains plugins to install by operator
var BasePluginsMap = map[string][]Plugin{
Must(New("kubernetes:1.13.8")).String(): {
Must(New("apache-httpcomponents-client-4-api:4.5.5-3.0")),
Must(New(ApacheComponentsClientPlugin)),
Must(New("cloudbees-folder:6.7")),
Must(New("credentials:2.1.18")),
Must(New("durable-task:1.28")),
Must(New("jackson2-api:2.9.8")),
Must(New(Jackson2ADIPlugin)),
Must(New("kubernetes-credentials:0.4.0")),
Must(New("plain-credentials:1.5")),
Must(New("structs:1.17")),
Expand All @@ -38,7 +31,7 @@ var BasePluginsMap = map[string][]Plugin{
},
Must(New("workflow-aggregator:2.6")).String(): {
Must(New("ace-editor:1.1")),
Must(New("apache-httpcomponents-client-4-api:4.5.5-3.0")),
Must(New(ApacheComponentsClientPlugin)),
Must(New("authentication-tokens:1.3")),
Must(New("branch-api:2.1.2")),
Must(New("cloudbees-folder:6.7")),
Expand All @@ -51,7 +44,7 @@ var BasePluginsMap = map[string][]Plugin{
Must(New("git-client:2.7.6")),
Must(New("git-server:1.7")),
Must(New("handlebars:1.1.1")),
Must(New("jackson2-api:2.9.8")),
Must(New(Jackson2ADIPlugin)),
Must(New("jquery-detached:1.2.1")),
Must(New("jsch:0.1.55")),
Must(New("junit:1.26.1")),
Expand Down Expand Up @@ -88,7 +81,7 @@ var BasePluginsMap = map[string][]Plugin{
Must(New("workflow-support:3.0")),
},
Must(New("git:3.9.1")).String(): {
Must(New("apache-httpcomponents-client-4-api:4.5.5-3.0")),
Must(New(ApacheComponentsClientPlugin)),
Must(New("credentials:2.1.18")),
Must(New("display-url-api:2.3.0")),
Must(New("git-client:2.7.6")),
Expand All @@ -114,3 +107,17 @@ var BasePluginsMap = map[string][]Plugin{
},
Must(New("simple-theme-plugin:0.5.1")).String(): {},
}

// BasePlugins returns map of plugins to install by operator
func BasePlugins() (plugins map[string][]string) {
plugins = map[string][]string{}

for rootPluginName, dependentPlugins := range BasePluginsMap {
plugins[rootPluginName] = []string{}
for _, pluginName := range dependentPlugins {
plugins[rootPluginName] = append(plugins[rootPluginName], pluginName.String())
}
}

return
}

0 comments on commit 7281032

Please sign in to comment.