Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nesmabadr committed Jul 10, 2024
1 parent 83a14db commit 30aea5f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 43 deletions.
10 changes: 6 additions & 4 deletions internal/manifest/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"context"
"errors"
"fmt"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/kyma-project/lifecycle-manager/internal/manifest/filemutex"
"github.com/kyma-project/lifecycle-manager/pkg/img"
"io"
"io/fs"
"os"
"path"
"path/filepath"

"github.com/google/go-containerregistry/pkg/authn"

"github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/kyma-project/lifecycle-manager/internal/manifest/filemutex"
"github.com/kyma-project/lifecycle-manager/pkg/img"
)

type PathExtractor struct {
Expand Down
9 changes: 5 additions & 4 deletions pkg/img/layer.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package img

import (
"context"
"encoding/json"
"errors"
"fmt"
"regexp"

"context"
"errors"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
containerregistryv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/kyma-project/lifecycle-manager/pkg/ocmextensions"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"

"github.com/kyma-project/lifecycle-manager/pkg/ocmextensions"
)

type LayerName string
Expand Down
55 changes: 26 additions & 29 deletions pkg/module/parse/template_to_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import (
"encoding/json"
"errors"
"fmt"
"io"

machineryruntime "k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/yaml"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/kyma-project/lifecycle-manager/internal/descriptor/provider"
"github.com/kyma-project/lifecycle-manager/pkg/img"
"github.com/kyma-project/lifecycle-manager/pkg/log"
"github.com/kyma-project/lifecycle-manager/pkg/module/common"
"github.com/kyma-project/lifecycle-manager/pkg/ocmextensions"
"github.com/kyma-project/lifecycle-manager/pkg/templatelookup"
"io"
machineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

var (
Expand Down Expand Up @@ -241,7 +241,11 @@ func (p *Parser) insertLayerIntoManifest(
CredSecretSelector: ociImage.CredSecretSelector,
}
case img.AssociatedResourcesLayer:
associatedResources, err := p.fetchAssociatedResourcesField(ctx, layer, version)
associatedResourcesLayer, ok := layer.LayerRepresentation.(*img.OCI)
if !ok {
return fmt.Errorf("%w: not an OCIImage", ErrAssociatedResourcesParsing)
}
associatedResources, err := p.fetchAssociatedResourcesField(ctx, associatedResourcesLayer, version)
if err != nil {
return err
}
Expand All @@ -260,31 +264,24 @@ func (p *Parser) insertLayerIntoManifest(
return nil
}

func (p *Parser) fetchAssociatedResourcesField(ctx context.Context, layer img.Layer, version string) ([]string,
error) {
associatedResourcesLayer, ok := layer.LayerRepresentation.(*img.OCI)

// associatedResourcesLayer := v1beta2.ImageSpec{
// Repo: associatedResourcesLayer.Repo,
// Name: associatedResourcesLayer.Name,
// Ref: associatedResourcesLayer.Ref,
// CredSecretSelector: associatedResourcesLayer.CredSecretSelector,
// Type: associatedResourcesLayer.Type,
// }
if !ok {
return nil, fmt.Errorf("%w: not an OCIImage", ErrAssociatedResourcesParsing)
func (p *Parser) fetchAssociatedResourcesField(ctx context.Context, layer *img.OCI, version string) ([]string,
error,
) {
associatedResourcesLayer := v1beta2.ImageSpec{
Repo: layer.Repo,
Name: layer.Name,
Ref: layer.Ref,
CredSecretSelector: layer.CredSecretSelector,
Type: v1beta2.OciRefType,
}

imageRef := fmt.Sprintf("%s/%s:%s@%s", associatedResourcesLayer.Repo, associatedResourcesLayer.Name, version,
associatedResourcesLayer.Ref)
logf.FromContext(ctx).V(log.InfoLevel).Info(fmt.Sprintf("imageRef: %s", imageRef))

// imageRef := fmt.Sprintf("%s/%s@%s", "europe-west3-docker.pkg.dev",
// "sap-kyma-jellyfish-dev/template-operator/component-descriptors/kyma-project.io/module/template-operator:1.0.0-new-ocm-format",
// "sha256:b46281580f6377bf10672b5a8f156d183d47c0ec3bcda8b807bd8c5d520884bd")

// keyChain, err := ocmextensions.LookupKeyChain(ctx, associatedResourcesLayer, p.Client)
keyChain := authn.NewMultiKeychain(google.Keychain, authn.DefaultKeychain)

keyChain, err := ocmextensions.LookupKeyChain(ctx, associatedResourcesLayer, p.Client)
if err != nil {
return nil, fmt.Errorf("failed to fetch keyChain: %w", err)
}
imgLayer, err := img.PullLayer(ctx, imageRef, keyChain)
if err != nil {
logf.FromContext(ctx).V(log.InfoLevel).Info(fmt.Sprintf("failed to pull layer %s: %v", imageRef, err))
Expand Down
11 changes: 7 additions & 4 deletions pkg/module/parse/template_to_module_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package parse_test

import (
"github.com/kyma-project/lifecycle-manager/pkg/module/parse"
"github.com/stretchr/testify/require"
"os"
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/kyma-project/lifecycle-manager/pkg/module/parse"
)

func TestFetchAssociatedResources(t *testing.T) {
Expand All @@ -14,13 +16,14 @@ func TestFetchAssociatedResources(t *testing.T) {
associatedResourcesFile, err := os.Open(associatedResourcesFileName)
require.NoError(t, err)

got, err := parse.FetchAssociatedResources(associatedResourcesFile)
got, err := parse.ReadAssociatedResources(associatedResourcesFile)
require.NoError(t, err)

expectedResources := []string{
"serverless.kyma-project.io/v1alpha2/functions",
"operator.kyma-project.io/v1alpha1/serverlesses",
}
if !reflect.DeepEqual(got, expectedResources) {
t.Errorf("FetchAssociatedResources() got = %v, want %v", got, expectedResources)
}

}
3 changes: 2 additions & 1 deletion pkg/ocmextensions/cred.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/authn/kubernetes"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
apicorev1 "k8s.io/api/core/v1"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kyma-project/lifecycle-manager/api/v1beta2"
)

var ErrNoAuthSecretFound = errors.New("no auth secret found")
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/controller/kyma/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
"strings"

Expand All @@ -27,7 +28,6 @@ import (
"github.com/kyma-project/lifecycle-manager/pkg/img"
. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
"k8s.io/utils/strings/slices"
"os"
)

const (
Expand Down

0 comments on commit 30aea5f

Please sign in to comment.