Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
fix: Handle gateway case where the baseline is missing or not labelled (
Browse files Browse the repository at this point in the history
#245)

The CLI gateway command crashes if there is no baseline namespace. The
baseline namespace does not get labelled properly if it already exists.
This change fixes those issues.
  • Loading branch information
laurentluce authored Sep 23, 2024
1 parent 63c640e commit b0b6bb2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion kardinal-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ var gatewayCmd = &cobra.Command{
}

if err := deployment.StartGateway(ctx, hostFlowIdMap); err != nil {
log.Fatal("An error occurred while creating a gateway", err)
log.Fatalf("An error occurred while creating a gateway: %v", err)
}
},
}
Expand Down
35 changes: 19 additions & 16 deletions kardinal-cli/deployment/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,28 @@ func StartGateway(ctx context.Context, hostFlowIdMap map[string]string) error {
return fmt.Errorf("an error occurred while creating a kubernetes client:\n %v", err)
}

for host, flowId := range hostFlowIdMap {
logrus.Printf("Starting gateway for host: %s", host)
// TODO move these values to a shared library between Kardinal Manager, Kontrol and Kardinal CLI
kardinalLabelKey := "kardinal.dev"
enabledKardinal := "enabled"

// TODO move these values to a shared library between Kardinal Manager, Kontrol and Kardinal CLI
kardinalLabelKey := "kardinal.dev"
enabledKardinal := "enabled"
namespaceLabels := map[string]string{
kardinalLabelKey: enabledKardinal,
}

namespaceLabels := map[string]string{
kardinalLabelKey: enabledKardinal,
}
namespaceList, err := client.GetNamespacesByLabels(ctx, namespaceLabels)
if err != nil {
return fmt.Errorf("failed to list namespaces from Kubernetes: %v", err)
}
if len(namespaceList.Items) == 0 {
return fmt.Errorf("cannot start gateway because no Kardinal namespace was found")
}
if len(namespaceList.Items) > 1 {
return fmt.Errorf("cannot start gateway because more than one Kardinal namespace was found")
}
baselineNamespace := namespaceList.Items[0]

namespaceList, err := client.GetNamespacesByLabels(ctx, namespaceLabels)
if err != nil {
return fmt.Errorf("failed to list namespaces from Kubernetes: %v", err)
}
if len(namespaceList.Items) > 1 {
return fmt.Errorf("cannot start gateway because more than one Kardinal namespace was found")
}
baselineNamespace := namespaceList.Items[0]
for host, flowId := range hostFlowIdMap {
logrus.Printf("Starting gateway for host: %s", host)

// Check for pods in the baseline namespace
err = assertBaselineNamespaceReady(client.GetClientSet(), flowId, baselineNamespace.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,11 @@ func (manager *ClusterManager) ensureNamespace(ctx context.Context, name string)
value, found := existingNamespace.Labels[istioLabel]
if !found || value != enabledIstioValue {
existingNamespace.Labels[istioLabel] = enabledIstioValue
manager.kubernetesClient.clientSet.CoreV1().Namespaces().Update(ctx, existingNamespace, globalUpdateOptions)
existingNamespace.Labels[kardinalLabelKey] = enabledKardinal
_, err = manager.kubernetesClient.clientSet.CoreV1().Namespaces().Update(ctx, existingNamespace, globalUpdateOptions)
if err != nil {
return stacktrace.Propagate(err, "Failed to update Namespace: %s", name)
}
}
} else {
newNamespace := corev1.Namespace{
Expand Down

0 comments on commit b0b6bb2

Please sign in to comment.