forked from shipwright-io/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Representing Shipwright resources as flags.
Extending `flags` package to support Build and BuildRun objects, reusing shared flags among resources. Adding more tests and documentation.
- Loading branch information
Showing
8 changed files
with
410 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package flags | ||
|
||
import ( | ||
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
"github.com/spf13/pflag" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// BuildSpecFlags creates a BuildSpec instance based on command-line flags. | ||
func BuildSpecFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildSpec { | ||
clusterBuildStrategyKind := buildv1alpha1.ClusterBuildStrategyKind | ||
spec := &buildv1alpha1.BuildSpec{ | ||
Source: buildv1alpha1.Source{ | ||
Credentials: &corev1.LocalObjectReference{}, | ||
}, | ||
Strategy: &buildv1alpha1.Strategy{ | ||
Kind: &clusterBuildStrategyKind, | ||
APIVersion: buildv1alpha1.SchemeGroupVersion.Version, | ||
}, | ||
Dockerfile: nil, | ||
Builder: &buildv1alpha1.Image{ | ||
Credentials: &corev1.LocalObjectReference{}, | ||
}, | ||
Output: buildv1alpha1.Image{ | ||
Credentials: &corev1.LocalObjectReference{}, | ||
}, | ||
Timeout: &metav1.Duration{}, | ||
} | ||
|
||
sourceFlags(flags, &spec.Source) | ||
strategyFlags(flags, spec.Strategy) | ||
|
||
flags.Var( | ||
NewStringPointerValue(spec.Dockerfile), | ||
"dockerfile", | ||
"path to dockerfile relative to repository", | ||
) | ||
|
||
imageFlags(flags, "builder", spec.Builder) | ||
imageFlags(flags, "output", &spec.Output) | ||
|
||
timeoutFlags(flags, spec.Timeout) | ||
|
||
return spec | ||
} | ||
|
||
// SanitizeBuildSpec checks for empty inner data structure and replaces them with nil. | ||
func SanitizeBuildSpec(b *buildv1alpha1.BuildSpec) { | ||
if b == nil { | ||
return | ||
} | ||
if b.Source.Credentials != nil && b.Source.Credentials.Name == "" { | ||
b.Source.Credentials = nil | ||
} | ||
if b.Builder != nil { | ||
if b.Builder.Credentials != nil && b.Builder.Credentials.Name == "" { | ||
b.Builder.Credentials = nil | ||
} | ||
if b.Builder.Image == "" && b.Builder.Credentials == nil { | ||
b.Builder = nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package flags | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
o "github.com/onsi/gomega" | ||
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestSanitizeBuildSpec(t *testing.T) { | ||
g := gomega.NewGomegaWithT(t) | ||
|
||
completeBuildSpec := buildv1alpha1.BuildSpec{ | ||
Source: buildv1alpha1.Source{ | ||
Credentials: &v1.LocalObjectReference{Name: "name"}, | ||
}, | ||
Builder: &buildv1alpha1.Image{ | ||
Credentials: &v1.LocalObjectReference{Name: "name"}, | ||
Image: "image", | ||
}, | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
in buildv1alpha1.BuildSpec | ||
out buildv1alpha1.BuildSpec | ||
}{{ | ||
name: "all empty should stay empty", | ||
in: buildv1alpha1.BuildSpec{}, | ||
out: buildv1alpha1.BuildSpec{}, | ||
}, { | ||
name: "should clean-up `.spec.source.credentials`", | ||
in: buildv1alpha1.BuildSpec{Source: buildv1alpha1.Source{ | ||
Credentials: &v1.LocalObjectReference{}, | ||
}}, | ||
out: buildv1alpha1.BuildSpec{}, | ||
}, { | ||
name: "should clean-up `.spec.builder.credentials`", | ||
in: buildv1alpha1.BuildSpec{Builder: &buildv1alpha1.Image{ | ||
Credentials: &v1.LocalObjectReference{}, | ||
}}, | ||
out: buildv1alpha1.BuildSpec{}, | ||
}, { | ||
name: "should clean-up `.spec.builder.image`", | ||
in: buildv1alpha1.BuildSpec{Builder: &buildv1alpha1.Image{}}, | ||
out: buildv1alpha1.BuildSpec{}, | ||
}, { | ||
name: "should not clean-up complete objects", | ||
in: completeBuildSpec, | ||
out: completeBuildSpec, | ||
}} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
copy := tt.in.DeepCopy() | ||
SanitizeBuildSpec(copy) | ||
g.Expect(tt.out).To(o.Equal(*copy)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package flags | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
v1 "k8s.io/api/core/v1" | ||
|
||
o "github.com/onsi/gomega" | ||
) | ||
|
||
func TestSanitizeBuildRunSpec(t *testing.T) { | ||
g := gomega.NewGomegaWithT(t) | ||
|
||
name := "name" | ||
completeBuildRunSpec := buildv1alpha1.BuildRunSpec{ | ||
ServiceAccount: &buildv1alpha1.ServiceAccount{Name: &name}, | ||
Output: &buildv1alpha1.Image{ | ||
Credentials: &v1.LocalObjectReference{Name: "name"}, | ||
Image: "image", | ||
}, | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
in buildv1alpha1.BuildRunSpec | ||
out buildv1alpha1.BuildRunSpec | ||
}{{ | ||
name: "all empty should stay empty", | ||
in: buildv1alpha1.BuildRunSpec{}, | ||
out: buildv1alpha1.BuildRunSpec{}, | ||
}, { | ||
name: "should clean-up service-account", | ||
in: buildv1alpha1.BuildRunSpec{ServiceAccount: &buildv1alpha1.ServiceAccount{}}, | ||
out: buildv1alpha1.BuildRunSpec{}, | ||
}, { | ||
name: "should clean-up output", | ||
in: buildv1alpha1.BuildRunSpec{Output: &buildv1alpha1.Image{}}, | ||
out: buildv1alpha1.BuildRunSpec{}, | ||
}, { | ||
name: "should not clean-up complete objects", | ||
in: completeBuildRunSpec, | ||
out: completeBuildRunSpec, | ||
}} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
copy := tt.in.DeepCopy() | ||
SanitizeBuildRunSpec(copy) | ||
g.Expect(tt.out).To(o.Equal(*copy)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
// Package flags contains command-line flags that can be reused over the project. | ||
// Package flags contains command-line flags that can be reused over the project, taking real | ||
// Shipwright Build-Controller resources as a direct representation as command-line flags. | ||
// | ||
// For instance: | ||
// | ||
// cmd := &cobra.Command{} | ||
// br := flags.BuildRunSpecFlags(cmd.Flags()) | ||
// flags.SanitizeBuildRunSpec(&br.Spec) | ||
// | ||
// The snippet above shows how to decorate an existing cobra.Command instance with flags, and return | ||
// an instantiated object, which will be receive the inputted values. And, to make sure inner items | ||
// are set to nil when all empty, to the resource in question can be used directly against Kubernetes | ||
// API. | ||
package flags |
Oops, something went wrong.