Skip to content

Commit

Permalink
#262 Allow configure Jenkins master pod labels
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszsek committed Jan 30, 2020
1 parent 28b7dac commit 762b3fb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/apis/jenkins/v1alpha2/jenkins_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ type JenkinsMaster struct {
// +optional
AnnotationsDeprecated map[string]string `json:"masterAnnotations,omitempty"`

// Map of string keys and values that can be used to organize and categorize
// (scope and select) objects. May match selectors of replication controllers
// and services.
// More info: http://kubernetes.io/docs/user-guide/labels
// +optional
Labels map[string]string `json:"labels,omitempty"`

// NodeSelector is a selector which must be true for the pod to fit on a node.
// Selector which must match a node's labels for the pod to be scheduled on that node.
// More info: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/jenkins/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/controller/jenkins/configuration/base/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ func (r *ReconcileJenkinsBaseConfiguration) checkForPodRecreation(currentJenkins
currentJenkinsMasterPod.Spec.NodeSelector, r.Configuration.Jenkins.Spec.Master.NodeSelector))
}

if !compareMap(r.Configuration.Jenkins.Spec.Master.Labels, currentJenkinsMasterPod.Labels) {
messages = append(messages, "Jenkins pod labels have changed")
verbose = append(verbose, fmt.Sprintf("Jenkins pod labels have changed, actual '%+v' required '%+v'",
currentJenkinsMasterPod.Labels, r.Configuration.Jenkins.Spec.Master.Labels))
}

if !compareMap(r.Configuration.Jenkins.Spec.Master.Annotations, currentJenkinsMasterPod.ObjectMeta.Annotations) {
messages = append(messages, "Jenkins pod annotations have changed")
verbose = append(verbose, fmt.Sprintf("Jenkins pod annotations have changed, actual '%+v' required '%+v'",
Expand Down
15 changes: 15 additions & 0 deletions pkg/controller/jenkins/configuration/base/resources/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,26 @@ func GetJenkinsMasterPodName(jenkins v1alpha2.Jenkins) string {
return fmt.Sprintf("jenkins-%s", jenkins.Name)
}

// GetJenkinsMasterPodLabels returns Jenkins pod labels for given CR
func GetJenkinsMasterPodLabels(jenkins v1alpha2.Jenkins) map[string]string {
var labels map[string]string
if jenkins.Spec.Master.Labels == nil {
labels = map[string]string{}
} else {
labels = jenkins.Spec.Master.Labels
}
for key, value := range BuildResourceLabels(&jenkins) {
labels[key] = value
}
return labels
}

// NewJenkinsMasterPod builds Jenkins Master Kubernetes Pod resource
func NewJenkinsMasterPod(objectMeta metav1.ObjectMeta, jenkins *v1alpha2.Jenkins) *corev1.Pod {
serviceAccountName := objectMeta.Name
objectMeta.Annotations = jenkins.Spec.Master.Annotations
objectMeta.Name = GetJenkinsMasterPodName(*jenkins)
objectMeta.Labels = GetJenkinsMasterPodLabels(*jenkins)

return &corev1.Pod{
TypeMeta: buildPodTypeMeta(),
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ func verifyJenkinsMasterPodAttributes(t *testing.T, jenkins *v1alpha2.Jenkins) {

assert.Equal(t, jenkins.Spec.Master.ImagePullSecrets, jenkinsPod.Spec.ImagePullSecrets)

assert.Equal(t, resources.GetJenkinsMasterPodLabels(*jenkins), jenkinsPod.Labels)

for _, actualContainer := range jenkinsPod.Spec.Containers {
if actualContainer.Name == resources.JenkinsMasterContainerName {
verifyContainer(t, resources.NewJenkinsMasterContainer(jenkins), actualContainer)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func getJenkins(t *testing.T, namespace, name string) *v1alpha2.Jenkins {

func getJenkinsMasterPod(t *testing.T, jenkins *v1alpha2.Jenkins) *corev1.Pod {
lo := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(resources.BuildResourceLabels(jenkins)).String(),
LabelSelector: labels.SelectorFromSet(resources.GetJenkinsMasterPodLabels(*jenkins)).String(),
}
podList, err := framework.Global.KubeClient.CoreV1().Pods(jenkins.ObjectMeta.Namespace).List(lo)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func waitForJenkinsBaseConfigurationToComplete(t *testing.T, jenkins *v1alpha2.J
func waitForRecreateJenkinsMasterPod(t *testing.T, jenkins *v1alpha2.Jenkins) {
err := wait.Poll(retryInterval, 30*retryInterval, func() (bool, error) {
lo := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(resources.BuildResourceLabels(jenkins)).String(),
LabelSelector: labels.SelectorFromSet(resources.GetJenkinsMasterPodLabels(*jenkins)).String(),
}
podList, err := framework.Global.KubeClient.CoreV1().Pods(jenkins.ObjectMeta.Namespace).List(lo)
if err != nil {
Expand Down

0 comments on commit 762b3fb

Please sign in to comment.