Skip to content

Commit

Permalink
remove unused structs move resources to .colony dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jarededwards committed Nov 19, 2024
1 parent bed4544 commit a5bff01
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 74 deletions.
57 changes: 49 additions & 8 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package cmd
import (
"encoding/json"
"fmt"
"html/template"
"os"
"path/filepath"
"time"

"github.com/konstructio/colony/internal/colony"
"github.com/konstructio/colony/internal/constants"
"github.com/konstructio/colony/internal/docker"
"github.com/konstructio/colony/internal/exec"
"github.com/konstructio/colony/internal/k8s"
"github.com/konstructio/colony/internal/logger"
"github.com/konstructio/colony/manifests"
Expand All @@ -20,6 +22,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ColonyTokens struct {
LoadBalancerIP string
LoadBalancerInterface string
}

func getInitCommand() *cobra.Command {
var apiKey, apiURL, loadBalancerIP, loadBalancerInterface string

Expand All @@ -43,23 +50,57 @@ func getInitCommand() *cobra.Command {

log.Info("colony api key provided is valid")

dockerCLI, err := docker.New(log)
//!
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error creating docker client: %w", err)
return fmt.Errorf("error getting user home directory: %w", err)
}
defer dockerCLI.Close()

pwd, err := os.Getwd()
err = exec.CreateDirIfNotExist(filepath.Join(homeDir, constants.ColonyDir, "k3s-bootstrap"))
if err != nil {
return fmt.Errorf("error creating directory templates: %w", err)
}

colonyYamlTmpl, err := manifests.Colony.ReadFile(fmt.Sprintf("colony/%s.tmpl", constants.ColonyYamlPath))
if err != nil {
return fmt.Errorf("error reading templates file: %w", err)
}

tmpl, err := template.New("colony").Parse(string(colonyYamlTmpl))
if err != nil {
return fmt.Errorf("error parsing template: %w", err)
}

colonyK3sBootstrapPath := filepath.Join(homeDir, constants.ColonyDir, "k3s-bootstrap", constants.ColonyYamlPath)
colonyKubeconfigPath := filepath.Join(homeDir, constants.ColonyDir, constants.KubeconfigHostPath)

outputFile, err := os.Create(colonyK3sBootstrapPath)
if err != nil {
return fmt.Errorf("error creating output file: %w", err)
}
defer outputFile.Close()

err = tmpl.Execute(outputFile, &ColonyTokens{
LoadBalancerIP: loadBalancerIP,
LoadBalancerInterface: loadBalancerInterface,
})
if err != nil {
return fmt.Errorf("error getting current working directory: %w", err)
return fmt.Errorf("error executing template: %w", err)
}
//!

dockerCLI, err := docker.New(log)
if err != nil {
return fmt.Errorf("error creating docker client: %w", err)
}
defer dockerCLI.Close()

err = dockerCLI.CreateColonyK3sContainer(ctx, loadBalancerIP, loadBalancerInterface, pwd)
err = dockerCLI.CreateColonyK3sContainer(ctx, colonyK3sBootstrapPath, colonyKubeconfigPath)
if err != nil {
return fmt.Errorf("error creating container: %w", err)
}

k8sClient, err := k8s.New(log, filepath.Join(pwd, constants.KubeconfigHostPath))
k8sClient, err := k8s.New(log, colonyKubeconfigPath)
if err != nil {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}
Expand Down Expand Up @@ -160,7 +201,7 @@ func getInitCommand() *cobra.Command {
return fmt.Errorf("error waiting for deployment: %w", err)
}

k8sClient, err = k8s.New(log, filepath.Join(pwd, constants.KubeconfigHostPath))
k8sClient, err = k8s.New(log, colonyKubeconfigPath)
if err != nil {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}
Expand Down
5 changes: 0 additions & 5 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,3 @@ var Version = DefaultVersion
type Config struct {
Version string
}

type DownloadJob struct {
DownloadURL string
Name string
}
1 change: 1 addition & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
KubeconfigHostPath = "kubeconfig"
KubeconfigDockerPath = "/output/kubeconfig"
ColonyYamlPath = "colony.yaml"
ColonyDir = ".colony"
ColonyNamespace = "tink-system"
ColonyAPISecretName = "colony-api"
)
72 changes: 11 additions & 61 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"
"errors"
"fmt"
"html/template"
"io"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -17,7 +15,6 @@ import (
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/konstructio/colony/internal/constants"
"github.com/konstructio/colony/internal/download"
"github.com/konstructio/colony/internal/logger"
)

Expand All @@ -28,11 +25,6 @@ type Client struct {
log *logger.Logger
}

type ColonyTokens struct {
LoadBalancerIP string
LoadBalancerInterface string
}

func New(logger *logger.Logger) (*Client, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
Expand Down Expand Up @@ -96,28 +88,9 @@ func (c *Client) RemoveColonyK3sContainer(ctx context.Context) error {
return nil
}

func (c *Client) CreateColonyK3sContainer(ctx context.Context, loadBalancerIP, loadBalancerInterface, pwd string) error {
func (c *Client) CreateColonyK3sContainer(ctx context.Context, colonyK3sBootstrapPath, colonyKubeconfigPath string) error {
log := logger.New(logger.Debug)

// TODO tag a new repo for permanent housing, removes templates from database
colonyTemplateURL := "https://raw.githubusercontent.com/konstructio/colony/refs/heads/add-jobs/manifests/colony/colony.yaml.tmpl"
colonyTemplateYaml := filepath.Join(pwd, fmt.Sprintf("%s.tmpl", constants.ColonyYamlPath))

err := download.FileFromURL(colonyTemplateURL, colonyTemplateYaml)
if err != nil {
return fmt.Errorf("error downloading file: %w", err)
}

log.Infof("downloaded colony.yaml successfully to %q", colonyTemplateYaml)

err = hydrateTemplate(pwd, ColonyTokens{
LoadBalancerIP: loadBalancerIP,
LoadBalancerInterface: loadBalancerInterface,
})
if err != nil {
return fmt.Errorf("error hydrating template: %w", err)
}

// check for an existing colony-k3s container
k3sColonyContainer, err := c.getColonyK3sContainer(ctx)
if k3sColonyContainer != nil {
Expand All @@ -144,24 +117,24 @@ func (c *Client) CreateColonyK3sContainer(ctx context.Context, loadBalancerIP, l
io.Copy(os.Stdout, reader)

env := []string{
fmt.Sprintf("K3S_KUBECONFIG_OUTPUT=%s", constants.KubeconfigDockerPath),
fmt.Sprintf("K3S_KUBECONFIG_OUTPUT=%s", colonyKubeconfigPath),
"K3S_KUBECONFIG_MODE=666",
}

mounts := []mount.Mount{
{
Type: mount.TypeBind,
Source: pwd,
Source: colonyKubeconfigPath,
Target: "/output",
},
{
Type: mount.TypeVolume,
Source: "k3s-server",
Target: "/var/lib/rancher/k3s",
},
// {
// Type: mount.TypeVolume,
// Source: "k3s-server",
// Target: "/var/lib/rancher/k3s",
// },
{
Type: mount.TypeBind,
Source: filepath.Join(pwd, constants.ColonyYamlPath),
Source: colonyK3sBootstrapPath,
Target: fmt.Sprintf("/var/lib/rancher/k3s/server/manifests/%s", constants.ColonyYamlPath),
},
{
Expand Down Expand Up @@ -201,9 +174,9 @@ func (c *Client) CreateColonyK3sContainer(ctx context.Context, loadBalancerIP, l
waitInterval := 2 * time.Second
timeout := 15 * time.Second

log.Infof("Checking for file %s every %.2f seconds...", filepath.Join(pwd, constants.KubeconfigHostPath), waitInterval.Seconds())
log.Infof("Checking for file %s every %.0f seconds...", colonyKubeconfigPath, waitInterval.Seconds())

err = waitUntilFileExists(log, filepath.Join(pwd, constants.KubeconfigHostPath), waitInterval, timeout)
err = waitUntilFileExists(log, colonyKubeconfigPath, waitInterval, timeout)
if err != nil {
return fmt.Errorf("error waiting for kubeconfig file: %w", err)
}
Expand Down Expand Up @@ -237,26 +210,3 @@ func waitUntilFileExists(log *logger.Logger, filename string, interval, timeout
}
}
}

func hydrateTemplate(pwd string, colonyTokens ColonyTokens) error {
tmpl, err := template.ParseFiles(filepath.Join(pwd, fmt.Sprintf("%s.tmpl", constants.ColonyYamlPath)))
if err != nil {
return fmt.Errorf("error parsing template: %w", err)
}

outputFile, err := os.Create(filepath.Join(pwd, constants.ColonyYamlPath))
if err != nil {
return fmt.Errorf("error creating output file: %w", err)
}
defer outputFile.Close()

err = tmpl.Execute(outputFile, &ColonyTokens{
LoadBalancerIP: colonyTokens.LoadBalancerIP,
LoadBalancerInterface: colonyTokens.LoadBalancerInterface,
})
if err != nil {
return fmt.Errorf("error executing template: %w", err)
}

return nil
}

0 comments on commit a5bff01

Please sign in to comment.