Skip to content

Commit

Permalink
adopt embeds in favor of downloading content
Browse files Browse the repository at this point in the history
  • Loading branch information
jarededwards committed Nov 19, 2024
1 parent 35d8661 commit 71a016c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 140 deletions.
70 changes: 28 additions & 42 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"path/filepath"
"time"

"github.com/konstructio/colony/configs"
"github.com/konstructio/colony/internal/colony"
"github.com/konstructio/colony/internal/constants"
"github.com/konstructio/colony/internal/docker"
"github.com/konstructio/colony/internal/k8s"
"github.com/konstructio/colony/internal/logger"
"github.com/konstructio/colony/manifests"
"github.com/konstructio/colony/scripts"
"github.com/spf13/cobra"

corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -166,15 +165,19 @@ func getInitCommand() *cobra.Command {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

log.Info("Applying tinkerbell templates")
if err := k8sClient.LoadMappingsFromKubernetes(); err != nil {
return fmt.Errorf("error loading dynamic mappings from kubernetes: %w", err)
}

log.Info("applying tinkerbell templates")
colonyTemplates, err := manifests.Templates.ReadDir("templates")
if err != nil {
return fmt.Errorf("error reading templates: %w", err)
}
var manifestsFiles []string

for _, file := range colonyTemplates {
content, err := manifests.Templates.ReadFile(file.Name())
content, err := manifests.Templates.ReadFile(filepath.Join("templates", file.Name()))
if err != nil {
return fmt.Errorf("error reading templates file: %w", err)
}
Expand All @@ -185,6 +188,27 @@ func getInitCommand() *cobra.Command {
return fmt.Errorf("error applying templates: %w", err)
}

log.Info("downloading operating systems for hook")

downloadTemplates, err := manifests.Downloads.ReadDir("downloads")
if err != nil {
return fmt.Errorf("error reading templates: %w", err)
}

var downloadFiles []string

for _, file := range downloadTemplates {
content, err := manifests.Downloads.ReadFile(filepath.Join("downloads", file.Name()))
if err != nil {
return fmt.Errorf("error reading templates file: %w", err)
}
downloadFiles = append(downloadFiles, string(content))
}

if err := k8sClient.ApplyManifests(ctx, downloadFiles); err != nil {
return fmt.Errorf("error applying templates: %w", err)
}

clusterRoleName := "smee-role"

patch := []map[string]interface{}{{
Expand All @@ -208,44 +232,6 @@ func getInitCommand() *cobra.Command {
return fmt.Errorf("error patching ClusterRole: %w", err)
}

downloadJobs := []configs.DownloadJob{
{
DownloadURL: "https://github.com/siderolabs/talos/releases/download/v1.8.0",
Name: "talos",
},
{
DownloadURL: "https://cloud-images.ubuntu.com/daily/server/jammy/current/jammy-server-cloudimg-amd64.img",
Name: "ubuntu-jammy",
},
}

for _, job := range downloadJobs {
script, err := scripts.Scripts.ReadFile(fmt.Sprintf("%s.sh", job.Name))
if err != nil {
return fmt.Errorf("error reading file: %w", err)
}

configMap, err := k8s.BuildConfigMap(job.Name, string(script))
if err != nil {
return fmt.Errorf("error building configmap: %w", err)
}

err = k8sClient.CreateConfigMap(ctx, configMap)
if err != nil {
return fmt.Errorf("error creating configmap: %w", err)
}

jobSpec, err := k8s.BuildJob(job.DownloadURL, job.Name)
if err != nil {
return fmt.Errorf("error building job: %w", err)
}

err = k8sClient.CreateJob(ctx, jobSpec)
if err != nil {
return fmt.Errorf("error creating job: %w", err)
}
}

return nil
},
}
Expand Down
96 changes: 1 addition & 95 deletions internal/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/konstructio/colony/internal/constants"
"github.com/konstructio/colony/internal/logger"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -130,32 +129,11 @@ func (c *Client) CreateSecret(ctx context.Context, secret *corev1.Secret) error
return nil
}

func (c *Client) CreateConfigMap(ctx context.Context, configMap *corev1.ConfigMap) error {
_, err := c.clientSet.CoreV1().ConfigMaps(configMap.GetNamespace()).Create(ctx, configMap, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("error creating ConfigMap: %w", err)
}

c.logger.Infof("ConfigMap %s created successfully in namespace %s", configMap.Name, configMap.Namespace)

return nil
}

func (c *Client) CreateJob(ctx context.Context, job *batchv1.Job) error {
job, err := c.clientSet.BatchV1().Jobs(job.GetNamespace()).Create(ctx, job, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("error creating Job: %w", err)
}

c.logger.Infof("job %s created successfully", job.Name)

return nil
}

func (c *Client) ApplyManifests(ctx context.Context, manifests []string) error {
decoderUnstructured := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)

for _, manifest := range manifests {
fmt.Println(manifest)
var obj unstructured.Unstructured
_, gvk, err := decoderUnstructured.Decode([]byte(manifest), nil, &obj)
if err != nil {
Expand Down Expand Up @@ -354,78 +332,6 @@ func (c *Client) returnDeploymentObject(ctx context.Context, matchLabel string,
return deployment, nil
}

func BuildJob(downloadURL, name string) (*batchv1.Job, error) {
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("download-%s", name),
Namespace: constants.ColonyNamespace,
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: fmt.Sprintf("download-%s", name),
Image: "bash:5.2.2",
Command: []string{"bash", "-c", "/script/entrypoint.sh"},
Args: []string{
downloadURL,
"/output",
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "hook-artifacts",
MountPath: "/output",
},
{
Name: fmt.Sprintf("download-%s", name),
MountPath: "/script",
},
},
},
},
RestartPolicy: corev1.RestartPolicyOnFailure,
Volumes: []corev1.Volume{
{
Name: "hook-artifacts",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/opt/hook",
Type: new(corev1.HostPathType),
},
},
},
{
Name: fmt.Sprintf("download-%s", name),
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: fmt.Sprintf("download-%s", name)},
},
},
},
},
},
},
},
}

