Skip to content

Commit

Permalink
[mod] postrun, prerun API에 tektonTaskParam을 body request로 받아 적용 (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwook-lee authored Dec 21, 2023
1 parent 149976e commit 6687b1f
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Current Operator version
VERSION ?= v0.6.2
VERSION ?= v0.6.3
REGISTRY ?= tmaxcloudck

# Image URL to use all building/pushing image targets
Expand Down
20 changes: 17 additions & 3 deletions api/v1/integrationconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,28 @@ const (
// IntegrationConfigAPIReqRunPreBody is a body struct for IntegrationConfig's api request
// +kubebuilder:object:generate=false
type IntegrationConfigAPIReqRunPreBody struct {
BaseBranch string `json:"base_branch"`
HeadBranch string `json:"head_branch"`
BaseBranch string `json:"base_branch"`
HeadBranch string `json:"head_branch"`
AddTektonTaskParams []AddTektonTaskParam `json:"addTektonTaskParams,omitempty"`
}

// IntegrationConfigAPIReqRunPostBody is a body struct for IntegrationConfig's api request
// +kubebuilder:object:generate=false
type IntegrationConfigAPIReqRunPostBody struct {
Branch string `json:"branch"`
Branch string `json:"branch"`
AddTektonTaskParams []AddTektonTaskParam `json:"addTektonTaskParams,omitempty"`
}

// AddTektonTaskParam represents additional Tekton task parameters
type AddTektonTaskParam struct {
JobName string `json:"jobName"`
TektonTask []TektonTaskDef `json:"tektonTask"`
}

// TektonTaskDef represents a definition for a Tekton task
type TektonTaskDef struct {
Name string `json:"name"`
StringVal string `json:"stringVal"`
}

// IntegrationConfigAPIReqWebhookURL is a body struct for IntegrationConfig's api request
Expand Down
35 changes: 35 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions config/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ spec:
containers:
- command:
- /controller
image: docker.io/tmaxcloudck/cicd-operator:v0.6.2
image: docker.io/tmaxcloudck/cicd-operator:v0.6.3
imagePullPolicy: Always
name: manager
env:
Expand Down Expand Up @@ -171,7 +171,7 @@ spec:
containers:
- command:
- /blocker
image: docker.io/tmaxcloudck/cicd-blocker:v0.6.2
image: docker.io/tmaxcloudck/cicd-blocker:v0.6.3
imagePullPolicy: Always
name: manager
resources:
Expand Down Expand Up @@ -231,7 +231,7 @@ spec:
containers:
- command:
- /webhook
image: docker.io/tmaxcloudck/cicd-webhook:v0.6.2
image: docker.io/tmaxcloudck/cicd-webhook:v0.6.3
imagePullPolicy: Always
name: manager
resources:
Expand Down Expand Up @@ -291,7 +291,7 @@ spec:
containers:
- command:
- /apiserver
image: docker.io/tmaxcloudck/cicd-api-server:v0.6.2
image: docker.io/tmaxcloudck/cicd-api-server:v0.6.3
imagePullPolicy: Always
name: manager
resources:
Expand Down
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ require (
sigs.k8s.io/controller-runtime v0.10.2
)

require (
github.com/fatih/color v1.12.0 // indirect
github.com/gobuffalo/flect v0.2.3 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
sigs.k8s.io/controller-tools v0.7.0 // indirect
)

require (
cloud.google.com/go v0.83.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ github.com/evanphx/json-patch v4.11.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQL
github.com/evanphx/json-patch/v5 v5.5.0 h1:bAmFiUJ+o0o2B4OiTFeE3MqCOtyo+jjPP9iZ0VRxYUc=
github.com/evanphx/json-patch/v5 v5.5.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
Expand Down Expand Up @@ -467,6 +469,7 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gobuffalo/flect v0.2.2/go.mod h1:vmkQwuZYhN5Pc4ljYQZzP+1sq+NEkK+lh20jmEmX3jc=
github.com/gobuffalo/flect v0.2.3 h1:f/ZukRnSNA/DUpSNDadko7Qc0PhGvsew35p/2tu+CRY=
github.com/gobuffalo/flect v0.2.3/go.mod h1:vmkQwuZYhN5Pc4ljYQZzP+1sq+NEkK+lh20jmEmX3jc=
github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
Expand Down Expand Up @@ -733,8 +736,12 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -817,6 +824,7 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/onsi/gomega v1.14.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
github.com/onsi/gomega v1.15.0 h1:WjP/FQ/sk43MRmnEcT+MlDw2TFvkrXlprrPST/IudjU=
github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
Expand Down Expand Up @@ -1677,12 +1685,10 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down Expand Up @@ -1778,6 +1784,8 @@ sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyz
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.22/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/controller-runtime v0.10.2 h1:jW8qiY+yMnnPx6O9hu63tgcwaKzd1yLYui+mpvClOOc=
sigs.k8s.io/controller-runtime v0.10.2/go.mod h1:CQp8eyUQZ/Q7PJvnIrB6/hgfTC1kBkGylwsLgOQi1WY=
sigs.k8s.io/controller-tools v0.7.0 h1:iZIz1vEcavyEfxjcTLs1WH/MPf4vhPCtTKhoHqV8/G0=
sigs.k8s.io/controller-tools v0.7.0/go.mod h1:bpBAo0VcSDDLuWt47evLhMLPxRPxMDInTEH/YbdeMK0=
sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.0.3/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
Expand Down
8 changes: 4 additions & 4 deletions hack/release-manifest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ for target in "${TARGETS[@]}"; do
append_target "$target"
done

sed -i "s/tmaxcloudck\/cicd-operator:latest/$REGISTRY\/cicd-operator:$VERSION/g" "$RELEASE_MANIFEST"
sed -i "s/tmaxcloudck\/cicd-blocker:latest/$REGISTRY\/cicd-blocker:$VERSION/g" "$RELEASE_MANIFEST"
sed -i "s/tmaxcloudck\/cicd-webhook:latest/$REGISTRY\/cicd-webhook:$VERSION/g" "$RELEASE_MANIFEST"
sed -i "s/tmaxcloudck\/cicd-api-server:latest/$REGISTRY\/cicd-api-server:$VERSION/g" "$RELEASE_MANIFEST"
sed -i "s|tmaxcloudck/cicd-operator:latest|$REGISTRY/cicd-operator:$VERSION|g" "$RELEASE_MANIFEST"
sed -i "s|tmaxcloudck/cicd-blocker:latest|$REGISTRY/cicd-blocker:$VERSION|g" "$RELEASE_MANIFEST"
sed -i "s|tmaxcloudck/cicd-webhook:latest|$REGISTRY/cicd-webhook:$VERSION|g" "$RELEASE_MANIFEST"
sed -i "s|tmaxcloudck/cicd-api-server:latest|$REGISTRY/cicd-api-server:$VERSION|g" "$RELEASE_MANIFEST"
133 changes: 118 additions & 15 deletions pkg/apiserver/apis/v1/integrationconfigs/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package integrationconfigs
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"

Expand Down Expand Up @@ -93,17 +93,32 @@ func (h *handler) runHandler(w http.ResponseWriter, req *http.Request, et git.Ev
},
}

userReqPre := &cicdv1.IntegrationConfigAPIReqRunPreBody{}
userReqPost := &cicdv1.IntegrationConfigAPIReqRunPostBody{}

switch et {
case git.EventTypePullRequest:
pr, err := buildPullRequestWebhook(req.Body, userEscaped)
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(userReqPre); err != nil {
log.Info(err.Error())
_ = utils.RespondError(w, http.StatusBadRequest, fmt.Sprintf("req: %s, cannot build pull_request webhook", reqID))
return
}
pr, err := buildPullRequestWebhook(userReqPre, userEscaped)
if err != nil {
log.Info(err.Error())
_ = utils.RespondError(w, http.StatusBadRequest, fmt.Sprintf("req: %s, cannot build pull_request webhook", reqID))
return
}
wh.PullRequest = pr
case git.EventTypePush:
push, err := buildPushWebhook(req.Body)
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(userReqPost); err != nil {
log.Info(err.Error())
_ = utils.RespondError(w, http.StatusBadRequest, fmt.Sprintf("req: %s, cannot build pull_request webhook", reqID))
return
}
push, err := buildPushWebhook(userReqPost)
if err != nil {
log.Info(err.Error())
_ = utils.RespondError(w, http.StatusBadRequest, fmt.Sprintf("req: %s, cannot build push webhook", reqID))
Expand All @@ -115,6 +130,26 @@ func (h *handler) runHandler(w http.ResponseWriter, req *http.Request, et git.Ev
Name: fmt.Sprintf("trigger-%s-end", userEscaped),
}

// Update IntegrationConfig based on the request
switch et {
case git.EventTypePullRequest:
updatedIC, err := updateIntegrationConfigPre(ic, userReqPre, et)
if err != nil {
log.Info(err.Error())
_ = utils.RespondError(w, http.StatusBadRequest, "cannot update pull_request integrationconfig. please check jobName is valid")
return
}
ic = updatedIC
case git.EventTypePush:
updatedIC, err := updateIntegrationConfigPost(ic, userReqPost, et)
if err != nil {
log.Info(err.Error())
_ = utils.RespondError(w, http.StatusBadRequest, "cannot update push integrationconfig. please check jobName is valid")
return
}
ic = updatedIC
}

// Trigger Run!
if err := server.HandleEvent(wh, ic, "dispatcher"); err != nil {
log.Info(err.Error())
Expand All @@ -125,13 +160,87 @@ func (h *handler) runHandler(w http.ResponseWriter, req *http.Request, et git.Ev
_ = utils.RespondJSON(w, struct{}{})
}

func buildPullRequestWebhook(body io.Reader, user string) (*git.PullRequest, error) {
userReq := &cicdv1.IntegrationConfigAPIReqRunPreBody{}
decoder := json.NewDecoder(body)
if err := decoder.Decode(userReq); err != nil {
return nil, err
func updateIntegrationConfigPost(ic *cicdv1.IntegrationConfig, userReqBody *cicdv1.IntegrationConfigAPIReqRunPostBody, et git.EventType) (*cicdv1.IntegrationConfig, error) {
targetJob := &ic.Spec.Jobs.PostSubmit
var existingJob *cicdv1.Job

for _, addParams := range userReqBody.AddTektonTaskParams {
jobName := addParams.JobName
if jobName == "" {
return nil, errors.New("JobName must be set")
}

for i, job := range *targetJob {
if job.Name == jobName {
existingJob = &(*targetJob)[i]
break
}
}

if existingJob == nil {
return nil, fmt.Errorf("job with name '%s' not found", jobName)
}

for _, taskDef := range addParams.TektonTask {
// Check if a parameter with the same name already exists
for i, existingParam := range existingJob.TektonTask.Params {
if existingParam.Name == taskDef.Name {
existingJob.TektonTask.Params = append(existingJob.TektonTask.Params[:i], existingJob.TektonTask.Params[i+1:]...)
break
}
}

existingJob.TektonTask.Params = append(existingJob.TektonTask.Params, cicdv1.ParameterValue{
Name: taskDef.Name,
StringVal: taskDef.StringVal,
})
}
}

return ic, nil
}

func updateIntegrationConfigPre(ic *cicdv1.IntegrationConfig, userReqBody *cicdv1.IntegrationConfigAPIReqRunPreBody, et git.EventType) (*cicdv1.IntegrationConfig, error) {
targetJob := &ic.Spec.Jobs.PreSubmit
var existingJob *cicdv1.Job

for _, addParams := range userReqBody.AddTektonTaskParams {
jobName := addParams.JobName
if jobName == "" {
return nil, errors.New("JobName must be set")
}

for i, job := range *targetJob {
if job.Name == jobName {
existingJob = &(*targetJob)[i]
break
}
}

if existingJob == nil {
return nil, fmt.Errorf("job with name '%s' not found", jobName)
}

for _, taskDef := range addParams.TektonTask {
// Check if a parameter with the same name already exists
for i, existingParam := range existingJob.TektonTask.Params {
if existingParam.Name == taskDef.Name {
existingJob.TektonTask.Params = append(existingJob.TektonTask.Params[:i], existingJob.TektonTask.Params[i+1:]...)
break
}
}

existingJob.TektonTask.Params = append(existingJob.TektonTask.Params, cicdv1.ParameterValue{
Name: taskDef.Name,
StringVal: taskDef.StringVal,
})
}
}

return ic, nil
}

func buildPullRequestWebhook(userReq *cicdv1.IntegrationConfigAPIReqRunPreBody, user string) (*git.PullRequest, error) {
baseBranch := userReq.BaseBranch
headBranch := userReq.HeadBranch
if baseBranch == "" {
Expand All @@ -158,13 +267,7 @@ func buildPullRequestWebhook(body io.Reader, user string) (*git.PullRequest, err
}, nil
}

func buildPushWebhook(body io.Reader) (*git.Push, error) {
userReq := &cicdv1.IntegrationConfigAPIReqRunPostBody{}
decoder := json.NewDecoder(body)
if err := decoder.Decode(userReq); err != nil {
return nil, err
}

func buildPushWebhook(userReq *cicdv1.IntegrationConfigAPIReqRunPostBody) (*git.Push, error) {
branch := userReq.Branch
if branch == "" {
branch = defaultBranch
Expand Down
Loading

0 comments on commit 6687b1f

Please sign in to comment.