Skip to content

Commit

Permalink
Intermediate change for usage of Deployment
Browse files Browse the repository at this point in the history
Allows for using the annotation jenkins.io/use-deployment
and setting the value to true makes the operator use a
Deployment instead of Pod for serving Jenkins.

This is part of an experimental feature.
  • Loading branch information
waveywaves committed May 12, 2020
1 parent 90c4cfb commit 55bf615
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 212 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
k8s.io/code-generator v0.17.4
k8s.io/gengo v0.0.0-20191010091904-7fa3014cb28f
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a
k8s.io/utils v0.0.0-20190801114015-581e00157fb1
sigs.k8s.io/controller-runtime v0.5.2
sigs.k8s.io/controller-tools v0.2.8
)
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package apis
import (
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"

"k8s.io/apimachinery/pkg/runtime"
)
Expand All @@ -19,4 +20,5 @@ func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1alpha2.SchemeBuilder.AddToScheme)
AddToSchemes = append(AddToSchemes, routev1.AddToScheme)
AddToSchemes = append(AddToSchemes, appsv1.AddToScheme)
}
56 changes: 56 additions & 0 deletions pkg/controller/jenkins/configuration/base/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package base

import (
"context"
"fmt"

"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base/resources"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/notifications/event"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/notifications/reason"
"github.com/jenkinsci/kubernetes-operator/version"
stackerr "github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func (r *ReconcileJenkinsBaseConfiguration) ensureJenkinsDeployment(meta metav1.ObjectMeta) (reconcile.Result, error) {
userAndPasswordHash, err := r.calculateUserAndPasswordHash()
if err != nil {
return reconcile.Result{}, err
}

jenkinsDeployment, err := r.GetJenkinsDeployment()
if apierrors.IsNotFound(err) {
jenkinsDeployment = resources.NewJenkinsDeployment(meta, r.Configuration.Jenkins)
*r.Notifications <- event.Event{
Jenkins: *r.Configuration.Jenkins,
Phase: event.PhaseBase,
Level: v1alpha2.NotificationLevelInfo,
Reason: reason.NewPodCreation(reason.OperatorSource, []string{"Creating a Jenkins Deployment"}),
}

r.logger.Info(fmt.Sprintf("Creating a new Jenkins Deployment %s/%s", jenkinsDeployment.Namespace, jenkinsDeployment.Name))
err := r.CreateResource(jenkinsDeployment)
if err != nil {
return reconcile.Result{}, stackerr.WithStack(err)
}

now := metav1.Now()
r.Configuration.Jenkins.Status = v1alpha2.JenkinsStatus{
OperatorVersion: version.Version,
ProvisionStartTime: &now,
LastBackup: r.Configuration.Jenkins.Status.LastBackup,
PendingBackup: r.Configuration.Jenkins.Status.LastBackup,
UserAndPasswordHash: userAndPasswordHash,
}
return reconcile.Result{Requeue: true}, r.Client.Update(context.TODO(), r.Configuration.Jenkins)
} else if err != nil && !apierrors.IsNotFound(err) {
return reconcile.Result{}, stackerr.WithStack(err)
}

// TODO (waveywaves): replace with a cleaner solution
_ = jenkinsDeployment // This is to escape the variable is never used golint err
return reconcile.Result{}, nil
}
111 changes: 67 additions & 44 deletions pkg/controller/jenkins/configuration/base/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,65 +49,88 @@ func New(config configuration.Configuration, logger logr.Logger, jenkinsAPIConne

// Reconcile takes care of base configuration
func (r *ReconcileJenkinsBaseConfiguration) Reconcile() (reconcile.Result, jenkinsclient.Jenkins, error) {
result := reconcile.Result{}
metaObject := resources.NewResourceObjectMeta(r.Configuration.Jenkins)

// Create Necessary
err := r.ensureResourcesRequiredForJenkinsPod(metaObject)
if err != nil {
return reconcile.Result{}, nil, err
}
r.logger.V(log.VDebug).Info("Kubernetes resources are present")

result, err := r.ensureJenkinsMasterPod(metaObject)
if err != nil {
return reconcile.Result{}, nil, err
}
if result.Requeue {
return result, nil, nil
}
r.logger.V(log.VDebug).Info("Jenkins master pod is present")
if useDeploymentForJenkinsMaster(r.Configuration.Jenkins) {
result, err = r.ensureJenkinsDeployment(metaObject)
if err != nil {
return reconcile.Result{}, nil, err
}
if result.Requeue {
return result, nil, nil
}
r.logger.V(log.VDebug).Info("Jenkins Deployment is present")
} else {
result, err = r.ensureJenkinsMasterPod(metaObject)
if err != nil {
return reconcile.Result{}, nil, err
}
if result.Requeue {
return result, nil, nil
}
r.logger.V(log.VDebug).Info("Jenkins master pod is present")

stopReconcileLoop, err := r.detectJenkinsMasterPodStartingIssues()
if err != nil {
return reconcile.Result{}, nil, err
}
if stopReconcileLoop {
return reconcile.Result{Requeue: false}, nil, nil
}
stopReconcileLoop, err := r.detectJenkinsMasterPodStartingIssues()
if err != nil {
return reconcile.Result{}, nil, err
}
if stopReconcileLoop {
return reconcile.Result{Requeue: false}, nil, nil
}

result, err = r.waitForJenkins()
if err != nil {
return reconcile.Result{}, nil, err
}
if result.Requeue {
return result, nil, nil
}
r.logger.V(log.VDebug).Info("Jenkins master pod is ready")
result, err = r.waitForJenkins()
if err != nil {
return reconcile.Result{}, nil, err
}
if result.Requeue {
return result, nil, nil
}
r.logger.V(log.VDebug).Info("Jenkins Pod is ready")

jenkinsClient, err := r.Configuration.GetJenkinsClient()
if err != nil {
return reconcile.Result{}, nil, err
}
r.logger.V(log.VDebug).Info("Jenkins API client set")
jenkinsClient, err := r.Configuration.GetJenkinsClient()
if err != nil {
return reconcile.Result{}, nil, err
}
r.logger.V(log.VDebug).Info("Jenkins API client set")

ok, err := r.verifyPlugins(jenkinsClient)
if err != nil {
return reconcile.Result{}, nil, err
}
if !ok {
//TODO add what plugins have been changed
message := "Some plugins have changed, restarting Jenkins"
r.logger.Info(message)
ok, err := r.verifyPlugins(jenkinsClient)
if err != nil {
return reconcile.Result{}, nil, err
}
if !ok {
//TODO add what plugins have been changed
message := "Some plugins have changed, restarting Jenkins"
r.logger.Info(message)

restartReason := reason.NewPodRestart(
reason.OperatorSource,
[]string{message},
)
return reconcile.Result{Requeue: true}, nil, r.Configuration.RestartJenkinsMasterPod(restartReason)
restartReason := reason.NewPodRestart(
reason.OperatorSource,
[]string{message},
)
return reconcile.Result{Requeue: true}, nil, r.Configuration.RestartJenkinsMasterPod(restartReason)
}
result, err = r.ensureBaseConfiguration(jenkinsClient)
if err != nil {
return result, nil, err // But return all other errors
}
}
return result, nil, err
}

result, err = r.ensureBaseConfiguration(jenkinsClient)

return result, jenkinsClient, err
func useDeploymentForJenkinsMaster(jenkins *v1alpha2.Jenkins) bool {
if val, ok := jenkins.Annotations["jenkins.io/use-deployment"]; ok {
if val == "true" {
return true
}
}
return false
}

func (r *ReconcileJenkinsBaseConfiguration) ensureResourcesRequiredForJenkinsPod(metaObject metav1.ObjectMeta) error {
Expand Down
Loading

0 comments on commit 55bf615

Please sign in to comment.