Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Do not use default values in the Kubernetes yamls #941

Merged
merged 4 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ spec:
produces:
KubernetesYamls:
disabled: false
config:
outputPath: "deploy/cicd/argocd"
useDefaultValuesInYamls: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would setDefaultValuesInYamls be a better name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ spec:
dependency:
matchLabels:
move2kube.konveyor.io/kubernetesclusterselector: "true"
config:
outputPath: "deploy/cicd/buildconfig"
useDefaultValuesInYamls: false
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ spec:
dependency:
matchLabels:
move2kube.konveyor.io/kubernetesclusterselector: "true"
config:
outputPath: "deploy/knative"
useDefaultValuesInYamls: false
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ spec:
config:
outputPath: "deploy/yamls"
ingressName: "{{ .ProjectName }}"
useDefaultValuesInK8sYamls: false
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ spec:
move2kube.konveyor.io/kubernetesclusterselector: "true"
config:
outputPath: "{{ $rel := Rel .YamlsPath }}source/{{ $rel }}{{ if ne $rel \".\" }}/..{{end}}/{{ FilePathBase .YamlsPath }}-versionchanged/"
useDefaultValuesInYamls: false
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ spec:
produces:
KubernetesYamls:
disabled: false
config:
outputPath: "deploy/cicd/tekton"
useDefaultValuesInYamls: false
12 changes: 6 additions & 6 deletions transformer/kubernetes/apiresource/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

