Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default boolean values #122

Merged
merged 3 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/devfile/library
go 1.15

require (
github.com/devfile/api/v2 v2.0.0-20211018184408-84c44e563f58
github.com/devfile/api/v2 v2.0.0-20211021164004-dabee4e633ed
github.com/fatih/color v1.7.0
github.com/gobwas/glob v0.2.3
github.com/golang/mock v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devfile/api/v2 v2.0.0-20211018184408-84c44e563f58 h1:tKWuUzT5zqWm4OId74zvQVSlildo2z8w28ghzHF+Jkw=
github.com/devfile/api/v2 v2.0.0-20211018184408-84c44e563f58/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q=
github.com/devfile/api/v2 v2.0.0-20211021164004-dabee4e633ed h1:OXF9l+MlJrirXAqKN6EZUVaHB0FKm7nh0EjpktwnBig=
github.com/devfile/api/v2 v2.0.0-20211021164004-dabee4e633ed/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
Expand Down
111 changes: 110 additions & 1 deletion pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ func ParseDevfile(args ParserArgs) (d DevfileObj, err error) {
flattenedDevfile = *args.FlattenedDevfile
}

return populateAndParseDevfile(d, &resolutionContextTree{}, tool, flattenedDevfile)
d, err = populateAndParseDevfile(d, &resolutionContextTree{}, tool, flattenedDevfile)

//set defaults only if we are flattening parent and parsing succeeded
if flattenedDevfile && err == nil {
setDefaults(d)
}

return d, err
}

// resolverTools contains required structs and data for resolving remote components of a devfile (plugins and parents)
Expand Down Expand Up @@ -281,6 +288,7 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool
d.Data.SetDevfileWorkspaceSpecContent(*mergedContent)
// remove parent from flatterned devfile
d.Data.SetParent(nil)
//setDefaults(d)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should remove this comment?


return nil
}
Expand Down Expand Up @@ -428,3 +436,104 @@ func convertDevWorskapceTemplateToDevObj(dwTemplate v1.DevWorkspaceTemplate) (d
return d, nil

}

//setDefaults sets the default values for nil boolean properties after the merging of devWorkspaceTemplateSpec is complete
func setDefaults(d DevfileObj) (err error) {
commands, err := d.Data.GetCommands(common.DevfileOptions{})

if err != nil {
return err
}

//set defaults on the commands
var cmdGroup *v1.CommandGroup
for i := range commands {
command := commands[i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick question on a glance, since command is just a new variable; will the d DevfileObj still have those changes? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, command references the same address location of the underlying command. e.g. The first ApplyCommand is referenced by d while the second is referenced by command
image

cmdGroup = nil

if command.Exec != nil {
exec := command.Exec
val := exec.GetHotReloadCapable()
exec.HotReloadCapable = &val
cmdGroup = exec.Group

} else if command.Composite != nil {
composite := command.Composite
val := composite.GetParallel()
composite.Parallel = &val
cmdGroup = composite.Group

} else if command.Apply != nil {
cmdGroup = command.Apply.Group
}

if cmdGroup != nil {
setIsDefault(cmdGroup)
}

}

//set defaults on the components

components, err := d.Data.GetComponents(common.DevfileOptions{})

if err != nil {
return err
}

var endpoints []v1.Endpoint
for i := range components {
component := components[i]
endpoints = nil

if component.Container != nil {
container := component.Container
val := container.GetDedicatedPod()
container.DedicatedPod = &val

val = container.GetMountSources()
container.MountSources = &val

endpoints = container.Endpoints

} else if component.Kubernetes != nil {
endpoints = component.Kubernetes.Endpoints

} else if component.Openshift != nil {

endpoints = component.Openshift.Endpoints

} else if component.Volume != nil {
volume := component.Volume
val := volume.GetEphemeral()
volume.Ephemeral = &val

} else if component.Image != nil {
dockerImage := component.Image.Dockerfile
if dockerImage != nil {
val := dockerImage.GetRootRequired()
dockerImage.RootRequired = &val
}
}

if endpoints != nil {
setEndpoints(endpoints)
}
}

return nil
}

///setIsDefault sets the default value of CommandGroup.IsDefault if nil
func setIsDefault(cmdGroup *v1.CommandGroup) {
val := cmdGroup.GetIsDefault()
cmdGroup.IsDefault = &val
}

//setEndpoints sets the default value of Endpoint.Secure if nil
func setEndpoints(endpoints []v1.Endpoint) {
for i := range endpoints {
val := endpoints[i].GetSecure()
endpoints[i].Secure = &val
}
}
Loading