mirror of
https://github.com/jenkinsci/kubernetes-operator.git
synced 2026-10-18 05:54:47 +02:00
update to go 1.22 and operator-sdk 1.35 (#1094)
Co-authored-by: brokenpip3 <brokenpip3@gmail.com> Co-authored-by: Ansh Garhewal <me@anshdevs.in> Co-authored-by: xmbhasin <xmbhasin@users.noreply.github.com>
This commit is contained in:
co-authored by
brokenpip3
Ansh Garhewal
xmbhasin
parent
1ec83b9de9
commit
9c09db8031
@@ -60,6 +60,14 @@ func (r *reconcileUserConfiguration) ReconcileOthers() (reconcile.Result, error)
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
if err := backupAndRestore.Backup(false); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
if err := backupAndRestore.EnsureBackupTrigger(); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
result, err := r.ensureSeedJobs()
|
||||
if err != nil {
|
||||
return reconcile.Result{}, err
|
||||
@@ -68,13 +76,6 @@ func (r *reconcileUserConfiguration) ReconcileOthers() (reconcile.Result, error)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
if err := backupAndRestore.Backup(false); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
if err := backupAndRestore.EnsureBackupTrigger(); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -218,6 +218,7 @@ func (s *seedJobs) EnsureSeedJobs(jenkins *v1alpha2.Jenkins) (done bool, err err
|
||||
})
|
||||
|
||||
if err != nil && !apierrors.IsNotFound(err) {
|
||||
|
||||
return false, stackerr.WithStack(err)
|
||||
}
|
||||
}
|
||||
@@ -236,6 +237,7 @@ func (s *seedJobs) EnsureSeedJobs(jenkins *v1alpha2.Jenkins) (done bool, err err
|
||||
|
||||
seedJobIDs := s.getAllSeedJobIDs(*jenkins)
|
||||
if !reflect.DeepEqual(seedJobIDs, jenkins.Status.CreatedSeedJobs) {
|
||||
// @ansh-devs fixed : calls to Update and Patch will not alter its status.
|
||||
jenkins.Status.CreatedSeedJobs = seedJobIDs
|
||||
return false, stackerr.WithStack(s.Client.Status().Update(context.TODO(), jenkins))
|
||||
}
|
||||
@@ -303,6 +305,7 @@ func (s *seedJobs) createJobs(jenkins *v1alpha2.Jenkins) (requeue bool, err erro
|
||||
// Operator will able to watch any changes made to them
|
||||
func (s *seedJobs) ensureLabelsForSecrets(jenkins v1alpha2.Jenkins) error {
|
||||
for _, seedJob := range jenkins.Spec.SeedJobs {
|
||||
|
||||
if seedJob.JenkinsCredentialType == v1alpha2.BasicSSHCredentialType || seedJob.JenkinsCredentialType == v1alpha2.UsernamePasswordCredentialType {
|
||||
requiredLabels := resources.BuildLabelsForWatchedResources(jenkins)
|
||||
requiredLabels[JenkinsCredentialTypeLabelName] = string(seedJob.JenkinsCredentialType)
|
||||
@@ -344,7 +347,7 @@ func (s *seedJobs) credentialValue(namespace string, seedJob v1alpha2.SeedJob) (
|
||||
}
|
||||
|
||||
func (s *seedJobs) getAllSeedJobIDs(jenkins v1alpha2.Jenkins) []string {
|
||||
var ids []string
|
||||
ids := make([]string, 0, len(jenkins.Spec.SeedJobs))
|
||||
for _, seedJob := range jenkins.Spec.SeedJobs {
|
||||
ids = append(ids, seedJob.ID)
|
||||
}
|
||||
@@ -369,11 +372,11 @@ func (s *seedJobs) isRecreatePodNeeded(jenkins v1alpha2.Jenkins) bool {
|
||||
|
||||
// createAgent deploys Jenkins agent to Kubernetes cluster
|
||||
func (s *seedJobs) createAgent(jenkinsClient jenkinsclient.Jenkins, k8sClient client.Client, jenkinsManifest *v1alpha2.Jenkins, namespace string, agentName string) error {
|
||||
_, err := jenkinsClient.GetNode(agentName)
|
||||
_, err := jenkinsClient.GetNode(context.TODO(), agentName)
|
||||
|
||||
// Create node if not exists
|
||||
if err != nil && err.Error() == "No node found" {
|
||||
_, err = jenkinsClient.CreateNode(agentName, 5, "The jenkins-operator generated agent", "/home/jenkins", agentName)
|
||||
_, err = jenkinsClient.CreateNode(context.TODO(), agentName, 5, "The jenkins-operator generated agent", "/home/jenkins", agentName)
|
||||
if err != nil {
|
||||
return stackerr.WithStack(err)
|
||||
}
|
||||
|
||||
@@ -97,8 +97,8 @@ func TestEnsureSeedJobs(t *testing.T) {
|
||||
seedJobCreatingScript, err := seedJobCreatingGroovyScript(jenkins.Spec.SeedJobs[0])
|
||||
assert.NoError(t, err)
|
||||
|
||||
jenkinsClient.EXPECT().GetNode(AgentName).Return(nil, nil).AnyTimes()
|
||||
jenkinsClient.EXPECT().CreateNode(AgentName, 1, "The jenkins-operator generated agent", "/home/jenkins", AgentName).Return(testNode, nil).AnyTimes()
|
||||
jenkinsClient.EXPECT().GetNode(context.TODO(), AgentName).Return(nil, nil).AnyTimes()
|
||||
jenkinsClient.EXPECT().CreateNode(context.TODO(), AgentName, 1, "The jenkins-operator generated agent", "/home/jenkins", AgentName).Return(testNode, nil).AnyTimes()
|
||||
jenkinsClient.EXPECT().GetNodeSecret(AgentName).Return(agentSecret, nil).AnyTimes()
|
||||
jenkinsClient.EXPECT().ExecuteScript(seedJobCreatingScript).AnyTimes()
|
||||
|
||||
@@ -128,7 +128,10 @@ func TestEnsureSeedJobs(t *testing.T) {
|
||||
jenkins.Spec.SeedJobs = []v1alpha2.SeedJob{}
|
||||
|
||||
jenkinsClient := jenkinsclient.NewMockJenkins(ctrl)
|
||||
fakeClient := fake.NewClientBuilder().Build()
|
||||
fakeClient := fake.NewClientBuilder().
|
||||
WithRuntimeObjects(jenkins).
|
||||
WithStatusSubresource(jenkins).
|
||||
Build()
|
||||
err := v1alpha2.SchemeBuilder.AddToScheme(scheme.Scheme)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -139,8 +142,8 @@ func TestEnsureSeedJobs(t *testing.T) {
|
||||
Jenkins: jenkins,
|
||||
}
|
||||
|
||||
jenkinsClient.EXPECT().GetNode(AgentName).AnyTimes()
|
||||
jenkinsClient.EXPECT().CreateNode(AgentName, 1, "The jenkins-operator generated agent", "/home/jenkins", AgentName).AnyTimes()
|
||||
jenkinsClient.EXPECT().GetNode(ctx, AgentName).AnyTimes()
|
||||
jenkinsClient.EXPECT().CreateNode(ctx, AgentName, 1, "The jenkins-operator generated agent", "/home/jenkins", AgentName).AnyTimes()
|
||||
jenkinsClient.EXPECT().GetNodeSecret(AgentName).Return(agentSecret, nil).AnyTimes()
|
||||
|
||||
seedJobsClient := New(jenkinsClient, config)
|
||||
@@ -155,13 +158,14 @@ func TestEnsureSeedJobs(t *testing.T) {
|
||||
|
||||
// when
|
||||
_, err = seedJobsClient.EnsureSeedJobs(jenkins)
|
||||
|
||||
// then
|
||||
assert.NoError(t, err)
|
||||
|
||||
var deployment appsv1.Deployment
|
||||
err = fakeClient.Get(ctx, types.NamespacedName{Namespace: jenkins.Namespace, Name: agentDeploymentName(*jenkins, AgentName)}, &deployment)
|
||||
|
||||
err = fakeClient.Get(ctx, types.NamespacedName{
|
||||
Namespace: jenkins.Namespace,
|
||||
Name: agentDeploymentName(*jenkins, AgentName),
|
||||
}, &deployment)
|
||||
assert.True(t, errors.IsNotFound(err), "Agent deployment hasn't been deleted")
|
||||
})
|
||||
}
|
||||
@@ -180,8 +184,8 @@ func TestCreateAgent(t *testing.T) {
|
||||
err := v1alpha2.SchemeBuilder.AddToScheme(scheme.Scheme)
|
||||
assert.NoError(t, err)
|
||||
|
||||
jenkinsClient.EXPECT().GetNode(AgentName).AnyTimes()
|
||||
jenkinsClient.EXPECT().CreateNode(AgentName, 1, "The jenkins-operator generated agent", "/home/jenkins", AgentName).AnyTimes()
|
||||
jenkinsClient.EXPECT().GetNode(context.TODO(), AgentName).AnyTimes()
|
||||
jenkinsClient.EXPECT().CreateNode(context.TODO(), AgentName, 1, "The jenkins-operator generated agent", "/home/jenkins", AgentName).AnyTimes()
|
||||
jenkinsClient.EXPECT().GetNodeSecret(AgentName).Return(agentSecret, nil).AnyTimes()
|
||||
|
||||
config := configuration.Configuration{
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
|
||||
// ValidateSeedJobs verify seed jobs configuration
|
||||
func (s *seedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) ([]string, error) {
|
||||
var messages []string
|
||||
messages := []string{}
|
||||
|
||||
if msg := s.validateIfIDIsUnique(jenkins.Spec.SeedJobs); len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
@@ -88,24 +88,28 @@ func (s *seedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) ([]string, error)
|
||||
}
|
||||
}
|
||||
|
||||
if seedJob.GitHubPushTrigger {
|
||||
if msg := s.validateGitHubPushTrigger(jenkins); len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
|
||||
}
|
||||
}
|
||||
}
|
||||
s.setSeedJobPushTriggers(seedJob, &messages, jenkins)
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
if seedJob.BitbucketPushTrigger {
|
||||
if msg := s.validateBitbucketPushTrigger(jenkins); len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
|
||||
}
|
||||
func (s *seedJobs) setSeedJobPushTriggers(seedJob v1alpha2.SeedJob, messages *[]string, jenkins v1alpha2.Jenkins) {
|
||||
if seedJob.GitHubPushTrigger {
|
||||
if msg := s.validateGitHubPushTrigger(jenkins); len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
*messages = append(*messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if seedJob.BitbucketPushTrigger {
|
||||
if msg := s.validateBitbucketPushTrigger(jenkins); len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
*messages = append(*messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
func (s *seedJobs) validateGitHubPushTrigger(jenkins v1alpha2.Jenkins) []string {
|
||||
@@ -125,6 +129,7 @@ func (s *seedJobs) validateBitbucketPushTrigger(jenkins v1alpha2.Jenkins) []stri
|
||||
}
|
||||
|
||||
func checkPluginExists(jenkins v1alpha2.Jenkins, name string) error {
|
||||
|
||||
exists := false
|
||||
for _, plugin := range jenkins.Spec.Master.BasePlugins {
|
||||
if plugin.Name == name {
|
||||
|
||||
@@ -107,7 +107,7 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("Invalid without id", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -178,7 +178,7 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("Invalid ed25519 private key in secret", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -261,7 +261,7 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("Invalid RSA private key in secret", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -508,7 +508,7 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("Invalid with empty username", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -715,7 +715,7 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("Invalid with empty app id", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -914,7 +914,7 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("Invalid with set githubPushTrigger and not installed github plugin", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -984,7 +984,7 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("Invalid with set bitbucketPushTrigger and not installed bitbucket plugin", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -1054,7 +1054,7 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user