Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Improve user experience in the Butane command-line utility

Do you like sugar with your Butane?

May 16, 2024
Steven Presti
Related topics:
ContainersDeveloper ToolsLinux
Related products:
Red Hat OpenShift

Share:

    Butane (formerly the Fedora CoreOS Config Transpiler, or FCCT) is a command-line utility that is used to generate human readable configuration into Ignition configuration files. Butane is an open source repo maintained by CoreOS, and contributed to and enhanced by many. If you take a look at the Butane repo and look in the Config folder, you can almost see some of the stakeholders. These are configurations which are used to target different platforms (i.e., FCOS, Flatcar Container Linux, Red Hat OpenShift, etc.) Each of these configurations will have a stable and experimental phase that also targets stable and experimental specs of Ignition. I would describe these as levers that a contributor can exercise to impact the functionality they want to expose to the end users of these target platforms.

    What do we mean by sugar?

    So what is this sugar, and why do we want it? With the primary focus of Butane being the translation of something that is easily understood to something that is more specific and less easily understood, honestly, you could view all of Butane as some form of sugar. I like to think of it as magic dust we use to make the user experience more enjoyable by reducing the amount of effort it takes to do common tasks. Sugar typically impacts all target platforms, and is added to the experimental spec, which gets stabilized eventually.

    Example use case

    As a user, if I want to create a file which is nested in a few directories (i.e.: /foo/bar/directory/myfile.txt) with a specific permission set, I do not want to have to describe each independent folder the file is nested in (i.e.: /foo, /foo/bar, /foo/bar/directory). Currently in Butane, you would have:

    variant: fcos
    version: 1.6.0-experimental
    storage:
      files:
        - path: /foo/bar/directory/myfile.txt
          user:
            name: builder
          contents:
            inline: my content!
      directories:
        - path: /foo/bar/directory/
          user:
            name: builder
          group:
            name: builder
        - path: /foo/bar
          user:
            name: builder
          group:
            name: builder
        - path: /foo
          user:
            name: builder
          group:
            name: builder
    

    As you can see in this example Config, there is already a lot of bloating and potential surface area that could expose the user to user error and large Butane files.

    Steps to add sugar

    UX changes

    The first thing you want to do is decide what type of user experience you want to provide the consumer. So looking at this and the problem provided, we know we want to eliminate the need to add configuration for the individual directories. We want some magical dust to be able to interpret what the user wants. Since the issue stems from the file being in a nested directory, lets look at a file's fields.

    Find what part of the translation is too bitter

    Looking at the latest spec, we can see that we have already defined a custom translator for file and registered it here. Initially, since we are adding sugar to files, you might think that our work belongs in the custom translator of files. However, since our sugar is going to be expanding directories, we need to place our sugar at a point in translation which the sugar can interact both with files and directories. Since we know we need to interact with both files and directories we can look at our schema, and find the earliest struct which contains and knows about both of them. In this case storage knows about both of them; you can see that here.

    Add some sugar!

     

    Info alert: Note

    At this point, please know that the following code is condensed and summarized to reduce bloating. I will try and focus on the important bits.

    Now that we know our storage struct is too bitter, we can go about adding the sugar to make it oh-so-sweet. To do this, we can create a custom translator similar to the custom file translator found here, but in this case storage will register the custom file translator as well. To do this, understand we are creating a map from Butane's struct to Ignition's. And we now have the chance to make changes to the outputted Ignition config. So our signature will look like this:

    func translateStorage(from Storage, options common.TranslateOptions) (to types.Storage, tm translate.TranslationSet, r report.Report) 
    

    Now we start defining our translator, we are translating from YAML to JSON.

    tr := translate.NewTranslator("yaml", "json", options)
    

    And we have some pre-existing custom translators that we can register like this:

    ...
    tr.AddCustomTranslator(translateDirectory)
    ...
    

    Now we can start translating from the Butane structs to Ignition. So looking at our storage struct we can see all the fields we need to add translation for. To do this, we start adding field by field, declaring the top most field our prefix:

    ...
    tm, r = translate.Prefixed(tr, "directories", &from.Directories, &to.Directories)
    translate.MergeP(tr, tm, &r, "disks", &from.Disks, &to.Disks)
    ...
    }
    

    Awesome! We are now at the point where our translator should handle all the requirements for converting from YAML to JSON between Butane and Ignition. The last part is adding our sugar.

    To do this, we can now work against the from object and iterate through our files. While iterating through the files, our sugar will look at the parent to see if action is required.

    Once we have decided that a file has directories that need to be expanded, we can then:

    // For each directory that needs to be expanded based off parent and file path
    // We create the Ignition struct of the directory
    renderedDir := types.Directory {
                        Node: types.Node{
                            Path:  dir,
                            Group: types.NodeGroup{ID: file.Group.ID, Name: file.Group.Name},
                            User:  types.NodeUser{ID: file.User.ID, Name: file.User.Name},
                        },
                        DirectoryEmbedded1: types.DirectoryEmbedded1{
                            Mode: file.Parent.Mode,
                        },
                    }
    // Append it to the return object `to`
    to.Directories = append(to.Directories, renderedDir)
    }
    

    Lastly, we need to do some housekeeping and update our report by associating all these directories to a context path by having a line similar to the following:

    tm.AddFromCommonSource(yamlPath, path.New("json", "directories"), to.Directories)
    

    Finally, we have created sugar and a custom translator. Now, let's register it.

    In the main translate function, we can register our custom translator like so:

    tr.AddCustomTranslator(translateStorage) 
    

    We can see this code work by creating some tests similar to the directory tests here.

    Updating documentation

    Ensuring that the documentation accurately reflects the changes made to Butane's functionality is crucial for maintaining clarity and transparency. You can see the long list of documentation here. This is every platform Config type that Butane supports. They are subsets of functionality but as a result they have a large overlap of documentation. Thankfully we don't need to manually update the docs on each of these targets, there is automation built in!

    1. Update the docs related to the struct

    Begin by including your new fields within the structured documentation file, accessible here. As these additions are in the file, navigate to this specific section within and add something like the following:

    - name: files
      children:
        - name: contents
          children:
            - name: source
              transforms:
                - regex: "Supported schemes are .* haven't been modified."
                  replacement: 'Only the [`data`](https://tools.ietf.org/html/rfc2397) scheme is supported.'
                  if:
                    - variant: openshift
        - name: mode
          use: mode
        - name: parent
          after: $
          desc: The parent directory for the specified file. By declaring a parent, directories from the parent to the file's target destination are automatically created.
          children:
            - name: path
              desc: The path of the directory within the file's 'path'.
            - name: mode
              desc: Directory modes are set to 0755 as a default if not specified, and the directory does not exist prior to the specified file.
    

    Once you've updated the YAML file, execute the ./generate command from the root directory of the Butane repository. This action triggers the automatic regeneration of all platform configuration documents, ensuring that they are synchronized with the latest information.

    2. Update examples in examples.md

    Finish the documentation update process by adding examples to the examples.md file. Here, you can include a a demonstration of the new sugar. Provide clear instructions and examples to assist users in understanding and using the sugar.

    Final thoughts

    The concept of sugar in Butane is as varied and nuanced as the use cases it addresses. It can be as simple as adding a touch of convenience to a common task, or as complex as restructuring core functionalities. Understanding its potential and implications can empower us to enhance Butane's day-to-day user experience significantly.

    Related Posts

    • How to customize Fedora CoreOS for dedicated workloads with OSTree

    • How to run containerized workloads securely and at scale with Fedora CoreOS

    • How the new RHEL 9.2 improves the developer experience

    • 3 steps toward improving container security

    • Improving user experience for mobile APIs using the cloud

    • What's new for developers in Red Hat OpenShift 4.15

    Recent Posts

    • Meet the Red Hat Node.js team at PowerUP 2025

    • How to use pipelines for AI/ML automation at the edge

    • What's new in network observability 1.8

    • LLM Compressor: Optimize LLMs for low-latency deployments

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    What’s up next?

    Convert CentOS Linux to RHEL share and feature image

    Convert2RHEL is a command-line utility that can streamline your migration path from CentOS Linux 7 to a fully supported Red Hat Enterprise Linux (RHEL) operating system. This cheat sheet outlines how to convert your CentOS Linux instance to RHEL in just 7 steps.

    Get the cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue