Skip to content

Commit

Permalink
GitLab Pull-Push checker
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwlde authored and Denys Lemeshko committed Oct 2, 2023
1 parent fe33c51 commit 5a12453
Show file tree
Hide file tree
Showing 15 changed files with 1,037 additions and 9 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ compress: .bin/upx

.PHONY: linux
linux:
GOOS=linux GOARCH=amd64 go build -o ./.bin/$(NAME)_linux_amd64 -ldflags "-X \"main.version=$(VERSION_TAG)\"" main.go
GOOS=linux GOARCH=arm64 go build -o ./.bin/$(NAME)_linux_arm64 -ldflags "-X \"main.version=$(VERSION_TAG)\"" main.go
GOOS=linux GOARCH=amd64 go build -o ./.bin/$(NAME)_linux_amd64 -ldflags "-w -s -X \"main.version=$(VERSION_TAG)\"" main.go
GOOS=linux GOARCH=arm64 go build -o ./.bin/$(NAME)_linux_arm64 -ldflags "-w -s -X \"main.version=$(VERSION_TAG)\"" main.go

.PHONY: darwin
darwin:
GOOS=darwin GOARCH=amd64 go build -o ./.bin/$(NAME)_darwin_amd64 -ldflags "-X \"main.version=$(VERSION_TAG)\"" main.go
GOOS=darwin GOARCH=arm64 go build -o ./.bin/$(NAME)_darwin_arm64 -ldflags "-X \"main.version=$(VERSION_TAG)\"" main.go
GOOS=darwin GOARCH=amd64 go build -o ./.bin/$(NAME)_darwin_amd64 -ldflags "-w -s -X \"main.version=$(VERSION_TAG)\"" main.go
GOOS=darwin GOARCH=arm64 go build -o ./.bin/$(NAME)_darwin_arm64 -ldflags "-w -s -X \"main.version=$(VERSION_TAG)\"" main.go

.PHONY: windows
windows:
GOOS=windows GOARCH=amd64 go build -o ./.bin/$(NAME).exe -ldflags "-X \"main.version=$(VERSION_TAG)\"" main.go
GOOS=windows GOARCH=amd64 go build -o ./.bin/$(NAME).exe -ldflags "-w -s -X \"main.version=$(VERSION_TAG)\"" main.go

.PHONY: binaries
binaries: linux darwin windows compress
Expand Down Expand Up @@ -162,11 +162,11 @@ dev:

.PHONY: build
build:
go build -o ./.bin/$(NAME) -ldflags "-X \"main.version=$(VERSION_TAG)\"" main.go
go build -o ./.bin/$(NAME) -ldflags "-w -s -X \"main.version=$(VERSION_TAG)\"" main.go

.PHONY: fast-build
fast-build:
go build --tags fast -o ./.bin/$(NAME) -ldflags "-X \"main.version=$(VERSION_TAG)\"" main.go
go build --tags fast -o ./.bin/$(NAME) -ldflags "-w -s -X \"main.version=$(VERSION_TAG)\"" main.go

