From b0b6bb2f60f058528a51e31dc7b7cd9a146b84cc Mon Sep 17 00:00:00 2001 From: Laurent Luce Date: Mon, 23 Sep 2024 14:34:50 -0400 Subject: [PATCH] fix: Handle gateway case where the baseline is missing or not labelled (#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. --- kardinal-cli/cmd/root.go | 2 +- kardinal-cli/deployment/gateway.go | 35 ++++++++++--------- .../cluster_manager/cluster_manager.go | 6 +++- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/kardinal-cli/cmd/root.go b/kardinal-cli/cmd/root.go index 88a82c5..1dd94e0 100644 --- a/kardinal-cli/cmd/root.go +++ b/kardinal-cli/cmd/root.go @@ -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) } }, } diff --git a/kardinal-cli/deployment/gateway.go b/kardinal-cli/deployment/gateway.go index e640de0..cf63f71 100644 --- a/kardinal-cli/deployment/gateway.go +++ b/kardinal-cli/deployment/gateway.go @@ -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) diff --git a/kardinal-manager/kardinal-manager/cluster_manager/cluster_manager.go b/kardinal-manager/kardinal-manager/cluster_manager/cluster_manager.go index 3d6da63..e30083e 100644 --- a/kardinal-manager/kardinal-manager/cluster_manager/cluster_manager.go +++ b/kardinal-manager/kardinal-manager/cluster_manager/cluster_manager.go @@ -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{