forked from VirtusLab/jenkins-operator
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intermediate Commit for transition from Pod to Deployment based Jenki…
…ns Controller 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 a temporary feature and has to be committed to avoid bigger PRs.
- Loading branch information
1 parent
2dea712
commit 4e52eb3
Showing
9 changed files
with
183 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
pkg/controller/jenkins/configuration/base/resources/deployment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package resources | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
// NewJenkinsMasterPod builds Jenkins Master Kubernetes Pod resource. | ||
func NewJenkinsDeployment(objectMeta metav1.ObjectMeta, jenkins *v1alpha2.Jenkins) *appsv1.Deployment { | ||
serviceAccountName := objectMeta.Name | ||
objectMeta.Annotations = jenkins.Spec.Master.Annotations | ||
objectMeta.Name = GetJenkinsDeploymentName(jenkins) | ||
selector := &metav1.LabelSelector{MatchLabels: objectMeta.Labels} | ||
return &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: objectMeta.Name, | ||
Namespace: objectMeta.Namespace, | ||
Labels: objectMeta.Labels, | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
BlockOwnerDeletion: pointer.BoolPtr(true), | ||
Controller: pointer.BoolPtr(true), | ||
Kind: jenkins.Kind, | ||
Name: jenkins.Name, | ||
APIVersion: jenkins.APIVersion, | ||
UID: jenkins.UID, | ||
}, | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Replicas: pointer.Int32Ptr(1), | ||
Strategy: appsv1.DeploymentStrategy{Type: appsv1.RollingUpdateDeploymentStrategyType}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: objectMeta, | ||
Spec: corev1.PodSpec{ | ||
ServiceAccountName: serviceAccountName, | ||
NodeSelector: jenkins.Spec.Master.NodeSelector, | ||
Containers: newContainers(jenkins), | ||
Volumes: append(GetJenkinsMasterPodBaseVolumes(jenkins), jenkins.Spec.Master.Volumes...), | ||
SecurityContext: jenkins.Spec.Master.SecurityContext, | ||
ImagePullSecrets: jenkins.Spec.Master.ImagePullSecrets, | ||
Tolerations: jenkins.Spec.Master.Tolerations, | ||
PriorityClassName: jenkins.Spec.Master.PriorityClassName, | ||
}, | ||
}, | ||
Selector: selector, | ||
}, | ||
} | ||
} | ||
|
||
// GetJenkinsMasterPodName returns Jenkins pod name for given CR | ||
func GetJenkinsDeploymentName(jenkins *v1alpha2.Jenkins) string { | ||
return fmt.Sprintf("jenkins-%s", jenkins.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.