func updateBottlerocketHostContainerMetadata()

in tools/version-tracker/pkg/commands/upgrade/upgrade.go [1026:1081]


func updateBottlerocketHostContainerMetadata(client *gogithub.Client, projectRootFilepath, projectPath, latestBottlerocketVersion string) ([]string, error) {
	updatedHostContainerFiles := []string{}
	hostContainersTOMLContents, err := github.GetFileContents(client, constants.BottlerocketOrgName, constants.BottlerocketRepoName, constants.BottlerocketHostContainersTOMLFile, latestBottlerocketVersion)
	if err != nil {
		return nil, fmt.Errorf("getting contents of Bottlerocket host containers file: %v", err)
	}

	var hostContainersTOMLMap interface{}
	err = toml.Unmarshal(hostContainersTOMLContents, &hostContainersTOMLMap)
	if err != nil {
		return nil, fmt.Errorf("unmarshalling Bottlerocket host containers file: %v", err)
	}

	for _, container := range constants.BottlerocketHostContainers {
		var hostContainerImageMetadata types.ImageMetadata
		hostContainerMetadataFilePath := filepath.Join(projectRootFilepath, fmt.Sprintf(constants.BottlerocketContainerMetadataFileFormat, strings.ToUpper(container)))
		hostContainerMetadataRelativeFilePath := filepath.Join(projectPath, fmt.Sprintf(constants.BottlerocketContainerMetadataFileFormat, strings.ToUpper(container)))
		hostContainerMetadataFileContents, err := os.ReadFile(hostContainerMetadataFilePath)
		if err != nil {
			return nil, fmt.Errorf("reading Bottlerocket %s container metadata file: %v", container, err)
		}
		err = yaml.Unmarshal(hostContainerMetadataFileContents, &hostContainerImageMetadata)
		if err != nil {
			return nil, fmt.Errorf("unmarshalling Bottlerocket %s container metadata file: %v", container, err)
		}

		hostContainerSourceCommand := hostContainersTOMLMap.(map[string]interface{})["metadata"].(map[string]interface{})["settings"].(map[string]interface{})["host-containers"].(map[string]interface{})[container].(map[string]interface{})["source"].(map[string]interface{})["setting-generator"].(map[string]interface{})["command"].(string)
		hostContainerSourceImageRegex := regexp.MustCompile(constants.BottlerocketHostContainerSourceImageRegex)
		hostContainerSourceImage := hostContainerSourceImageRegex.FindAllStringSubmatch(hostContainerSourceCommand, -1)[0][1]
		hostContainerSourceImageTag := strings.Split(hostContainerSourceImage, ":")[1]

		if hostContainerImageMetadata.Tag != hostContainerSourceImageTag {
			hostContainerImageMetadata.Tag = hostContainerSourceImageTag
			skopeoInspectCmd := exec.Command("skopeo", "inspect", fmt.Sprintf("docker://%s", hostContainerSourceImage), "--override-os", "linux", "--override-arch", "amd64", "--format", "{{.Digest}}")
			stdout, err := command.ExecCommand(skopeoInspectCmd)
			if err != nil {
				return nil, fmt.Errorf("running skopeo inspect command: %v", err)
			}
			hostContainerImageMetadata.ImageDigest = stdout

			updatedHostContainerMetadataFileContents, err := yaml.Marshal(hostContainerImageMetadata)
			if err != nil {
				return nil, fmt.Errorf("marshalling updated Bottlerocket %s container: %v", container, err)
			}

			err = os.WriteFile(hostContainerMetadataFilePath, updatedHostContainerMetadataFileContents, 0o644)
			if err != nil {
				return nil, fmt.Errorf("writing Bottlerocket releases file: %v", err)
			}

			updatedHostContainerFiles = append(updatedHostContainerFiles, hostContainerMetadataRelativeFilePath)
		}
	}

	return updatedHostContainerFiles, nil
}