Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error during showJenkinsUrl #1010

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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