forked from LaconicNetwork/kompose
Merge pull request #1709 from axel7083/main
feat: adding support for --profile cmd option
This commit is contained in:
commit
ddd5f33745
@ -33,6 +33,7 @@ func (k *Kompose) Convert(options ConvertOptions) ([]runtime.Object, error) {
|
|||||||
PushImageRegistry: options.PushImageRegistry,
|
PushImageRegistry: options.PushImageRegistry,
|
||||||
CreateDeploymentConfig: k.createDeploymentConfig(options),
|
CreateDeploymentConfig: k.createDeploymentConfig(options),
|
||||||
EmptyVols: false,
|
EmptyVols: false,
|
||||||
|
Profiles: options.Profiles,
|
||||||
Volumes: *options.VolumeType,
|
Volumes: *options.VolumeType,
|
||||||
PVCRequestSize: options.PvcRequestSize,
|
PVCRequestSize: options.PvcRequestSize,
|
||||||
InsecureRepository: k.insecureRepository(options),
|
InsecureRepository: k.insecureRepository(options),
|
||||||
|
|||||||
@ -2,6 +2,8 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
v1 "k8s.io/api/core/v1"
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
@ -69,7 +71,7 @@ func TestConvertWithDefaultOptions(t *testing.T) {
|
|||||||
client, err := NewClient(WithErrorOnWarning())
|
client, err := NewClient(WithErrorOnWarning())
|
||||||
assert.Check(t, is.Equal(err, nil))
|
assert.Check(t, is.Equal(err, nil))
|
||||||
objects, err := client.Convert(ConvertOptions{
|
objects, err := client.Convert(ConvertOptions{
|
||||||
OutFile: "./testdata/generated/",
|
ToStdout: true,
|
||||||
InputFiles: []string{
|
InputFiles: []string{
|
||||||
"./testdata/docker-compose.yaml",
|
"./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: "One profile only",
|
||||||
|
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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
17
client/testdata/docker-compose-profiles.yaml
vendored
Normal file
17
client/testdata/docker-compose-profiles.yaml
vendored
Normal file
@ -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"
|
||||||
@ -44,6 +44,7 @@ type ConvertOptions struct {
|
|||||||
PvcRequestSize string
|
PvcRequestSize string
|
||||||
WithKomposeAnnotations *bool
|
WithKomposeAnnotations *bool
|
||||||
InputFiles []string
|
InputFiles []string
|
||||||
|
Profiles []string
|
||||||
Provider
|
Provider
|
||||||
GenerateNetworkPolicies bool
|
GenerateNetworkPolicies bool
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,6 +46,7 @@ var (
|
|||||||
ConvertDeploymentConfig bool
|
ConvertDeploymentConfig bool
|
||||||
ConvertReplicas int
|
ConvertReplicas int
|
||||||
ConvertController string
|
ConvertController string
|
||||||
|
ConvertProfiles []string
|
||||||
ConvertPushImage bool
|
ConvertPushImage bool
|
||||||
ConvertNamespace string
|
ConvertNamespace string
|
||||||
ConvertPushImageRegistry string
|
ConvertPushImageRegistry string
|
||||||
@ -115,6 +116,7 @@ var convertCmd = &cobra.Command{
|
|||||||
IsReplicaSetFlag: cmd.Flags().Lookup("replicas").Changed,
|
IsReplicaSetFlag: cmd.Flags().Lookup("replicas").Changed,
|
||||||
IsDeploymentConfigFlag: cmd.Flags().Lookup("deployment-config").Changed,
|
IsDeploymentConfigFlag: cmd.Flags().Lookup("deployment-config").Changed,
|
||||||
YAMLIndent: ConvertYAMLIndent,
|
YAMLIndent: ConvertYAMLIndent,
|
||||||
|
Profiles: ConvertProfiles,
|
||||||
WithKomposeAnnotation: WithKomposeAnnotation,
|
WithKomposeAnnotation: WithKomposeAnnotation,
|
||||||
MultipleContainerMode: MultipleContainerMode,
|
MultipleContainerMode: MultipleContainerMode,
|
||||||
ServiceGroupMode: ServiceGroupMode,
|
ServiceGroupMode: ServiceGroupMode,
|
||||||
@ -199,6 +201,8 @@ func init() {
|
|||||||
|
|
||||||
convertCmd.Flags().IntVar(&ConvertYAMLIndent, "indent", 2, "Spaces length to indent generated yaml files")
|
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 profiles`)
|
||||||
|
|
||||||
// In order to 'separate' both OpenShift and Kubernetes only flags. A custom help page is created
|
// In order to 'separate' both OpenShift and Kubernetes only flags. A custom help page is created
|
||||||
customHelp := `Usage:{{if .Runnable}}
|
customHelp := `Usage:{{if .Runnable}}
|
||||||
{{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
|
{{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
|
||||||
|
|||||||
@ -217,7 +217,7 @@ func Convert(opt kobject.ConvertOptions) ([]runtime.Object, error) {
|
|||||||
komposeObject := kobject.KomposeObject{
|
komposeObject := kobject.KomposeObject{
|
||||||
ServiceConfigs: make(map[string]kobject.ServiceConfig),
|
ServiceConfigs: make(map[string]kobject.ServiceConfig),
|
||||||
}
|
}
|
||||||
komposeObject, err = l.LoadFile(opt.InputFiles)
|
komposeObject, err = l.LoadFile(opt.InputFiles, opt.Profiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf(err.Error())
|
log.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,6 +53,7 @@ type ConvertOptions struct {
|
|||||||
BuildRepo string
|
BuildRepo string
|
||||||
BuildBranch string
|
BuildBranch string
|
||||||
Build string
|
Build string
|
||||||
|
Profiles []string
|
||||||
PushImage bool
|
PushImage bool
|
||||||
PushImageRegistry string
|
PushImageRegistry string
|
||||||
CreateChart bool
|
CreateChart bool
|
||||||
|
|||||||
@ -149,14 +149,19 @@ func checkUnsupportedKey(composeProject *types.Project) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LoadFile loads a compose file into KomposeObject
|
// 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
|
// Gather the working directory
|
||||||
workingDir, err := getComposeFileDir(files)
|
workingDir, err := getComposeFileDir(files)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return kobject.KomposeObject{}, err
|
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 {
|
if err != nil {
|
||||||
return kobject.KomposeObject{}, errors.Wrap(err, "Unable to create compose options")
|
return kobject.KomposeObject{}, errors.Wrap(err, "Unable to create compose options")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
// Loader interface defines loader that loads files and converts it to kobject representation
|
// Loader interface defines loader that loads files and converts it to kobject representation
|
||||||
type Loader interface {
|
type Loader interface {
|
||||||
LoadFile(files []string) (kobject.KomposeObject, error)
|
LoadFile(files []string, profiles []string) (kobject.KomposeObject, error)
|
||||||
///Name() string
|
///Name() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user