Skip to content

Commit

Permalink
Default workload type to web (#498)
Browse files Browse the repository at this point in the history
feat: Default workload type to web

Signed-off-by: Wendy Arango <warango@vmware.com>
  • Loading branch information
warango4 committed Mar 21, 2023
1 parent da285d2 commit 336df50
Show file tree
Hide file tree
Showing 12 changed files with 1,446 additions and 653 deletions.
5 changes: 5 additions & 0 deletions pkg/apis/cartographer/v1alpha1/workload_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ func (w *Workload) IsAnnotationExists(key string) bool {
return ok
}

func (w *Workload) IsLabelExists(key string) bool {
_, ok := w.Labels[key]
return ok
}

func (w *Workload) MergeLabels(key, value string) {
if w.Labels == nil {
w.Labels = map[string]string{}
Expand Down
154 changes: 154 additions & 0 deletions pkg/apis/cartographer/v1alpha1/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,160 @@ func TestWorkload_Load(t *testing.T) {
}
}

func TestWorkload_IsAnnotationExists(t *testing.T) {
tests := []struct {
name string
seed *Workload
exists bool
annotation string
}{
{
name: "annotation exists in workload",
seed: &Workload{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"hello": "world",
},
},
},
annotation: "hello",
exists: true,
}, {
name: "annotation does not exist in workload",
seed: &Workload{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"my-annotation": "my-value",
},
},
},
annotation: "hello",
exists: false,
}, {
name: "no annotations",
seed: &Workload{},
annotation: "hello",
exists: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
annotationExists := test.seed.IsAnnotationExists(test.annotation)
if diff := cmp.Diff(test.exists, annotationExists); diff != "" {
t.Errorf("IsAnnotationExists() (-want, +got) = %v", diff)
}
})
}
}

func TestWorkload_IsLabelExists(t *testing.T) {
tests := []struct {
name string
seed *Workload
exists bool
label string
}{
{
name: "label exists in workload",
seed: &Workload{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"hello": "world",
},
},
},
label: "hello",
exists: true,
}, {
name: "label does not exist in workload",
seed: &Workload{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"my-annotation": "my-value",
},
},
},
label: "hello",
exists: false,
}, {
name: "no labels",
seed: &Workload{},
label: "hello",
exists: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
labelExists := test.seed.IsLabelExists(test.label)
if diff := cmp.Diff(test.exists, labelExists); diff != "" {
t.Errorf("IsLabelExists() (-want, +got) = %v", diff)
}
})
}
}

func TestWorkload_DeleteAnnotation(t *testing.T) {
tests := []struct {
name string
seed *Workload
expected *Workload
toRemove string
}{
{
name: "delete annotation",
seed: &Workload{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"hello": "world",
"my-annotation": "my-value",
},
},
},
toRemove: "hello",
expected: &Workload{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"my-annotation": "my-value",
},
},
},
}, {
name: "annotation does not exist",
seed: &Workload{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"my-annotation": "my-value",
},
},
},
expected: &Workload{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"my-annotation": "my-value",
},
},
},
toRemove: "hello",
}, {
name: "no annotations",
seed: &Workload{},
expected: &Workload{},
toRemove: "hello",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.seed.RemoveAnnotations(test.toRemove)
if diff := cmp.Diff(test.seed, test.expected); diff != "" {
t.Errorf("RemoveAnnotations() (-want, +got) = %v", diff)
}
})
}
}

