Skip to content

Commit

Permalink
Merge pull request #78 from yangcao77/355-kubeCRD-reference
Browse files Browse the repository at this point in the history
parse parent via kube crd reference
  • Loading branch information
yangcao77 authored Apr 13, 2021
2 parents 7a52b22 + d135abf commit ffe3a0a
Show file tree
Hide file tree
Showing 12 changed files with 800 additions and 228 deletions.
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.4.0
github.com/google/go-cmp v0.5.2
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
github.com/mattn/go-colorable v0.1.2 // indirect
Expand All @@ -17,8 +17,10 @@ require (
github.com/spf13/afero v1.2.2
github.com/stretchr/testify v1.6.1
github.com/xeipuuv/gojsonschema v1.2.0
k8s.io/api v0.19.0
k8s.io/apimachinery v0.19.0
k8s.io/api v0.19.2
k8s.io/apimachinery v0.19.2
k8s.io/client-go v0.19.2
k8s.io/klog v1.0.0
sigs.k8s.io/controller-runtime v0.7.0
sigs.k8s.io/yaml v1.2.0
)
225 changes: 225 additions & 0 deletions go.sum

Large diffs are not rendered by default.

49 changes: 9 additions & 40 deletions pkg/devfile/parser/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ type DevfileCtx struct {

// filesystem for devfile
fs filesystem.Filesystem

// trace of all url referenced
uriMap map[string]bool

// registry URLs list
registryURLs []string
}

// NewDevfileCtx returns a new DevfileCtx type object
Expand All @@ -58,6 +52,15 @@ func NewURLDevfileCtx(url string) DevfileCtx {
}
}

// NewByteContentDevfileCtx set devfile content from byte data and returns a new DevfileCtx type object and error
func NewByteContentDevfileCtx(data []byte) (d DevfileCtx, err error) {
err = d.SetDevfileContentFromBytes(data)
if err != nil {
return DevfileCtx{}, err
}
return d, nil
}

