Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 74 additions & 81 deletions cmd/rig/cmd/dev/kind/create.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package kind

import (
"bytes"
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"regexp"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
Expand Down Expand Up @@ -58,7 +54,7 @@ func (c Cmd) create(cmd *cobra.Command, args []string) error {
fmt.Println()
fmt.Println("Creating a new Rig cluster requries setting up an admin user and project")
fmt.Println("Run the following command once the new Rig server has finished starting up")
fmt.Println("kubectl", "exec", "--tty", "--stdin", "--namespace", "rig-system", "deploy/rig", "--", "rig-admin", "init")
fmt.Println("kubectl", "exec", "--tty", "--stdin", "--namespace", "rig-system", "deploy/rig-platform", "--", "rig-admin", "init")

return nil
}
Expand All @@ -69,69 +65,97 @@ func (c Cmd) deploy(cmd *cobra.Command, args []string) error {
return err
}

var err error
if dockerTag == "" {
dockerTag, err = getLatestTag("rig")
if err != nil {
return err
}
}
dockerTag = strings.TrimPrefix(dockerTag, "v")
rigImage := fmt.Sprintf("ghcr.io/rigdev/rig:%s", dockerTag)
res, err := c.DockerClient.ImageList(ctx, types.ImageListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{
Key: "reference",
Value: rigImage,
}),
})
if err != nil {
if platformDockerTag == "" {
platformDockerTag = "latest"
}
if err := c.deployInner(ctx, deployParams{
dockerImage: "ghcr.io/rigdev/rig-platform",
dockerTag: platformDockerTag,
chartName: "rig-platform",
chartPath: platformChartPath,
customArgs: []string{"--set", fmt.Sprintf("image.tag=%s", platformDockerTag),
"--set", "rig.telemetry.enabled=false",
"--set", "postgres.enabled=true",
"--set", "rig.cluster.dev_registry.host=localhost:30000",
"--set", "rig.cluster.dev_registry.cluster_host=registry:5000",
"--set", "service.type=NodePort"},
}); err != nil {
return err
}
if len(res) == 0 {
if err := runCmd("docker", "pull", rigImage); err != nil {
return err
}
}

if err := runCmd("kind", "load", "docker-image", rigImage, "-n", "rig"); err != nil {
if operatorDockerTag == "" {
operatorDockerTag = "latest"
}
if err := c.deployInner(ctx, deployParams{
dockerImage: "ghcr.io/rigdev/rig-operator",
dockerTag: operatorDockerTag,
chartName: "rig-operator",
chartPath: operatorChartPath,
customArgs: []string{"--set", fmt.Sprintf("image.tag=%s", operatorDockerTag)},
}); err != nil {
return err
}

if helmChartTag == "" {
helmChartTag, err = getLatestTag("charts")
if err != nil {
return err
}
}
return nil
}

type deployParams struct {
dockerImage string
dockerTag string
chartName string
chartPath string
customArgs []string
}

chart := "rig"
if chartPath != "" {
chart = chartPath
func (c Cmd) deployInner(ctx context.Context, p deployParams) error {
if err := c.loadImage(ctx, p.dockerImage, p.dockerTag); err != nil {
return err
}
chart := p.chartName
if p.chartPath != "" {
chart = p.chartPath
}
cArgs := []string{
"--kube-context", "kind-rig",
"upgrade", "--install", "rig", chart,
"upgrade", "--install", p.chartName, chart,
"--namespace", "rig-system",
"--version", dockerTag,
"--set", fmt.Sprintf("image.tag=%s", dockerTag),
"--set", "mongodb.enabled=true",
"--set", "rig.telemetry.enabled=false",
"--set", "rig.cluster.dev_registry.host=localhost:30000",
"--set", "rig.cluster.dev_registry.cluster_host=registry:5000",
"--set", "service.type=NodePort",
"--set", fmt.Sprintf("image.tag=%s", operatorDockerTag),
"--create-namespace",
}
if chartPath == "" {
cArgs = append(args, "--repo", "https://charts.rig.dev")
cArgs = append(cArgs, p.customArgs...)
if operatorChartPath == "" {
cArgs = append(cArgs, "--repo", "https://charts.rig.dev")
}

if err := runCmd(
"helm", cArgs...,
); err != nil {
if err := runCmd("helm", cArgs...); err != nil {
return err
}

if err := runCmd("kubectl", "--context", "kind-rig", "rollout", "restart", "deployment", "-n", "rig-system", "rig"); err != nil {
if err := runCmd("kubectl", "--context", "kind-rig", "rollout", "restart", "deployment", "-n", "rig-system", p.chartName); err != nil {
return err
}

return nil
}

func (c Cmd) loadImage(ctx context.Context, image, tag string) error {
imageTag := fmt.Sprintf("%s:%s", image, tag)
res, err := c.DockerClient.ImageList(ctx, types.ImageListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{
Key: "reference",
Value: imageTag,
}),
})
if err != nil {
return err
}
if len(res) == 0 {
if err := runCmd("docker", "pull", imageTag); err != nil {
return err
}
}

if err := runCmd("kind", "load", "docker-image", imageTag, "-n", "rig"); err != nil {
return err
}

Expand Down Expand Up @@ -171,37 +195,6 @@ func setupKindRigCluster() error {
return nil
}

func getLatestTag(repo string) (string, error) {
client := http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("http://api.github.com/repos/rigdev/%s/releases/latest", repo), nil)
if err != nil {
return "", err
}
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
resp, err := client.Do(req)
if err != nil {
return "", err
}

var buffer bytes.Buffer
b := make([]byte, 1000)
n := 0
for !errors.Is(err, io.EOF) {
n, err = resp.Body.Read(b)
buffer.Write(b[:n])
}

tag := struct {
TagName string `json:"tag_name"`
}{}
if err := json.Unmarshal(buffer.Bytes(), &tag); err != nil {
return "", err
}

return tag.TagName, nil
}

func setupK8s() error {
if err := runCmd("kubectl", "--context", "kind-rig", "get", "namespace", "rig-system"); err != nil {
if err := runCmd("kubectl", "--context", "kind-rig", "create", "namespace", "rig-system"); err != nil {
Expand Down
21 changes: 12 additions & 9 deletions cmd/rig/cmd/dev/kind/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
)

var (
dockerTag string
helmChartTag string
chartPath string
platformDockerTag string
platformChartPath string
operatorDockerTag string
operatorChartPath string
)

type Cmd struct {
Expand Down Expand Up @@ -42,9 +43,10 @@ func (c *Cmd) Setup(parent *cobra.Command) {
base.OmitProject: "",
},
}
create.Flags().StringVarP(&dockerTag, "docker-tag", "d", "", "The Rig docker image tag. Defaults to the latest one")
create.Flags().StringVarP(&helmChartTag, "helm-chart-tag", "c", "", "The tag of Rig's Helm chart. Defaults to the latest one")
create.Flags().StringVarP(&chartPath, "chart-path", "p", "", "If set, uses the helm chart at chart-path to build Rig.")
create.Flags().StringVarP(&platformDockerTag, "platform-docker-tag", "p", "", "The rig-platform docker image tag. Defaults to latest.")
create.Flags().StringVar(&platformChartPath, "platform-chart-path", "", "If set, uses the helm chart at platform-chart-path to build rig-platform.")
create.Flags().StringVarP(&operatorDockerTag, "operator-docker-tag", "o", "", "The rig-operator docker image tag. Defaults to latest.")
create.Flags().StringVar(&operatorChartPath, "operator-chart-path", "", "If set, uses the helm chart at operator-chart-path to build rig-operator.")
kind.AddCommand(create)

deploy := &cobra.Command{
Expand All @@ -58,9 +60,10 @@ func (c *Cmd) Setup(parent *cobra.Command) {
},
}
kind.AddCommand(deploy)
deploy.Flags().StringVarP(&dockerTag, "docker-tag", "d", "", "The Rig docker image tag. Defaults to the latest one")
deploy.Flags().StringVarP(&helmChartTag, "helm-chart-tag", "c", "", "The tag of Rig's Helm chart. Defaults to the latest one")
deploy.Flags().StringVarP(&chartPath, "chart-path", "p", "", "If set, uses the helm chart at chart-path to build Rig.")
deploy.Flags().StringVarP(&platformDockerTag, "platform-docker-tag", "p", "", "The rig-platform docker image tag. Defaults to latest.")
deploy.Flags().StringVar(&platformChartPath, "platform-chart-path", "", "If set, uses the helm chart at platform-chart-path to build rig-platform.")
deploy.Flags().StringVarP(&operatorDockerTag, "operator-docker-tag", "o", "", "The rig-operator docker image tag. Defaults to latest.")
deploy.Flags().StringVar(&operatorChartPath, "operator-chart-path", "", "If set, uses the helm chart at operator-chart-path to build rig-operator.")

clean := &cobra.Command{
Use: "clean",
Expand Down