Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into app-watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MeNsaaH committed Jan 10, 2024
2 parents c9e8a9b + 197a3c0 commit 2a18303
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 12 deletions.
4 changes: 2 additions & 2 deletions charts/kubechecks/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: v2
name: kubechecks
description: A Helm chart for kubechecks
version: 0.4.0
appVersion: "1.1.1"
version: 0.4.1
appVersion: "1.3.3"
type: application
maintainers:
- name: zapier
2 changes: 1 addition & 1 deletion charts/kubechecks/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Common labels
helm.sh/chart: {{ include "kubechecks.chart" . }}
{{ include "kubechecks.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
app.kubernetes.io/version: {{ coalesce .Values.deployment.image.tag .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- if .Values.commonLabels }}
Expand Down
24 changes: 24 additions & 0 deletions charts/kubechecks/tests/deployment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,27 @@ tests:
name: new-configmap
- secretMap:
name: new-secret
- it: should render tag as annotation
chart:
appVersion: 1.0.0
version: 0.1.0
release:
name: kubechecks
set:
deployment:
image:
tag: 2.0.0
asserts:
- isKind:
of: Deployment
- equal:
path: spec.template.spec.containers[0].image
value: ghcr.io/zapier/kubechecks:2.0.0
- equal:
path: metadata.labels
value:
app.kubernetes.io/instance: kubechecks
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: kubechecks
app.kubernetes.io/version: 2.0.0
helm.sh/chart: kubechecks-0.1.0
9 changes: 7 additions & 2 deletions pkg/config/walk_kustomize_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -48,6 +49,11 @@ func walkKustomizeFiles(result *AppDirectory, fs fs.FS, appName, dirpath string)
}

for _, resource := range kustomize.Resources {
if strings.Contains(resource, "://") {
// no reason to walk remote files, since they can't be changed
continue
}

var relPath string
if len(resource) >= 1 && resource[0] == '/' {
relPath = resource[1:]
Expand All @@ -57,8 +63,7 @@ func walkKustomizeFiles(result *AppDirectory, fs fs.FS, appName, dirpath string)

file, err := fs.Open(relPath)
if err != nil {
log.Warn().Err(err).Msgf("failed to read %s", relPath)
continue
return errors.Wrapf(err, "failed to read %s", relPath)
}
stat, err := file.Stat()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/walk_kustomize_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ resources:
- ../overlays/base
- ./overlays/dev
- /common/overlays/prod
- https://google.com/some/url
`)},

"test/app2/kustomization.yaml": {
Expand Down
2 changes: 2 additions & 0 deletions pkg/local/gitRepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (rd *ReposDirectory) Register(ctx context.Context, cloneUrl string) string

func (rd *ReposDirectory) fetchLatest() {
cmd := exec.Command("git", "pull")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
err := cmd.Run()
if err != nil {
log.Err(err).Msg("failed to pull latest")
Expand Down
2 changes: 1 addition & 1 deletion pkg/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func buildComment(ctx context.Context, apps map[string]*AppResults) string {
sb.WriteString("<summary>\n\n")
sb.WriteString(fmt.Sprintf("## ArgoCD Application Checks: `%s` %s\n", appName, appState.Emoji()))
sb.WriteString("</summary>\n\n")
sb.WriteString(strings.Join(checkStrings, "---\n"))
sb.WriteString(strings.Join(checkStrings, "\n\n---\n\n"))
sb.WriteString("</details>")
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -92,3 +93,61 @@ func TestMessageIsSuccess(t *testing.T) {
})
}
}

func TestMultipleItemsWithNewlines(t *testing.T) {
var (
message = NewMessage("name", 1, 2)
ctx = context.Background()
)
message.AddNewApp(ctx, "first-app")
message.AddToAppMessage(ctx, "first-app", CheckResult{
State: StateSuccess,
Summary: "summary-1",
Details: "detail-1",
})
message.AddToAppMessage(ctx, "first-app", CheckResult{
State: StateSuccess,
Summary: "summary-2",
Details: "detail-2",
})
message.AddNewApp(ctx, "second-app")
message.AddToAppMessage(ctx, "second-app", CheckResult{
State: StateSuccess,
Summary: "summary-1",
Details: "detail-1",
})
message.AddToAppMessage(ctx, "second-app", CheckResult{
State: StateSuccess,
Summary: "summary-2",
Details: "detail-2",
})
result := message.BuildComment(ctx)

// header rows need double newlines before and after
index := 0
newline := uint8('\n')
for {
index++
foundAt := strings.Index(result[index:], "---")
if foundAt == -1 {
break // couldn't be found, we're done
}
index += foundAt

if index < 2 {
continue // hyphens are at the beginning of the string, we're fine
}

if result[index-1] == '-' || result[index+3] == '-' {
continue // not a triple-hyphen, but a more-than-triple-hyphen, move on
}

// must be preceded by two newlines
assert.Equal(t, newline, result[index-1])
assert.Equal(t, newline, result[index-2])

// must be followed by two newlines
assert.Equal(t, newline, result[index+3])
assert.Equal(t, newline, result[index+4])
}
}
14 changes: 13 additions & 1 deletion pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,20 @@ func (r *Repo) execCommand(name string, args ...string) *exec.Cmd {
return cmd
}

func censorVcsToken(v *viper.Viper, args []string) []string {
vcsToken := v.GetString("vcs-token")

var argsToLog []string
for _, arg := range args {
argsToLog = append(argsToLog, strings.Replace(arg, vcsToken, "********", 10))
}
return argsToLog
}

func execCommand(name string, args ...string) *exec.Cmd {
log.Debug().Strs("args", args).Msg("building command")
argsToLog := censorVcsToken(viper.GetViper(), args)

log.Debug().Strs("args", argsToLog).Msg("building command")
cmd := exec.Command(name, args...)
return cmd
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ func TestGetCloneUrl(t *testing.T) {
})
}
}

func TestCensorVcsToken(t *testing.T) {
v := viper.New()
v.Set("vcs-token", "hre")
result := censorVcsToken(v, []string{"one", "two", "three"})
assert.Equal(t, []string{"one", "two", "t********e"}, result)
}
5 changes: 5 additions & 0 deletions pkg/vcs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/zapier/kubechecks/pkg/repo"
)

const (
DefaultVcsUsername = "kubechecks"
DefaultVcsEmail = "[email protected]"
)

var (
// ErrInvalidType is a sentinel error for use in client implementations
ErrInvalidType = errors.New("invalid event type")
Expand Down
22 changes: 18 additions & 4 deletions pkg/vcs/github_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,26 @@ func CreateGithubClient() (*Client, error) {
return nil, errors.Wrap(err, "failed to get user")
}

return &Client{
client := &Client{
Client: googleClient,
v4Client: shurcoolClient,
username: *user.Login,
email: *user.Email,
}, nil
}
if user != nil {
if user.Login != nil {
client.username = *user.Login
}
if user.Email != nil {
client.email = *user.Email
}
}

if client.username == "" {
client.username = vcs.DefaultVcsUsername
}
if client.email == "" {
client.email = vcs.DefaultVcsEmail
}
return client, nil
}

func (c *Client) Username() string { return c.username }
Expand Down
9 changes: 8 additions & 1 deletion pkg/vcs/gitlab_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ func CreateGitlabClient() (*Client, error) {
return nil, errors.Wrap(err, "failed to get current user")
}

return &Client{Client: c, username: user.Username, email: user.Email}, nil
client := &Client{Client: c, username: user.Username, email: user.Email}
if client.username == "" {
client.username = vcs.DefaultVcsUsername
}
if client.email == "" {
client.email = vcs.DefaultVcsEmail
}
return client, nil
}

func (c *Client) Email() string { return c.email }
Expand Down

0 comments on commit 2a18303

Please sign in to comment.