Skip to content

Commit

Permalink
Merge pull request #111 from SaschaSchwarze0/sascha-retention-flags
Browse files Browse the repository at this point in the history
Add retention flags
  • Loading branch information
openshift-merge-robot authored Apr 12, 2022
2 parents fe4d67d + efa4351 commit 9185dde
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 54 deletions.
38 changes: 21 additions & 17 deletions docs/shp_build_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ shp build create <name> [flags]
### Options

```
--builder-credentials-secret string name of the secret with builder-image pull credentials
--builder-image string image employed during the building process
--dockerfile string path to dockerfile relative to repository
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
-h, --help help for create
--output-credentials-secret string name of the secret with builder-image pull credentials
--output-image string image employed during the building process
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])
--source-context-dir string use a inner directory as context directory
--source-credentials-secret string name of the secret with git repository credentials
--source-revision string git repository source revision
--source-url string git repository source URL
--strategy-apiversion string kubernetes api-version of the build-strategy resource (default "v1alpha1")
--strategy-kind string build-strategy kind (default "ClusterBuildStrategy")
--strategy-name string build-strategy name (default "buildpacks-v3")
--timeout duration build process timeout
--builder-credentials-secret string name of the secret with builder-image pull credentials
--builder-image string image employed during the building process
--dockerfile string path to dockerfile relative to repository
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
-h, --help help for create
--output-credentials-secret string name of the secret with builder-image pull credentials
--output-image string image employed during the building process
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])
--retention-failed-limit uint number of failed BuildRuns to be kept (default 65535)
--retention-succeeded-limit uint number of succeeded BuildRuns to be kept (default 65535)
--retention-ttl-after-failed duration duration to delete a failed BuildRun after completion
--retention-ttl-after-succeeded duration duration to delete a succeeded BuildRun after completion
--source-context-dir string use a inner directory as context directory
--source-credentials-secret string name of the secret with git repository credentials
--source-revision string git repository source revision
--source-url string git repository source URL
--strategy-apiversion string kubernetes api-version of the build-strategy resource (default "v1alpha1")
--strategy-kind string build-strategy kind (default "ClusterBuildStrategy")
--strategy-name string build-strategy name (default "buildpacks-v3")
--timeout duration build process timeout
```

### Options inherited from parent commands
Expand Down
26 changes: 14 additions & 12 deletions docs/shp_build_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ shp build run <name> [flags]
### Options

```
--buildref-apiversion string API version of build resource to reference
--buildref-name string name of build resource to reference
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
-F, --follow Start a build and watch its log until it completes or fails.
-h, --help help for run
--output-credentials-secret string name of the secret with builder-image pull credentials
--output-image string image employed during the building process
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])
--sa-generate generate a Kubernetes service-account for the build
--sa-name string Kubernetes service-account name
--timeout duration build process timeout
--buildref-apiversion string API version of build resource to reference
--buildref-name string name of build resource to reference
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
-F, --follow Start a build and watch its log until it completes or fails.
-h, --help help for run
--output-credentials-secret string name of the secret with builder-image pull credentials
--output-image string image employed during the building process
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])
--retention-ttl-after-failed duration duration to delete the BuildRun after it failed
--retention-ttl-after-succeeded duration duration to delete the BuildRun after it succeeded
--sa-generate generate a Kubernetes service-account for the build
--sa-name string Kubernetes service-account name
--timeout duration build process timeout
```

### Options inherited from parent commands
Expand Down
26 changes: 14 additions & 12 deletions docs/shp_build_upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ shp build upload <build-name> [path/to/source|.] [flags]
### Options

```
--buildref-apiversion string API version of build resource to reference
--buildref-name string name of build resource to reference
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
-F, --follow Start a build and watch its log until it completes or fails.
-h, --help help for upload
--output-credentials-secret string name of the secret with builder-image pull credentials
--output-image string image employed during the building process
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])
--sa-generate generate a Kubernetes service-account for the build
--sa-name string Kubernetes service-account name
--timeout duration build process timeout
--buildref-apiversion string API version of build resource to reference
--buildref-name string name of build resource to reference
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
-F, --follow Start a build and watch its log until it completes or fails.
-h, --help help for upload
--output-credentials-secret string name of the secret with builder-image pull credentials
--output-image string image employed during the building process
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])
--retention-ttl-after-failed duration duration to delete the BuildRun after it failed
--retention-ttl-after-succeeded duration duration to delete the BuildRun after it succeeded
--sa-generate generate a Kubernetes service-account for the build
--sa-name string Kubernetes service-account name
--timeout duration build process timeout
```

### Options inherited from parent commands
Expand Down
24 changes: 13 additions & 11 deletions docs/shp_buildrun_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ shp buildrun create <name> [flags]
### Options