return job, nil
}

func BuildConfigMap(name, script string) (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("download-%s", name),
Namespace: constants.ColonyNamespace,
},
Data: map[string]string{
"entrypoint.sh": script,
},
}

return cm, nil
}

// WaitForKubernetesAPIHealthy waits for the Kubernetes API to be healthy
// by checking the server version every 5 seconds or until the timeout is reached.
func (c *Client) WaitForKubernetesAPIHealthy(ctx context.Context, timeout time.Duration) error {
Expand Down
19 changes: 19 additions & 0 deletions manifests/downloads/cm-download-talos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: download-talos
namespace: tink-system
data:
entrypoint.sh: |-
#!/usr/bin/env bash
# This script is designed to download specific Talos files required for an IPXE script to work.
set -euxo pipefail
if ! which wget &>/dev/null; then
apk add --update wget
fi
base_url=$1
output_dir=$2
files=("initramfs-amd64.xz" "vmlinuz-amd64")
for file in "${files[@]}"; do
wget "${base_url}/${file}" -O "${output_dir}/${file}"
done
24 changes: 24 additions & 0 deletions manifests/downloads/cm-download-ubuntu-jammy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: download-ubuntu-jammy
namespace: tink-system
data:
entrypoint.sh: |-
#!/usr/bin/env bash
# This script is designed to download a cloud image file (.img) and then convert it to a .raw.gz file.
# This is purpose built so non-raw cloud image files can be used with the "image2disk" action.
# See https://artifacthub.io/packages/tbaction/tinkerbell-community/image2disk.
set -euxo pipefail
if ! which pigz qemu-img &>/dev/null; then
apk add --update pigz qemu-img
fi
image_url=$1
file=$2/${image_url##*/}
file=${file%.*}.raw.gz
if [[ ! -f "$file" ]]; then
wget "$image_url" -O image.img
qemu-img convert -O raw image.img image.raw
pigz <image.raw >"$file"
rm -f image.img image.raw
fi
32 changes: 32 additions & 0 deletions manifests/downloads/job-download-talos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: batch/v1
kind: Job
metadata:
name: download-talos
namespace: tink-system
spec:
template:
spec:
containers:
- name: download-talos
image: bash:5.2.2
command: ["/script/entrypoint.sh"]
args:
[
"https://github.com/siderolabs/talos/releases/download/v1.8.0",
"/output",
]
volumeMounts:
- mountPath: /output
name: hook-artifacts
- mountPath: /script
name: configmap-volume
restartPolicy: OnFailure
volumes:
- name: hook-artifacts
hostPath:
path: /opt/hook
type: DirectoryOrCreate
- name: configmap-volume
configMap:
defaultMode: 0700
name: download-talos
32 changes: 32 additions & 0 deletions manifests/downloads/job-download-ubuntu-jammy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: batch/v1
kind: Job
metadata:
name: download-ubuntu-jammy
namespace: tink-system
spec:
template:
spec:
containers:
- name: download-ubuntu-jammy
image: bash:5.2.2
command: ["/script/entrypoint.sh"]
args:
[
"https://cloud-images.ubuntu.com/daily/server/jammy/current/jammy-server-cloudimg-amd64.img",
"/output",
]
volumeMounts:
- mountPath: /output
name: hook-artifacts
- mountPath: /script
name: configmap-volume
restartPolicy: OnFailure
volumes:
- name: hook-artifacts
hostPath:
path: /opt/hook
type: DirectoryOrCreate
- name: configmap-volume
configMap:
defaultMode: 0700
name: download-ubuntu-jammy
9 changes: 6 additions & 3 deletions manifests/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"embed"
)

//go:embed templates/*.yaml
var Templates embed.FS

//go:embed colony/*.yaml.tmpl
var Colony embed.FS

//go:embed downloads/*.yaml
var Downloads embed.FS

//go:embed templates/*.yaml
var Templates embed.FS

0 comments on commit 71a016c

Please sign in to comment.