func TestWorkload_MergeServiceAccountName(t *testing.T) {
serviceAccount := "test-service-account"
updatedServiceAccount := "updated-service-account"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ apiVersion: carto.run/v1alpha1
kind: Workload
metadata:
name: spring-petclinic
labels:
apps.tanzu.vmware.com/workload-type: web
annotations:
preserve-me: should-exist
controller-gen.kubebuilder.io/version: v0.7.0
Expand Down
10 changes: 6 additions & 4 deletions pkg/commands/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
const (
AnnotationReservedKey = "annotations"
MavenOverwrittenNoticeMsg = "Maven configuration flags have overwritten values provided by \"--params-yaml\"."
WebTypeReservedKey = "web"
)

func NewWorkloadCommand(ctx context.Context, c *cli.Config) *cobra.Command {
Expand Down Expand Up @@ -222,7 +223,7 @@ func (opts *WorkloadOptions) LoadDefaults(c *cli.Config) {
opts.ExcludePathFile = c.TanzuIgnoreFile
}

func (opts *WorkloadOptions) ApplyOptionsToWorkload(ctx context.Context, workload *cartov1alpha1.Workload) context.Context {
func (opts *WorkloadOptions) ApplyOptionsToWorkload(ctx context.Context, workload *cartov1alpha1.Workload, workloadExists bool) context.Context {
for _, label := range opts.Labels {
parts := parsers.DeletableKeyValue(label)
if len(parts) == 1 {
Expand Down Expand Up @@ -292,7 +293,8 @@ func (opts *WorkloadOptions) ApplyOptionsToWorkload(ctx context.Context, workloa
workload.MergeLabels(apis.AppPartOfLabelName, opts.App)
}

if opts.Type != "" {
if (cli.CommandFromContext(ctx).Flags().Changed(cli.StripDash(flags.TypeFlagName)) ||
(!workload.IsLabelExists(apis.WorkloadTypeLabelName) && !workloadExists)) && opts.Type != "" {
workload.MergeLabels(apis.WorkloadTypeLabelName, opts.Type)
}

Expand Down Expand Up @@ -796,9 +798,9 @@ func (opts *WorkloadOptions) DefineFlags(ctx context.Context, c *cli.Config, cmd
cli.NamespaceFlag(ctx, cmd, c, &opts.Namespace)
cmd.Flags().StringVarP(&opts.FilePath, cli.StripDash(flags.FilePathFlagName), "f", "", "`file path` containing the description of a single workload, other flags are layered on top of this resource. Use value \"-\" to read from stdin")
cmd.Flags().StringVarP(&opts.App, cli.StripDash(flags.AppFlagName), "a", "", "application `name` the workload is a part of")
cmd.Flags().StringVarP(&opts.Type, cli.StripDash(flags.TypeFlagName), "t", "", "distinguish workload `type`")
cmd.Flags().StringVarP(&opts.Type, cli.StripDash(flags.TypeFlagName), "t", WebTypeReservedKey, "distinguish workload `type`")
cmd.RegisterFlagCompletionFunc(cli.StripDash(flags.TypeFlagName), func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"web"}, cobra.ShellCompDirectiveNoFileComp
return []string{WebTypeReservedKey}, cobra.ShellCompDirectiveNoFileComp
})
cmd.Flags().StringSliceVarP(&opts.Labels, cli.StripDash(flags.LabelFlagName), "l", []string{}, "label is represented as a `\"key=value\" pair` (\"key-\" to remove, flag can be used multiple times)")
cmd.Flags().StringSliceVar(&opts.Annotations, cli.StripDash(flags.AnnotationFlagName), []string{}, "annotation is represented as a `\"key=value\" pair` (\"key-\" to remove, flag can be used multiple times)")
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/workload_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func (opts *WorkloadApplyOptions) Exec(ctx context.Context, c *cli.Config) error

workload.Name = opts.Name
workload.Namespace = opts.Namespace
ctx = opts.ApplyOptionsToWorkload(ctx, workload)
workloadExists := currentWorkload != nil
ctx = opts.ApplyOptionsToWorkload(ctx, workload, workloadExists)

// validate complex flag interactions with existing state
errs = workload.Validate()
Expand All @@ -150,8 +151,6 @@ func (opts *WorkloadApplyOptions) Exec(ctx context.Context, c *cli.Config) error
return nil
}

workloadExists := currentWorkload != nil

if err := opts.PublishLocalSource(ctx, c, currentWorkload, workload, shouldPrint); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 336df50

Please sign in to comment.