Skip to content

Commit

Permalink
Add include property to GitRepositories
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@gmail.com>
Signed-off-by: Philip Laine <philip.laine@xenit.se>
  • Loading branch information
Philip Laine committed May 10, 2021
1 parent 347f577 commit 82e5a9a
Show file tree
Hide file tree
Showing 13 changed files with 747 additions and 275 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ ifeq (, $(shell which gen-crd-api-reference-docs))
API_REF_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$API_REF_GEN_TMP_DIR ;\
go mod init tmp ;\
go get github.com/ahmetb/gen-crd-api-reference-docs@v0.2.0 ;\
go get github.com/ahmetb/gen-crd-api-reference-docs@v0.3.0 ;\
rm -rf $$API_REF_GEN_TMP_DIR ;\
}
API_REF_GEN=$(GOBIN)/gen-crd-api-reference-docs
Expand Down
35 changes: 34 additions & 1 deletion api/v1beta1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ type GitRepositorySpec struct {
// This option is available only when using the 'go-git' GitImplementation.
// +optional
RecurseSubmodules bool `json:"recurseSubmodules,omitempty"`

// Extra git repositories to map into the repository
Include []GitRepositoryInclude `json:"include,omitempty"`
}

func (in *GitRepositoryInclude) GetFromPath() string {
return in.FromPath
}

func (in *GitRepositoryInclude) GetToPath() string {
if in.ToPath == "" {
return in.GitRepositoryRef.Name
}
return in.ToPath
}

// GitRepositoryInclude defines a source with a from and to path.
type GitRepositoryInclude struct {
// Reference to a GitRepository to include.
GitRepositoryRef meta.LocalObjectReference `json:"repository"`

// The path to copy contents from, defaults to the root directory.
// +optional
FromPath string `json:"fromPath"`

// The path to copy contents to, defaults to the name of the source ref.
// +optional
ToPath string `json:"toPath"`
}

// GitRepositoryRef defines the Git ref used for pull and checkout operations.
Expand Down Expand Up @@ -138,6 +166,10 @@ type GitRepositoryStatus struct {
// +optional
Artifact *Artifact `json:"artifact,omitempty"`

// IncludedArtifacts represents the included artifacts from the last successful repository sync.
// +optional
IncludedArtifacts []*Artifact `json:"includedArtifacts,omitempty"`

meta.ReconcileRequestStatus `json:",inline"`
}

Expand Down Expand Up @@ -166,8 +198,9 @@ func GitRepositoryProgressing(repository GitRepository) GitRepository {
// GitRepositoryReady sets the given Artifact and URL on the GitRepository and
// sets the meta.ReadyCondition to 'True', with the given reason and message. It
// returns the modified GitRepository.
func GitRepositoryReady(repository GitRepository, artifact Artifact, url, reason, message string) GitRepository {
func GitRepositoryReady(repository GitRepository, artifact Artifact, includedArtifacts []*Artifact, url, reason, message string) GitRepository {
repository.Status.Artifact = &artifact
repository.Status.IncludedArtifacts = includedArtifacts
repository.Status.URL = url
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
return repository
Expand Down
32 changes: 32 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

57 changes: 57 additions & 0 deletions config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ spec:
a default will be used, consult the documentation for your version
to find out what those are.
type: string
include:
description: Extra git repositories to map into the repository
items:
description: GitRepositoryInclude defines a source with a from and
to path.
properties:
fromPath:
description: The path to copy contents from, defaults to the
root directory.
type: string
repository:
description: Reference to a GitRepository to include.
properties:
name:
description: Name of the referent
type: string
required:
- name
type: object
toPath:
description: The path to copy contents to, defaults to the name
of the source ref.
type: string
required:
- repository
type: object
type: array
interval:
description: The interval at which to check for repository updates.
type: string
Expand Down Expand Up @@ -245,6 +272,36 @@ spec:
- type
type: object
type: array
includedArtifacts:
description: IncludedArtifacts represents the included artifacts from
the last successful repository sync.
items:
description: Artifact represents the output of a source synchronisation.
properties:
checksum:
description: Checksum is the SHA1 checksum of the artifact.
type: string
lastUpdateTime:
description: LastUpdateTime is the timestamp corresponding to
the last update of this artifact.
format: date-time
type: string
path:
description: Path is the relative file path of this artifact.
type: string
revision:
description: Revision is a human readable identifier traceable
in the origin source system. It can be a Git commit SHA, Git
tag, a Helm index timestamp, a Helm chart version, etc.
type: string
url:
description: URL is the HTTP address of this artifact.
type: string
required:
- path
- url
type: object
type: array
lastHandledReconcileAt:
description: LastHandledReconcileAt holds the value of the most recent
reconcile request value, so a change can be detected.
Expand Down
23 changes: 23 additions & 0 deletions controllers/artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package controllers

import sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"

// hasArtifactUpdated returns true if any of the revisions in the current artifacts
// does not match any of the artifacts in the updated artifacts
func hasArtifactUpdated(current []*sourcev1.Artifact, updated []*sourcev1.Artifact) bool {
if len(current) != len(updated) {
return true
}

OUTER:
for _, c := range current {
for _, u := range updated {
if u.HasRevision(c.Revision) {
continue OUTER
}
}
return true
}

return false
}
110 changes: 110 additions & 0 deletions controllers/artifact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package controllers

import (
"testing"

sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
)

func TestHasUpdated(t *testing.T) {
tests := []struct {
name string
current []*sourcev1.Artifact
updated []*sourcev1.Artifact
expected bool
}{
{
name: "not updated single",
current: []*sourcev1.Artifact{
{
Revision: "foo",
},
},
updated: []*sourcev1.Artifact{
{
Revision: "foo",
},
},
expected: false,
},
{
name: "updated single",
current: []*sourcev1.Artifact{
{
Revision: "foo",
},
},
updated: []*sourcev1.Artifact{
{
Revision: "bar",
},
},
expected: true,
},
{
name: "not updated multiple",
current: []*sourcev1.Artifact{
{
Revision: "foo",
},
{
Revision: "bar",
},
},
updated: []*sourcev1.Artifact{
{
Revision: "foo",
},
{
Revision: "bar",
},
},
expected: false,
},
{
name: "updated multiple",
current: []*sourcev1.Artifact{
{
Revision: "foo",
},
{
Revision: "bar",
},
},
updated: []*sourcev1.Artifact{
{
Revision: "foo",
},
{
Revision: "baz",
},
},
expected: true,
},
{
name: "updated different artifact count",
current: []*sourcev1.Artifact{
{
Revision: "foo",
},
{
Revision: "bar",
},
},
updated: []*sourcev1.Artifact{
{
Revision: "foo",
},
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := hasArtifactUpdated(tt.current, tt.updated)
if result != tt.expected {
t.Errorf("Archive() result = %v, wantResult %v", result, tt.expected)
}
})
}
}
Loading

0 comments on commit 82e5a9a

Please sign in to comment.