Skip to content

Commit

Permalink
fix: Fixing handling fo build for k3s
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed May 11, 2024
1 parent 41fdf22 commit 3ffbd1a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
7 changes: 4 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
KETCHUP_DB_HOST=localhost
KETCHUP_DB_PORT=5432
KETCHUP_DB_USER=postgres
KETCHUP_DB_NAME=postgres
KETCHUP_DB_PASS=<secret>
KETCHUP_DB_PORT=5432
KETCHUP_DB_USER=postgres
KETCHUP_GITHUB_TOKEN=
KETCHUP_NOTIFIER_DRY_RUN=true
KETCHUP_REDIS_ADDRESS=localhost:6379
KETCHUP_REDIS_PASSWORD=
KETCHUP_GITHUB_TOKEN=
27 changes: 27 additions & 0 deletions cmd/checker/checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"context"
"flag"
"fmt"
"os"

"github.com/ViBiOh/flags"
"github.com/ViBiOh/ketchup/pkg/provider/github"
)

func main() {
fs := flag.NewFlagSet("checker", flag.ExitOnError)
fs.Usage = flags.Usage(fs)

githubConfig := github.Flags(fs, "github")

_ = fs.Parse(os.Args[1:])

ctx := context.Background()

githubApp := github.New(githubConfig, nil, nil, nil)

patterns, err := githubApp.LatestVersions(ctx, "k3s-io/k3s", []string{"stable", "latest"})
fmt.Println(patterns, err)
}
37 changes: 23 additions & 14 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,38 @@ type Service struct {
mailer model.Mailer
helm model.HelmProvider
clock GetNow
pushURL string
dryRun bool
}

type Config struct {
PushURL string
DryRun bool
}

func Flags(fs *flag.FlagSet, prefix string) *Config {
var config Config

flags.New("PushUrl", "Pushgateway URL").Prefix(prefix).DocPrefix("notifier").StringVar(fs, &config.PushURL, "", nil)
flags.New("DryRun", "Run in dry-run").Prefix(prefix).DocPrefix("notifier").BoolVar(fs, &config.DryRun, false, nil)

return &config
}

func New(config *Config, repositoryService model.RepositoryService, ketchupService model.KetchupService, userService user.Service, mailerService model.Mailer, helmService model.HelmProvider) Service {
return Service{
pushURL: config.PushURL,
clock: time.Now,
repository: repositoryService,
ketchup: ketchupService,
user: userService,
mailer: mailerService,
helm: helmService,
dryRun: config.DryRun,
}
}

func (s Service) Notify(ctx context.Context) error {
if err := s.repository.Clean(ctx); err != nil {
return fmt.Errorf("clean repository before starting: %w", err)
if !s.dryRun {
if err := s.repository.Clean(ctx); err != nil {
return fmt.Errorf("clean repository before starting: %w", err)
}
}

newReleases, err := s.getNewReleases(ctx)
Expand All @@ -65,17 +67,22 @@ func (s Service) Notify(ctx context.Context) error {
}

sort.Sort(model.ReleaseByRepositoryIDAndPattern(newReleases))
if err := s.updateRepositories(ctx, newReleases); err != nil {
return fmt.Errorf("update repositories: %w", err)

if !s.dryRun {
if err := s.updateRepositories(ctx, newReleases); err != nil {
return fmt.Errorf("update repositories: %w", err)
}
}

ketchupsToNotify, err := s.getKetchupToNotify(ctx, newReleases)
if err != nil {
return fmt.Errorf("get ketchup to notify: %w", err)
}

if err := s.sendNotification(ctx, "ketchup", ketchupsToNotify); err != nil {
return fmt.Errorf("send notification: %w", err)
if !s.dryRun {
if err := s.sendNotification(ctx, "ketchup", ketchupsToNotify); err != nil {
return fmt.Errorf("send notification: %w", err)
}
}

return nil
Expand Down Expand Up @@ -215,10 +222,12 @@ func (s Service) handleUpdateWhenNotify(ctx context.Context, ketchup model.Ketch

log := slog.With("repository", ketchup.Repository.ID).With("user", ketchup.User.ID).With("pattern", ketchup.Pattern)

log.InfoContext(ctx, "Auto-updating ketchup", "version", release.Version.Name)
if err := s.ketchup.UpdateVersion(ctx, ketchup.User.ID, ketchup.Repository.ID, ketchup.Pattern, release.Version.Name); err != nil {
log.LogAttrs(ctx, slog.LevelError, "update ketchup", slog.Any("error", err))
return release.SetUpdated(1)
if !s.dryRun {
log.InfoContext(ctx, "Auto-updating ketchup", "version", release.Version.Name)
if err := s.ketchup.UpdateVersion(ctx, ketchup.User.ID, ketchup.Repository.ID, ketchup.Pattern, release.Version.Name); err != nil {
log.LogAttrs(ctx, slog.LevelError, "update ketchup", slog.Any("error", err))
return release.SetUpdated(1)
}
}

return release.SetUpdated(2)
Expand Down
2 changes: 1 addition & 1 deletion pkg/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestFlags(t *testing.T) {
want string
}{
"simple": {
"Usage of simple:\n -pushUrl string\n \t[notifier] Pushgateway URL ${SIMPLE_PUSH_URL}\n",
"Usage of simple:\n -dryRun\n \t[notifier] Run in dry-run ${SIMPLE_DRY_RUN}\n",
},
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/semver/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ func TestCheck(t *testing.T) {
},
true,
},
"suffix build": {
safeParsePattern("stable"),
args{
version: safeParse("v1.28.4+k3s2"),
},
true,
},
"suffix rc and build": {
safeParsePattern("stable"),
args{
version: safeParse("v1.30.0-rc2+k3s1"),
},
false,
},
}

for intention, testCase := range cases {
Expand Down
18 changes: 16 additions & 2 deletions pkg/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ var (
semverMatcher = regexp.MustCompile(`(?i)^(?P<prefix>[a-zA-Z-]*)(?P<major>[0-9]+)(?:\.(?P<minor>[0-9]+))?(?:\.(?P<patch>[0-9]+))?(?P<prerelease>-[a-zA-Z0-9.]+)?(?P<build>\+[a-zA-Z0-9.]+)?$`)
semverMatchNames = semverMatcher.SubexpNames()

nonFinalVersions = []string{"alpha", "beta", "canary", "edge", "rc", "test"}
nonFinalVersions = []string{"alpha", "beta", "canary", "edge", "rc", "test", "preview"}

allowedPrefixes = []string{"v", "stable-"}
ignoredPrefixes = []string{"sealed-secrets-"} // manually ignore historic stuff

stableBuild = []*regexp.Regexp{
regexp.MustCompile(`k3s[0-9]`),
}

ErrPrefixInvalid = errors.New("invalid prefix")
)

Expand Down Expand Up @@ -183,7 +187,7 @@ func isPrefixAllowed(matches map[string]string, allowedPrefix string) bool {
}

func parseNonFinalVersion(matches map[string]string) NonFinalVersion {
if len(matches["build"]) > 0 {
if build := matches["build"]; len(build) > 0 && !isKnownBuild(build) {
return 0
}

Expand All @@ -200,6 +204,16 @@ func parseNonFinalVersion(matches map[string]string) NonFinalVersion {
return -1
}

func isKnownBuild(build string) bool {
for _, knownBuild := range stableBuild {
if knownBuild.MatchString(build) {
return true
}
}

return false
}

func safeParse(version string) Version {
output, err := Parse(version, "")
if err != nil {
Expand Down

0 comments on commit 3ffbd1a

Please sign in to comment.