Skip to content

Commit

Permalink
move tenant UUID param from query to path
Browse files Browse the repository at this point in the history
  • Loading branch information
leoporoli committed Jul 2, 2024
1 parent 0f627e0 commit b45eeaa
Show file tree
Hide file tree
Showing 15 changed files with 411 additions and 508 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ minikube tunnel
Configure it by setting the following environment variables:

```bash
KARDINAL_MANAGER_CLUSTER_CONFIG_ENDPOINT=http://localhost:8080/cluster-resources
KARDINAL_MANAGER_CLUSTER_CONFIG_ENDPOINT=http://localhost:8080/tenant/{36e22127-3c9e-4110-aa83-af552cd94b88}/cluster-resources
KARDINAL_MANAGER_FETCHER_JOB_DURATION_SECONDS=10
KARDINAL_MANAGER_TENANT_UUID=36e22127-3c9e-4110-aa83-af552cd94b88
```

or in the `kardinal-manager/deployment/k8s.yaml`:
Expand All @@ -113,11 +112,9 @@ or in the `kardinal-manager/deployment/k8s.yaml`:
env:
- name: KARDINAL_MANAGER_CLUSTER_CONFIG_ENDPOINT
# This is valid for reaching out the Kardinal Kontrol if this is running on the host
value: "http://host.minikube.internal:8080/cluster-resources"
value: "http://localhost:8080/tenant/{36e22127-3c9e-4110-aa83-af552cd94b88}/cluster-resources"
- name: KARDINAL_MANAGER_FETCHER_JOB_DURATION_SECONDS
value: "10"
- name: KARDINAL_MANAGER_TENANT_UUID
value: "36e22127-3c9e-4110-aa83-af552cd94b88"
```
NOTE: you can get your tenant UUID by running any CLI command
Expand Down
41 changes: 13 additions & 28 deletions kardinal-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"kardinal.cli/tenant"
"log"
"net/http"
Expand Down Expand Up @@ -49,7 +48,8 @@ var deployCmd = &cobra.Command{
if err != nil {
log.Fatal("Error getting or creating user tenant UUID", err)
}
deploy(tenantUuid, services)

deploy(tenantUuid.String(), services)
},
}

Expand All @@ -70,7 +70,7 @@ var createCmd = &cobra.Command{
}

fmt.Printf("Creating service %s with image %s in development mode...\n", serviceName, imageName)
createDevFlow(tenantUuid, services, imageName, serviceName)
createDevFlow(tenantUuid.String(), services, imageName, serviceName)
},
}

Expand All @@ -88,7 +88,7 @@ var deleteCmd = &cobra.Command{
if err != nil {
log.Fatal("Error getting or creating user tenant UUID", err)
}
deleteFlow(tenantUuid, services)
deleteFlow(tenantUuid.String(), services)

fmt.Print("Deleting dev flow")
},
Expand Down Expand Up @@ -155,24 +155,17 @@ func parseComposeFile(composeFile string) ([]types.ServiceConfig, error) {
return project.Services, nil
}

func createDevFlow(tenantUuid uuid.UUID, services []types.ServiceConfig, imageLocator, serviceName string) {
func createDevFlow(tenantUuid api_types.Uuid, services []types.ServiceConfig, imageLocator, serviceName string) {
ctx := context.Background()

params := &api_types.PostFlowCreateParams{
Tenant: tenantUuid.String(),
}

// fmt.Printf("Services:\n%v", services)
// fmt.Printf("%v", serviceName)
// fmt.Printf("%v", imageLocator)
body := api_types.PostFlowCreateJSONRequestBody{
body := api_types.PostTenantUuidFlowCreateJSONRequestBody{
DockerCompose: &services,
ServiceName: &serviceName,
ImageLocator: &imageLocator,
}
client := getKontrolServiceClient()

resp, err := client.PostFlowCreateWithResponse(ctx, params, body)
resp, err := client.PostTenantUuidFlowCreateWithResponse(ctx, tenantUuid, body)
if err != nil {
log.Fatalf("Failed to create dev flow: %v", err)
}
Expand All @@ -181,39 +174,31 @@ func createDevFlow(tenantUuid uuid.UUID, services []types.ServiceConfig, imageLo
fmt.Printf("Response: %s\n", resp)
}

func deploy(tenantUuid uuid.UUID, services []types.ServiceConfig) {
func deploy(tenantUuid api_types.Uuid, services []types.ServiceConfig) {
ctx := context.Background()

params := &api_types.PostDeployParams{
Tenant: tenantUuid.String(),
}

body := api_types.PostDeployJSONRequestBody{
body := api_types.PostTenantUuidDeployJSONRequestBody{
DockerCompose: &services,
}
client := getKontrolServiceClient()

resp, err := client.PostDeployWithResponse(ctx, params, body)
resp, err := client.PostTenantUuidDeployWithResponse(ctx, tenantUuid, body)
if err != nil {
log.Fatalf("Failed to deploy: %v", err)
}

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

func deleteFlow(tenantUuid uuid.UUID, services []types.ServiceConfig) {
func deleteFlow(tenantUuid api_types.Uuid, services []types.ServiceConfig) {
ctx := context.Background()

params := &api_types.PostFlowDeleteParams{
Tenant: tenantUuid.String(),
}

body := api_types.PostFlowDeleteJSONRequestBody{
body := api_types.PostTenantUuidFlowDeleteJSONRequestBody{
DockerCompose: &services,
}
client := getKontrolServiceClient()

resp, err := client.PostFlowDeleteWithResponse(ctx, params, body)
resp, err := client.PostTenantUuidFlowDeleteWithResponse(ctx, tenantUuid, body)
if err != nil {
log.Fatalf("Failed to delete flow: %v", err)
}
Expand Down
4 changes: 1 addition & 3 deletions kardinal-manager/deployment/k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ spec:
value: "443"
- name: KARDINAL_MANAGER_CLUSTER_CONFIG_ENDPOINT
# This is valid for reaching out the Kardinal Kontrol if this is running on the host
value: "http://host.minikube.internal:8080/cluster-resources"
value: "http://host.minikube.internal:8080/tenant/58d33536-3c9e-4110-aa83-bf112ae94a49/cluster-resources"
- name: KARDINAL_MANAGER_FETCHER_JOB_DURATION_SECONDS
value: "10"
- name: KARDINAL_MANAGER_TENANT_UUID
value: "58d33536-3c9e-4110-aa83-bf112ae94a49"
17 changes: 4 additions & 13 deletions kardinal-manager/kardinal-manager/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ const (
type fetcher struct {
clusterManager *cluster_manager.ClusterManager
configEndpoint string
tenantUuidStr string
}

func NewFetcher(clusterManager *cluster_manager.ClusterManager, configEndpoint string, tenantUuidStr string) *fetcher {
return &fetcher{clusterManager: clusterManager, configEndpoint: configEndpoint, tenantUuidStr: tenantUuidStr}
func NewFetcher(clusterManager *cluster_manager.ClusterManager, configEndpoint string) *fetcher {
return &fetcher{clusterManager: clusterManager, configEndpoint: configEndpoint}
}

func (fetcher *fetcher) Run(ctx context.Context) error {
Expand Down Expand Up @@ -78,18 +77,10 @@ func (fetcher *fetcher) getClusterResourcesFromCloud() (*types.ClusterResources,

configEndpointURL, err := url.Parse(fetcher.configEndpoint)
if err != nil {
return nil, err
return nil, stacktrace.Propagate(err, "An error occurred parsing the config endpoint '%s'", fetcher.configEndpoint)
}

queryValues := configEndpointURL.Query()

queryValues.Add(tenantParamKey, fetcher.tenantUuidStr)

configEndpointURL.RawQuery = queryValues.Encode()

configEndpointURLStr := configEndpointURL.String()

resp, err := http.Get(configEndpointURLStr)
resp, err := http.Get(configEndpointURL.String())
if err != nil {
return nil, stacktrace.Propagate(err, "Error fetching cluster resources from endpoint '%s'", fetcher.configEndpoint)
}
Expand Down
7 changes: 1 addition & 6 deletions kardinal-manager/kardinal-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ func main() {
logrus.Fatal("An error occurred getting the config endpoint from the env vars!\nError was: %s", err)
}

tenantUuidStr, err := utils.GetFromEnvVar(tenantUuidEnvVarKey, "the tenant uuid")
if err != nil {
logrus.Fatal("An error occurred getting the tenant UUID from the env vars!\nError was: %s", err)
}

clusterManager, err := cluster_manager.CreateClusterManager()
if err != nil {
logrus.Fatal("An error occurred while creating the cluster manager!\nError was: %s", err)
}

fetcher := fetcher.NewFetcher(clusterManager, configEndpoint, tenantUuidStr)
fetcher := fetcher.NewFetcher(clusterManager, configEndpoint)

if err = fetcher.Run(ctx); err != nil {
logrus.Fatalf("An error occurred while running the fetcher!\nError was: %s", err)
Expand Down
Loading

0 comments on commit b45eeaa

Please sign in to comment.