mirror of
https://github.com/jenkinsci/kubernetes-operator.git
synced 2026-10-18 14:01:04 +02:00
Update seedjob and jenkins client
This commit is contained in:
@@ -13,29 +13,40 @@ import (
|
||||
// Jenkins defines Jenkins API
|
||||
type Jenkins interface {
|
||||
GenerateToken(userName, tokenName string) (*UserToken, error)
|
||||
//Info() (*gojenkins.executorResponse, error)
|
||||
CreateNode(name string, numExecutors int, description string, remoteFS string, options ...interface{}) (*gojenkins.Node, error)
|
||||
Info() (*gojenkins.ExecutorResponse, error)
|
||||
SafeRestart() error
|
||||
CreateNode(name string, numExecutors int, description string, remoteFS string, label string, options ...interface{}) (*gojenkins.Node, error)
|
||||
DeleteNode(name string) (bool, error)
|
||||
CreateFolder(name string, parents ...string) (*gojenkins.Folder, error)
|
||||
CreateJobInFolder(config string, jobName string, parentIDs ...string) (*gojenkins.Job, error)
|
||||
CreateJob(config string, options ...interface{}) (*gojenkins.Job, error)
|
||||
RenameJob(job string, name string) *gojenkins.Job
|
||||
CopyJob(copyFrom string, newName string) (*gojenkins.Job, error)
|
||||
DeleteJob(name string) (bool, error)
|
||||
BuildJob(name string, options ...interface{}) (bool, error)
|
||||
BuildJob(name string, options ...interface{}) (int64, error)
|
||||
GetNode(name string) (*gojenkins.Node, error)
|
||||
GetLabel(name string) (*gojenkins.Label, error)
|
||||
GetBuild(jobName string, number int64) (*gojenkins.Build, error)
|
||||
GetJob(id string) (*gojenkins.Job, error)
|
||||
GetJob(id string, parentIDs ...string) (*gojenkins.Job, error)
|
||||
GetSubJob(parentId string, childId string) (*gojenkins.Job, error)
|
||||
GetFolder(id string, parents ...string) (*gojenkins.Folder, error)
|
||||
GetAllNodes() ([]*gojenkins.Node, error)
|
||||
//GetAllBuildIds(job string) ([]jobBuild, error)
|
||||
//GetAllJobNames() ([]job, error)
|
||||
GetAllBuildIds(job string) ([]gojenkins.JobBuild, error)
|
||||
GetAllJobNames() ([]gojenkins.InnerJob, error)
|
||||
GetAllJobs() ([]*gojenkins.Job, error)
|
||||
GetQueue() (*gojenkins.Queue, error)
|
||||
GetQueueUrl() string
|
||||
//GetArtifactData(id string) (*fingerPrintResponse, error)
|
||||
GetQueueItem(id int64) (*gojenkins.Task, error)
|
||||
GetArtifactData(id string) (*gojenkins.FingerPrintResponse, error)
|
||||
GetPlugins(depth int) (*gojenkins.Plugins, error)
|
||||
UninstallPlugin(name string) error
|
||||
HasPlugin(name string) (*gojenkins.Plugin, error)
|
||||
InstallPlugin(name string, version string) error
|
||||
ValidateFingerPrint(id string) (bool, error)
|
||||
GetView(name string) (*gojenkins.View, error)
|
||||
GetAllViews() ([]*gojenkins.View, error)
|
||||
CreateView(name string, viewType string) (*gojenkins.View, error)
|
||||
Poll() (int, error)
|
||||
}
|
||||
|
||||
type jenkins struct {
|
||||
@@ -82,7 +93,6 @@ func New(url, user, passwordOrToken string) (Jenkins, error) {
|
||||
SslVerify: true,
|
||||
Client: http.DefaultClient,
|
||||
BasicAuth: &gojenkins.BasicAuth{Username: user, Password: passwordOrToken},
|
||||
Headers: http.Header{},
|
||||
}
|
||||
if _, err := jenkinsClient.Init(); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -240,7 +240,6 @@ func (r *ReconcileJenkinsBaseConfiguration) waitForJenkins(meta metav1.ObjectMet
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// FIXME(bantoniak) move jenkins client out of base.reconcile because it's needed for user.reconcile as well
|
||||
func (r *ReconcileJenkinsBaseConfiguration) getJenkinsClient(meta metav1.ObjectMeta) (jenkinsclient.Jenkins, error) {
|
||||
jenkinsURL, err := jenkinsclient.BuildJenkinsAPIUrl(
|
||||
r.jenkins.ObjectMeta.Namespace, meta.Name, resources.HTTPPortInt, r.local, r.minikube)
|
||||
|
||||
@@ -31,7 +31,7 @@ func New(k8sClient k8s.Client, jenkinsClient jenkins.Jenkins, logger logr.Logger
|
||||
|
||||
// Reconcile it's a main reconciliation loop for user supplied configuration
|
||||
func (r *ReconcileUserConfiguration) Reconcile() (*reconcile.Result, error) {
|
||||
if !r.validate(r.jenkins) {
|
||||
if !r.validate(r.k8sClient, r.jenkins) {
|
||||
r.logger.V(log.VWarn).Info("Please correct Jenkins CR")
|
||||
return &reconcile.Result{}, nil
|
||||
}
|
||||
|
||||
@@ -6,14 +6,14 @@ import (
|
||||
k8s "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// ConfigureSeedJobsName this is the job name
|
||||
// ConfigureSeedJobsName this is the fixed seed job name
|
||||
ConfigureSeedJobsName = "Configure Seed Jobs"
|
||||
|
||||
deployKeyIDParameterName = "DEPLOY_KEY_ID"
|
||||
@@ -52,15 +52,29 @@ func ConfigureSeedJobs(jenkinsClient jenkins.Jenkins, k8sClient k8s.Client, jenk
|
||||
|
||||
// configureSeedJobsPipeline configures seed jobs and deploy keys
|
||||
func configureSeedJobsPipeline(jenkinsClient jenkins.Jenkins) error {
|
||||
// FIXME(bantoniak) implement CreateOrUpdateJob()
|
||||
_, err := jenkinsClient.CreateJob(seedJobConfigXML, ConfigureSeedJobsName)
|
||||
if err != nil && strings.Contains(err.Error(), "A job already exists") {
|
||||
// skip, job already exists
|
||||
return nil
|
||||
return createOrUpdateSeedJob(jenkinsClient)
|
||||
}
|
||||
|
||||
// FIXME this function should be part of jenkins client API
|
||||
func createOrUpdateSeedJob(jenkinsClient jenkins.Jenkins) error {
|
||||
job, err := jenkinsClient.GetJob(ConfigureSeedJobsName)
|
||||
if jobNotExists(err) {
|
||||
_, err := jenkinsClient.CreateJob(seedJobConfigXML, ConfigureSeedJobsName)
|
||||
return err
|
||||
} else if err != nil {
|
||||
err := job.UpdateConfig(seedJobConfigXML)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func jobNotExists(err error) bool {
|
||||
if err != nil {
|
||||
return err.Error() == errors.New("404").Error()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// triggerConfigureSeedJobsPipeline triggers and configures seed job for specific GitHub repository
|
||||
func triggerConfigureSeedJobsPipeline(jenkinsClient jenkins.Jenkins, deployKeyID, privateKey, repositoryURL, repositoryBranch, targets, displayName string) error {
|
||||
options := map[string]string{
|
||||
@@ -71,7 +85,7 @@ func triggerConfigureSeedJobsPipeline(jenkinsClient jenkins.Jenkins, deployKeyID
|
||||
targetsParameterName: targets,
|
||||
displayNameParameterName: displayName,
|
||||
}
|
||||
// FIXME(bantoniak) implement EnsureJob()
|
||||
// FIXME implement EnsureJob()
|
||||
_, err := jenkinsClient.BuildJob(ConfigureSeedJobsName, options)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -1,25 +1,54 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
virtuslabv1alpha1 "github.com/VirtusLab/jenkins-operator/pkg/apis/virtuslab/v1alpha1"
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
k8s "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (r *ReconcileUserConfiguration) validate(jenkins *virtuslabv1alpha1.Jenkins) bool {
|
||||
func (r *ReconcileUserConfiguration) validate(k8sClient k8s.Client, jenkins *virtuslabv1alpha1.Jenkins) bool {
|
||||
// validate jenkins.Spec.SeedJobs
|
||||
if jenkins.Spec.SeedJobs != nil {
|
||||
for _, seedJob := range jenkins.Spec.SeedJobs {
|
||||
|
||||
// validate seed job id is not empty
|
||||
if len(seedJob.ID) == 0 {
|
||||
r.logger.V(0).Info("seed job id can't be empty")
|
||||
return false
|
||||
}
|
||||
|
||||
// validate repository url match private key
|
||||
if strings.Contains(seedJob.RepositoryURL, "git@") {
|
||||
if seedJob.PrivateKey.SecretKeyRef == nil {
|
||||
r.logger.V(0).Info("private key can't be empty while using ssh repository url")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// validate private key from secret
|
||||
if seedJob.PrivateKey.SecretKeyRef != nil {
|
||||
deployKeySecret := &v1.Secret{}
|
||||
namespaceName := types.NamespacedName{Namespace: jenkins.Namespace, Name: seedJob.PrivateKey.SecretKeyRef.Name}
|
||||
err := k8sClient.Get(context.TODO(), namespaceName, deployKeySecret)
|
||||
if err != nil {
|
||||
r.logger.V(0).Info("couldn't read private key secret")
|
||||
return false
|
||||
}
|
||||
|
||||
privateKey := string(deployKeySecret.Data[seedJob.PrivateKey.SecretKeyRef.Key])
|
||||
if privateKey != "" {
|
||||
r.logger.V(0).Info("private key is empty")
|
||||
return false
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(privateKey, "-----BEGIN RSA PRIVATE KEY-----") {
|
||||
r.logger.V(0).Info("private key has wrong prefix")
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user