From a7c63a650c0300111b1a0902643dcad9c69418bb Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Sun, 17 Sep 2023 17:41:05 +0200 Subject: [PATCH 1/6] Adding support for --profile cmd option --- cmd/convert.go | 4 ++++ output.yaml | 0 pkg/app/app.go | 2 +- pkg/kobject/kobject.go | 2 ++ pkg/loader/compose/compose.go | 10 ++++++++-- pkg/loader/loader.go | 2 +- 6 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 output.yaml diff --git a/cmd/convert.go b/cmd/convert.go index c2972f55..74a3b8aa 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -46,6 +46,7 @@ var ( ConvertDeploymentConfig bool ConvertReplicas int ConvertController string + ConvertProfiles []string ConvertPushImage bool ConvertNamespace string ConvertPushImageRegistry string @@ -115,6 +116,7 @@ var convertCmd = &cobra.Command{ IsReplicaSetFlag: cmd.Flags().Lookup("replicas").Changed, IsDeploymentConfigFlag: cmd.Flags().Lookup("deployment-config").Changed, YAMLIndent: ConvertYAMLIndent, + Profiles: ConvertProfiles, WithKomposeAnnotation: WithKomposeAnnotation, MultipleContainerMode: MultipleContainerMode, ServiceGroupMode: ServiceGroupMode, @@ -199,6 +201,8 @@ func init() { convertCmd.Flags().IntVar(&ConvertYAMLIndent, "indent", 2, "Spaces length to indent generated yaml files") + convertCmd.Flags().StringArrayVar(&ConvertProfiles, "profile", []string{}, `Specify the profile to use, can use multiple.`) + // In order to 'separate' both OpenShift and Kubernetes only flags. A custom help page is created customHelp := `Usage:{{if .Runnable}} {{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}} diff --git a/output.yaml b/output.yaml new file mode 100644 index 00000000..e69de29b diff --git a/pkg/app/app.go b/pkg/app/app.go index 25a930a1..918f66f9 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -217,7 +217,7 @@ func Convert(opt kobject.ConvertOptions) ([]runtime.Object, error) { komposeObject := kobject.KomposeObject{ ServiceConfigs: make(map[string]kobject.ServiceConfig), } - komposeObject, err = l.LoadFile(opt.InputFiles) + komposeObject, err = l.LoadFile(opt.InputFiles, opt.Profiles) if err != nil { log.Fatalf(err.Error()) } diff --git a/pkg/kobject/kobject.go b/pkg/kobject/kobject.go index 525008c5..37446e7d 100644 --- a/pkg/kobject/kobject.go +++ b/pkg/kobject/kobject.go @@ -53,6 +53,7 @@ type ConvertOptions struct { BuildRepo string BuildBranch string Build string + Profiles []string PushImage bool PushImageRegistry string CreateChart bool @@ -117,6 +118,7 @@ type ServiceConfig struct { Args []string `compose:"args"` VolList []string `compose:"volumes"` Network []string `compose:"network"` + Profiles []string `compose:"profiles"` Labels map[string]string `compose:"labels"` Annotations map[string]string `compose:""` CPUSet string `compose:"cpuset"` diff --git a/pkg/loader/compose/compose.go b/pkg/loader/compose/compose.go index 97396243..6998e3ae 100644 --- a/pkg/loader/compose/compose.go +++ b/pkg/loader/compose/compose.go @@ -149,14 +149,19 @@ func checkUnsupportedKey(composeProject *types.Project) []string { } // LoadFile loads a compose file into KomposeObject -func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) { +func (c *Compose) LoadFile(files []string, profiles []string) (kobject.KomposeObject, error) { // Gather the working directory workingDir, err := getComposeFileDir(files) if err != nil { return kobject.KomposeObject{}, err } - projectOptions, err := cli.NewProjectOptions(files, cli.WithOsEnv, cli.WithWorkingDirectory(workingDir), cli.WithInterpolation(true)) + projectOptions, err := cli.NewProjectOptions( + files, cli.WithOsEnv, + cli.WithWorkingDirectory(workingDir), + cli.WithInterpolation(true), + cli.WithProfiles(profiles), + ) if err != nil { return kobject.KomposeObject{}, errors.Wrap(err, "Unable to create compose options") } @@ -462,6 +467,7 @@ func dockerComposeToKomposeMapping(composeObject *types.Project) (kobject.Kompos serviceConfig.Privileged = composeServiceConfig.Privileged serviceConfig.User = composeServiceConfig.User serviceConfig.ReadOnly = composeServiceConfig.ReadOnly + serviceConfig.Profiles = []string{"frontend-dev"} serviceConfig.Stdin = composeServiceConfig.StdinOpen serviceConfig.Tty = composeServiceConfig.Tty serviceConfig.TmpFs = composeServiceConfig.Tmpfs diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go index bbea343c..def6f378 100644 --- a/pkg/loader/loader.go +++ b/pkg/loader/loader.go @@ -25,7 +25,7 @@ import ( // Loader interface defines loader that loads files and converts it to kobject representation type Loader interface { - LoadFile(files []string) (kobject.KomposeObject, error) + LoadFile(files []string, profiles []string) (kobject.KomposeObject, error) ///Name() string } From a712272be95230c4af53940998aab8e169113cb7 Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Thu, 21 Sep 2023 21:37:14 +0200 Subject: [PATCH 2/6] Delete output.yaml --- output.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 output.yaml diff --git a/output.yaml b/output.yaml deleted file mode 100644 index e69de29b..00000000 From 82eb2a8e10299b4e3b92758702f67b08e23e43c3 Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Thu, 21 Sep 2023 22:03:51 +0200 Subject: [PATCH 3/6] test: adding tests for convert --- client/convert.go | 1 + client/convert_test.go | 90 +++++++++++++++++++- client/testdata/docker-compose-profiles.yaml | 17 ++++ client/types.go | 1 + output.yaml | 0 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 client/testdata/docker-compose-profiles.yaml delete mode 100644 output.yaml diff --git a/client/convert.go b/client/convert.go index 3e62a622..c56122b2 100644 --- a/client/convert.go +++ b/client/convert.go @@ -33,6 +33,7 @@ func (k *Kompose) Convert(options ConvertOptions) ([]runtime.Object, error) { PushImageRegistry: options.PushImageRegistry, CreateDeploymentConfig: k.createDeploymentConfig(options), EmptyVols: false, + Profiles: options.Profiles, Volumes: *options.VolumeType, PVCRequestSize: options.PvcRequestSize, InsecureRepository: k.insecureRepository(options), diff --git a/client/convert_test.go b/client/convert_test.go index b7014936..58fc81d5 100644 --- a/client/convert_test.go +++ b/client/convert_test.go @@ -2,6 +2,8 @@ package client import ( "fmt" + v1 "k8s.io/api/core/v1" + "sort" "testing" "gotest.tools/v3/assert" @@ -69,7 +71,7 @@ func TestConvertWithDefaultOptions(t *testing.T) { client, err := NewClient(WithErrorOnWarning()) assert.Check(t, is.Equal(err, nil)) objects, err := client.Convert(ConvertOptions{ - OutFile: "./testdata/generated/", + ToStdout: true, InputFiles: []string{ "./testdata/docker-compose.yaml", }, @@ -81,3 +83,89 @@ func TestConvertWithDefaultOptions(t *testing.T) { } } } + +func TestConvertWithProfiles(t *testing.T) { + client, err := NewClient(WithErrorOnWarning()) + assert.Check(t, is.Equal(err, nil)) + + type Want struct { + deploymentsNames []string + servicesNames []string + } + + tests := []struct { + name string + options ConvertOptions + want Want + }{ + { + name: "No profiles provided", + options: ConvertOptions{ + ToStdout: true, + InputFiles: []string{ + "./testdata/docker-compose-profiles.yaml", + }, + }, + want: Want{ + deploymentsNames: nil, + servicesNames: nil, + }, + }, + { + name: "All profiles provided", + options: ConvertOptions{ + ToStdout: true, + InputFiles: []string{ + "./testdata/docker-compose-profiles.yaml", + }, + Profiles: []string{"hello", "world"}, + }, + want: Want{ + deploymentsNames: []string{"backend", "frontend", "database"}, + servicesNames: []string{"backend", "frontend", "database"}, + }, + }, + { + name: "All profiles provided", + options: ConvertOptions{ + ToStdout: true, + InputFiles: []string{ + "./testdata/docker-compose-profiles.yaml", + }, + Profiles: []string{"hello"}, + }, + want: Want{ + deploymentsNames: []string{"backend", "frontend"}, + servicesNames: []string{"backend", "frontend"}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + objects, err := client.Convert(tt.options) + assert.Check(t, is.Equal(err, nil)) + + sort.Strings(tt.want.deploymentsNames) + sort.Strings(tt.want.servicesNames) + + var deploymentsNames []string + var servicesNames []string + + for _, object := range objects { + if deployment, ok := object.(*appsv1.Deployment); ok { + deploymentsNames = append(deploymentsNames, deployment.Name) + } + + if service, ok := object.(*v1.Service); ok { + servicesNames = append(servicesNames, service.Name) + } + } + + sort.Strings(deploymentsNames) + sort.Strings(servicesNames) + + assert.Check(t, is.DeepEqual(deploymentsNames, tt.want.deploymentsNames)) + assert.Check(t, is.DeepEqual(servicesNames, tt.want.servicesNames)) + }) + } +} diff --git a/client/testdata/docker-compose-profiles.yaml b/client/testdata/docker-compose-profiles.yaml new file mode 100644 index 00000000..3f42dd2a --- /dev/null +++ b/client/testdata/docker-compose-profiles.yaml @@ -0,0 +1,17 @@ +version: '3' +services: + backend: + image: dummy:tag + profiles: ['hello', 'world'] + ports: + - "80:80" + frontend: + image: dummy:tag + profiles: [ 'hello' ] + ports: + - "80:80" + database: + image: dummy:tag + profiles: [ 'world' ] + ports: + - "80:80" diff --git a/client/types.go b/client/types.go index 35e94abe..a5c73855 100644 --- a/client/types.go +++ b/client/types.go @@ -44,6 +44,7 @@ type ConvertOptions struct { PvcRequestSize string WithKomposeAnnotations *bool InputFiles []string + Profiles []string Provider GenerateNetworkPolicies bool } diff --git a/output.yaml b/output.yaml deleted file mode 100644 index e69de29b..00000000 From 0302093e0a8140bb81a87fdf00f542a8b18a4530 Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Thu, 21 Sep 2023 22:09:18 +0200 Subject: [PATCH 4/6] fix: removing unused property --- pkg/kobject/kobject.go | 1 - pkg/loader/compose/compose.go | 1 - 2 files changed, 2 deletions(-) diff --git a/pkg/kobject/kobject.go b/pkg/kobject/kobject.go index 37446e7d..2da35cf6 100644 --- a/pkg/kobject/kobject.go +++ b/pkg/kobject/kobject.go @@ -118,7 +118,6 @@ type ServiceConfig struct { Args []string `compose:"args"` VolList []string `compose:"volumes"` Network []string `compose:"network"` - Profiles []string `compose:"profiles"` Labels map[string]string `compose:"labels"` Annotations map[string]string `compose:""` CPUSet string `compose:"cpuset"` diff --git a/pkg/loader/compose/compose.go b/pkg/loader/compose/compose.go index 6998e3ae..c750e007 100644 --- a/pkg/loader/compose/compose.go +++ b/pkg/loader/compose/compose.go @@ -467,7 +467,6 @@ func dockerComposeToKomposeMapping(composeObject *types.Project) (kobject.Kompos serviceConfig.Privileged = composeServiceConfig.Privileged serviceConfig.User = composeServiceConfig.User serviceConfig.ReadOnly = composeServiceConfig.ReadOnly - serviceConfig.Profiles = []string{"frontend-dev"} serviceConfig.Stdin = composeServiceConfig.StdinOpen serviceConfig.Tty = composeServiceConfig.Tty serviceConfig.TmpFs = composeServiceConfig.Tmpfs From 821b1afbf6a8d47735542c9fb34b5e40200fda1a Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Thu, 21 Sep 2023 22:10:07 +0200 Subject: [PATCH 5/6] fix: typo --- client/convert_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/convert_test.go b/client/convert_test.go index 58fc81d5..89a6b4c6 100644 --- a/client/convert_test.go +++ b/client/convert_test.go @@ -126,7 +126,7 @@ func TestConvertWithProfiles(t *testing.T) { }, }, { - name: "All profiles provided", + name: "One profile only", options: ConvertOptions{ ToStdout: true, InputFiles: []string{ From 134db384b2398d27e68e0ee8af85c95bd82acb00 Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Tue, 26 Sep 2023 08:41:47 +0200 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Charlie Drage --- cmd/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/convert.go b/cmd/convert.go index 74a3b8aa..898227bc 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -201,7 +201,7 @@ func init() { convertCmd.Flags().IntVar(&ConvertYAMLIndent, "indent", 2, "Spaces length to indent generated yaml files") - convertCmd.Flags().StringArrayVar(&ConvertProfiles, "profile", []string{}, `Specify the profile to use, can use multiple.`) + convertCmd.Flags().StringArrayVar(&ConvertProfiles, "profile", []string{}, `Specify the profile to use, can use multiple profiles`) // In order to 'separate' both OpenShift and Kubernetes only flags. A custom help page is created customHelp := `Usage:{{if .Runnable}}