Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
implement new repository oci naming method (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Schrodi authored Jun 9, 2021
1 parent 25b218c commit c428fad
Show file tree
Hide file tree
Showing 29 changed files with 540 additions and 460 deletions.
7 changes: 7 additions & 0 deletions bindings-go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ install-requirements:
format:
@$(REPO_ROOT)/hack/format.sh $(REPO_ROOT)/apis $(REPO_ROOT)/codec $(REPO_ROOT)/examples

.PHONY: generate
generate:
@$(REPO_ROOT)/hack/generate-code.sh

.PHONY: test
test:
@go test $(REPO_ROOT)/...
Expand All @@ -31,6 +35,9 @@ check:
@echo "Run lint"; golangci-lint run --timeout 10m $(REPO_ROOT)/...
@$(REPO_ROOT)/hack/check.sh $(REPO_ROOT)/apis $(REPO_ROOT)/codec $(REPO_ROOT)/examples

.PHONY: verify
verify: check test

.PHONY: revendor
revendor:
@go mod tidy
Expand Down
26 changes: 23 additions & 3 deletions bindings-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ component:
provider: internal
repositoryContexts: []
repositoryContexts:
- type: ociRegistry
baseUrl: example.com
sources: []
componentReferences: []
Expand Down Expand Up @@ -77,6 +79,25 @@ component:
}
```

##### Repository Context

:warning: Note that the following examples use the above described component descriptor.

```go
component := &v2.ComponentDescriptor{}
err := codec.Decode(data, component)
check(err)

// get the latest repository context.
// the context is returned as unstructured object (similar to the access types) as differnt repository types
// with different attributes are possible.
unstructuredRepoCtx := component.GetEffectiveRepositoryContext()
// decode the unstructured type into a specific type
ociRepo := &v2.OCIRegistryRepository{}
check(unstructuredRepoCtx.DecodeInto(ociRepo))
fmt.Printf("%s\n", ociRepo.BaseURL) // prints "example.com"
```

##### Select Resources

:warning: Note that the following examples use the above described component descriptor.
Expand All @@ -99,9 +120,8 @@ fmt.Printf("%#v\n", res)

// get the access for a resource
// specific access type can be decoded using the access type codec.
accessTypeCodec := v2.NewCodec(nil, nil, nil)
ociAccess := &v2.OCIRegistryAccess{}
check(accessTypeCodec.Decode(res.Access.Raw, ociAccess))
check(res.Access.DecodeInto(ociAccess))
fmt.Println(ociAccess.ImageReference) // prints: k8s.gcr.io/hyperkube:v1.16.4
```

Expand Down
125 changes: 10 additions & 115 deletions bindings-go/apis/v2/accesstypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@

package v2

import (
"encoding/json"

"github.com/ghodss/yaml"
)

// KnownAccessTypes contains all known access serializer
var KnownAccessTypes = KnownTypes{
OCIRegistryType: DefaultJSONTypedObjectCodec,
OCIBlobType: DefaultJSONTypedObjectCodec,
GitHubAccessType: DefaultJSONTypedObjectCodec,
WebType: DefaultJSONTypedObjectCodec,
LocalFilesystemBlobType: DefaultJSONTypedObjectCodec,
OCIRegistryType: DefaultJSONTypedObjectCodec,
OCIBlobType: DefaultJSONTypedObjectCodec,
RelativeOciReferenceType: DefaultJSONTypedObjectCodec,
GitHubAccessType: DefaultJSONTypedObjectCodec,
WebType: DefaultJSONTypedObjectCodec,
LocalFilesystemBlobType: DefaultJSONTypedObjectCodec,
}

// OCIRegistryType is the access type of a oci registry.
Expand All @@ -50,24 +45,10 @@ func NewOCIRegistryAccess(ref string) TypedObjectAccessor {
}
}

