Skip to content

Commit

Permalink
patch: added ability to insert metadata into the image being created
Browse files Browse the repository at this point in the history
Adds the ability to insert metadata into the image being created.
Bumps the Trivy version.

Removes some fluff from when I was playing with env vars as an option.
  • Loading branch information
drew-viles committed Jan 19, 2023
1 parent 09e4fa6 commit 26f500f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
3 changes: 3 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ To use baskio to build an image, an Openstack cluster is required.`,
buildGitDir := build.CreateRepoDirectory()
build.FetchBuildRepo(buildGitDir, imageRepoFlag, viper.GetBool("build.enable-nvidia-support"))

metadata := ostack.GenerateBuilderMetadata()
ostack.UpdatePackerBuildersJson(buildGitDir, metadata)

capiPath := filepath.Join(buildGitDir, "images", "capi")
packerBuildConfig.GenerateVariablesFile(capiPath)

Expand Down
9 changes: 2 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ This tool has been designed to automatically build images for the Openstack poti
It could be extended out to provide images for a variety of other builders however for now it's main goal is to work with Openstack.`,
}

buildCmd := NewBuildCommand()
scanCmd := NewScanCommand()
rootCmd.PersistentFlags().StringVar(&cloudsPathFlag, "clouds-file", "~/.config/openstack/clouds.yaml", "The location of the openstack clouds.yaml file to use")
rootCmd.PersistentFlags().StringVar(&cloudNameFlag, "cloud-name", "", "The name of the cloud profile to use from the clouds.yaml file")
rootCmd.PersistentFlags().StringVar(&baskioConfigFlag, "baskio-config", "baskio.yaml", "The location of a baskio config file")
Expand All @@ -50,8 +48,8 @@ It could be extended out to provide images for a variety of other builders howev

commands := []*cobra.Command{
versionCmd(),
buildCmd,
scanCmd,
NewBuildCommand(),
NewScanCommand(),
NewPublishCommand(),
}
rootCmd.AddCommand(commands...)
Expand All @@ -60,7 +58,6 @@ It could be extended out to provide images for a variety of other builders howev

// initConfig will initialise viper and the configuration file.
func initConfig() {
viper.SetEnvPrefix("baskio")
if baskioConfigFlag != "" {
viper.SetConfigFile(baskioConfigFlag)
} else {
Expand All @@ -74,8 +71,6 @@ func initConfig() {
}
}

viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
"ubuntu-2004",
"ubuntu-2204",
}
TrivyVersion = "0.36.1"
)

// Year is used in reports parsing. It is the top level and contains multiple Month(s).
Expand Down
70 changes: 70 additions & 0 deletions pkg/openstack/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package ostack
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/google/uuid"
"github.com/spf13/viper"
Expand Down Expand Up @@ -161,6 +163,74 @@ func generateImageName(semVer string) string {
return imageName + "-" + imageUUID.String()[:strings.Index(imageUUID.String(), "-")]
}

// GenerateBuilderMetadata generates some glance metadata for the image.
func GenerateBuilderMetadata() map[string]string {
gpu := viper.GetString("build.nvidia-driver-version")
if len(gpu) == 0 {
gpu = "no_gpu"
}
return map[string]string{
"os": viper.GetString("build.build-os"),
"k8s": viper.GetString("build.kubernetes_version"),
"gpu": gpu,
"date": time.RFC3339,
}
}

// UpdatePackerBuildersJson pre-populates the metadata field in the packer.json file as objects cannot be passed as variables in packer.
func UpdatePackerBuildersJson(dir string, metadata map[string]string) {
file, err := os.OpenFile(filepath.Join(dir, "images", "capi", "packer", "openstack", "packer.json"), os.O_RDWR, 0644)
if err != nil {
log.Fatalln(err)
}
defer file.Close()

data, err := io.ReadAll(file)
if err != nil {
log.Fatalln(err)
}

res := addMetadataToBuilders(metadata, data)

err = file.Truncate(0)
if err != nil {
log.Fatalln(err)
}
_, err = file.Seek(0, 0)
if err != nil {
log.Fatalln(err)
}

_, err = file.Write(res)
if err != nil {
log.Fatalln(err)
}
}

// addMetadataToBuilders inserts the metadata into the packer's builder section.
func addMetadataToBuilders(metadata map[string]string, data []byte) []byte {
jsonStruct := struct {
Builders []map[string]interface{} `json:"builders"`
PostProcessors []map[string]interface{} `json:"post-processors"`
Provisioners []map[string]interface{} `json:"provisioners"`
Variables map[string]interface{} `json:"variables"`
}{}

err := json.Unmarshal(data, &jsonStruct)
if err != nil {
log.Fatalln(err)
}

jsonStruct.Builders[0]["metadata"] = metadata

res, err := json.Marshal(jsonStruct)
if err != nil {
log.Fatalln(err)
}

return res
}

// GenerateVariablesFile converts the PackerBuildConfig into a build configuration file that packer can use.
func (p *PackerBuildConfig) GenerateVariablesFile(buildGitDir string) {
outputFileName := strings.Join([]string{"tmp", ".json"}, "")
Expand Down
3 changes: 2 additions & 1 deletion pkg/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ostack

import (
"fmt"
"github.com/eschercloudai/baskio/pkg/constants"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (c *Client) CreateKeypair(KeyNamePrefix string) *keypairs.KeyPair {

// CreateServer creates a compute instance in Openstack.
func (c *Client) CreateServer(keypair *keypairs.KeyPair, imageID, flavorName, networkID string, enableConfigDrive bool) (*servers.Server, string) {
trivyVersion := "0.31.3"
trivyVersion := constants.TrivyVersion
client := createComputeClient(c)

serverFlavorID := c.GetFlavorIDByName(flavorName)
Expand Down

0 comments on commit 26f500f

Please sign in to comment.