```
--buildref-apiversion string API version of build resource to reference
--buildref-name string name of build resource to reference
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
-h, --help help for create
--output-credentials-secret string name of the secret with builder-image pull credentials
--output-image string image employed during the building process
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])
--sa-generate generate a Kubernetes service-account for the build
--sa-name string Kubernetes service-account name
--timeout duration build process timeout
--buildref-apiversion string API version of build resource to reference
--buildref-name string name of build resource to reference
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
-h, --help help for create
--output-credentials-secret string name of the secret with builder-image pull credentials
--output-image string image employed during the building process
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])
--retention-ttl-after-failed duration duration to delete the BuildRun after it failed
--retention-ttl-after-succeeded duration duration to delete the BuildRun after it succeeded
--sa-generate generate a Kubernetes service-account for the build
--sa-name string Kubernetes service-account name
--timeout duration build process timeout
```

### Options inherited from parent commands
Expand Down
6 changes: 5 additions & 1 deletion pkg/shp/cmd/build/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams
}

for _, b := range buildList.Items {
fmt.Fprintf(writer, columnTemplate, b.Name, b.Spec.Output.Image, b.Status.Message)
message := ""
if b.Status.Message != nil {
message = *b.Status.Message
}
fmt.Fprintf(writer, columnTemplate, b.Name, b.Spec.Output.Image, message)
}

writer.Flush()
Expand Down
37 changes: 37 additions & 0 deletions pkg/shp/flags/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"k8s.io/utils/pointer"
)

func pointerUInt(value uint) *uint {
return &value
}

// BuildSpecFromFlags creates a BuildSpec instance based on command-line flags.
func BuildSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildSpec {
clusterBuildStrategyKind := buildv1alpha1.ClusterBuildStrategyKind
Expand All @@ -33,6 +37,12 @@ func BuildSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildSpec {
Annotations: map[string]string{},
},
Timeout: &metav1.Duration{},
Retention: &buildv1alpha1.BuildRetention{
FailedLimit: pointerUInt(65535),
SucceededLimit: pointerUInt(65535),
TTLAfterFailed: &metav1.Duration{},
TTLAfterSucceeded: &metav1.Duration{},
},
}