func (_ *OCIRegistryAccess) GetType() string {
func (a *OCIRegistryAccess) GetType() string {
return OCIRegistryType
}

func (O OCIRegistryAccess) GetData() ([]byte, error) {
return json.Marshal(O)
}

func (O *OCIRegistryAccess) SetData(bytes []byte) error {
var newOCIImage OCIRegistryAccess
if err := json.Unmarshal(bytes, &newOCIImage); err != nil {
return err
}

O.ImageReference = newOCIImage.ImageReference
return nil
}

// RelativeOciReferenceType is the access type of a relative oci reference.
const RelativeOciReferenceType = "relativeOciReference"

Expand All @@ -93,20 +74,6 @@ func (_ *RelativeOciAccess) GetType() string {
return RelativeOciReferenceType
}

func (O RelativeOciAccess) GetData() ([]byte, error) {
return json.Marshal(O)
}

func (O *RelativeOciAccess) SetData(bytes []byte) error {
var newRelativeOCIImage RelativeOciAccess
if err := json.Unmarshal(bytes, &newRelativeOCIImage); err != nil {
return err
}

O.Reference = newRelativeOCIImage.Reference
return nil
}

// OCIBlobType is the access type of a oci blob in a manifest.
const OCIBlobType = "ociBlob"

Expand Down Expand Up @@ -144,23 +111,6 @@ func (_ *OCIBlobAccess) GetType() string {
return OCIBlobType
}

func (a OCIBlobAccess) GetData() ([]byte, error) {
return json.Marshal(a)
}

func (a *OCIBlobAccess) SetData(bytes []byte) error {
var newOCILayer OCIBlobAccess
if err := json.Unmarshal(bytes, &newOCILayer); err != nil {
return err
}

a.Reference = newOCILayer.Reference
a.MediaType = newOCILayer.MediaType
a.Digest = newOCILayer.Digest
a.Size = newOCILayer.Size
return nil
}

// LocalOCIBlobType is the access type of a oci blob in the current component descriptor manifest.
const LocalOCIBlobType = "localOciBlob"

Expand All @@ -185,20 +135,7 @@ func (_ *LocalOCIBlobAccess) GetType() string {
return LocalOCIBlobType
}

func (a LocalOCIBlobAccess) GetData() ([]byte, error) {
return json.Marshal(a)
}

func (a *LocalOCIBlobAccess) SetData(bytes []byte) error {
var newAccess OCIBlobAccess
if err := json.Unmarshal(bytes, &newAccess); err != nil {
return err
}
a.Digest = newAccess.Digest
return nil
}

// LocalBlobType is the access type of a oci blob in a manifest.
// LocalFilesystemBlobType is the access type of a blob in a local filesystem.
const LocalFilesystemBlobType = "localFilesystemBlob"

// NewLocalFilesystemBlobAccess creates a new localFilesystemBlob accessor.
Expand Down Expand Up @@ -226,19 +163,6 @@ func (_ *LocalFilesystemBlobAccess) GetType() string {
return LocalFilesystemBlobType
}

func (a LocalFilesystemBlobAccess) GetData() ([]byte, error) {
return json.Marshal(a)
}

func (a *LocalFilesystemBlobAccess) SetData(bytes []byte) error {
var newAccess LocalFilesystemBlobAccess
if err := json.Unmarshal(bytes, &newAccess); err != nil {
return err
}
a.Filename = newAccess.Filename
return nil
}

// WebType is the type of a web component
const WebType = "web"

Expand All @@ -264,24 +188,10 @@ func (_ *Web) GetType() string {
return WebType
}

func (w Web) GetData() ([]byte, error) {
return yaml.Marshal(w)
}

func (w *Web) SetData(bytes []byte) error {
var newWeb Web
if err := json.Unmarshal(bytes, &newWeb); err != nil {
return err
}

w.URL = newWeb.URL
return nil
}

// WebType is the type of a web component
// GitHubAccessType is the type of a git object.
const GitHubAccessType = "github"

// GitHubAccess describes a github respository resource access.
// GitHubAccess describes a github repository resource access.
type GitHubAccess struct {
ObjectType `json:",inline"`

Expand Down Expand Up @@ -309,18 +219,3 @@ func NewGitHubAccess(url, ref, commit string) TypedObjectAccessor {
func (a GitHubAccess) GetType() string {
return GitHubAccessType
}

func (a GitHubAccess) GetData() ([]byte, error) {
return yaml.Marshal(a)
}

func (a *GitHubAccess) SetData(bytes []byte) error {
var newGitHubAccess GitHubAccess
if err := json.Unmarshal(bytes, &newGitHubAccess); err != nil {
return err
}

a.RepoURL = newGitHubAccess.RepoURL
a.Ref = newGitHubAccess.Ref
return nil
}
7 changes: 1 addition & 6 deletions bindings-go/apis/v2/cdutils/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ func GetImageReferenceByName(cd *cdv2.ComponentDescriptor, name string) (string,
if res.Access.GetType() != cdv2.OCIRegistryType {
return "", fmt.Errorf("resource is expected to be of type %q but is of type %q", cdv2.OCIRegistryType, res.Access.GetType())
}

data, err := res.Access.GetData()
if err != nil {
return "", err
}
ociImageAccess := &cdv2.OCIRegistryAccess{}
if err := cdv2.NewDefaultCodec().Decode(data, ociImageAccess); err != nil {
if err := res.Access.DecodeInto(ociImageAccess); err != nil {
return "", err
}
return ociImageAccess.ImageReference, nil
Expand Down
27 changes: 0 additions & 27 deletions bindings-go/apis/v2/cdutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cdutils

import (
"encoding/json"
"fmt"

v2 "github.com/gardener/component-spec/bindings-go/apis/v2"
)
Expand Down Expand Up @@ -121,29 +120,3 @@ func SetExtraIdentityField(o *v2.IdentityObjectMeta, key, val string) {
}
o.ExtraIdentity[key] = val
}

// ToUnstructuredTypedObject converts a typed object to a unstructured object.
func ToUnstructuredTypedObject(codec v2.TypedObjectCodec, obj v2.TypedObjectAccessor) (*v2.UnstructuredAccessType, error) {
data, err := codec.Encode(obj)
if err != nil {
return nil, err
}

uObj := &v2.UnstructuredAccessType{}
if err := uObj.Decode(data, uObj); err != nil {
return nil, err
}
return uObj, nil
}

// FromUnstructuredObject converts a unstructured object into a typed object.
func FromUnstructuredObject(codec v2.TypedObjectCodec, uObj *v2.UnstructuredAccessType, obj v2.TypedObjectAccessor) error {
data, err := uObj.GetData()
if err != nil {
return fmt.Errorf("unable to get data from unstructured object: %w", err)
}
if err := codec.Decode(data, obj); err != nil {
return fmt.Errorf("unable to decode object %q into %q: %w", uObj.GetType(), obj.GetType(), err)
}
return err
}
42 changes: 27 additions & 15 deletions bindings-go/apis/v2/codecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package v2
import (
"encoding/json"
"fmt"
"strings"
)

// KnownTypeValidationFunc defines a function that can validate types.
Expand Down Expand Up @@ -99,23 +98,10 @@ var _ TypedObjectEncoder = DefaultJSONTypedObjectEncoder{}

// Encode is the Encode implementation of the TypedObjectEncoder interface.
func (e DefaultJSONTypedObjectEncoder) Encode(obj TypedObjectAccessor) ([]byte, error) {
obj.SetType(obj.GetType()) // hardcord the correct type if the type was not correctly constructed.
obj.SetType(obj.GetType()) // hardcode the correct type if the type was not correctly constructed.
return json.Marshal(obj)
}

// ValidateAccessType validates that a type is known or of a generic type.
// todo: revisit; currently "x-" specifies a generic type
func ValidateAccessType(ttype string) error {
if _, ok := KnownAccessTypes[ttype]; ok {
return nil
}

if !strings.HasPrefix(ttype, "x-") {
return fmt.Errorf("unknown non generic types %s", ttype)
}
return nil
}

type codec struct {
knownTypes KnownTypes
defaultCodec TypedObjectCodec
Expand Down Expand Up @@ -189,3 +175,29 @@ func (c *codec) Encode(acc TypedObjectAccessor) ([]byte, error) {

return codec.Encode(acc)
}

// ToUnstructuredTypedObject converts a typed object to a unstructured object.
func ToUnstructuredTypedObject(codec TypedObjectCodec, obj TypedObjectAccessor) (*UnstructuredTypedObject, error) {
data, err := codec.Encode(obj)
if err != nil {
return nil, err
}

uObj := &UnstructuredTypedObject{}
if err := json.Unmarshal(data, uObj); err != nil {
return nil, err
}
return uObj, nil
}

// FromUnstructuredObject converts a unstructured object into a typed object.
func FromUnstructuredObject(codec TypedObjectCodec, uObj *UnstructuredTypedObject, obj TypedObjectAccessor) error {
data, err := uObj.GetRaw()
if err != nil {
return fmt.Errorf("unable to get data from unstructured object: %w", err)
}
if err := codec.Decode(data, obj); err != nil {
return fmt.Errorf("unable to decode object %q into %q: %w", uObj.GetType(), obj.GetType(), err)
}
return err
}
Loading

0 comments on commit c428fad

Please sign in to comment.