Skip to content

Commit

Permalink
feat: parse namespace k8s objects (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
tedim52 authored Aug 19, 2024
1 parent b99682e commit cd0cef7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
24 changes: 15 additions & 9 deletions kardinal-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var deployCmd = &cobra.Command{
Short: "Deploy services",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
serviceConfigs, ingressConfigs, err := parseKubernetesManifestFile(kubernetesManifestFile)
serviceConfigs, ingressConfigs, namespace, err := parseKubernetesManifestFile(kubernetesManifestFile)
if err != nil {
log.Fatalf("Error loading k8s manifest file: %v", err)
}
Expand All @@ -88,7 +88,7 @@ var deployCmd = &cobra.Command{
log.Fatal("Error getting or creating user tenant UUID", err)
}

deploy(tenantUuid.String(), serviceConfigs, ingressConfigs)
deploy(tenantUuid.String(), serviceConfigs, ingressConfigs, namespace)
},
}

Expand All @@ -104,7 +104,7 @@ var templateCreateCmd = &cobra.Command{
// A valid template only modifies services
// A valid template has metadata.name
// A valid template modifies at least one service
serviceConfigs, _, err := parseKubernetesManifestFile(templateYamlFile)
serviceConfigs, _, _, err := parseKubernetesManifestFile(templateYamlFile)
if err != nil {
log.Fatalf("Error loading template file: %v", err)
}
Expand Down Expand Up @@ -390,14 +390,15 @@ func parsePairs(pairs []string) map[string]string {
return pairsMap
}

func parseKubernetesManifestFile(kubernetesManifestFile string) ([]api_types.ServiceConfig, []api_types.IngressConfig, error) {
func parseKubernetesManifestFile(kubernetesManifestFile string) ([]api_types.ServiceConfig, []api_types.IngressConfig, string, error) {
fileBytes, err := loadKubernetesManifestFile(kubernetesManifestFile)
if err != nil {
log.Fatalf("Error loading kubernetest manifest file: %v", err)
return nil, nil, err
return nil, nil, "", err
}

manifest := string(fileBytes)
var namespace string
serviceConfigs := map[string]*api_types.ServiceConfig{}
ingressConfigs := map[string]*api_types.IngressConfig{}
decode := scheme.Codecs.UniversalDeserializer().Decode
Expand All @@ -407,7 +408,7 @@ func parseKubernetesManifestFile(kubernetesManifestFile string) ([]api_types.Ser
}
obj, _, err := decode([]byte(spec), nil, nil)
if err != nil {
return nil, nil, stacktrace.Propagate(err, "An error occurred parsing the spec: %s", spec)
return nil, nil, "", stacktrace.Propagate(err, "An error occurred parsing the spec: %s", spec)
}
switch obj := obj.(type) {
case *corev1.Service:
Expand Down Expand Up @@ -436,8 +437,12 @@ func parseKubernetesManifestFile(kubernetesManifestFile string) ([]api_types.Ser
ingress := obj
ingressName := getObjectName(ingress.GetObjectMeta().(*metav1.ObjectMeta))
ingressConfigs[ingressName] = &api_types.IngressConfig{Ingress: *ingress}
case *corev1.Namespace:
namespaceObj := obj
namespaceName := getObjectName(namespaceObj.GetObjectMeta().(*metav1.ObjectMeta))
namespace = namespaceName
default:
return nil, nil, stacktrace.NewError("An error occurred parsing the manifest because of an unsupported kubernetes type")
return nil, nil, "", stacktrace.NewError("An error occurred parsing the manifest because of an unsupported kubernetes type")
}
}

Expand All @@ -451,7 +456,7 @@ func parseKubernetesManifestFile(kubernetesManifestFile string) ([]api_types.Ser
finalIngressConfigs = append(finalIngressConfigs, *ingressConfig)
}

return finalServiceConfigs, finalIngressConfigs, nil
return finalServiceConfigs, finalIngressConfigs, namespace, nil
}

func parseTemplateArgs(filename string) (map[string]interface{}, error) {
Expand Down Expand Up @@ -557,12 +562,13 @@ func createDevFlow(tenantUuid api_types.Uuid, pairsMap map[string]string, templa
os.Exit(1)
}

func deploy(tenantUuid api_types.Uuid, serviceConfigs []api_types.ServiceConfig, ingressConfigs []api_types.IngressConfig) {
func deploy(tenantUuid api_types.Uuid, serviceConfigs []api_types.ServiceConfig, ingressConfigs []api_types.IngressConfig, namespace string) {
ctx := context.Background()

body := api_types.PostTenantUuidDeployJSONRequestBody{
ServiceConfigs: &serviceConfigs,
IngressConfigs: &ingressConfigs,
Namespace: &namespace,
}
client := getKontrolServiceClient()

Expand Down
46 changes: 23 additions & 23 deletions libs/cli-kontrol-api/api/golang/server/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/cli-kontrol-api/api/golang/types/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/cli-kontrol-api/api/typescript/client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export interface components {
MainClusterConfig: {
"service-configs"?: components["schemas"]["ServiceConfig"][];
"ingress-configs"?: components["schemas"]["IngressConfig"][];
namespace?: string;
};
Flow: {
"flow-id": string;
Expand Down
2 changes: 2 additions & 0 deletions libs/cli-kontrol-api/specs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ components:
type: array
items:
$ref: "#/components/schemas/IngressConfig"
namespace:
type: string

Flow:
type: object
Expand Down

0 comments on commit cd0cef7

Please sign in to comment.