// TransformIRAndPersist transforms IR to yamls and writes to filesystem
func TransformIRAndPersist(ir irtypes.EnhancedIR, outputPath string, apis []IAPIResource, targetCluster collecttypes.ClusterMetadata) (files []string, err error) {
func TransformIRAndPersist(ir irtypes.EnhancedIR, outputPath string, apis []IAPIResource, targetCluster collecttypes.ClusterMetadata, useDefaultValsInYamls bool) (files []string, err error) {
logrus.Trace("TransformIRAndPersist start")
defer logrus.Trace("TransformIRAndPersist end")
targetObjs := []runtime.Object{}
Expand All @@ -46,7 +46,7 @@ func TransformIRAndPersist(ir irtypes.EnhancedIR, outputPath string, apis []IAPI
return nil, fmt.Errorf("failed to create the deploy directory at path '%s' . Error: %w", outputPath, err)
}
logrus.Debugf("Total %d services to be serialized.", len(targetObjs))
convertedObjs, err := convertVersion(targetObjs, targetCluster.Spec)
convertedObjs, err := convertVersion(targetObjs, targetCluster.Spec, useDefaultValsInYamls)
if err != nil {
return nil, fmt.Errorf("failed to fix, convert and transform the objects. Error: %w", err)
}
Expand All @@ -58,7 +58,7 @@ func TransformIRAndPersist(ir irtypes.EnhancedIR, outputPath string, apis []IAPI
}

// TransformObjsAndPersist transforms versions of yamls in current directory and writes to filesystem
func TransformObjsAndPersist(inputPath, outputPath string, apis []IAPIResource, targetCluster collecttypes.ClusterMetadata) (files []string, err error) {
func TransformObjsAndPersist(inputPath, outputPath string, apis []IAPIResource, targetCluster collecttypes.ClusterMetadata, useDefaultValsInYamls bool) (files []string, err error) {
targetObjs := []runtime.Object{}
if pendingObjs := k8sschema.GetKubernetesObjsInDir(inputPath); len(pendingObjs) != 0 {
for _, apiResource := range apis {
Expand All @@ -72,7 +72,7 @@ func TransformObjsAndPersist(inputPath, outputPath string, apis []IAPIResource,
logrus.Errorf("failed to create deploy directory at path '%s' . Error: %q", outputPath, err)
}
logrus.Debugf("Total %d services to be serialized.", len(targetObjs))
convertedObjs, err := convertVersion(targetObjs, targetCluster.Spec)
convertedObjs, err := convertVersion(targetObjs, targetCluster.Spec, useDefaultValsInYamls)
if err != nil {
logrus.Errorf("Failed to fix, convert and transform the objects. Error: %q", err)
}
Expand Down Expand Up @@ -105,11 +105,11 @@ func writeObjects(outputPath string, objs []runtime.Object) ([]string, error) {
return filesWritten, nil
}

func convertVersion(objs []runtime.Object, clusterSpec collecttypes.ClusterMetadataSpec) ([]runtime.Object, error) {
func convertVersion(objs []runtime.Object, clusterSpec collecttypes.ClusterMetadataSpec, useDefaultValsInYamls bool) ([]runtime.Object, error) {
newobjs := []runtime.Object{}
for _, obj := range objs {
fixedobj := fixer.Fix(obj)
newobj, err := k8sschema.ConvertToSupportedVersion(fixedobj, clusterSpec)
newobj, err := k8sschema.ConvertToSupportedVersion(fixedobj, clusterSpec, useDefaultValsInYamls)
if err != nil {
logrus.Errorf("failed to convert to supported version. Writing as is. Error: %q", err)
newobj = obj
Expand Down
8 changes: 6 additions & 2 deletions transformer/kubernetes/argocdtransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type ArgoCD struct {

// ArgoCDYamlConfig stores the ArgoCD related information
type ArgoCDYamlConfig struct {
OutputPath string `yaml:"outputPath"`
OutputPath string `yaml:"outputPath"`
UseDefaultValuesInYamls bool `yaml:"useDefaultValuesInYamls"`
}

const (
Expand All @@ -60,6 +61,9 @@ func (t *ArgoCD) Init(tc transformertypes.Transformer, env *environment.Environm
if t.ArgoCDConfig.OutputPath == "" {
t.ArgoCDConfig.OutputPath = defaultArgoCDYamlsOutputPath
}
if !t.ArgoCDConfig.UseDefaultValuesInYamls {
t.ArgoCDConfig.UseDefaultValuesInYamls = useDefaultValuesInK8sYamls
}
return nil
}

Expand Down Expand Up @@ -109,7 +113,7 @@ func (t *ArgoCD) Transform(newArtifacts []transformertypes.Artifact, alreadySeen
tempDest := filepath.Join(t.Env.TempPath, deployCICDDir)
logrus.Debugf("Generating ArgoCD yamls for CI/CD")
enhancedIR := t.setupEnhancedIR(ir, t.Env.GetProjectName())
files, err := apiresource.TransformIRAndPersist(enhancedIR, tempDest, resources, clusterConfig)
files, err := apiresource.TransformIRAndPersist(enhancedIR, tempDest, resources, clusterConfig, t.ArgoCDConfig.UseDefaultValuesInYamls)
if err != nil {
logrus.Errorf("failed to transform and persist IR. Error: %q", err)
continue
Expand Down
8 changes: 6 additions & 2 deletions transformer/kubernetes/buildconfigtransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ type BuildConfig struct {

// BuildConfigYamlConfig stores the BuildConfig related information
type BuildConfigYamlConfig struct {
OutputPath string `yaml:"outputPath"`
OutputPath string `yaml:"outputPath"`
UseDefaultValuesInYamls bool `yaml:"useDefaultValuesInYamls"`
}

const (
Expand All @@ -72,6 +73,9 @@ func (t *BuildConfig) Init(tc transformertypes.Transformer, env *environment.Env
if t.BuildConfigConfig.OutputPath == "" {
t.BuildConfigConfig.OutputPath = defaultBuildConfigYamlsOutputPath
}
if !t.BuildConfigConfig.UseDefaultValuesInYamls {
t.BuildConfigConfig.UseDefaultValuesInYamls = useDefaultValuesInK8sYamls
}
return nil
}

Expand Down Expand Up @@ -120,7 +124,7 @@ func (t *BuildConfig) Transform(newArtifacts []transformertypes.Artifact, alread
tempDest := filepath.Join(t.Env.TempPath, deployCICDDir)
logrus.Infof("Generating Buildconfig pipeline for CI/CD")
enhancedIR := t.setupEnhancedIR(ir, t.Env.GetProjectName())
files, err := apiresource.TransformIRAndPersist(enhancedIR, tempDest, apis, clusterConfig)
files, err := apiresource.TransformIRAndPersist(enhancedIR, tempDest, apis, clusterConfig, t.BuildConfigConfig.UseDefaultValuesInYamls)
if err != nil {
logrus.Errorf("Unable to transform and persist IR : %s", err)
return nil, nil, err
Expand Down
26 changes: 17 additions & 9 deletions transformer/kubernetes/k8sschema/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import (
)

// ConvertToSupportedVersion converts obj to a supported Version
func ConvertToSupportedVersion(obj runtime.Object, clusterSpec collecttypes.ClusterMetadataSpec) (runtime.Object, error) {
newobj, err := convertToSupportedVersion(obj, clusterSpec)
func ConvertToSupportedVersion(obj runtime.Object, clusterSpec collecttypes.ClusterMetadataSpec, useDefaultValsInYamls bool) (runtime.Object, error) {
newobj, err := convertToSupportedVersion(obj, clusterSpec, useDefaultValsInYamls)
if err != nil {
logrus.Debugf("Unable to transform object to a supported version : %s.", err)
if obj.GetObjectKind().GroupVersionKind().Version == core.SchemeGroupVersion.Version {
newobj, err = ConvertToPreferredVersion(obj, clusterSpec)
newobj, err = ConvertToPreferredVersion(obj, clusterSpec, useDefaultValsInYamls)
if err != nil {
logrus.Warnf("Unable to convert (%+v) to preferred version : %s", obj.GetObjectKind(), err)
newobj = obj
Expand All @@ -50,7 +50,7 @@ func ConvertToSupportedVersion(obj runtime.Object, clusterSpec collecttypes.Clus
}

// ConvertToSupportedVersion converts obj to a supported Version
func convertToSupportedVersion(obj runtime.Object, clusterSpec collecttypes.ClusterMetadataSpec) (newobj runtime.Object, err error) {
func convertToSupportedVersion(obj runtime.Object, clusterSpec collecttypes.ClusterMetadataSpec, useDefaultValsInYamls bool) (newobj runtime.Object, err error) {
objgvk := obj.GetObjectKind().GroupVersionKind()
objgv := objgvk.GroupVersion()
kind := objgvk.Kind
Expand All @@ -77,15 +77,19 @@ func convertToSupportedVersion(obj runtime.Object, clusterSpec collecttypes.Clus
logrus.Debugf("Unable to convert : %s", err)
continue
}
scheme.Default(newobj)
if useDefaultValsInYamls {
scheme.Default(newobj)
}
return newobj, err
}
scheme.Default(obj)
if useDefaultValsInYamls {
scheme.Default(obj)
}
return obj, fmt.Errorf("unable to convert to a supported version : %+v", obj.GetObjectKind())
}

// ConvertToPreferredVersion converts obj to a preferred Version
func ConvertToPreferredVersion(obj runtime.Object, clusterSpec collecttypes.ClusterMetadataSpec) (newobj runtime.Object, err error) {
func ConvertToPreferredVersion(obj runtime.Object, clusterSpec collecttypes.ClusterMetadataSpec, useDefaultValsInYamls bool) (newobj runtime.Object, err error) {
objgvk := obj.GetObjectKind().GroupVersionKind()
objgv := objgvk.GroupVersion()
kind := objgvk.Kind
Expand All @@ -112,11 +116,15 @@ func ConvertToPreferredVersion(obj runtime.Object, clusterSpec collecttypes.Clus
logrus.Debugf("Unable to convert : %s", err)
continue
}
scheme.Default(newobj)
if useDefaultValsInYamls {
scheme.Default(newobj)
}
return newobj, err
}
}
scheme.Default(obj)
if useDefaultValsInYamls {
scheme.Default(obj)
}
return obj, fmt.Errorf("unable to convert to a preferred version : %+v", obj.GetObjectKind())
}

Expand Down
8 changes: 6 additions & 2 deletions transformer/kubernetes/knativetransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type Knative struct {

// KnativeYamlConfig stores the knative related information
type KnativeYamlConfig struct {
OutputPath string `yaml:"outputPath"`
OutputPath string `yaml:"outputPath"`
UseDefaultValuesInYamls bool `yaml:"useDefaultValuesInYamls"`
}

// Init Initializes the transformer
Expand All @@ -60,6 +61,9 @@ func (t *Knative) Init(tc transformertypes.Transformer, env *environment.Environ
if t.KnativeConfig.OutputPath == "" {
t.KnativeConfig.OutputPath = defaultKnativeYamlsOutputPath
}
if !t.KnativeConfig.UseDefaultValuesInYamls {
t.KnativeConfig.UseDefaultValuesInYamls = useDefaultValuesInK8sYamls
}
return nil
}

Expand Down Expand Up @@ -105,7 +109,7 @@ func (t *Knative) Transform(newArtifacts []transformertypes.Artifact, alreadySee
logrus.Debugf("Starting Kubernetes transform")
logrus.Debugf("Total services to be transformed : %d", len(ir.Services))
apis := []apiresource.IAPIResource{&apiresource.KnativeService{}}
files, err := apiresource.TransformIRAndPersist(irtypes.NewEnhancedIRFromIR(ir), tempDest, apis, clusterConfig)
files, err := apiresource.TransformIRAndPersist(irtypes.NewEnhancedIRFromIR(ir), tempDest, apis, clusterConfig, t.KnativeConfig.UseDefaultValuesInYamls)
if err != nil {
logrus.Errorf("Unable to transform and persist IR : %s", err)
return nil, nil, err
Expand Down
15 changes: 10 additions & 5 deletions transformer/kubernetes/kubernetestransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import (
)

const (
outputPathTemplateName = "OutputPath"
defaultK8sYamlsOutputPath = common.DeployDir + string(os.PathSeparator) + "yamls"
outputPathTemplateName = "OutputPath"
defaultK8sYamlsOutputPath = common.DeployDir + string(os.PathSeparator) + "yamls"
useDefaultValuesInK8sYamls = false
)

// Kubernetes implements Transformer interface
Expand All @@ -46,8 +47,9 @@ type Kubernetes struct {

// KubernetesYamlConfig stores the k8s related information
type KubernetesYamlConfig struct {
IngressName string `yaml:"ingressName"`
OutputPath string `yaml:"outputPath"`
IngressName string `yaml:"ingressName"`
OutputPath string `yaml:"outputPath"`
UseDefaultValuesInK8sYamls bool `yaml:"useDefaultValuesInK8sYamls"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use the same name in all configs? like say setDefaultValuesInYamls?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

// KubernetesPathTemplateConfig implements Kubernetes template config interface
Expand All @@ -72,6 +74,9 @@ func (t *Kubernetes) Init(tc transformertypes.Transformer, e *environment.Enviro
if t.KubernetesConfig.OutputPath == "" {
t.KubernetesConfig.OutputPath = defaultK8sYamlsOutputPath
}
if !t.KubernetesConfig.UseDefaultValuesInK8sYamls {
t.KubernetesConfig.UseDefaultValuesInK8sYamls = useDefaultValuesInK8sYamls
}
return nil
}

Expand Down Expand Up @@ -139,7 +144,7 @@ func (t *Kubernetes) Transform(newArtifacts []transformertypes.Artifact, already
new(apiresource.ImageStream),
new(apiresource.NetworkPolicy),
}
files, err := apiresource.TransformIRAndPersist(irtypes.NewEnhancedIRFromIR(ir), tempDest, apis, clusterConfig)
files, err := apiresource.TransformIRAndPersist(irtypes.NewEnhancedIRFromIR(ir), tempDest, apis, clusterConfig, t.KubernetesConfig.UseDefaultValuesInK8sYamls)
if err != nil {
return nil, nil, fmt.Errorf("failed to transform and persist the IR. Error: %w", err)
}
Expand Down
8 changes: 6 additions & 2 deletions transformer/kubernetes/kubernetesversionchangertransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type KubernetesVersionChanger struct {

// KubernetesVersionChangerYamlConfig stores the config
type KubernetesVersionChangerYamlConfig struct {
OutputPath string `yaml:"outputPath"`
OutputPath string `yaml:"outputPath"`
UseDefaultValuesInYamls bool `yaml:"useDefaultValuesInYamls"`
}

// OutputPathParams stores the possible path params
Expand All @@ -66,6 +67,9 @@ func (t *KubernetesVersionChanger) Init(tc transformertypes.Transformer, e *envi
if t.KVCConfig.OutputPath == "" {
t.KVCConfig.OutputPath = defaultKVCOutputPath
}
if !t.KVCConfig.UseDefaultValuesInYamls {
t.KVCConfig.UseDefaultValuesInYamls = useDefaultValuesInK8sYamls
}
return nil
}

Expand Down Expand Up @@ -123,7 +127,7 @@ func (t *KubernetesVersionChanger) Transform(newArtifacts []transformertypes.Art
return nil
}
if objs := k8sschema.GetKubernetesObjsInDir(path); len(objs) != 0 {
_, err := apiresource.TransformObjsAndPersist(path, filepath.Join(tempDest, relInputPath), apis, clusterConfig)
_, err := apiresource.TransformObjsAndPersist(path, filepath.Join(tempDest, relInputPath), apis, clusterConfig, t.KVCConfig.UseDefaultValuesInYamls)
if err != nil {
logrus.Errorf("Unable to transform objs at %s : %s", path, err)
return nil
Expand Down
8 changes: 6 additions & 2 deletions transformer/kubernetes/tektontransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ type Tekton struct {

// TektonYamlConfig stores the Tekton related information
type TektonYamlConfig struct {
OutputPath string `yaml:"outputPath"`
OutputPath string `yaml:"outputPath"`
UseDefaultValuesInYamls bool `yaml:"useDefaultValuesInYamls"`
}

// Init Initializes the transformer
Expand All @@ -85,6 +86,9 @@ func (t *Tekton) Init(tc transformertypes.Transformer, env *environment.Environm
if t.TektonConfig.OutputPath == "" {
t.TektonConfig.OutputPath = defaultTektonYamlsOutputPath
}
if !t.TektonConfig.UseDefaultValuesInYamls {
t.TektonConfig.UseDefaultValuesInYamls = useDefaultValuesInK8sYamls
}
return nil
}

Expand Down Expand Up @@ -139,7 +143,7 @@ func (t *Tekton) Transform(newArtifacts []transformertypes.Artifact, alreadySeen
tempDest := filepath.Join(t.Env.TempPath, deployCICDDir)
logrus.Debugf("Generating Tekton pipeline for CI/CD")
enhancedIR := t.setupEnhancedIR(ir, t.Env.GetProjectName())
files, err := apiresource.TransformIRAndPersist(enhancedIR, tempDest, resources, clusterConfig)
files, err := apiresource.TransformIRAndPersist(enhancedIR, tempDest, resources, clusterConfig, t.TektonConfig.UseDefaultValuesInYamls)
if err != nil {
logrus.Errorf("Unable to transform and persist IR : %s", err)
return nil, nil, err
Expand Down