Skip to content

Commit

Permalink
fix: bug where all yaml files were treated as K8s yamls by the Parame…
Browse files Browse the repository at this point in the history
…terizer (#1162)

Signed-off-by: Harikrishnan Balagopal <[email protected]>
  • Loading branch information
HarikrishnanBalagopal authored Mar 21, 2024
1 parent 14357a9 commit d049f65
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
51 changes: 43 additions & 8 deletions transformer/kubernetes/k8sschema/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/google/go-cmp/cmp"
"github.com/konveyor/move2kube/common"
Expand Down Expand Up @@ -49,25 +50,32 @@ func Intersection(objs1 []runtime.Object, objs2 []runtime.Object) []runtime.Obje
return objs
}

// GetInfoFromK8sResource returns some useful information given a k8s resource
func GetInfoFromK8sResource(k8sResource K8sResourceT) (kind, apiVersion, name string, err error) {
logrus.Trace("start getInfoFromK8sResource")
defer logrus.Trace("end getInfoFromK8sResource")
// GetOnlyGVKInfoFromK8sResource returns the Group Version Kind information given a k8s resource
func GetOnlyGVKInfoFromK8sResource(k8sResource K8sResourceT) (kind, apiVersion string, err error) {
kindI, ok := k8sResource["kind"]
if !ok {
return "", "", "", fmt.Errorf("there is no kind specified in the k8s resource %+v", k8sResource)
return "", "", fmt.Errorf("there is no kind specified in the k8s resource %+v", k8sResource)
}
kind, ok = kindI.(string)
if !ok {
return "", "", "", fmt.Errorf("expected kind to be of type string. Actual value %+v is of type %T", kindI, kindI)
return "", "", fmt.Errorf("expected kind to be of type string. Actual value %+v is of type %T", kindI, kindI)
}
apiVersionI, ok := k8sResource["apiVersion"]
if !ok {
return kind, "", "", fmt.Errorf("there is no apiVersion specified in the k8s resource %+v", k8sResource)
return kind, "", fmt.Errorf("there is no apiVersion specified in the k8s resource %+v", k8sResource)
}
apiVersion, ok = apiVersionI.(string)
if !ok {
return kind, "", "", fmt.Errorf("expected apiVersion to be of type string. Actual value %+v is of type %T", apiVersionI, apiVersionI)
return kind, "", fmt.Errorf("expected apiVersion to be of type string. Actual value %+v is of type %T", apiVersionI, apiVersionI)
}
return kind, apiVersion, nil
}

// GetInfoFromK8sResource returns some useful information given a k8s resource
func GetInfoFromK8sResource(k8sResource K8sResourceT) (kind, apiVersion, name string, err error) {
kind, apiVersion, err = GetOnlyGVKInfoFromK8sResource(k8sResource)
if err != nil {
return "", "", "", err
}
metadataI, ok := k8sResource["metadata"]
if !ok {
Expand Down Expand Up @@ -158,6 +166,13 @@ func GetK8sResourcesFromYaml(k8sYaml string) ([]K8sResourceT, error) {
if err := yaml.Unmarshal([]byte(k8sYaml), &resourceI); err != nil {
return nil, fmt.Errorf("failed to unmarshal the string '%s' as YAML. Error: %w", k8sYaml, err)
}
resourceMap, ok := resourceI.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected the YAML to be a map[string]interface{} . Actual type %T value %+v", resourceI, resourceI)
}
if _, _, err := GetOnlyGVKInfoFromK8sResource(resourceMap); err != nil {
return nil, fmt.Errorf("failed to get the Group Version Kind info from the YAML. Error: %w", err)
}
resourceJSONBytes, err := json.Marshal(resourceI)
if err != nil {
return nil, fmt.Errorf("failed to marshal the K8s resource as JSON. K8s resource: %+v Error: %w", resourceI, err)
Expand Down Expand Up @@ -197,3 +212,23 @@ func GetKubernetesObjsInDir(dir string) []runtime.Object {
}
return objs
}

// GetGVKNFromK gets the GVK and metadata.name information for a K8s resource
func GetGVKNFromK(k K8sResourceT) (group string, version string, kind string, metadataName string, err error) {
var apiVersion string
kind, apiVersion, metadataName, err = GetInfoFromK8sResource(k)
if err != nil {
return kind, "", "", metadataName, err
}
t1s := strings.Split(apiVersion, "/")
if len(t1s) == 0 || len(t1s) > 2 {
err = fmt.Errorf("failed to get group and version from %s", apiVersion)
return kind, apiVersion, apiVersion, metadataName, err
}
if len(t1s) == 1 {
version = t1s[0]
} else if len(t1s) == 2 {
group, version = t1s[0], t1s[1]
}
return group, version, kind, metadataName, nil
}
27 changes: 4 additions & 23 deletions transformer/kubernetes/parameterizer/parameterizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Parameterize(srcDir, outDir string, packSpecConfig ParameterizerConfigT, ps
packSpecConfig.Envs = normEnvs
pathedKs, err := k8sschema.GetK8sResourcesWithPaths(cleanSrcDir, false)
if err != nil {
return filesWritten, err
return filesWritten, fmt.Errorf("failed to get the K8s resources in the directory '%s' . Error: %w", cleanSrcDir, err)
}
if packSpecConfig.Helm != "" {
// helm chart with multiple values.yaml
Expand Down Expand Up @@ -152,12 +152,12 @@ func Parameterize(srcDir, outDir string, packSpecConfig ParameterizerConfigT, ps
// compute the json patch
currKustPatches := map[string]map[string]PatchT{} // keyed by env and json pointer/path
if err := parameterize(TargetKustomize, packSpecConfig.Envs, k, ps, nil, currKustPatches, nil); err != nil {
logrus.Errorf("Unable to parameterize %s : %s", finalKPath, err)
logrus.Errorf("failed to parameterize for Kustomize the path '%s' . Error: %q", finalKPath, err)
}
// patch metadata to put in kustomization.yaml
group, version, kind, metadataName, err := getGVKNFromK(k)
group, version, kind, metadataName, err := k8sschema.GetGVKNFromK(k)
if err != nil {
logrus.Errorf("Unable to get GVK info for %s : %s", finalKPath, err)
logrus.Errorf("failed to get the Group Version Kind info from the YAML at path '%s' . Error: %q", finalKPath, err)
continue
}
patchFilename := fmt.Sprintf("%s-%s-%s-%s.yaml", group, version, kind, metadataName)
Expand Down Expand Up @@ -278,25 +278,6 @@ func Parameterize(srcDir, outDir string, packSpecConfig ParameterizerConfigT, ps
// ------------------------------
// Utilities

func getGVKNFromK(k k8sschema.K8sResourceT) (group string, version string, kind string, metadataName string, err error) {
var apiVersion string
kind, apiVersion, metadataName, err = k8sschema.GetInfoFromK8sResource(k)
if err != nil {
return kind, "", "", metadataName, err
}
t1s := strings.Split(apiVersion, "/")
if len(t1s) == 0 || len(t1s) > 2 {
err = fmt.Errorf("failed to get group and version from %s", apiVersion)
return kind, apiVersion, apiVersion, metadataName, err
}
if len(t1s) == 1 {
version = t1s[0]
} else if len(t1s) == 2 {
group, version = t1s[0], t1s[1]
}
return group, version, kind, metadataName, nil
}

func getParameters(templ string) ([]string, error) {
matches := stringInterpRegex.FindAllStringSubmatch(templ, -1)
if len(matches) == 0 {
Expand Down

0 comments on commit d049f65

Please sign in to comment.