Skip to content

Commit

Permalink
feat: store Kontrol location (#20)
Browse files Browse the repository at this point in the history
- Storing the Kontrol location value to be reused between the cmds
- Print traffic configuration URL
- Use the Kardinal Manager container's image pull policy depending on
the fetching destination

---------

Co-authored-by: lostbean <[email protected]>
  • Loading branch information
leoporoli and lostbean authored Jul 10, 2024
1 parent 40e9d75 commit ad42d22
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 42 deletions.
111 changes: 85 additions & 26 deletions kardinal-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"kardinal.cli/deployment"
"kardinal.cli/kontrol"
"kardinal.cli/tenant"
"log"
"net/http"
Expand All @@ -23,13 +25,14 @@ const (
kontrolServiceApiUrl = "ad718d90d54d54dd084dea50a9f011af-1140086995.us-east-1.elb.amazonaws.com"
kontrolServicePort = 8080

kontrolLocationLocalMinikube = "local-minikube"
kontrolLocationKloudKontrol = "kloud-kontrol"
kontrolBaseURLTmpl = "%s://%s"
kontrolClusterResourcesEndpointTmpl = "%s/tenant/%s/cluster-resources"

kontrolClusterResourcesEndpointTmpl = "%s://%s/tenant/%s/cluster-resources"
kontrolTrafficConfigurationURLTmpl = "%s/%s/traffic-configuration"

localMinikubeKontrolAPIHost = "host.minikube.internal:8080"
kloudKontrolAPIHost = "app.kardinal.dev/api"
kloudKontrolHost = "app.kardinal.dev"
kloudKontrolAPIHost = kloudKontrolHost + "/api"

httpSchme = "http"
httpsScheme = httpSchme + "s"
Expand Down Expand Up @@ -112,24 +115,28 @@ var deleteCmd = &cobra.Command{
}

var deployManagerCmd = &cobra.Command{
Use: fmt.Sprintf("deploy [kontrol location] accepted values: %s and %s ", kontrolLocationLocalMinikube, kontrolLocationKloudKontrol),
Use: fmt.Sprintf("deploy [kontrol location] accepted values: %s and %s ", kontrol.KontrolLocationLocalMinikube, kontrol.KontrolLocationKloudKontrol),
Short: "Deploy Kardinal manager into the cluster",
ValidArgs: []string{kontrolLocationLocalMinikube, kontrolLocationKloudKontrol},
ValidArgs: []string{kontrol.KontrolLocationLocalMinikube, kontrol.KontrolLocationKloudKontrol},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {

kontroLocation := args[0]
kontrolLocation := args[0]

if err := kontrol.SaveKontrolLocation(kontrolLocation); err != nil {
log.Fatal("Error saving the Kontrol location", err)
}

tenantUuid, err := tenant.GetOrCreateUserTenantUUID()
if err != nil {
log.Fatal("Error getting or creating user tenant UUID", err)
}

if err := deployManager(tenantUuid.String(), kontroLocation); err != nil {
if err := deployManager(tenantUuid.String(), kontrolLocation); err != nil {
log.Fatal("Error deploying Kardinal manager", err)
}

fmt.Printf("Kardinal manager deployed using '%s' Kontrol", kontroLocation)
logrus.Infof("Kardinal manager deployed using '%s' Kontrol", kontrolLocation)
},
}

Expand Down Expand Up @@ -241,6 +248,14 @@ func deploy(tenantUuid api_types.Uuid, services []types.ServiceConfig) {
}

fmt.Printf("Response: %s\n", string(resp.Body))

trafficConfigurationURL, err := getTrafficConfigurationURL(tenantUuid)
if err != nil {
logrus.Warningf("The command run successfully but it was impossible to print the traffic configuration URL because and error ocurred, please make sure to run the 'kardinal manager deploy' command first")
return
}

logrus.Infof("Visit: %s", trafficConfigurationURL)
}

func deleteFlow(tenantUuid api_types.Uuid, services []types.ServiceConfig) {
Expand All @@ -260,26 +275,15 @@ func deleteFlow(tenantUuid api_types.Uuid, services []types.ServiceConfig) {
}

func deployManager(tenantUuid api_types.Uuid, kontrolLocation string) error {
var (
ctx = context.Background()
scheme string
host string
)

switch kontrolLocation {
case kontrolLocationLocalMinikube:
scheme = httpSchme
host = localMinikubeKontrolAPIHost
case kontrolLocationKloudKontrol:
scheme = httpsScheme
host = kloudKontrolAPIHost
default:
return stacktrace.NewError("invalid kontrol location: %s", kontrolLocation)
}
ctx := context.Background()

clusterResourcesURL := fmt.Sprintf(kontrolClusterResourcesEndpointTmpl, scheme, host, tenantUuid)
clusterResourcesURL, err := getClusterResourcesURL(tenantUuid)
if err != nil {
return stacktrace.Propagate(err, "Error getting cluster resources URL")
}

if err := deployment.DeployKardinalManagerInCluster(ctx, clusterResourcesURL); err != nil {
if err := deployment.DeployKardinalManagerInCluster(ctx, clusterResourcesURL, kontrolLocation); err != nil {
return stacktrace.Propagate(err, "An error occurred deploying Kardinal manager into the cluster with cluster resources URL '%s'", clusterResourcesURL)
}

Expand Down Expand Up @@ -311,3 +315,58 @@ func getKontrolServiceClient() *api.ClientWithResponses {
return client
}
}

func getKontrolBaseURL(useApiHost bool) (string, error) {
kontrolLocation, err := kontrol.GetKontrolLocation()
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred getting the Kontrol location")
}

var (
scheme string
host string
)

switch kontrolLocation {
case kontrol.KontrolLocationLocalMinikube:
scheme = httpSchme
host = localMinikubeKontrolAPIHost
case kontrol.KontrolLocationKloudKontrol:
scheme = httpsScheme
if useApiHost {
host = kloudKontrolAPIHost
} else {
host = kloudKontrolHost
}
default:
return "", stacktrace.NewError("invalid Kontrol location: %s", kontrolLocation)
}

baseURL := fmt.Sprintf(kontrolBaseURLTmpl, scheme, host)

return baseURL, nil
}

func getTrafficConfigurationURL(tenantUuid api_types.Uuid) (string, error) {

kontrolBaseURL, err := getKontrolBaseURL(false)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred getting the Kontrol base URL")
}

trafficConfigurationURL := fmt.Sprintf(kontrolTrafficConfigurationURLTmpl, kontrolBaseURL, tenantUuid)

return trafficConfigurationURL, nil
}

func getClusterResourcesURL(tenantUuid api_types.Uuid) (string, error) {

kontrolBaseURL, err := getKontrolBaseURL(true)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred getting the Kontrol base URL")
}

clusterResourcesURL := fmt.Sprintf(kontrolClusterResourcesEndpointTmpl, kontrolBaseURL, tenantUuid)

return clusterResourcesURL, nil
}
33 changes: 24 additions & 9 deletions kardinal-cli/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package deployment
import (
"bytes"
"context"
"kardinal.cli/kontrol"
"text/template"

"github.com/kurtosis-tech/stacktrace"
Expand Down Expand Up @@ -72,6 +73,7 @@ spec:
containers:
- name: kardinal-manager
image: kurtosistech/kardinal-manager:latest
imagePullPolicy: {{.KardinalManagerContainerImagePullPolicy}}
env:
- name: KUBERNETES_SERVICE_HOST
value: "kubernetes.default.svc"
Expand All @@ -85,13 +87,14 @@ spec:
)

type templateData struct {
Namespace string
ClusterResourcesURL string
KardinalAppIDLabelKey string
KardinalManagerAppIDLabelValue string
Namespace string
ClusterResourcesURL string
KardinalAppIDLabelKey string
KardinalManagerAppIDLabelValue string
KardinalManagerContainerImagePullPolicy string
}

func DeployKardinalManagerInCluster(ctx context.Context, clusterResourcesURL string) error {
func DeployKardinalManagerInCluster(ctx context.Context, clusterResourcesURL string, kontrolLocation string) error {
kubernetesClientObj, err := createKubernetesClient()
if err != nil {
return stacktrace.Propagate(err, "An error occurred while creating the Kubernetes client")
Expand All @@ -102,11 +105,23 @@ func DeployKardinalManagerInCluster(ctx context.Context, clusterResourcesURL str
return stacktrace.Propagate(err, "An error occurred while parsing the kardinal-manager deployment template")
}

var imagePullPolicy string

switch kontrolLocation {
case kontrol.KontrolLocationLocalMinikube:
imagePullPolicy = "Never"
case kontrol.KontrolLocationKloudKontrol:
imagePullPolicy = "Always"
default:
stacktrace.NewError("invalid Kontrol location: %s", kontrolLocation)
}

templateDataObj := templateData{
Namespace: kardinalNamespace,
ClusterResourcesURL: clusterResourcesURL,
KardinalAppIDLabelKey: consts.KardinalAppIDLabelKey,
KardinalManagerAppIDLabelValue: consts.KardinalManagerAppIDLabelValue,
Namespace: kardinalNamespace,
ClusterResourcesURL: clusterResourcesURL,
KardinalAppIDLabelKey: consts.KardinalAppIDLabelKey,
KardinalManagerAppIDLabelValue: consts.KardinalManagerAppIDLabelValue,
KardinalManagerContainerImagePullPolicy: imagePullPolicy,
}

yamlFileContentsBuffer := &bytes.Buffer{}
Expand Down
42 changes: 38 additions & 4 deletions kardinal-cli/go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
module kardinal.cli

go 1.22
go 1.22.0

toolchain go1.22.3

require (
github.com/adrg/xdg v0.4.0
github.com/compose-spec/compose-go v1.20.2
github.com/google/uuid v1.5.0
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409
github.com/spf13/cobra v1.8.0
k8s.io/apimachinery v0.30.2
k8s.io/client-go v0.30.2
)

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.30.2 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api => ../libs/cli-kontrol-api
Expand All @@ -23,7 +59,6 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -38,6 +73,5 @@ require (
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1
)
Loading

0 comments on commit ad42d22

Please sign in to comment.