// ParserArgs is the struct to pass into parser functions which contains required info for parsing devfile.parserArgs:= parser.ParserArgs{
Path: path,
FlattenedDevfile: &flattenedDevfile,
RegistryURLs: registryURLs,
DefaultNamespace: defaultNamespace,
Context: context,
K8sClient: client,
}
// Parses the devfile and validates the devfile data// if top-level variables are not substituted successfully, the warnings can be logged by parsing variableWarningdevfile, variableWarning, err:=devfilePkg.ParseDevfileAndValidate(parserArgs)
To get specific content from devfile
// To get all the components from the devfilecomponents, err:=devfile.Data.GetComponents(DevfileOptions{})
// To get all the components from the devfile with attributes tagged - tool: console-import// & import: {strategy: Dockerfile}components, err:=devfile.Data.GetComponents(DevfileOptions{
Filter: map[string]interface{}{
"tool": "console-import",
"import": map[string]interface{}{
"strategy": "Dockerfile",
},
},
})
// To get all the volume componentscomponents, err:=devfile.Data.GetComponents(DevfileOptions{
ComponentOptions: ComponentOptions{
ComponentType: v1.VolumeComponentType,
},
})
// To get all the exec commands that belong to the build groupcommands, err:=devfile.Data.GetCommands(DevfileOptions{
CommandOptions: CommandOptions{
CommandType: v1.ExecCommandType,
CommandGroupKind: v1.BuildCommandGroupKind,
},
})
// To get a slice of Kubernetes containers of type corev1.Container from the devfile component containerscontainers, err:=generator.GetContainers(devfile)
// To generate a Kubernetes deployment of type v1.DeploymentdeployParams:= generator.DeploymentParams{
TypeMeta: generator.GetTypeMeta(deploymentKind, deploymentAPIVersion),
ObjectMeta: generator.GetObjectMeta(name, namespace, labels, annotations),
InitContainers: initContainers,
Containers: containers,
Volumes: volumes,
PodSelectorLabels: labels,
}
deployment:=generator.GetDeployment(deployParams)
To update devfile content
// To update an existing component in devfile objecterr:=devfile.Data.UpdateComponent(v1.Component{
Name: "component1",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: "image1",
},
},
},
})
// To add a new component to devfile objecterr:=devfile.Data.AddComponents([]v1.Component{
{
Name: "component2",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: "image2",
},
},
},
},
})
// To delete a component from the devfile objecterr:=devfile.Data.DeleteComponent(componentName)
// If the devfile object has been created with devfile path already set, can simply call WriteYamlDevfile to writes the devfileerr:=devfile.WriteYamlDevfile()
// To write to a devfile from scratch// create a new DevfileData with a specific devfile versiondevfileData, err:=data.NewDevfileData(devfileVersion)
// set schema versiondevfileData.SetSchemaVersion(devfileVersion)
// add devfile content use library APIsdevfileData.AddComponents([]v1.Component{...})
devfileData.AddCommands([]v1.Commands{...})
......// create a new DevfileCtxctx:=devfileCtx.NewDevfileCtx(devfilePath)
err=ctx.SetAbsPath()
// create devfile object with the new DevfileCtx and DevfileDatadevfile:= parser.DevfileObj{
Ctx: ctx,
Data: devfileData,
}
// write to the devfile on diskerr=devfile.WriteYamlDevfile()
Projects using devfile/library
The following projects are consuming this library as a Golang dependency
请发表评论