Skip to content

Commit

Permalink
fix: error during showJenkinsUrl
Browse files Browse the repository at this point in the history
Signed-off-by: Bird <aflybird0@gmail.com>
  • Loading branch information
aFlyBird0 committed Aug 18, 2022
1 parent 77caefb commit cd92974
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
12 changes: 12 additions & 0 deletions internal/pkg/plugin/jenkins/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jenkins

import (
"fmt"
"strings"

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
Expand Down Expand Up @@ -39,3 +42,12 @@ func setDefaultValue(defaultOpts *helm.Options) plugininstaller.MutableOperation
return opts.encode()
}
}

// refer: https://github.com/jenkinsci/helm-charts/blob/30766b45faf639dbad45e2c66330ee5fcdc7e37f/charts/jenkins/templates/_helpers.tpl#L46-L57
func (opts *jenkinsOptions) getJenkinsFullName() string {
if strings.Contains(opts.Chart.ChartName, opts.Chart.ReleaseName) {
return opts.Chart.ReleaseName
}

return fmt.Sprintf("%s-%s", opts.Chart.ReleaseName, opts.Chart.ChartName)
}
26 changes: 17 additions & 9 deletions internal/pkg/plugin/jenkins/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
)

func buildPasswdOfAdminCommand(opts jenkinsOptions) string {
method := fmt.Sprintf("kubectl exec --namespace jenkins -it svc/%s-jenkins -c jenkins "+
"-- /bin/cat /run/secrets/additional/chart-admin-password && echo", opts.Chart.ReleaseName)
jenkinsFullName := opts.getJenkinsFullName()

return fmt.Sprintf("kubectl exec --namespace %s -it svc/%s -c jenkins "+
"-- /bin/cat /run/secrets/additional/chart-admin-password && echo", opts.Chart.Namespace, jenkinsFullName)

return method
}

func howToGetPasswdOfAdmin(options plugininstaller.RawOptions) error {
Expand Down Expand Up @@ -48,21 +49,25 @@ func getPasswdOfAdmin(options plugininstaller.RawOptions) (string, error) {
}

// getJenkinsURL returns the jenkins url of the jenkins, format: hostname:port
// there are duplicated code in getJenkinsURLForTestEnv, need to refactor
func getJenkinsURL(options plugininstaller.RawOptions) (string, error) {
opts, err := newOptions(options)
if err != nil {
return "", err
}

jenkinsFullName := opts.getJenkinsFullName()
commands := []string{
`jsonpath="{.spec.ports[0].nodePort}"`,
fmt.Sprintf(`NODE_PORT=$(kubectl get -n jenkins -o jsonpath=$jsonpath services %s-jenkins)`, opts.Chart.ReleaseName),
fmt.Sprintf(`NODE_PORT=$(kubectl get -n %s -o jsonpath=$jsonpath services %s)`, opts.Chart.Namespace, jenkinsFullName),
`jsonpath="{.items[0].status.addresses[0].address}"`,
`NODE_IP=$(kubectl get nodes -n jenkins -o jsonpath=$jsonpath)`,
fmt.Sprintf(`NODE_IP=$(kubectl get nodes -n %s -o jsonpath=$jsonpath)`, opts.Chart.Namespace),
`echo $NODE_IP:$NODE_PORT`,
}
commandsOneLine := strings.Join(commands, " && ")
log.Debugf("commands of get jenkins url: %s", commandsOneLine)

cmd := exec.Command("sh", "-c", strings.Join(commands, " && "))
cmd := exec.Command("sh", "-c", commandsOneLine)

output, err := cmd.Output()
if err != nil {
Expand All @@ -79,15 +84,18 @@ func getJenkinsURLForTestEnv(options plugininstaller.RawOptions) (string, error)
return "", err
}

jenkinsFullName := opts.getJenkinsFullName()
commands := []string{
`jsonpath="{.spec.ports[0].nodePort}"`,
fmt.Sprintf(`NODE_PORT=$(kubectl get -n jenkins -o jsonpath=$jsonpath services %s-jenkins)`, opts.Chart.ReleaseName),
fmt.Sprintf(`NODE_PORT=$(kubectl get -n %s -o jsonpath=$jsonpath services %s)`, opts.Chart.Namespace, jenkinsFullName),
`jsonpath="{.items[0].status.addresses[0].address}"`,
`NODE_IP=127.0.0.1`,
`echo $NODE_IP:$NODE_PORT`,
}
commandsOneLine := strings.Join(commands, " && ")
log.Debugf("commands of get jenkins url: %s", commandsOneLine)

cmd := exec.Command("sh", "-c", strings.Join(commands, " && "))
cmd := exec.Command("sh", "-c", commandsOneLine)

output, err := cmd.Output()
if err != nil {
Expand Down Expand Up @@ -130,7 +138,7 @@ func showJenkinsUrl(options plugininstaller.RawOptions) error {
log.Error(err)
return err
}
log.Info("Jenkins url in K8s:", fmt.Sprintf("http://%s/login", urlInK8s))
log.Info("Jenkins url in K8s: ", fmt.Sprintf("http://%s/login", urlInK8s))

}

Expand Down

0 comments on commit cd92974

Please sign in to comment.