From f0a23fec2e65693dd46f68b909bd5cca22e5342b Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Tue, 27 Feb 2024 20:57:51 +0800 Subject: [PATCH] solve controller start error when timeout in CloneOptionTrait is nil (#1029) --- .../devops/jenkins/internal/bitbucket.go | 17 +------ pkg/client/devops/jenkins/internal/git.go | 19 ++----- pkg/client/devops/jenkins/internal/github.go | 18 +------ pkg/client/devops/jenkins/internal/gitlab.go | 22 +------- pkg/client/devops/jenkins/internal/scm.go | 50 +++++++++++++++++++ pkg/kapis/imagebuilder/v1alpha1/handler.go | 2 +- 6 files changed, 59 insertions(+), 69 deletions(-) create mode 100644 pkg/client/devops/jenkins/internal/scm.go diff --git a/pkg/client/devops/jenkins/internal/bitbucket.go b/pkg/client/devops/jenkins/internal/bitbucket.go index 4921ce79..d640f370 100644 --- a/pkg/client/devops/jenkins/internal/bitbucket.go +++ b/pkg/client/devops/jenkins/internal/bitbucket.go @@ -141,22 +141,7 @@ func GetBitbucketServerSourceFromEtree(source *etree.Element) *devopsv1alpha3.Bi } } - if cloneTrait := traits.SelectElement( - "jenkins.plugins.git.traits.CloneOptionTrait"); cloneTrait != nil { - if cloneExtension := cloneTrait.SelectElement( - "extension"); cloneExtension != nil { - s.CloneOption = &devopsv1alpha3.GitCloneOption{} - if value, err := strconv.ParseBool(cloneExtension.SelectElement("shallow").Text()); err == nil { - s.CloneOption.Shallow = value - } - if value, err := strconv.ParseInt(cloneExtension.SelectElement("timeout").Text(), 10, 32); err == nil { - s.CloneOption.Timeout = int(value) - } - if value, err := strconv.ParseInt(cloneExtension.SelectElement("depth").Text(), 10, 32); err == nil { - s.CloneOption.Depth = int(value) - } - } - } + s.CloneOption = parseFromCloneTrait(traits.SelectElement("jenkins.plugins.git.traits.CloneOptionTrait")) if regexTrait := traits.SelectElement( "jenkins.scm.impl.trait.RegexSCMHeadFilterTrait"); regexTrait != nil { diff --git a/pkg/client/devops/jenkins/internal/git.go b/pkg/client/devops/jenkins/internal/git.go index 564b76b4..cc3384b2 100644 --- a/pkg/client/devops/jenkins/internal/git.go +++ b/pkg/client/devops/jenkins/internal/git.go @@ -90,22 +90,9 @@ func GetGitSourcefromEtree(source *etree.Element) *devopsv1alpha3.GitSource { "jenkins.plugins.git.traits.TagDiscoveryTrait"); tagDiscoverTrait != nil { gitSource.DiscoverTags = true } - if cloneTrait := traits.SelectElement( - "jenkins.plugins.git.traits.CloneOptionTrait"); cloneTrait != nil { - if cloneExtension := cloneTrait.SelectElement( - "extension"); cloneExtension != nil { - gitSource.CloneOption = &devopsv1alpha3.GitCloneOption{} - if value, err := strconv.ParseBool(cloneExtension.SelectElement("shallow").Text()); err == nil { - gitSource.CloneOption.Shallow = value - } - if value, err := strconv.ParseInt(cloneExtension.SelectElement("timeout").Text(), 10, 32); err == nil { - gitSource.CloneOption.Timeout = int(value) - } - if value, err := strconv.ParseInt(cloneExtension.SelectElement("depth").Text(), 10, 32); err == nil { - gitSource.CloneOption.Depth = int(value) - } - } - } + + gitSource.CloneOption = parseFromCloneTrait(traits.SelectElement("jenkins.plugins.git.traits.CloneOptionTrait")) + if regexTrait := traits.SelectElement( "jenkins.scm.impl.trait.RegexSCMHeadFilterTrait"); regexTrait != nil { if regex := regexTrait.SelectElement("regex"); regex != nil { diff --git a/pkg/client/devops/jenkins/internal/github.go b/pkg/client/devops/jenkins/internal/github.go index b4e5ba0d..a166c52a 100644 --- a/pkg/client/devops/jenkins/internal/github.go +++ b/pkg/client/devops/jenkins/internal/github.go @@ -138,22 +138,8 @@ func GetGithubSourcefromEtree(source *etree.Element) *devopsv1alpha3.GithubSourc klog.Warningf("invalid Gitlab discover PR trust value: %s", trust[1]) } } - if cloneTrait := traits.SelectElement( - "jenkins.plugins.git.traits.CloneOptionTrait"); cloneTrait != nil { - if cloneExtension := cloneTrait.SelectElement( - "extension"); cloneExtension != nil { - githubSource.CloneOption = &devopsv1alpha3.GitCloneOption{} - if value, err := strconv.ParseBool(cloneExtension.SelectElement("shallow").Text()); err == nil { - githubSource.CloneOption.Shallow = value - } - if value, err := strconv.ParseInt(cloneExtension.SelectElement("timeout").Text(), 10, 32); err == nil { - githubSource.CloneOption.Timeout = int(value) - } - if value, err := strconv.ParseInt(cloneExtension.SelectElement("depth").Text(), 10, 32); err == nil { - githubSource.CloneOption.Depth = int(value) - } - } - } + + githubSource.CloneOption = parseFromCloneTrait(traits.SelectElement("jenkins.plugins.git.traits.CloneOptionTrait")) if regexTrait := traits.SelectElement( "jenkins.scm.impl.trait.RegexSCMHeadFilterTrait"); regexTrait != nil { diff --git a/pkg/client/devops/jenkins/internal/gitlab.go b/pkg/client/devops/jenkins/internal/gitlab.go index a8360521..d902d740 100644 --- a/pkg/client/devops/jenkins/internal/gitlab.go +++ b/pkg/client/devops/jenkins/internal/gitlab.go @@ -137,26 +137,8 @@ func GetGitlabSourceFromEtree(source *etree.Element) (gitSource *devopsv1alpha3. klog.Warningf("invalid Gitlab discover PR trust value: %s", trust[1]) } } - if cloneTrait := traits.SelectElement( - "jenkins.plugins.git.traits.CloneOptionTrait"); cloneTrait != nil { - if cloneExtension := cloneTrait.SelectElement( - "extension"); cloneExtension != nil { - gitSource.CloneOption = &devopsv1alpha3.GitCloneOption{} - if value, err := strconv.ParseBool(cloneExtension.SelectElement("shallow").Text()); err == nil { - gitSource.CloneOption.Shallow = value - } - if timeout := cloneExtension.SelectElement("timeout"); timeout != nil { - if value, err := strconv.ParseInt(timeout.Text(), 10, 32); err == nil { - gitSource.CloneOption.Timeout = int(value) - } - } - if depth := cloneExtension.SelectElement("depth"); depth != nil { - if value, err := strconv.ParseInt(depth.Text(), 10, 32); err == nil { - gitSource.CloneOption.Depth = int(value) - } - } - } - } + + gitSource.CloneOption = parseFromCloneTrait(traits.SelectElement("jenkins.plugins.git.traits.CloneOptionTrait")) if regexTrait := traits.SelectElement( "jenkins.scm.impl.trait.RegexSCMHeadFilterTrait"); regexTrait != nil { diff --git a/pkg/client/devops/jenkins/internal/scm.go b/pkg/client/devops/jenkins/internal/scm.go new file mode 100644 index 00000000..fe3b002c --- /dev/null +++ b/pkg/client/devops/jenkins/internal/scm.go @@ -0,0 +1,50 @@ +/* +Copyright 2024 The KubeSphere Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import ( + "github.com/beevik/etree" + devopsv1alpha3 "kubesphere.io/devops/pkg/api/devops/v1alpha3" + "strconv" +) + +// common functions for all kinds of scm + +func parseFromCloneTrait(cloneTrait *etree.Element) *devopsv1alpha3.GitCloneOption { + var cloneOption *devopsv1alpha3.GitCloneOption + if cloneTrait != nil { + if cloneExtension := cloneTrait.SelectElement("extension"); cloneExtension != nil { + cloneOption = &devopsv1alpha3.GitCloneOption{} + if shallow := cloneExtension.SelectElement("shallow"); shallow != nil { + if value, err := strconv.ParseBool(shallow.Text()); err == nil { + cloneOption.Shallow = value + } + } + if timeout := cloneExtension.SelectElement("timeout"); timeout != nil { + if value, err := strconv.ParseInt(timeout.Text(), 10, 32); err == nil { + cloneOption.Timeout = int(value) + } + } + if depth := cloneExtension.SelectElement("depth"); depth != nil { + if value, err := strconv.ParseInt(depth.Text(), 10, 32); err == nil { + cloneOption.Depth = int(value) + } + } + } + } + return cloneOption +} diff --git a/pkg/kapis/imagebuilder/v1alpha1/handler.go b/pkg/kapis/imagebuilder/v1alpha1/handler.go index b3a33625..307b23d9 100644 --- a/pkg/kapis/imagebuilder/v1alpha1/handler.go +++ b/pkg/kapis/imagebuilder/v1alpha1/handler.go @@ -32,7 +32,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -const LabelKeyLanguage = "language" +const LabelKeyLanguage = "language" // apiHandlerOption holds some useful tools for API handler. type apiHandlerOption struct {