From 9a5ea6e57fd6a038dbd6cec1a6a64319904fa1d3 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Wed, 31 Jan 2024 17:40:48 -0800 Subject: [PATCH 1/8] Better support for dev takeover * Disable kaniko cache by default (#74) because its causing problems. --- api/v1alpha1/repo.go | 25 ++++++++++++ cmd/commands/takeover.go | 17 ++------ pkg/gcp/gcb.go | 2 +- pkg/github/uris.go | 22 +++++++++++ pkg/github/uris_test.go | 45 +++++++++++++++++++++ pkg/gitops/repocontroller.go | 18 +++++++++ pkg/gitops/takeover.go | 76 ++++++++++++++++++++++++++++++++++++ pkg/gitops/takeover_test.go | 56 ++++++++++++++++++++++++++ 8 files changed, 246 insertions(+), 15 deletions(-) create mode 100644 pkg/github/uris.go create mode 100644 pkg/github/uris_test.go create mode 100644 pkg/gitops/takeover.go create mode 100644 pkg/gitops/takeover_test.go diff --git a/api/v1alpha1/repo.go b/api/v1alpha1/repo.go index bd65468..4d2878e 100644 --- a/api/v1alpha1/repo.go +++ b/api/v1alpha1/repo.go @@ -2,6 +2,7 @@ package v1alpha1 import ( "strings" + "time" ) var ( @@ -35,6 +36,23 @@ type RepoSpec struct { // Selectors is one or more labelselectors used to filter resources // to sync. A resource must match one of the label selectors in order to be included Selectors []LabelSelector `yaml:"selectors,omitempty"` + + // Pause causes the controller to pause regular ManifestSync hydration for the specified amount of type + // This causes the manifests to be hydrated in a takeover configuration + Pause string `yaml:"pause,omitempty"` + + // RepoMappings is a list of one or more mappings from one repository to another repository(or branch). + // This is used to rewrite the sourceRepositories in ManifestSync resources in order to hydrate from a + // branch. + RepoMappings []RepoMapping `yaml:"repoMappings,omitempty"` +} + +// RepoMapping is a mapping from a repository to a directory +type RepoMapping struct { + // Input is the input URI of the repository to use. + Input string `yaml:"input"` + // Output is the output repostiroy to use. + Output string `yaml:"output"` } // IsValid returns true if the config is valid. @@ -67,6 +85,13 @@ func (c *RepoConfig) IsValid() (string, bool) { errors = append(errors, "At least one selector must be specified.") } + if c.Spec.Pause != "" { + _, err := time.ParseDuration(c.Spec.Pause) + if err != nil { + errors = append(errors, "Pause is not a valid duration") + } + } + if len(errors) > 0 { return "RepoConfig is invalid. " + strings.Join(errors, ". "), false } diff --git a/cmd/commands/takeover.go b/cmd/commands/takeover.go index b7169fe..28fd4fb 100644 --- a/cmd/commands/takeover.go +++ b/cmd/commands/takeover.go @@ -6,8 +6,6 @@ import ( "path/filepath" "time" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/go-logr/zapr" "github.com/jlewi/hydros/api/v1alpha1" "github.com/jlewi/hydros/pkg/files" @@ -97,20 +95,11 @@ func TakeOver(args *TakeOverArgs) error { return errors.Wrapf(err, "Failed to decode ManifestSync from file %v", manifestPath) } - tEnd := time.Now().Add(args.Pause) - - k8sTime := metav1.NewTime(tEnd) - v, err := k8sTime.MarshalJSON() - if err != nil { - return errors.Wrapf(err, "Failed to marshal time %v", tEnd) - } - m.Metadata.Annotations = map[string]string{ - // We need to mark it as a takeover otherwise we won't override pauses. - v1alpha1.TakeoverAnnotation: "true", - v1alpha1.PauseAnnotation: string(v), + if err := gitops.SetTakeOverAnnotations(m, args.Pause); err != nil { + return errors.Wrapf(err, "Failed to set takeover annotations") } - log.Info("Pausing automatic syncs", "pauseUntil", string(v)) + log.Info("Pausing automatic syncs") syncer, err := gitops.NewSyncer(m, manager, gitops.SyncWithWorkDir(args.WorkDir), gitops.SyncWithLogger(log)) if err != nil { return err diff --git a/pkg/gcp/gcb.go b/pkg/gcp/gcb.go index b0fb074..2a7d97c 100644 --- a/pkg/gcp/gcb.go +++ b/pkg/gcp/gcb.go @@ -55,8 +55,8 @@ func DefaultBuild() *cbpb.Build { Steps: []*cbpb.BuildStep{ { Name: kanikoBuilder, + // N.B. We don't enable caching by default because that can cause problems. Args: []string{ - "--cache=true", // Set the date as a build arg // This is so that it can be passed to the builder and used to set the date in the image // of the build diff --git a/pkg/github/uris.go b/pkg/github/uris.go new file mode 100644 index 0000000..0c28713 --- /dev/null +++ b/pkg/github/uris.go @@ -0,0 +1,22 @@ +package github + +import ( + "github.com/jlewi/hydros/api/v1alpha1" + "net/url" +) + +// GitHubRepoToURI converts a GitHubRepo to a URI in the gogetter form. +// It assumes the protocol is https. +func GitHubRepoToURI(repo v1alpha1.GitHubRepo) url.URL { + u := url.URL{ + Scheme: "https", + Host: "github.com", + Path: "/" + repo.Org + "/" + repo.Repo + ".git", + } + + if repo.Branch != "" { + u.RawQuery = "ref=" + repo.Branch + } + + return u +} diff --git a/pkg/github/uris_test.go b/pkg/github/uris_test.go new file mode 100644 index 0000000..daa3e00 --- /dev/null +++ b/pkg/github/uris_test.go @@ -0,0 +1,45 @@ +package github + +import ( + "github.com/google/go-cmp/cmp" + "github.com/jlewi/hydros/api/v1alpha1" + "net/url" + "testing" +) + +func Test_GitHubRepoToURL(t *testing.T) { + type testCase struct { + name string + repo v1alpha1.GitHubRepo + + // Compare the actual URL to one parsed from a string + expected string + } + + testCases := []testCase{ + { + name: "main", + repo: v1alpha1.GitHubRepo{ + Org: "jlewi", + Repo: "hydros", + Branch: "jlewi/cicd", + }, + expected: "https://github.com/jlewi/hydros.git?ref=jlewi/cicd", + }, + } + + for _, c := range testCases { + t.Run(c.name, func(t *testing.T) { + actual := GitHubRepoToURI(c.repo) + + expectedU, err := url.Parse(c.expected) + if err != nil { + t.Fatalf("Error parsing expected URL %v", err) + } + + if d := cmp.Diff(expectedU, &actual); d != "" { + t.Errorf("Unexpected diff:\n%v", d) + } + }) + } +} diff --git a/pkg/gitops/repocontroller.go b/pkg/gitops/repocontroller.go index 6150fc7..b1fbdcc 100644 --- a/pkg/gitops/repocontroller.go +++ b/pkg/gitops/repocontroller.go @@ -292,6 +292,24 @@ func (c *RepoController) applyManifest(ctx context.Context, r *resource) error { return errors.Wrapf(err, "Error decoding manifest") } + // Rewrite the source repo if necessary + if err := rewriteRepos(ctx, manifest, c.config.Spec.RepoMappings); err != nil { + return err + } + + pause := c.config.Spec.Pause + if pause != "" { + pauseDur, err := time.ParseDuration(pause) + if err != nil { + return errors.Wrapf(err, "Error parsing pause duration %v", pause) + } + + if err := SetTakeOverAnnotations(manifest, pauseDur); err != nil { + return errors.Wrapf(err, "Failed to set takeover annotations") + } + log.Info("Pausing automatic syncs; doing a takeover") + } + // Create a workDir for this syncer // Each ManifestSync should get its own workDir // This should be stable names so that they get reused on each sync diff --git a/pkg/gitops/takeover.go b/pkg/gitops/takeover.go new file mode 100644 index 0000000..48822d0 --- /dev/null +++ b/pkg/gitops/takeover.go @@ -0,0 +1,76 @@ +package gitops + +import ( + "context" + "github.com/jlewi/hydros/api/v1alpha1" + gh "github.com/jlewi/hydros/pkg/github" + "github.com/jlewi/hydros/pkg/github/ghrepo" + "github.com/jlewi/hydros/pkg/util" + "github.com/pkg/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "net/url" + "time" +) + +// rewriteRepos rewrites the repos in the manifest to the new repos if necessary. +func rewriteRepos(ctx context.Context, m *v1alpha1.ManifestSync, mappings []v1alpha1.RepoMapping) error { + log := util.LogFromContext(ctx) + + if m == nil { + return errors.New("Manifest is nil") + } + + if mappings == nil { + return nil + } + + srcRepoURL := gh.GitHubRepoToURI(m.Spec.SourceRepo) + + for _, mapping := range mappings { + if srcRepoURL.String() != mapping.Input { + continue + } + + log.Info("Rewriting source repo", "old", srcRepoURL.String(), "new", mapping.Output, "manifestSync.Name", m.Metadata.Name) + + u, err := url.Parse(mapping.Output) + if err != nil { + return errors.Wrapf(err, "Could not parse URL %v", mapping.Output) + } + + r, err := ghrepo.FromURL(u) + if err != nil { + return errors.Wrapf(err, "Could not parse URL %v", srcRepoURL.String()) + } + + // ref parameter specifies the reference to checkout + // https://github.com/hashicorp/go-getter#protocol-specific-options + branch := u.Query().Get("ref") + if branch == "" { + return errors.Wrapf(err, "Branch is not specified in URL %v; it should be specified as a query argument e.g. ?ref=main", mapping.Output) + } + m.Spec.SourceRepo.Org = r.RepoOwner() + m.Spec.SourceRepo.Repo = r.RepoName() + m.Spec.SourceRepo.Branch = branch + return nil + } + return nil +} + +// SetTakeOverAnnotations sets the takeover annotations on the manifest. +func SetTakeOverAnnotations(m *v1alpha1.ManifestSync, pause time.Duration) error { + tEnd := time.Now().Add(pause) + + k8sTime := metav1.NewTime(tEnd) + v, err := k8sTime.MarshalJSON() + if err != nil { + return errors.Wrapf(err, "Failed to marshal time %v", tEnd) + } + m.Metadata.Annotations = map[string]string{ + // We need to mark it as a takeover otherwise we won't override pauses. + v1alpha1.TakeoverAnnotation: "true", + v1alpha1.PauseAnnotation: string(v), + } + + return nil +} diff --git a/pkg/gitops/takeover_test.go b/pkg/gitops/takeover_test.go new file mode 100644 index 0000000..0c5be58 --- /dev/null +++ b/pkg/gitops/takeover_test.go @@ -0,0 +1,56 @@ +package gitops + +import ( + "context" + "github.com/google/go-cmp/cmp" + "github.com/jlewi/hydros/api/v1alpha1" + "testing" +) + +func Test_rewriteRepos(t *testing.T) { + type testCase struct { + name string + manifest *v1alpha1.ManifestSync + expected v1alpha1.GitHubRepo + mappings []v1alpha1.RepoMapping + } + + cases := []testCase{ + { + name: "basic", + manifest: &v1alpha1.ManifestSync{ + Spec: v1alpha1.ManifestSyncSpec{ + SourceRepo: v1alpha1.GitHubRepo{ + Org: "jlewi", + Repo: "hydros", + Branch: "main", + }, + }, + }, + mappings: []v1alpha1.RepoMapping{ + { + Input: "https://github.com/jlewi/hydros.git?ref=main", + Output: "https://github.com/jlewi/hydros.git?ref=jlewi/cicd", + }, + }, + expected: v1alpha1.GitHubRepo{ + Org: "jlewi", + Repo: "hydros", + Branch: "jlewi/cicd", + }, + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + err := rewriteRepos(context.Background(), c.manifest, c.mappings) + if err != nil { + t.Fatalf("Error rewriting repos: %v", err) + } + + if d := cmp.Diff(c.expected, c.manifest.Spec.SourceRepo); d != "" { + t.Errorf("Unexpected diff:\n%v", d) + } + }) + } +} From c91cfa6a6a52c111187b9a380e00ed6edb7eecd3 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Wed, 31 Jan 2024 17:56:35 -0800 Subject: [PATCH 2/8] hydros committing changes before build --- .goreleaser.yaml | 4 ++-- Dockerfile | 15 +++++++++++++-- cmd/commands/apply.go | 2 +- cmd/commands/version.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/main.go | 1 + 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 cmd/commands/version.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index e2e8027..9fce551 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -17,8 +17,8 @@ builds: # Custom ldflags templates. # Default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser`. - #ldflags: - #- "-s -w -X github.com/jlewi/roboweb/kubedr/cmd/commands.version={{.Version}} -X github.com/jlewi/roboweb/kubedr/cmd/commands.commit={{.Commit}} -X github.com/jlewi/roboweb/kubedr/cmd/commands.date={{.Date}} -X github.com/jlewi/roboweb/kubedr/cmd/commands.builtBy=goreleaser" + ldflags: + - "-s -w -X github.com/jlewi/hydros/cmd/commands.version={{.Version}} -X github.com/jlewi/hydro/cmd/commands.commit={{.Commit}} -X github.com/jlewi/hydros/cmd/commands.date={{.Date}} -X github.com/jlewi/hydros/cmd/commands.builtBy=goreleaser" archives: # https://goreleaser.com/customization/archive/?h=archives - id: "binary" diff --git a/Dockerfile b/Dockerfile index 9c75dc4..3dc85dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,11 +2,15 @@ ARG BUILD_IMAGE=golang:1.19 ARG RUNTIME_IMAGE=cgr.dev/chainguard/static:latest FROM ${BUILD_IMAGE} as builder +# Build Args need to be after the FROM stage otherwise they don't get passed through to the RUN statment +ARG VERSION=unknown +ARG DATE=unknown +ARG COMMIT=unknown + WORKDIR /workspace/ COPY . /workspace - ## Build # N.B Disabling CGO can potentially cause problems on MacOSX and darwin builds because some networking requires # it https://github.com/golang/go/issues/16345. We use to build with CGO_ENABLED=- to disable it at Primer @@ -15,7 +19,14 @@ COPY . /workspace # environment. # # TODO(jeremy): We should be setting version information here -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o hydros cmd/main.go +# The LDFLAG can't be specified multiple times so we use an environment variable to build it up over multiple lines +RUN LDFLAGS="-s -w -X github.com/jlewi/hydros/cmd/commands.version=${VERSION}" && \ + LDFLAGS="${LDFLAGS} -X github.com/jlewi/hydros/cmd/commands.commit=${COMMIT}" && \ + LDFLAGS="${LDFLAGS} -X github.com/jlewi/hydros/cmd/commands.date=${DATE}" && \ + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on \ + go build \ + -ldflags "${LDFLAGS}" \ + -a -o hydros cmd/main.go # TODO(jeremy): This won't be able to run Syncer until we update syncer to use GoGit and get rid of shelling # out to other tools. diff --git a/cmd/commands/apply.go b/cmd/commands/apply.go index bc18b6b..cccd18f 100644 --- a/cmd/commands/apply.go +++ b/cmd/commands/apply.go @@ -42,7 +42,7 @@ func NewApplyCmd() *cobra.Command { log.Info("apply takes at least one argument which should be the file or directory YAML to apply.") return } - + logVersion() paths := []string{} for _, resourcePath := range args { diff --git a/cmd/commands/version.go b/cmd/commands/version.go new file mode 100644 index 0000000..8525bb3 --- /dev/null +++ b/cmd/commands/version.go @@ -0,0 +1,36 @@ +package commands + +import ( + "fmt" + "github.com/go-logr/zapr" + "go.uber.org/zap" + "io" + + "github.com/spf13/cobra" +) + +// N.B these will get set by goreleaser +// https://goreleaser.com/cookbooks/using-main.version/?h=using+main.version +var ( + version = "dev" + commit = "none" + date = "unknown" + builtBy = "unknown" +) + +func NewVersionCmd(name string, w io.Writer) *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "Return version", + Example: fmt.Sprintf("%s version", name), + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprintf(w, "%s %s, commit %s, built at %s by %s\n", name, version, commit, date, builtBy) + }, + } + return cmd +} + +func logVersion() { + log := zapr.NewLogger(zap.L()) + log.Info("binary version", "version", version, "commit", commit, "date", date, "builtBy", builtBy) +} diff --git a/cmd/main.go b/cmd/main.go index 6f5efad..e8a5487 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -122,6 +122,7 @@ func init() { rootCmd.AddCommand(commands.NewTakeOverCmd()) rootCmd.AddCommand(commands.NewHydrosServerCmd()) rootCmd.AddCommand(commands.NewCloneCmd()) + rootCmd.AddCommand(commands.NewVersionCmd()) rootCmd.PersistentFlags().BoolVar(&gOptions.devLogger, "dev-logger", false, "If true configure the logger for development; i.e. non-json output") rootCmd.PersistentFlags().StringVarP(&gOptions.level, "log-level", "", "info", "Log level: error info or debug") From 088f5d7b426a438be54a86e98fc478fea53ccf52 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Wed, 31 Jan 2024 18:28:44 -0800 Subject: [PATCH 3/8] Fix #69 - when matching globs against paths in a tarball we need to strip any leading "/" in the path. --- pkg/tarutil/builder.go | 12 ++++++-- pkg/tarutil/builder_test.go | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/pkg/tarutil/builder.go b/pkg/tarutil/builder.go index 5fa9d54..bc24a16 100644 --- a/pkg/tarutil/builder.go +++ b/pkg/tarutil/builder.go @@ -163,9 +163,9 @@ func copyTarBall(tw *tar.Writer, s *v1alpha1.ImageSource) error { // Check if any of the patterns match var source *v1alpha1.SourceMapping for _, s := range s.Mappings { - isMatch, err := doublestar.Match(s.Src, header.Name) + isMatch, err := matchGlobToHeader(s.Src, header.Name) if err != nil { - return errors.Wrapf(err, "Error matching glob %v against %v", s.Src, header.Name) + return err } if isMatch { @@ -216,6 +216,14 @@ func copyTarBall(tw *tar.Writer, s *v1alpha1.ImageSource) error { } } +func matchGlobToHeader(glob string, headerName string) (bool, error) { + // We need to strip the leading / if any from the glob. + // https://github.com/jlewi/hydros/issues/69 + // the paths in the tarball don't have them + glob = strings.TrimPrefix(glob, "/") + return doublestar.Match(glob, headerName) +} + // splitIntoParent splits a path into a parent and glob // e.g. ../foo/bar/*.txt -> ../foo/bar, *.txt // doublestar.Glob has a comment about a function SplitPattern that we could potentially use diff --git a/pkg/tarutil/builder_test.go b/pkg/tarutil/builder_test.go index a96435b..92c685f 100644 --- a/pkg/tarutil/builder_test.go +++ b/pkg/tarutil/builder_test.go @@ -62,6 +62,24 @@ func Test_Build(t *testing.T) { "dirB/file2.txt", }, }, + { + // test that a leading slash in the src is handled correctly + name: "test-leading-slash", + source: []*v1alpha1.ImageSource{ + { + URI: "file://" + filepath.Join(cwd, "test_data", "dirA"), + Mappings: []*v1alpha1.SourceMapping{ + { + Src: "/*.txt", + }, + }, + }, + }, + + expected: []string{ + "file1.txt", + }, + }, } for _, c := range cases { @@ -239,3 +257,45 @@ func Test_splitParent(t *testing.T) { }) } } + +func Test_matchGlobToHeader(t *testing.T) { + type testCase struct { + name string + glob string + header string + isMatch bool + } + + cases := []testCase{ + { + name: "basic", + glob: "pkg/**/*.go", + header: "pkg/app.go", + isMatch: true, + }, + { + name: "glob-has-leading-slash", + glob: "/pkg/**/*.go", + header: "pkg/app.go", + isMatch: true, + }, + { + name: "not-a-match", + glob: "/pkg/**/*.go", + header: "app.go", + isMatch: false, + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + actual, err := matchGlobToHeader(c.glob, c.header) + if err != nil { + t.Fatalf("Error matching glob %v", err) + } + if actual != c.isMatch { + t.Errorf("Expected result %v; got %v", c.isMatch, actual) + } + }) + } +} From a67decfe02c2b8dfc2f4fb443458364141072e11 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Wed, 31 Jan 2024 18:31:12 -0800 Subject: [PATCH 4/8] Fix version command. --- cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index e8a5487..25827f8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -122,7 +122,7 @@ func init() { rootCmd.AddCommand(commands.NewTakeOverCmd()) rootCmd.AddCommand(commands.NewHydrosServerCmd()) rootCmd.AddCommand(commands.NewCloneCmd()) - rootCmd.AddCommand(commands.NewVersionCmd()) + rootCmd.AddCommand(commands.NewVersionCmd("hydros", os.Stdout)) rootCmd.PersistentFlags().BoolVar(&gOptions.devLogger, "dev-logger", false, "If true configure the logger for development; i.e. non-json output") rootCmd.PersistentFlags().StringVarP(&gOptions.level, "log-level", "", "info", "Log level: error info or debug") From 334eb68cd666bb64c82034dbba3164ea229e6975 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Wed, 31 Jan 2024 18:33:13 -0800 Subject: [PATCH 5/8] Revert disabling kaniko caching. --- pkg/gcp/gcb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/gcp/gcb.go b/pkg/gcp/gcb.go index 2a7d97c..b0fb074 100644 --- a/pkg/gcp/gcb.go +++ b/pkg/gcp/gcb.go @@ -55,8 +55,8 @@ func DefaultBuild() *cbpb.Build { Steps: []*cbpb.BuildStep{ { Name: kanikoBuilder, - // N.B. We don't enable caching by default because that can cause problems. Args: []string{ + "--cache=true", // Set the date as a build arg // This is so that it can be passed to the builder and used to set the date in the image // of the build From 52e97d5e302e9fb7637903cb3e6b00d72c51cf50 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Wed, 31 Jan 2024 19:02:30 -0800 Subject: [PATCH 6/8] update comment --- api/v1alpha1/repo.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/v1alpha1/repo.go b/api/v1alpha1/repo.go index 4d2878e..7b2aaf2 100644 --- a/api/v1alpha1/repo.go +++ b/api/v1alpha1/repo.go @@ -14,6 +14,7 @@ var ( ) // RepoConfig specifies a repository that should be checked out and periodically sync'd. +// TODO(jeremy): RepoConfig is a terrible name. type RepoConfig struct { APIVersion string `yaml:"apiVersion"` Kind string `yaml:"kind"` From 2dc3ebc824c6d2681bb6f5666e25b2defcf81811 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Thu, 1 Feb 2024 10:53:47 -0800 Subject: [PATCH 7/8] tidy. --- cmd/commands/version.go | 3 ++- pkg/github/uris.go | 3 ++- pkg/github/uris_test.go | 5 +++-- pkg/gitops/takeover.go | 5 +++-- pkg/gitops/takeover_test.go | 3 ++- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cmd/commands/version.go b/cmd/commands/version.go index 8525bb3..fcd4840 100644 --- a/cmd/commands/version.go +++ b/cmd/commands/version.go @@ -2,9 +2,10 @@ package commands import ( "fmt" + "io" + "github.com/go-logr/zapr" "go.uber.org/zap" - "io" "github.com/spf13/cobra" ) diff --git a/pkg/github/uris.go b/pkg/github/uris.go index 0c28713..74e78f4 100644 --- a/pkg/github/uris.go +++ b/pkg/github/uris.go @@ -1,8 +1,9 @@ package github import ( - "github.com/jlewi/hydros/api/v1alpha1" "net/url" + + "github.com/jlewi/hydros/api/v1alpha1" ) // GitHubRepoToURI converts a GitHubRepo to a URI in the gogetter form. diff --git a/pkg/github/uris_test.go b/pkg/github/uris_test.go index daa3e00..60ba97e 100644 --- a/pkg/github/uris_test.go +++ b/pkg/github/uris_test.go @@ -1,10 +1,11 @@ package github import ( - "github.com/google/go-cmp/cmp" - "github.com/jlewi/hydros/api/v1alpha1" "net/url" "testing" + + "github.com/google/go-cmp/cmp" + "github.com/jlewi/hydros/api/v1alpha1" ) func Test_GitHubRepoToURL(t *testing.T) { diff --git a/pkg/gitops/takeover.go b/pkg/gitops/takeover.go index 48822d0..0a8ceaf 100644 --- a/pkg/gitops/takeover.go +++ b/pkg/gitops/takeover.go @@ -2,14 +2,15 @@ package gitops import ( "context" + "net/url" + "time" + "github.com/jlewi/hydros/api/v1alpha1" gh "github.com/jlewi/hydros/pkg/github" "github.com/jlewi/hydros/pkg/github/ghrepo" "github.com/jlewi/hydros/pkg/util" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "net/url" - "time" ) // rewriteRepos rewrites the repos in the manifest to the new repos if necessary. diff --git a/pkg/gitops/takeover_test.go b/pkg/gitops/takeover_test.go index 0c5be58..e8207fc 100644 --- a/pkg/gitops/takeover_test.go +++ b/pkg/gitops/takeover_test.go @@ -2,9 +2,10 @@ package gitops import ( "context" + "testing" + "github.com/google/go-cmp/cmp" "github.com/jlewi/hydros/api/v1alpha1" - "testing" ) func Test_rewriteRepos(t *testing.T) { From 701890a3d0149a6eab5db3792340046b47365481 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Thu, 1 Feb 2024 11:03:28 -0800 Subject: [PATCH 8/8] Fix test. --- pkg/tarutil/builder_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/tarutil/builder_test.go b/pkg/tarutil/builder_test.go index 92c685f..3016703 100644 --- a/pkg/tarutil/builder_test.go +++ b/pkg/tarutil/builder_test.go @@ -22,7 +22,7 @@ func Test_Build(t *testing.T) { util.SetupLogger("info", true) tDir, err := os.MkdirTemp("", "") - + defer os.RemoveAll(tDir) if err != nil { t.Fatalf("Error creating temp dir %v", err) } @@ -84,7 +84,7 @@ func Test_Build(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - oFile := filepath.Join(tDir, "test.tar.gz") + oFile := filepath.Join(tDir, c.name+" _test.tar.gz") if err := Build(c.source, oFile); err != nil { t.Fatalf("Error building tarball for image %+v", err) }