-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Watch for devfile dependencies (#6020)
* GetReferencedLocalFiles * Watch for all devfile dependencies * Integration test
- Loading branch information
Showing
16 changed files
with
942 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
schemaVersion: 2.1.0 | ||
metadata: | ||
name: child | ||
parent: | ||
uri: parent/parent-devfile-components-only.yaml | ||
commands: | ||
- exec: | ||
commandLine: GOCACHE=${PROJECT_SOURCE}/.cache go build main.go | ||
component: runtime | ||
group: | ||
isDefault: true | ||
kind: build | ||
workingDir: ${PROJECT_SOURCE} | ||
id: build | ||
- exec: | ||
commandLine: ./main | ||
component: runtime | ||
group: | ||
isDefault: true | ||
kind: run | ||
workingDir: ${PROJECT_SOURCE} | ||
id: run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
commands: | ||
- exec: | ||
commandLine: GOCACHE=${PROJECT_SOURCE}/.cache go build main.go | ||
component: runtime | ||
group: | ||
isDefault: true | ||
kind: build | ||
workingDir: ${PROJECT_SOURCE} | ||
id: build | ||
- exec: | ||
commandLine: ./main | ||
component: runtime | ||
group: | ||
isDefault: true | ||
kind: run | ||
workingDir: ${PROJECT_SOURCE} | ||
id: run | ||
components: | ||
- container: | ||
endpoints: | ||
- name: http | ||
targetPort: 8080 | ||
image: quay.io/devfile/golang:latest | ||
memoryLimit: 1024Mi | ||
mountSources: true | ||
name: runtime | ||
- kubernetes: | ||
uri: "manifest.yaml" | ||
name: kube-cmp | ||
metadata: | ||
name: my-go-app | ||
schemaVersion: 2.1.0 | ||
parent: | ||
uri: parent/parent-devfile-empty.yaml |
17 changes: 17 additions & 0 deletions
17
pkg/libdevfile/testdata/child-devfile-components-only.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
schemaVersion: 2.1.0 | ||
metadata: | ||
name: child | ||
parent: | ||
uri: "parent/parent-devfile-commands-only.yaml" | ||
components: | ||
- container: | ||
endpoints: | ||
- name: http | ||
targetPort: 8080 | ||
image: quay.io/devfile/golang:latest | ||
memoryLimit: 1024Mi | ||
mountSources: true | ||
name: runtime | ||
- kubernetes: | ||
uri: "manifest.yaml" | ||
name: kube-cmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
schemaVersion: 2.1.0 | ||
metadata: | ||
name: child | ||
parent: | ||
uri: "parent/parent-devfile.yaml" |
20 changes: 20 additions & 0 deletions
20
pkg/libdevfile/testdata/parent/parent-devfile-commands-only.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
commands: | ||
- exec: | ||
commandLine: GOCACHE=${PROJECT_SOURCE}/.cache go build main.go | ||
component: runtime | ||
group: | ||
isDefault: true | ||
kind: build | ||
workingDir: ${PROJECT_SOURCE} | ||
id: build | ||
- exec: | ||
commandLine: ./main | ||
component: runtime | ||
group: | ||
isDefault: true | ||
kind: run | ||
workingDir: ${PROJECT_SOURCE} | ||
id: run | ||
schemaVersion: 2.1.0 | ||
metadata: | ||
name: parent |
15 changes: 15 additions & 0 deletions
15
pkg/libdevfile/testdata/parent/parent-devfile-components-only.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
components: | ||
- container: | ||
endpoints: | ||
- name: http | ||
targetPort: 8080 | ||
image: quay.io/devfile/golang:latest | ||
memoryLimit: 1024Mi | ||
mountSources: true | ||
name: runtime | ||
- kubernetes: | ||
uri: "manifest.yaml" | ||
name: kube-cmp | ||
metadata: | ||
name: my-go-app | ||
schemaVersion: 2.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
metadata: | ||
name: my-go-app | ||
schemaVersion: 2.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
commands: | ||
- exec: | ||
commandLine: GOCACHE=${PROJECT_SOURCE}/.cache go build main.go | ||
component: runtime | ||
group: | ||
isDefault: true | ||
kind: build | ||
workingDir: ${PROJECT_SOURCE} | ||
id: build | ||
- exec: | ||
commandLine: ./main | ||
component: runtime | ||
group: | ||
isDefault: true | ||
kind: run | ||
workingDir: ${PROJECT_SOURCE} | ||
id: run | ||
components: | ||
- container: | ||
endpoints: | ||
- name: http | ||
targetPort: 8080 | ||
image: quay.io/devfile/golang:latest | ||
memoryLimit: 1024Mi | ||
mountSources: true | ||
name: runtime | ||
- kubernetes: | ||
uri: "manifest.yaml" | ||
name: kube-cmp | ||
metadata: | ||
name: my-go-app | ||
schemaVersion: 2.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package libdevfile | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" | ||
"github.com/devfile/api/v2/pkg/attributes" | ||
"github.com/devfile/api/v2/pkg/validation" | ||
"github.com/devfile/library/pkg/devfile/parser" | ||
"github.com/devfile/library/pkg/devfile/parser/data/v2/common" | ||
) | ||
|
||
const _importSourceAttributeUriPrefix = "uri: " | ||
|
||
// GetReferencedLocalFiles returns the local files referenced by the Devfile. This includes: | ||
// - the non-inlined Kubernetes and Openshift components | ||
// - the Dockerfiles of Image components | ||
// - the parent devfile | ||
// - resursively, the local files referenced by the parent Devfile | ||
// The passed Devfile must be flattened | ||
func GetReferencedLocalFiles(devfileObj parser.DevfileObj) (result []string, err error) { | ||
|
||
setResult := map[string]struct{}{} | ||
|
||
parent := devfileObj.Data.GetParent() | ||
if parent != nil { | ||
return nil, errors.New("devfile must be flattened") | ||
} | ||
|
||
components, err := devfileObj.Data.GetComponents(common.DevfileOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, component := range components { | ||
var componentType v1alpha2.ComponentType | ||
componentType, err = common.GetComponentType(component) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch componentType { | ||
case v1alpha2.KubernetesComponentType: | ||
setResult, err = appendUriIfFile(setResult, component.Kubernetes.Uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
case v1alpha2.OpenshiftComponentType: | ||
setResult, err = appendUriIfFile(setResult, component.Openshift.Uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
case v1alpha2.ImageComponentType: | ||
if component.Image.Dockerfile != nil { | ||
setResult, err = appendUriIfFile(setResult, component.Image.Dockerfile.Uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
setResult, err = getFromAttributes(setResult, component.Attributes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
commands, err := devfileObj.Data.GetCommands(common.DevfileOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, command := range commands { | ||
setResult, err = getFromAttributes(setResult, command.Attributes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
result = make([]string, 0, len(setResult)) | ||
for k := range setResult { | ||
result = append(result, k) | ||
} | ||
sort.Strings(result) | ||
return result, nil | ||
} | ||
|
||
// appendUriIfFile appends uri to the result if the uri is a local path | ||
func appendUriIfFile(result map[string]struct{}, uri string) (map[string]struct{}, error) { | ||
if uri != "" { | ||
u, err := url.Parse(uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if u.Scheme == "" { | ||
result[uri] = struct{}{} | ||
} | ||
} | ||
return result, nil | ||
} | ||
|
||
// getFromAttributes extracts paths from attributes entries with key "api.devfile.io/imported-from" | ||
// containing a uri reference as a local path | ||
func getFromAttributes(result map[string]struct{}, attributes attributes.Attributes) (map[string]struct{}, error) { | ||
if val, ok := attributes[validation.ImportSourceAttribute]; ok { | ||
strVal := string(val.Raw) | ||
strVal = strings.Trim(strVal, `"`) | ||
if strings.HasPrefix(strVal, _importSourceAttributeUriPrefix) { | ||
parentUri := strings.TrimLeft(strVal, _importSourceAttributeUriPrefix) | ||
var err error | ||
result, err = appendUriIfFile(result, parentUri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
return result, nil | ||
} |
Oops, something went wrong.