Skip to content

Commit

Permalink
jenkinsci#318 Fix running Jenkins-Operator locally
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikolaj Karebski committed Apr 3, 2020
1 parent 970449f commit fe74f40
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
5 changes: 4 additions & 1 deletion pkg/controller/jenkins/configuration/base/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ func (r *ReconcileJenkinsBaseConfiguration) createInitConfigurationConfigMap(met
}

func (r *ReconcileJenkinsBaseConfiguration) createBaseConfigurationConfigMap(meta metav1.ObjectMeta) error {
configMap := resources.NewBaseConfigurationConfigMap(meta, r.Configuration.Jenkins)
configMap, err := resources.NewBaseConfigurationConfigMap(meta, r.Configuration.Jenkins)
if err != nil {
return stackerr.WithStack(err)
}
return stackerr.WithStack(r.CreateOrUpdateResource(configMap))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,16 @@ func GetBaseConfigurationConfigMapName(jenkins *v1alpha2.Jenkins) string {
}

// NewBaseConfigurationConfigMap builds Kubernetes config map used to base configuration
func NewBaseConfigurationConfigMap(meta metav1.ObjectMeta, jenkins *v1alpha2.Jenkins) *corev1.ConfigMap {
func NewBaseConfigurationConfigMap(meta metav1.ObjectMeta, jenkins *v1alpha2.Jenkins) (*corev1.ConfigMap, error) {
meta.Name = GetBaseConfigurationConfigMapName(jenkins)
jenkinsServiceFQDN, err := GetJenkinsHTTPServiceFQDN(jenkins)
if err != nil {
return nil, err
}
jenkinsSlavesServiceFQDN, err := GetJenkinsSlavesServiceFQDN(jenkins)
if err != nil {
return nil, err
}
groovyScriptsMap := map[string]string{
basicSettingsGroovyScriptName: fmt.Sprintf(basicSettingsFmt, constants.DefaultAmountOfExecutors),
enableCSRFGroovyScriptName: enableCSRF,
Expand All @@ -188,8 +196,8 @@ func NewBaseConfigurationConfigMap(meta metav1.ObjectMeta, jenkins *v1alpha2.Jen
disableInsecureFeaturesGroovyScriptName: disableInsecureFeatures,
configureKubernetesPluginGroovyScriptName: fmt.Sprintf(configureKubernetesPluginFmt,
jenkins.ObjectMeta.Namespace,
fmt.Sprintf("http://%s:%d", GetJenkinsHTTPServiceFQDN(jenkins), jenkins.Spec.Service.Port),
fmt.Sprintf("%s:%d", GetJenkinsSlavesServiceFQDN(jenkins), jenkins.Spec.SlaveService.Port),
fmt.Sprintf("http://%s:%d", jenkinsServiceFQDN, jenkins.Spec.Service.Port),
fmt.Sprintf("%s:%d", jenkinsSlavesServiceFQDN, jenkins.Spec.SlaveService.Port),
),
configureViewsGroovyScriptName: configureViews,
disableJobDslScriptApprovalGroovyScriptName: disableJobDSLScriptApproval,
Expand All @@ -201,5 +209,5 @@ func NewBaseConfigurationConfigMap(meta metav1.ObjectMeta, jenkins *v1alpha2.Jen
TypeMeta: buildConfigMapTypeMeta(),
ObjectMeta: meta,
Data: groovyScriptsMap,
}
}, nil
}
47 changes: 35 additions & 12 deletions pkg/controller/jenkins/configuration/base/resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package resources

import (
"fmt"

"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"

corev1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -43,33 +43,56 @@ func GetJenkinsSlavesServiceName(jenkins *v1alpha2.Jenkins) string {
}

// GetJenkinsHTTPServiceFQDN returns Kubernetes service FQDN used for expose Jenkins HTTP endpoint
func GetJenkinsHTTPServiceFQDN(jenkins *v1alpha2.Jenkins) string {
clusterDomain := getClusterDomain()
func GetJenkinsHTTPServiceFQDN(jenkins *v1alpha2.Jenkins) (string, error) {
clusterDomain, err := getClusterDomain()
if err != nil {
return "", err
}

return fmt.Sprintf("%s-http-%s.%s.svc.%s", constants.OperatorName, jenkins.ObjectMeta.Name, jenkins.ObjectMeta.Namespace, clusterDomain)
return fmt.Sprintf("%s-http-%s.%s.svc.%s", constants.OperatorName, jenkins.ObjectMeta.Name, jenkins.ObjectMeta.Namespace, clusterDomain), nil
}

// GetJenkinsSlavesServiceFQDN returns Kubernetes service FQDN used for expose Jenkins slave endpoint
func GetJenkinsSlavesServiceFQDN(jenkins *v1alpha2.Jenkins) string {
clusterDomain := getClusterDomain()
func GetJenkinsSlavesServiceFQDN(jenkins *v1alpha2.Jenkins) (string, error) {
clusterDomain, err := getClusterDomain()
if err != nil {
return "", err
}

return fmt.Sprintf("%s-slave-%s.%s.svc.%s", constants.OperatorName, jenkins.ObjectMeta.Name, jenkins.ObjectMeta.Namespace, clusterDomain)
return fmt.Sprintf("%s-slave-%s.%s.svc.%s", constants.OperatorName, jenkins.ObjectMeta.Name, jenkins.ObjectMeta.Namespace, clusterDomain), nil
}

// GetClusterDomain returns Kubernetes cluster domain, default to "cluster.local"
func getClusterDomain() string {
apiSvc := "kubernetes.default.svc"

func getClusterDomain() (string, error) {
clusterDomain := "cluster.local"

if ok, err := isRunningInCluster(); !ok {
return clusterDomain, nil
} else if err != nil {
return "", nil
}

apiSvc := "kubernetes.default.svc"

cname, err := net.LookupCNAME(apiSvc)
if err != nil {
return clusterDomain
return "", err
}

clusterDomain = strings.TrimPrefix(cname, "kubernetes.default.svc")
clusterDomain = strings.TrimPrefix(clusterDomain, ".")
clusterDomain = strings.TrimSuffix(clusterDomain, ".")

return clusterDomain
return clusterDomain, nil
}

func isRunningInCluster() (bool, error) {
_, err := k8sutil.GetOperatorNamespace()
if err != nil {
if err == k8sutil.ErrNoNamespace || err == k8sutil.ErrRunLocal {
return false, nil
}
return true, nil
}
return false, err
}
21 changes: 16 additions & 5 deletions pkg/controller/jenkins/configuration/user/seedjobs/seedjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ func (s SeedJobs) createAgent(jenkinsClient jenkinsclient.Jenkins, k8sClient cli
return err
}

deployment := agentDeployment(jenkinsManifest, namespace, agentName, secret)
deployment, err := agentDeployment(jenkinsManifest, namespace, agentName, secret)
if err != nil {
return err
}

err = k8sClient.Create(context.TODO(), deployment)
if apierrors.IsAlreadyExists(err) {
Expand All @@ -373,7 +376,15 @@ func agentDeploymentName(jenkins v1alpha2.Jenkins, agentName string) string {
return fmt.Sprintf("%s-%s", agentName, jenkins.Name)
}

func agentDeployment(jenkins *v1alpha2.Jenkins, namespace string, agentName string, secret string) *appsv1.Deployment {
func agentDeployment(jenkins *v1alpha2.Jenkins, namespace string, agentName string, secret string) (*appsv1.Deployment, error) {
jenkinsSlavesServiceFQDN, err := resources.GetJenkinsSlavesServiceFQDN(jenkins)
if err != nil {
return nil, err
}
jenkinsHttpServiceFQDN, err := resources.GetJenkinsHTTPServiceFQDN(jenkins)
if err != nil {
return nil, err
}
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: agentDeploymentName(*jenkins, agentName),
Expand Down Expand Up @@ -403,7 +414,7 @@ func agentDeployment(jenkins *v1alpha2.Jenkins, namespace string, agentName stri
{
Name: "JENKINS_TUNNEL",
Value: fmt.Sprintf("%s:%d",
resources.GetJenkinsSlavesServiceFQDN(jenkins),
jenkinsSlavesServiceFQDN,
jenkins.Spec.SlaveService.Port),
},
{
Expand All @@ -417,7 +428,7 @@ func agentDeployment(jenkins *v1alpha2.Jenkins, namespace string, agentName stri
{
Name: "JENKINS_URL",
Value: fmt.Sprintf("http://%s:%d",
resources.GetJenkinsHTTPServiceFQDN(jenkins),
jenkinsHttpServiceFQDN,
jenkins.Spec.Service.Port,
),
},
Expand Down Expand Up @@ -465,7 +476,7 @@ func agentDeployment(jenkins *v1alpha2.Jenkins, namespace string, agentName stri
},
},
},
}
}, nil
}

func seedJobCreatingGroovyScript(seedJob v1alpha2.SeedJob) (string, error) {
Expand Down

0 comments on commit fe74f40

Please sign in to comment.