.PHONY: install
install:
Expand Down
4 changes: 4 additions & 0 deletions api/v1/canary_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type CanarySpec struct {
MongoDB []MongoDBCheck `yaml:"mongodb,omitempty" json:"mongodb,omitempty"`
CloudWatch []CloudWatchCheck `yaml:"cloudwatch,omitempty" json:"cloudwatch,omitempty"`
GitHub []GitHubCheck `yaml:"github,omitempty" json:"github,omitempty"`
GitLab []GitLabCheck `yaml:"gitlab,omitempty" json:"gitlab,omitempty"`
Kubernetes []KubernetesCheck `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty"`
Folder []FolderCheck `yaml:"folder,omitempty" json:"folder,omitempty"`
Exec []ExecCheck `yaml:"exec,omitempty" json:"exec,omitempty"`
Expand Down Expand Up @@ -161,6 +162,9 @@ func (spec CanarySpec) GetAllChecks() []external.Check {
for _, check := range spec.GitHub {
checks = append(checks, check)
}
for _, check := range spec.GitLab {
checks = append(checks, check)
}
for _, check := range spec.Kubernetes {
checks = append(checks, check)
}
Expand Down
18 changes: 18 additions & 0 deletions api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,23 @@ func (c GitHubCheck) GetEndpoint() string {
return strings.ReplaceAll(c.Query, " ", "-")
}

type GitLabCheck struct {
Description `yaml:",inline" json:",inline"`
Templatable `yaml:",inline" json:",inline"`
ConnectionName string `yaml:"connection,omitempty" json:"connection,omitempty"`
Repository string `yaml:"repository" json:"repository"`
Username string `yaml:"username" json:"username"`
Password types.EnvVar `yaml:"password,omitempty" json:"password,omitempty"`
}

func (c GitLabCheck) GetType() string {
return "gitProtocol"
}

func (c GitLabCheck) GetEndpoint() string {
return strings.ReplaceAll(c.Repository, "/", "-")
}

type ConfigDBCheck struct {
Templatable `yaml:",inline" json:",inline"`
Description `yaml:",inline" json:",inline"`
Expand Down Expand Up @@ -1353,6 +1370,7 @@ var AllChecks = []external.Check{
ExecCheck{},
FolderCheck{},
GitHubCheck{},
GitLabCheck{},
HelmCheck{},
HTTPCheck{},
ICMPCheck{},
Expand Down
25 changes: 25 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.

1 change: 1 addition & 0 deletions checks/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var All = []Checker{
&ExecChecker{},
&FolderChecker{},
&GitHubChecker{},
&GitLabChecker{},
&HTTPChecker{},
&IcmpChecker{},
&JmeterChecker{},
Expand Down
120 changes: 120 additions & 0 deletions checks/gitlab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package checks

import (
"fmt"
"os"
"path"
"time"

"github.com/flanksource/canary-checker/api/context"
"github.com/flanksource/canary-checker/api/external"
v1 "github.com/flanksource/canary-checker/api/v1"
"github.com/flanksource/canary-checker/pkg"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)

const (
TestFileName = "test.txt"
)

type GitLabChecker struct{}

func (c *GitLabChecker) Type() string {
return "gitlab"
}

func (c *GitLabChecker) Run(ctx *context.Context) pkg.Results {
var results pkg.Results
for _, conf := range ctx.Canary.Spec.GitLab {
results = append(results, c.Check(ctx, conf)...)
}
return results
}

func pushChanges(repoURL, username, password string) error {
dir, err := os.MkdirTemp("", "repo-clone-")
if err != nil {
return fmt.Errorf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)

r, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: repoURL,
Auth: &http.BasicAuth{Username: username, Password: password},
})
if err != nil {
return fmt.Errorf("failed to clone repo: %v", err)
}

w, err := r.Worktree()
if err != nil {
return fmt.Errorf("failed to get worktree: %v", err)
}

filePath := path.Join(dir, TestFileName)
currentTime := time.Now().Format("2006-01-02-04-05")

// Check if the file exists and if not we will create it
if _, err := os.Stat(filePath); os.IsNotExist(err) {
err = os.WriteFile(filePath, []byte(currentTime), 0644)
if err != nil {
return fmt.Errorf("failed to write to file: %v", err)
}
}

updatedContent := fmt.Sprintf("Updated at: %s", currentTime)
err = os.WriteFile(filePath, []byte(updatedContent), 0644)
if err != nil {
return fmt.Errorf("failed to update file: %v", err)
}

if _, err := w.Add(TestFileName); err != nil {
return fmt.Errorf("failed to add changes to staging area: %v", err)
}

commitMsg := fmt.Sprintf("Updated %s with time %s", TestFileName, currentTime)
if _, err := w.Commit(commitMsg, &git.CommitOptions{
Author: &object.Signature{
Name: username,
Email: fmt.Sprintf("%[email protected]", username),
When: time.Now(),
},
}); err != nil {
return fmt.Errorf("failed to commit changes: %v", err)
}

if err := r.Push(&git.PushOptions{
Auth: &http.BasicAuth{Username: username, Password: password},
}); err != nil {
return fmt.Errorf("failed to push changes: %v", err)
}

return nil
}

func (c *GitLabChecker) Check(ctx *context.Context, extConfig external.Check) pkg.Results {
check := extConfig.(v1.GitLabCheck)
result := pkg.Success(check, ctx.Canary)
var results pkg.Results
results = append(results, result)

// Fetching GitLab Token
password, err := ctx.GetEnvValueFromCache(check.Password)
if err != nil {
return results.Failf("error fetching gitlab token from env cache: %v", err)
}

// Push Changes
if err := pushChanges(check.Repository, check.Username, password); err != nil {
return results.Failf("error pushing changes: %v", err)
}

details := map[string]string{
"msg": "GitLab pull/push command succeeded.",
}
result.AddDetails(details)

return results
}
119 changes: 119 additions & 0 deletions config/deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2908,6 +2908,125 @@ spec:
- query
type: object
type: array
gitlab:
items:
properties:
connection:
type: string
description:
description: Description for the check
type: string
display:
properties:
expr:
type: string
javascript:
type: string
jsonPath:
type: string
template:
type: string
type: object
icon:
description: Icon for overwriting default icon on the dashboard
type: string
labels:
additionalProperties:
type: string
description: Labels for the check
type: object
metrics:
description: Metrics to expose from check results
items:
properties:
labels:
items:
properties:
name:
type: string
value:
type: string
valueExpr:
type: string
required:
- name
type: object
type: array
name:
type: string
type:
type: string
value:
type: string
type: object
type: array
name:
description: Name of the check
type: string
password:
properties:
name:
type: string
value:
type: string
valueFrom:
properties:
configMapKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
secretKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
type: object
type: object
repository:
type: string
test:
properties:
expr:
type: string
javascript:
type: string
jsonPath:
type: string
template:
type: string
type: object
transform:
properties:
expr:
type: string
javascript:
type: string
jsonPath:
type: string
template:
type: string
type: object
transformDeleteStrategy:
description: Transformed checks have a delete strategy on deletion they can either be marked healthy, unhealthy or left as is
type: string
username:
type: string
required:
- name
- repository
- username
- password
type: object
type: array
helm:
items:
properties:
Expand Down
Loading

0 comments on commit 5a12453

Please sign in to comment.