Skip to content

Commit

Permalink
refactor: moves conversion to unstr to dedicated package (opendatahub…
Browse files Browse the repository at this point in the history
…-io#1113)

We rely on conversion to Unstructured resources in quite a few places in the code base. With the introduction of a dedicated pkg, we can keep those functions together and reuse them across the code base.

> [!IMPORTANT]
> While converting Kustomize `ResMap` we do not use `MustYaml` func anymore, as this one panics. We attempt to convert to YAML and handle the error instead.

(cherry picked from commit 447f5ad)
  • Loading branch information
bartoszmajsak authored and VaishnaviHire committed Jul 24, 2024
1 parent 86ac280 commit 36d0724
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
52 changes: 52 additions & 0 deletions pkg/conversion/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package conversion

import (
"regexp"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/yaml"
)

const (
resourceSeparator = "(?m)^---[ \t]*$"
)

// StrToUnstructured converts a string containing multiple resources in YAML format to a slice of Unstructured objects.
// The input string is split by "---" separator and each part is unmarshalled into an Unstructured object.
func StrToUnstructured(resources string) ([]*unstructured.Unstructured, error) {
splitter := regexp.MustCompile(resourceSeparator)
objectStrings := splitter.Split(resources, -1)
objs := make([]*unstructured.Unstructured, 0, len(objectStrings))
for _, str := range objectStrings {
if strings.TrimSpace(str) == "" {
continue
}
u := &unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(str), u); err != nil {
return nil, err
}

objs = append(objs, u)
}
return objs, nil
}

// ResMapToUnstructured converts a ResMap to a slice of Unstructured objects.
func ResMapToUnstructured(resMap resmap.ResMap) ([]*unstructured.Unstructured, error) {
resources := make([]*unstructured.Unstructured, 0, resMap.Size())
for _, res := range resMap.Resources() {
u := &unstructured.Unstructured{}
asYAML, errToYAML := res.AsYAML()
if errToYAML != nil {
return nil, errToYAML
}
if errUnmarshal := yaml.Unmarshal(asYAML, u); errUnmarshal != nil {
return nil, errUnmarshal
}
resources = append(resources, u)
}

return resources, nil
}
18 changes: 2 additions & 16 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/kyaml/filesys"

"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/conversion"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/plugins"
Expand Down Expand Up @@ -181,7 +181,7 @@ func DeployManifestsFromPath(
return fmt.Errorf("failed applying labels plugin when preparing Kustomize resources. %w", err)
}

objs, err := GetResources(resMap)
objs, err := conversion.ResMapToUnstructured(resMap)
if err != nil {
return err
}
Expand All @@ -196,20 +196,6 @@ func DeployManifestsFromPath(
return nil
}

func GetResources(resMap resmap.ResMap) ([]*unstructured.Unstructured, error) {
resources := make([]*unstructured.Unstructured, 0, resMap.Size())
for _, res := range resMap.Resources() {
u := &unstructured.Unstructured{}
err := yaml.Unmarshal([]byte(res.MustYaml()), u)
if err != nil {
return nil, err
}
resources = append(resources, u)
}

return resources, nil
}

func manageResource(ctx context.Context, cli client.Client, obj *unstructured.Unstructured, owner metav1.Object, applicationNamespace, componentName string, enabled bool) error {
// Return if resource is of Kind: Namespace and Name: odhApplicationsNamespace
if obj.GetKind() == "Namespace" && obj.GetName() == applicationNamespace {
Expand Down
26 changes: 4 additions & 22 deletions pkg/feature/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"io"
"io/fs"
"path/filepath"
"regexp"
"strings"

"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/conversion"
)

type Manifest interface {
Expand Down Expand Up @@ -43,7 +43,7 @@ func (b *rawManifest) Process(_ any) ([]*unstructured.Unstructured, error) {
}
resources := string(content)

unstructuredObjs, convertErr := convertToUnstructuredSlice(resources)
unstructuredObjs, convertErr := conversion.StrToUnstructured(resources)
if convertErr != nil {
return nil, fmt.Errorf("failed to convert resources defined in %s to unstructured objects: %w", b.path, convertErr)
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (t *templateManifest) Process(data any) ([]*unstructured.Unstructured, erro

resources := buffer.String()

unstructuredObjs, convertErr := convertToUnstructuredSlice(resources)
unstructuredObjs, convertErr := conversion.StrToUnstructured(resources)
if convertErr != nil {
return nil, fmt.Errorf("failed to convert resources defined in %s to unstructured objects: %w", t.path, convertErr)
}
Expand Down Expand Up @@ -163,21 +163,3 @@ func CreateTemplateManifestFrom(fsys fs.FS, path string) *templateManifest {
func isTemplateManifest(path string) bool {
return strings.Contains(filepath.Base(path), ".tmpl.")
}

func convertToUnstructuredSlice(resources string) ([]*unstructured.Unstructured, error) {
splitter := regexp.MustCompile(yamlResourceSeparator)
objectStrings := splitter.Split(resources, -1)
objs := make([]*unstructured.Unstructured, 0, len(objectStrings))
for _, str := range objectStrings {
if strings.TrimSpace(str) == "" {
continue
}
u := &unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(str), u); err != nil {
return nil, err
}

objs = append(objs, u)
}
return objs, nil
}
4 changes: 0 additions & 4 deletions pkg/feature/raw_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
)

const (
yamlResourceSeparator = "(?m)^---[ \t]*$"
)

func applyResources(ctx context.Context, cli client.Client, objects []*unstructured.Unstructured, metaOptions ...cluster.MetaOptions) error {
for _, source := range objects {
target := source.DeepCopy()
Expand Down

0 comments on commit 36d0724

Please sign in to comment.