Skip to content

Commit

Permalink
Merge pull request #1403 from qu1queee/qu1queee/conversion_local_source
Browse files Browse the repository at this point in the history
Ensure conversion for local source takes place
  • Loading branch information
SaschaSchwarze0 authored Oct 18, 2023
2 parents 53e9df3 + 7ce6db1 commit f8a2b63
Show file tree
Hide file tree
Showing 14 changed files with 502 additions and 61 deletions.
47 changes: 46 additions & 1 deletion deploy/crds/shipwright.io_buildruns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6388,7 +6388,8 @@ spec:
description: BuildRunSpec defines the desired state of BuildRun
properties:
build:
description: Build refers to an embedded build specification
description: Build refers to an embedded build specification This
field is mandatory
properties:
name:
description: 'Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names'
Expand Down Expand Up @@ -6703,6 +6704,17 @@ spec:
description: URL describes the URL of the Git repository.
type: string
type: object
local:
description: LocalSource
properties:
name:
description: Name of the local step
type: string
timeout:
description: Timeout how long the BuildSource execution
must take.
type: string
type: object
ociArtifact:
description: OCIArtifact
properties:
Expand Down Expand Up @@ -8786,6 +8798,26 @@ spec:
which is used for resource control. Default serviceaccount will
be set if it is empty
type: string
source:
description: Source refers to the location where the source code is,
this could only be a local source
properties:
local:
description: LocalSource
properties:
name:
description: Name of the local step
type: string
timeout:
description: Timeout how long the BuildSource execution must
take.
type: string
type: object
type:
description: Type is the BuildRunSource qualifier, the type of
the data-source. Only LocalType is supported.
type: string
type: object
state:
description: State is used for canceling a buildrun (and maybe more
later on).
Expand Down Expand Up @@ -10354,6 +10386,8 @@ spec:
- name
type: object
type: array
required:
- build
type: object
status:
description: BuildRunStatus defines the observed state of BuildRun
Expand Down Expand Up @@ -10662,6 +10696,17 @@ spec:
description: URL describes the URL of the Git repository.
type: string
type: object
local:
description: LocalSource
properties:
name:
description: Name of the local step
type: string
timeout:
description: Timeout how long the BuildSource execution
must take.
type: string
type: object
ociArtifact:
description: OCIArtifact
properties:
Expand Down
11 changes: 11 additions & 0 deletions deploy/crds/shipwright.io_builds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,17 @@ spec:
description: URL describes the URL of the Git repository.
type: string
type: object
local:
description: LocalSource
properties:
name:
description: Name of the local step
type: string
timeout:
description: Timeout how long the BuildSource execution must
take.
type: string
type: object
ociArtifact:
description: OCIArtifact
properties:
Expand Down
21 changes: 21 additions & 0 deletions docs/buildrun.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ spec:
image: foo/bar:latest
```

### Defining the Build Source

BuildRun's support the specification of a Local type source. This is useful for working on development
mode, without forcing a user to commit/push changes to their related version control system. For more information please
refer to [SHIP 0016 - enabling local source code](https://github.com/shipwright-io/community/blob/main/ships/0016-enable-local-source-code-support.md).

```yaml
apiVersion: shipwright.io/v1beta1
kind: BuildRun
metadata:
name: local-buildrun
spec:
build:
name: a-build
source:
type: Local
local:
name: local-source
timeout: 3m
```

### Defining ParamValues

A `BuildRun` resource can define _paramValues_ for parameters specified in the build strategy. If a value has been provided for a parameter with the same name in the `Build` already, then the value from the `BuildRun` will have precedence.
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/build/v1alpha1/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ type Source struct {
// +optional
Credentials *corev1.LocalObjectReference `json:"credentials,omitempty"`
}

// IsLocalCopyType tells if we have an entry of the type local
func IsLocalCopyType(sources []BuildSource) (int, bool) {
for i, bs := range sources {
if bs.Type == LocalCopy {
return i, true
}
}
return -1, false
}
54 changes: 37 additions & 17 deletions pkg/apis/build/v1beta1/build_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,35 @@ func (src *Build) ConvertFrom(ctx context.Context, obj *unstructured.Unstructure
func (dest *BuildSpec) ConvertFrom(orig *v1alpha1.BuildSpec) error {
// Handle BuildSpec Source
specSource := Source{}
if orig.Source.BundleContainer != nil {
specSource.Type = OCIArtifactType
specSource.OCIArtifact = &OCIArtifact{
Image: orig.Source.BundleContainer.Image,
Prune: (*PruneOption)(orig.Source.BundleContainer.Prune),
}
if orig.Source.Credentials != nil {
specSource.OCIArtifact.PullSecret = &orig.Source.Credentials.Name

// only interested on spec.sources as long as an item of the list
// is of the type LocalCopy. Otherwise, we move into bundle or git types.
index, isLocal := v1alpha1.IsLocalCopyType(orig.Sources)
if isLocal {
specSource.Type = LocalType
specSource.LocalSource = &Local{
Name: orig.Sources[index].Name,
Timeout: orig.Sources[index].Timeout,
}
} else {
specSource.Type = GitType
specSource.GitSource = &Git{
URL: orig.Source.URL,
Revision: orig.Source.Revision,
}
if orig.Source.Credentials != nil {
specSource.GitSource.CloneSecret = &orig.Source.Credentials.Name
if orig.Source.BundleContainer != nil {
specSource.Type = OCIArtifactType
specSource.OCIArtifact = &OCIArtifact{
Image: orig.Source.BundleContainer.Image,
Prune: (*PruneOption)(orig.Source.BundleContainer.Prune),
}
if orig.Source.Credentials != nil {
specSource.OCIArtifact.PullSecret = &orig.Source.Credentials.Name
}
} else {
specSource.Type = GitType
specSource.GitSource = &Git{
URL: orig.Source.URL,
Revision: orig.Source.Revision,
}
if orig.Source.Credentials != nil {
specSource.GitSource.CloneSecret = &orig.Source.Credentials.Name
}
}
}
specSource.ContextDir = orig.Source.ContextDir
Expand Down Expand Up @@ -213,8 +225,16 @@ func (dest *BuildSpec) ConvertFrom(orig *v1alpha1.BuildSpec) error {
}

func (dest *BuildSpec) ConvertTo(bs *v1alpha1.BuildSpec) error {
// Handle BuildSpec Source
bs.Source = getAlphaBuildSource(*dest)
// Handle BuildSpec Sources or Source
if dest.Source.Type == LocalType && dest.Source.LocalSource != nil {
bs.Sources = append(bs.Sources, v1alpha1.BuildSource{
Name: dest.Source.LocalSource.Name,
Type: v1alpha1.LocalCopy,
Timeout: dest.Source.LocalSource.Timeout,
})
} else {
bs.Source = getAlphaBuildSource(*dest)
}

// Handle BuildSpec Trigger
if dest.Trigger != nil {
Expand Down
34 changes: 27 additions & 7 deletions pkg/apis/build/v1beta1/buildrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ func (src *BuildRun) ConvertTo(ctx context.Context, obj *unstructured.Unstructur
return err
}
alphaBuildRun.Spec.BuildSpec = &newBuildSpec
} else {
} else if src.Spec.Build.Name != nil {
alphaBuildRun.Spec.BuildRef = &v1alpha1.BuildRef{
Name: src.Spec.Build.Name,
Name: *src.Spec.Build.Name,
}
}

// BuildRunSpec Sources
if src.Spec.Source != nil && src.Spec.Source.Type == LocalType && src.Spec.Source.LocalSource != nil {
alphaBuildRun.Spec.Sources = append(alphaBuildRun.Spec.Sources, v1alpha1.BuildSource{
Name: src.Spec.Source.LocalSource.Name,
Type: v1alpha1.LocalCopy,
Timeout: src.Spec.Source.LocalSource.Timeout,
})
}

// BuildRunSpec ServiceAccount
// With the deprecation of serviceAccount.Generate, serviceAccount is set to ".generate" to have the SA created on fly.
if src.Spec.ServiceAccount != nil && *src.Spec.ServiceAccount == ".generate" {
Expand Down Expand Up @@ -181,14 +190,25 @@ func (src *BuildRun) ConvertFrom(ctx context.Context, obj *unstructured.Unstruct
func (dest *BuildRunSpec) ConvertFrom(orig *v1alpha1.BuildRunSpec) error {

// BuildRunSpec BuildSpec
dest.Build = &ReferencedBuild{}
if orig.BuildSpec != nil {
if dest.Build.Build != nil {
dest.Build.Build.ConvertFrom(orig.BuildSpec)
}
dest.Build.Build = &BuildSpec{}
dest.Build.Build.ConvertFrom(orig.BuildSpec)
}
if orig.BuildRef != nil {
dest.Build.Name = orig.BuildRef.Name
dest.Build.Name = &orig.BuildRef.Name
}

// only interested on spec.sources as long as an item of the list
// is of the type LocalCopy. Otherwise, we move into bundle or git types.
index, isLocal := v1alpha1.IsLocalCopyType(orig.Sources)
if isLocal {
dest.Source = &BuildRunSource{
Type: LocalType,
LocalSource: &Local{
Name: orig.Sources[index].Name,
Timeout: orig.Sources[index].Timeout,
},
}
}

if orig.ServiceAccount != nil {
Expand Down
14 changes: 10 additions & 4 deletions pkg/apis/build/v1beta1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@ type ReferencedBuild struct {
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
//
// +optional
Name string `json:"name,omitempty"`
Name *string `json:"name,omitempty"`
}

// BuildRunSpec defines the desired state of BuildRun
type BuildRunSpec struct {
// Build refers to an embedded build specification
// This field is mandatory
//
Build ReferencedBuild `json:"build"`

// Source refers to the location where the source code is,
// this could only be a local source
//
// +optional
Build *ReferencedBuild `json:"build,omitempty"`
Source *BuildRunSource `json:"source,omitempty"`

// ServiceAccount refers to the kubernetes serviceaccount
// which is used for resource control.
Expand Down Expand Up @@ -373,8 +379,8 @@ func (brs *BuildRunStatus) SetCondition(condition *Condition) {
// BuildName returns the name of the associated build, which can be a referenced
// build resource or an embedded build specification
func (buildrunSpec *BuildRunSpec) BuildName() string {
if buildrunSpec.Build != nil {
return buildrunSpec.Build.Name
if buildrunSpec.Build.Name != nil {
return *buildrunSpec.Build.Name
}

// Only BuildRuns with a ReferencedBuild can actually return a proper Build name
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/build/v1beta1/buildstrategy_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ func (src *BuildStrategySpec) ConvertTo(bs *v1alpha1.BuildStrategySpec) {
}

for argIndex, arg := range buildStep.Args {
if strings.Contains(arg, "$(params.dockerfile)") {
if strings.Contains(arg, "$(params.builder-image)") {
buildStep.Args[argIndex] = strings.ReplaceAll(arg, "$(params.builder-image)", "$(build.builder.image)")
}
}

for envIndex, env := range buildStep.Env {
if strings.Contains(env.Value, "$(params.dockerfile)") {
if strings.Contains(env.Value, "$(params.builder-image)") {
buildStep.Env[envIndex].Value = strings.ReplaceAll(env.Value, "$(params.builder-image)", "$(build.builder.image)")
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func (src *BuildStrategySpec) ConvertFrom(bs v1alpha1.BuildStrategySpec) {
}
if strings.Contains(arg, "$(build.builder.image)") {
usesBuilderImage = true
step.Command[argIndex] = strings.ReplaceAll(arg, "$(build.builder.image)", "$(params.builder-image)")
step.Args[argIndex] = strings.ReplaceAll(arg, "$(build.builder.image)", "$(params.builder-image)")
}
}

Expand All @@ -227,7 +227,7 @@ func (src *BuildStrategySpec) ConvertFrom(bs v1alpha1.BuildStrategySpec) {
}
if strings.Contains(env.Value, "$(build.builder.image)") {
usesBuilderImage = true
step.Command[envIndex] = strings.ReplaceAll(env.Value, "$(build.builder.image)", "$(params.builder-image)")
step.Env[envIndex].Value = strings.ReplaceAll(env.Value, "$(build.builder.image)", "$(params.builder-image)")
}
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/build/v1beta1/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Local struct {
//
// +optional
Timeout *metav1.Duration `json:"timeout,omitempty"`

// Name of the local step
Name string `json:"name,omitempty"`
}

// Git describes the git repository to pull
Expand Down Expand Up @@ -101,4 +104,21 @@ type Source struct {
//
// +optional
GitSource *Git `json:"git,omitempty"`

// LocalSource
//
// +optional
LocalSource *Local `json:"local,omitempty"`
}

// BuildRunSource describes the local source to use
type BuildRunSource struct {
// Type is the BuildRunSource qualifier, the type of the data-source.
// Only LocalType is supported.
//
// +optional
Type BuildSourceType `json:"type,omitempty"`
// LocalSource
//
LocalSource *Local `json:"local,omitempty"`
}
Loading

0 comments on commit f8a2b63

Please sign in to comment.