diff --git a/pkg/controller/jenkins/configuration/base/reconcile.go b/pkg/controller/jenkins/configuration/base/reconcile.go index b33f8f107..a53d124af 100644 --- a/pkg/controller/jenkins/configuration/base/reconcile.go +++ b/pkg/controller/jenkins/configuration/base/reconcile.go @@ -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)) } diff --git a/pkg/controller/jenkins/configuration/base/resources/base_configuration_configmap.go b/pkg/controller/jenkins/configuration/base/resources/base_configuration_configmap.go index 7c8c149ef..a9e088ec7 100644 --- a/pkg/controller/jenkins/configuration/base/resources/base_configuration_configmap.go +++ b/pkg/controller/jenkins/configuration/base/resources/base_configuration_configmap.go @@ -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, @@ -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, @@ -201,5 +209,5 @@ func NewBaseConfigurationConfigMap(meta metav1.ObjectMeta, jenkins *v1alpha2.Jen TypeMeta: buildConfigMapTypeMeta(), ObjectMeta: meta, Data: groovyScriptsMap, - } + }, nil } diff --git a/pkg/controller/jenkins/configuration/base/resources/service.go b/pkg/controller/jenkins/configuration/base/resources/service.go index 1ff6d342f..bbd3d328e 100644 --- a/pkg/controller/jenkins/configuration/base/resources/service.go +++ b/pkg/controller/jenkins/configuration/base/resources/service.go @@ -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" @@ -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 +} \ No newline at end of file diff --git a/pkg/controller/jenkins/configuration/user/seedjobs/seedjobs.go b/pkg/controller/jenkins/configuration/user/seedjobs/seedjobs.go index f64ad2dc8..80a874374 100644 --- a/pkg/controller/jenkins/configuration/user/seedjobs/seedjobs.go +++ b/pkg/controller/jenkins/configuration/user/seedjobs/seedjobs.go @@ -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) { @@ -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), @@ -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), }, { @@ -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, ), }, @@ -465,7 +476,7 @@ func agentDeployment(jenkins *v1alpha2.Jenkins, namespace string, agentName stri }, }, }, - } + }, nil } func seedJobCreatingGroovyScript(seedJob v1alpha2.SeedJob) (string, error) {