// populateDevfile checks the API version is supported and returns the JSON schema for the given devfile API Version
func (d *DevfileCtx) populateDevfile() (err error) {

Expand Down Expand Up @@ -87,13 +90,6 @@ func (d *DevfileCtx) Populate() (err error) {
return err
}
klog.V(4).Infof("absolute devfile path: '%s'", d.absPath)
if d.uriMap == nil {
d.uriMap = make(map[string]bool)
}
if d.uriMap[d.absPath] {
return fmt.Errorf("URI %v is recursively referenced", d.absPath)
}
d.uriMap[d.absPath] = true
// Read and save devfile content
if err := d.SetDevfileContent(); err != nil {
return err
Expand All @@ -107,13 +103,6 @@ func (d *DevfileCtx) PopulateFromURL() (err error) {
if err != nil {
return err
}
if d.uriMap == nil {
d.uriMap = make(map[string]bool)
}
if d.uriMap[d.url] {
return fmt.Errorf("URI %v is recursively referenced", d.url)
}
d.uriMap[d.url] = true
// Read and save devfile content
if err := d.SetDevfileContent(); err != nil {
return err
Expand Down Expand Up @@ -154,23 +143,3 @@ func (d *DevfileCtx) SetAbsPath() (err error) {
return nil

}

// GetURIMap func returns current devfile uri map
func (d *DevfileCtx) GetURIMap() map[string]bool {
return d.uriMap
}

// SetURIMap set uri map in the devfile ctx
func (d *DevfileCtx) SetURIMap(uriMap map[string]bool) {
d.uriMap = uriMap
}

// GetRegistryURLs func returns current devfile registry URLs
func (d *DevfileCtx) GetRegistryURLs() []string {
return d.registryURLs
}

// SetRegistryURLs set registry URLs in the devfile ctx
func (d *DevfileCtx) SetRegistryURLs(registryURLs []string) {
d.registryURLs = registryURLs
}
6 changes: 3 additions & 3 deletions pkg/devfile/parser/data/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestNewDevfileData(t *testing.T) {
t.Run("valid devfile apiVersion", func(t *testing.T) {

var (
version = APIVersion200
version = APISchemaVersion200
want = reflect.TypeOf(&v2.DevfileV2{})
obj, err = NewDevfileData(string(version))
got = reflect.TypeOf(obj)
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestGetDevfileJSONSchema(t *testing.T) {
t.Run("valid devfile apiVersion", func(t *testing.T) {

var (
version = APIVersion200
version = APISchemaVersion200
want = v200.JsonSchema200
got, err = GetDevfileJSONSchema(string(version))
)
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestIsApiVersionSupported(t *testing.T) {
t.Run("valid devfile apiVersion", func(t *testing.T) {

var (
version = APIVersion200
version = APISchemaVersion200
want = true
got = IsApiVersionSupported(string(version))
)
Expand Down
5 changes: 3 additions & 2 deletions pkg/devfile/parser/data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ type DevfileData interface {
GetVolumeMountPaths(mountName, containerName string) ([]string, error)

// workspace related methods
GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent
SetDevfileWorkspace(content v1.DevWorkspaceTemplateSpecContent)
GetDevfileWorkspaceSpecContent() *v1.DevWorkspaceTemplateSpecContent
SetDevfileWorkspaceSpecContent(content v1.DevWorkspaceTemplateSpecContent)
SetDevfileWorkspaceSpec(spec v1.DevWorkspaceTemplateSpec)

// utils
GetDevfileContainerComponents(common.DevfileOptions) ([]v1.Component, error)
Expand Down
13 changes: 9 additions & 4 deletions pkg/devfile/parser/data/v2/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
)

// GetDevfileWorkspace returns the workspace content for the devfile
func (d *DevfileV2) GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent {
// GetDevfileWorkspaceSpecContent returns the workspace spec content for the devfile
func (d *DevfileV2) GetDevfileWorkspaceSpecContent() *v1.DevWorkspaceTemplateSpecContent {

return &d.DevWorkspaceTemplateSpecContent
}

// SetDevfileWorkspace sets the workspace content
func (d *DevfileV2) SetDevfileWorkspace(content v1.DevWorkspaceTemplateSpecContent) {
// SetDevfileWorkspaceSpecContent sets the workspace spec content
func (d *DevfileV2) SetDevfileWorkspaceSpecContent(content v1.DevWorkspaceTemplateSpecContent) {
d.DevWorkspaceTemplateSpecContent = content
}

// SetDevfileWorkspaceSpec sets the workspace spec
func (d *DevfileV2) SetDevfileWorkspaceSpec(spec v1.DevWorkspaceTemplateSpec) {
d.DevWorkspaceTemplateSpec = spec
}
6 changes: 3 additions & 3 deletions pkg/devfile/parser/data/v2/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
)

func TestDevfile200_SetDevfileWorkspace(t *testing.T) {
func TestDevfile200_SetDevfileWorkspaceSpecContent(t *testing.T) {

type args struct {
name string
Expand Down Expand Up @@ -65,9 +65,9 @@ func TestDevfile200_SetDevfileWorkspace(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.devfilev2.SetDevfileWorkspace(tt.workspace)
tt.devfilev2.SetDevfileWorkspaceSpecContent(tt.workspace)
if !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) {
t.Errorf("TestDevfile200_SetDevfileWorkspace() expected %v, got %v", tt.expectedDevfilev2, tt.devfilev2)
t.Errorf("TestDevfile200_SetDevfileWorkspaceSpecContent() expected %v, got %v", tt.expectedDevfilev2, tt.devfilev2)
}
})
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/devfile/parser/data/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ type supportedApiVersion string

// Supported devfile API versions
const (
APIVersion200 supportedApiVersion = "2.0.0"
APIVersion210 supportedApiVersion = "2.1.0"
APISchemaVersion200 supportedApiVersion = "2.0.0"
APISchemaVersion210 supportedApiVersion = "2.1.0"
APIVersionAlpha2 supportedApiVersion = "v1alpha2"
)

// ------------- Init functions ------------- //
Expand All @@ -25,8 +26,9 @@ var apiVersionToDevfileStruct map[supportedApiVersion]reflect.Type
// Initializes a map of supported devfile api versions and devfile structs
func init() {
apiVersionToDevfileStruct = make(map[supportedApiVersion]reflect.Type)
apiVersionToDevfileStruct[APIVersion200] = reflect.TypeOf(v2.DevfileV2{})
apiVersionToDevfileStruct[APIVersion210] = reflect.TypeOf(v2.DevfileV2{})
apiVersionToDevfileStruct[APISchemaVersion200] = reflect.TypeOf(v2.DevfileV2{})
apiVersionToDevfileStruct[APISchemaVersion210] = reflect.TypeOf(v2.DevfileV2{})
apiVersionToDevfileStruct[APIVersionAlpha2] = reflect.TypeOf(v2.DevfileV2{})
}

// Map to store mappings between supported devfile API versions and respective devfile JSON schemas
Expand All @@ -35,6 +37,8 @@ var devfileApiVersionToJSONSchema map[supportedApiVersion]string
// init initializes a map of supported devfile apiVersions with it's respective devfile JSON schema
func init() {
devfileApiVersionToJSONSchema = make(map[supportedApiVersion]string)
devfileApiVersionToJSONSchema[APIVersion200] = v200.JsonSchema200
devfileApiVersionToJSONSchema[APIVersion210] = v210.JsonSchema210
devfileApiVersionToJSONSchema[APISchemaVersion200] = v200.JsonSchema200
devfileApiVersionToJSONSchema[APISchemaVersion210] = v210.JsonSchema210
// should use hightest v2 schema version since it is expected to be backward compatible with the same api version
devfileApiVersionToJSONSchema[APIVersionAlpha2] = v210.JsonSchema210
}
Loading

0 comments on commit ffe3a0a

Please sign in to comment.