Giter VIP home page Giter VIP logo

library's Introduction

Devfile Library

Apache2.0 License

About

The Devfile Parser library is a Golang module that:

  1. parses the devfile.yaml as specified by the api & schema.
  2. writes to the devfile.yaml with the updated data.
  3. generates Kubernetes objects for the various devfile resources.
  4. defines util functions for the devfile.

Usage

The function documentation can be accessed via pkg.go.dev.

  1. To parse a devfile, visit parse.go source file

    // ParserArgs is the struct to pass into parser functions which contains required info for parsing devfile.
    parserArgs := parser.ParserArgs{
     	Path:                           path,
     	FlattenedDevfile:               &flattenedDevfile,
     	ConvertKubernetesContentInUri:  &convertKubernetesContentInUri
     	RegistryURLs:                   registryURLs,
     	DefaultNamespace:               defaultNamespace,
     	Context:                        context,
     	K8sClient:                      client,
    		ExternalVariables:              externalVariables,
     }
    
    // Parses the devfile and validates the devfile data
    // if top-level variables are not substituted successfully, the warnings can be logged by parsing variableWarning
    devfile, variableWarning, err := devfilePkg.ParseDevfileAndValidate(parserArgs)
  2. To override the HTTP request and response timeouts for a devfile with a parent reference from a registry URL, specify the HTTPTimeout value in the parser arguments

       // specify the timeout in seconds  
       httpTimeout := 20 
       parserArgs := parser.ParserArgs{
          HTTPTimeout: &httpTimeout
       }
  3. To get specific content from devfile

    // To get all the components from the devfile
    components, 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 components
    components, err := devfile.Data.GetComponents(DevfileOptions{
     	ComponentOptions: ComponentOptions{
     		ComponentType: v1.VolumeComponentType,
     	},
    })
    
    // To get all the exec commands that belong to the build group
    commands, err := devfile.Data.GetCommands(DevfileOptions{
     	CommandOptions: CommandOptions{
     		CommandType: v1.ExecCommandType,
     		CommandGroupKind: v1.BuildCommandGroupKind,
     	},
    })
  4. To get the Kubernetes objects from the devfile, visit generators.go source file

     // To get a slice of Kubernetes containers of type corev1.Container from the devfile component containers
     containers, err := generator.GetContainers(devfile)
    
     // To generate a Kubernetes deployment of type v1.Deployment
     deployParams := 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)
  5. To update devfile content

    // To update an existing component in devfile object
    err := 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 object
    err := 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 object
    err := devfile.Data.DeleteComponent(componentName)
  6. To write to a devfile, visit writer.go source file

    // If the devfile object has been created with devfile path already set, can simply call WriteYamlDevfile to writes the devfile
    err := devfile.WriteYamlDevfile()
    
    
    // To write to a devfile from scratch
    // create a new DevfileData with a specific devfile version
    devfileData, err := data.NewDevfileData(devfileVersion)
    
    // set schema version
    devfileData.SetSchemaVersion(devfileVersion)
    
    // add devfile content use library APIs
    devfileData.AddComponents([]v1.Component{...})
    devfileData.AddCommands([]v1.Commands{...})
    ......
    
    // create a new DevfileCtx
    ctx := devfileCtx.NewDevfileCtx(devfilePath)
    err = ctx.SetAbsPath()
    
    // create devfile object with the new DevfileCtx and DevfileData
    devfile := parser.DevfileObj{
     	Ctx:  ctx,
     	Data: devfileData,
    }
     
    // write to the devfile on disk
    err = devfile.WriteYamlDevfile()
  7. To parse the outerloop Kubernetes/OpenShift component's uri or inline content, call the read and parse functions

    // Read the YAML content
    values, err := ReadKubernetesYaml(src, fs)
    
    // Get the Kubernetes resources
    resources, err := ParseKubernetesYaml(values)

Projects using devfile/library

The following projects are consuming this library as a Golang dependency

Tests

To run unit tests and api tests. Visit library tests to find out more information on tests

make test

Issues

Issues are tracked in the devfile/api repo with the label area/library

Releases

For devfile/library releases, please check the release page.

Contributing

Please see our contributing.md.

library's People

Contributors

yangcao77 avatar maysunfaisal avatar wtam2018 avatar ranakan19 avatar mmulholla avatar kim-tsao avatar mmulholl avatar dharmit avatar valaparthvi avatar sleshchenko avatar michael-valdron avatar sbose78 avatar amisevsk avatar johnmcollier avatar mike-hoang avatar feloy avatar girishramnani avatar jaideepr97 avatar mohammedzee1000 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.