Skip to content

Commit

Permalink
use client-go for configmap and job creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jarededwards committed Nov 19, 2024
1 parent f3b01e3 commit 2b22f20
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 199 deletions.
131 changes: 44 additions & 87 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/konstructio/colony/internal/k8s"
"github.com/konstructio/colony/internal/logger"
"github.com/spf13/cobra"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -87,8 +87,8 @@ func getInitCommand() *cobra.Command {
// Create the secret
apiKeySecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "colony-api",
Namespace: "tink-system",
Name: constants.ColonyAPISecretName,
Namespace: constants.ColonyNamespace,
},
Data: map[string][]byte{
"api-key": []byte(apiKey),
Expand All @@ -108,7 +108,7 @@ func getInitCommand() *cobra.Command {
mgmtKubeConfigSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "mgmt-kubeconfig",
Namespace: "tink-system",
Namespace: constants.ColonyNamespace,
},
Data: map[string][]byte{
"kubeconfig": k8sconfig,
Expand Down Expand Up @@ -140,7 +140,7 @@ func getInitCommand() *cobra.Command {
ctx,
"app.kubernetes.io/name",
"colony-agent",
"tink-system",
constants.ColonyNamespace,
180,
)
if err != nil {
Expand All @@ -156,7 +156,7 @@ func getInitCommand() *cobra.Command {
ctx,
"app",
"hegel",
"tink-system",
constants.ColonyNamespace,
180,
)
if err != nil {
Expand All @@ -172,7 +172,7 @@ func getInitCommand() *cobra.Command {
ctx,
"app",
"rufio",
"tink-system",
constants.ColonyNamespace,
180,
)
if err != nil {
Expand All @@ -188,7 +188,7 @@ func getInitCommand() *cobra.Command {
ctx,
"app",
"smee",
"tink-system",
constants.ColonyNamespace,
180,
)
if err != nil {
Expand All @@ -204,7 +204,7 @@ func getInitCommand() *cobra.Command {
ctx,
"app",
"tink-server",
"tink-system",
constants.ColonyNamespace,
180,
)
if err != nil {
Expand All @@ -220,7 +220,7 @@ func getInitCommand() *cobra.Command {
ctx,
"app",
"tink-controller",
"tink-system",
constants.ColonyNamespace,
180,
)
if err != nil {
Expand Down Expand Up @@ -289,86 +289,44 @@ func getInitCommand() *cobra.Command {
return fmt.Errorf("error patching ClusterRole: %w", err)
}

talosConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "download-talos",
Namespace: "tink-system",
var downloadJobs = []configs.DownloadJob{
{
DownloadURL: "https://github.com/siderolabs/talos/releases/download/v1.8.0",
Name: "talos",
},
Data: map[string]string{
"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`,
{
DownloadURL: "https://cloud-images.ubuntu.com/daily/server/jammy/current/jammy-server-cloudimg-amd64.img",
Name: "ubuntu-jammy",
},
}

err = k8sClient.CreateConfigMap(ctx, talosConfigMap)
if err != nil {
return fmt.Errorf("error creating configmap: %w", err)
}
talosJob := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "download-talos",
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "download-talos",
Image: "bash:5.2.2",
Command: []string{"/script/entrypoint.sh"},
Args: []string{
"https://github.com/siderolabs/talos/releases/download/v1.8.0",
"/output",
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "hook-artifacts",
MountPath: "/output",
},
{
Name: "configmap-volume",
MountPath: "/script",
},
},
},
},
RestartPolicy: corev1.RestartPolicyOnFailure,
Volumes: []corev1.Volume{
{
Name: "hook-artifacts",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/opt/hook",
Type: new(corev1.HostPathType), // DirectoryOrCreate by default
},
},
},
{
Name: "configmap-volume",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
DefaultMode: new(int32), // 0700 in octal
},
},
},
},
},
},
},
}
err = k8sClient.CreateJob(ctx, talosJob)
if err != nil {
return fmt.Errorf("error creating job: %w", err)
for _, job := range downloadJobs {

Check failure on line 303 in cmd/init.go

View workflow job for this annotation

GitHub Actions / run-tests

unnecessary leading newline (whitespace)

script, err := os.ReadFile(filepath.Join(pwd, "scripts", 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.Name, job.DownloadURL)
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)
}

}

Check failure on line 330 in cmd/init.go

View workflow job for this annotation

GitHub Actions / run-tests

unnecessary trailing newline (whitespace)

return nil
Expand All @@ -381,7 +339,6 @@ func getInitCommand() *cobra.Command {
cmd.Flags().StringVar(&loadBalancerIP, "load-balancer-ip", "", "the local network interface for colony to use")

cmd.MarkFlagRequired("api-key")
cmd.MarkFlagRequired("api-url")
cmd.MarkFlagRequired("load-balancer-interface")
cmd.MarkFlagRequired("load-balancer-ip")
return cmd
Expand Down
5 changes: 5 additions & 0 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ var Version = DefaultVersion
type Config struct {
Version string
}

type DownloadJob struct {
DownloadURL string
Name string
}
80 changes: 78 additions & 2 deletions internal/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (c *Client) PatchClusterRole(ctx context.Context, clusterRoleName string, c
return nil
}

func (c *Client) CreateSecret(ctx context.Context, secret *v1.Secret) error {
func (c *Client) CreateSecret(ctx context.Context, secret *corev1.Secret) error {
s, err := c.clientSet.CoreV1().Secrets(secret.GetNamespace()).Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("error creating secret: %w", err)
Expand All @@ -127,7 +127,7 @@ func (c *Client) CreateSecret(ctx context.Context, secret *v1.Secret) error {
return nil
}

func (c *Client) CreateConfigMap(ctx context.Context, configMap *v1.ConfigMap) error {
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)
Expand Down Expand Up @@ -304,3 +304,79 @@ func (c *Client) ReturnDeploymentObject(ctx context.Context, matchLabel string,
}
return deployment, nil
}

func BuildJob(downloadURL, name string) (*batchv1.Job, error) {

Check failure on line 308 in internal/k8s/k8s.go

View workflow job for this annotation

GitHub Actions / run-tests

unnecessary leading newline (whitespace)

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{"/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

}

Check failure on line 366 in internal/k8s/k8s.go

View workflow job for this annotation

GitHub Actions / run-tests

unnecessary trailing newline (whitespace)

func BuildConfigMap(name, script string) (*corev1.ConfigMap, error) {

Check failure on line 368 in internal/k8s/k8s.go

View workflow job for this annotation

GitHub Actions / run-tests

unnecessary leading newline (whitespace)

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

}

Check failure on line 382 in internal/k8s/k8s.go

View workflow job for this annotation

GitHub Actions / run-tests

unnecessary trailing newline (whitespace)
Loading

0 comments on commit 2b22f20

Please sign in to comment.