sourceFlags(flags, &spec.Source)
Expand All @@ -44,6 +54,7 @@ func BuildSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildSpec {
envFlags(flags, &spec.Env)
imageLabelsFlags(flags, spec.Output.Labels)
imageAnnotationsFlags(flags, spec.Output.Annotations)
buildRetentionFlags(flags, spec.Retention)

return spec
}
Expand All @@ -56,9 +67,15 @@ func SanitizeBuildSpec(b *buildv1alpha1.BuildSpec) {
if b.Source.Credentials != nil && b.Source.Credentials.Name == "" {
b.Source.Credentials = nil
}
if b.Source.ContextDir != nil && *b.Source.ContextDir == "" {
b.Source.ContextDir = nil
}
if b.Source.Revision != nil && *b.Source.Revision == "" {
b.Source.Revision = nil
}
if b.Source.URL != nil && *b.Source.URL == "" {
b.Source.URL = nil
}
if b.Builder != nil {
if b.Builder.Credentials != nil && b.Builder.Credentials.Name == "" {
b.Builder.Credentials = nil
Expand All @@ -76,4 +93,24 @@ func SanitizeBuildSpec(b *buildv1alpha1.BuildSpec) {
if b.Dockerfile != nil && *b.Dockerfile == "" {
b.Dockerfile = nil
}
if b.Output.Credentials != nil && b.Output.Credentials.Name == "" {
b.Output.Credentials = nil
}
if b.Retention != nil {
if b.Retention.FailedLimit != nil && *b.Retention.FailedLimit == 65535 {
b.Retention.FailedLimit = nil
}
if b.Retention.SucceededLimit != nil && *b.Retention.SucceededLimit == 65535 {
b.Retention.SucceededLimit = nil
}
if b.Retention.TTLAfterFailed != nil && b.Retention.TTLAfterFailed.Duration == 0 {
b.Retention.TTLAfterFailed = nil
}
if b.Retention.TTLAfterSucceeded != nil && b.Retention.TTLAfterSucceeded.Duration == 0 {
b.Retention.TTLAfterSucceeded = nil
}
if b.Retention.FailedLimit == nil && b.Retention.SucceededLimit == nil && b.Retention.TTLAfterFailed == nil && b.Retention.TTLAfterSucceeded == nil {
b.Retention = nil
}
}
}
65 changes: 65 additions & 0 deletions pkg/shp/flags/build_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flags

import (
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -43,6 +44,16 @@ func TestBuildSpecFromFlags(t *testing.T) {
Timeout: &metav1.Duration{
Duration: 1 * time.Second,
},
Retention: &buildv1alpha1.BuildRetention{
FailedLimit: pointerUInt(10),
SucceededLimit: pointerUInt(5),
TTLAfterFailed: &metav1.Duration{
Duration: 48 * time.Hour,
},
TTLAfterSucceeded: &metav1.Duration{
Duration: 30 * time.Minute,
},
},
}

cmd := &cobra.Command{}
Expand Down Expand Up @@ -112,6 +123,34 @@ func TestBuildSpecFromFlags(t *testing.T) {

g.Expect(*expected.Timeout).To(Equal(*spec.Timeout), "spec.timeout")
})

t.Run(".spec.retention.failedLimit", func(t *testing.T) {
err := flags.Set(RetentionFailedLimitFlag, strconv.FormatUint(uint64(*expected.Retention.FailedLimit), 10))
g.Expect(err).To(BeNil())

g.Expect(*expected.Retention.FailedLimit).To(Equal(*spec.Retention.FailedLimit), "spec.retention.failedLimit")
})

t.Run(".spec.retention.succeededLimit", func(t *testing.T) {
err := flags.Set(RetentionSucceededLimitFlag, strconv.FormatUint(uint64(*expected.Retention.SucceededLimit), 10))
g.Expect(err).To(BeNil())

g.Expect(*expected.Retention.SucceededLimit).To(Equal(*spec.Retention.SucceededLimit), "spec.retention.succeededLimit")
})

t.Run(".spec.retention.ttlAfterFailed", func(t *testing.T) {
err := flags.Set(RetentionTTLAfterFailedFlag, expected.Retention.TTLAfterFailed.Duration.String())
g.Expect(err).To(BeNil())

g.Expect(*expected.Retention.TTLAfterFailed).To(Equal(*spec.Retention.TTLAfterFailed), "spec.retention.ttlAfterFailed")
})

t.Run(".spec.retention.ttlAfterSucceeded", func(t *testing.T) {
err := flags.Set(RetentionTTLAfterSucceededFlag, expected.Retention.TTLAfterSucceeded.Duration.String())
g.Expect(err).To(BeNil())

g.Expect(*expected.Retention.TTLAfterSucceeded).To(Equal(*spec.Retention.TTLAfterSucceeded), "spec.retention.ttlAfterSucceeded")
})
}

func TestSanitizeBuildSpec(t *testing.T) {
Expand Down Expand Up @@ -175,6 +214,32 @@ func TestSanitizeBuildSpec(t *testing.T) {
out: buildv1alpha1.BuildSpec{Source: buildv1alpha1.Source{
Revision: nil,
}},
}, {
name: "should clean-up an empty retention",
in: buildv1alpha1.BuildSpec{
Retention: &buildv1alpha1.BuildRetention{},
},
out: buildv1alpha1.BuildSpec{},
}, {
name: "should clean-up an empty source contextDir",
in: buildv1alpha1.BuildSpec{
Source: buildv1alpha1.Source{
ContextDir: pointer.String(""),
},
},
out: buildv1alpha1.BuildSpec{
Source: buildv1alpha1.Source{},
},
}, {
name: "should clean-up an empty source URL",
in: buildv1alpha1.BuildSpec{
Source: buildv1alpha1.Source{
URL: pointer.String(""),
},
},
out: buildv1alpha1.BuildSpec{
Source: buildv1alpha1.Source{},
},
}}

for _, tt := range testCases {
Expand Down
22 changes: 21 additions & 1 deletion pkg/shp/flags/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func BuildRunSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildRunSpec {
Annotations: map[string]string{},
},
Env: []corev1.EnvVar{},
Retention: &buildv1alpha1.BuildRunRetention{
TTLAfterFailed: &metav1.Duration{},
TTLAfterSucceeded: &metav1.Duration{},
},
}

buildRefFlags(flags, spec.BuildRef)
Expand All @@ -35,6 +39,7 @@ func BuildRunSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildRunSpec {
envFlags(flags, &spec.Env)
imageLabelsFlags(flags, spec.Output.Labels)
imageAnnotationsFlags(flags, spec.Output.Annotations)
buildRunRetentionFlags(flags, spec.Retention)

return spec
}
Expand All @@ -45,7 +50,11 @@ func SanitizeBuildRunSpec(br *buildv1alpha1.BuildRunSpec) {
return
}
if br.BuildRef != nil {
if br.BuildRef.Name == "" && br.BuildRef.APIVersion != nil && *br.BuildRef.APIVersion == "" {
if br.BuildRef.APIVersion != nil && *br.BuildRef.APIVersion == "" {
br.BuildRef.APIVersion = nil
}

if br.BuildRef.Name == "" && br.BuildRef.APIVersion == nil {
br.BuildRef = nil
}
}
Expand All @@ -70,4 +79,15 @@ func SanitizeBuildRunSpec(br *buildv1alpha1.BuildRunSpec) {
if len(br.Env) == 0 {
br.Env = nil
}
if br.Retention != nil {
if br.Retention.TTLAfterFailed != nil && br.Retention.TTLAfterFailed.Duration == 0 {
br.Retention.TTLAfterFailed = nil
}
if br.Retention.TTLAfterSucceeded != nil && br.Retention.TTLAfterSucceeded.Duration == 0 {
br.Retention.TTLAfterSucceeded = nil
}
if br.Retention.TTLAfterFailed == nil && br.Retention.TTLAfterSucceeded == nil {
br.Retention = nil
}
}
}
Loading

0 comments on commit 9185dde

Please sign in to comment.