-
Notifications
You must be signed in to change notification settings - Fork 35
/
pipelinerun.go
139 lines (122 loc) · 3.87 KB
/
pipelinerun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package handlers
import (
"context"
"fmt"
"net/http"
"strings"
"text/template"
"time"
"github.com/gorilla/mux"
"github.com/jenkins-x-plugins/jx-pipeline/pkg/cloud/buckets"
"github.com/jenkins-x-plugins/jx-pipeline/pkg/tektonlog"
jxclientv1 "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned/typed/jenkins.io/v1"
"github.com/jenkins-x/jx-helpers/v3/pkg/kube/activities"
visualizer "github.com/jenkins-x/jx-pipelines-visualizer"
"github.com/sirupsen/logrus"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tknclient "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
"github.com/unrolled/render"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type PipelineRunHandler struct {
PAInterfaceFactory func(namespace string) jxclientv1.PipelineActivityInterface
TektonClient tknclient.Interface
StoredPipelineRunsURLTemplate *template.Template
Namespace string
Store *visualizer.Store
Render *render.Render
Logger *logrus.Logger
}
func (h *PipelineRunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pipelineRunName := vars["pipelineRun"]
ns := vars["namespace"]
if ns == "" {
ns = h.Namespace
}
ctx := context.Background()
pr, err := h.TektonClient.TektonV1beta1().PipelineRuns(ns).Get(ctx, pipelineRunName, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if errors.IsNotFound(err) {
pr = nil
}
if pr == nil {
pr, err = h.loadPipelineRunFromStorage(ctx, ns, pipelineRunName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if pr == nil {
http.NotFound(w, r)
return
}
var (
owner = activities.GetLabel(pr.Labels, activities.OwnerLabels)
repo = activities.GetLabel(pr.Labels, activities.RepoLabels)
branch = activities.GetLabel(pr.Labels, activities.BranchLabels)
build = pr.Labels["build"]
)
if owner == "" || repo == "" || branch == "" || build == "" {
pa, err := tektonlog.GetPipelineActivityForPipelineRun(context.TODO(), h.PAInterfaceFactory(pr.Namespace), pr)
if err != nil && !errors.IsNotFound(err) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if pa == nil {
http.NotFound(w, r)
return
}
owner = pa.Spec.GitOwner
repo = pa.Spec.GitRepository
branch = pa.Spec.GitBranch
build = pa.Spec.Build
}
redirectURL := fmt.Sprintf("/ns-%s/%s/%s/%s/%s", ns, owner, repo, branch, build)
http.Redirect(w, r, redirectURL, http.StatusMovedPermanently)
}
func (h *PipelineRunHandler) loadPipelineRunFromStorage(ctx context.Context, namespace, name string) (*v1beta1.PipelineRun, error) {
storedPipelineRunURL, err := h.storedPipelineRunURL(namespace, name)
if err != nil {
return nil, err
}
if storedPipelineRunURL == "" {
return nil, nil
}
httpFn := func(urlString string) (string, func(*http.Request), error) {
return urlString, func(*http.Request) {}, nil
}
reader, err := buckets.ReadURL(ctx, storedPipelineRunURL, 30*time.Second, httpFn)
if err != nil {
if strings.Contains(err.Error(), "object doesn't exist") {
return nil, nil
}
return nil, err
}
defer reader.Close()
var pr v1beta1.PipelineRun
err = yaml.NewDecoder(reader).Decode(&pr)
if err != nil {
return nil, err
}
return &pr, nil
}
func (h *PipelineRunHandler) storedPipelineRunURL(namespace, name string) (string, error) {
if h.StoredPipelineRunsURLTemplate == nil {
return "", nil
}
sb := new(strings.Builder)
err := h.StoredPipelineRunsURLTemplate.Execute(sb, map[string]string{
"Namespace": namespace,
"Name": name,
})
if err != nil {
return "", fmt.Errorf("failed to generate stored pipeline run URL: %w", err)
}
return sb.String(), nil
}