Skip to content

Commit

Permalink
Revise code to add conversion webhook selector label without using da…
Browse files Browse the repository at this point in the history
…tastore.

Signed-off-by: James Munson <james.munson@suse.com>
  • Loading branch information
james-munson committed Jul 2, 2024
1 parent 189acee commit cf89658
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
33 changes: 27 additions & 6 deletions app/daemon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"context"
"fmt"
"net/http"
_ "net/http/pprof" // for runtime profiling
Expand All @@ -11,6 +12,7 @@ import (
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/longhorn/go-iscsi-helper/iscsi"

Expand Down Expand Up @@ -144,33 +146,52 @@ func startManager(c *cli.Context) error {
return fmt.Errorf("failed to detect the node IP")
}

podName, err := util.GetRequiredEnv(types.EnvPodName)
if err != nil {
return fmt.Errorf("failed to detect the manager pod name")
}

podNamespace, err := util.GetRequiredEnv(types.EnvPodNamespace)
if err != nil {
return fmt.Errorf("failed to detect the manager pod namespace")
}

ctx := signals.SetupSignalContext()

logger := logrus.StandardLogger().WithField("node", currentNodeID)

// Conversion webhook needs to be started first since we use its port 9501 as readiness port.
// longhorn-manager pod becomes ready only when conversion webhook is running.
// The services in the longhorn-manager can then start to receive the requests.
// Conversion webhook does not need or use longhorn datastore.
// Conversion webhook does not use datastore, since it is a prerequisite for
// datastore operation.
clientsWithoutDatastore, err := client.NewClients(kubeconfigPath, false, ctx.Done())
if err != nil {
return err
}
clients, err := client.NewClients(kubeconfigPath, true, ctx.Done())
if err != nil {
if err := webhook.StartWebhook(ctx, types.WebhookTypeConversion, clientsWithoutDatastore); err != nil {
return err
}

if err := webhook.StartWebhook(ctx, types.WebhookTypeConversion, clientsWithoutDatastore); err != nil {
return err
// This adds the label for the conversion webhook's selector. We do it the hard way without datastore to avoid chicken-and-egg.
pod, _ := clientsWithoutDatastore.Clients.K8s.CoreV1().Pods(podNamespace).Get(context.Background(), podName, v1.GetOptions{})
labels := types.GetConversionWebhookLabel()
for key, value := range labels {
pod.Labels[key] = value
}
if err := clients.Datastore.AddLabelToManagerPod(currentNodeID, types.GetConversionWebhookLabel()); err != nil {
_, err = clientsWithoutDatastore.Clients.K8s.CoreV1().Pods(podNamespace).Update(context.Background(), pod, v1.UpdateOptions{})
if err != nil {
return err
}
if err := webhook.CheckWebhookServiceAvailability(types.WebhookTypeConversion); err != nil {
return err
}

clients, err := client.NewClients(kubeconfigPath, true, ctx.Done())
if err != nil {
return err
}

if err := webhook.StartWebhook(ctx, types.WebhookTypeAdmission, clients); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ const (

const (
EnvNodeName = "NODE_NAME"
EnvPodName = "POD_NAME"
EnvPodNamespace = "POD_NAMESPACE"
EnvPodIP = "POD_IP"
EnvServiceAccount = "SERVICE_ACCOUNT"
Expand Down

0 comments on commit cf89658

Please sign in to comment.