diff --git a/app/daemon.go b/app/daemon.go index bb217a0181..e1c391b1b8 100644 --- a/app/daemon.go +++ b/app/daemon.go @@ -1,6 +1,7 @@ package app import ( + "context" "fmt" "net/http" _ "net/http/pprof" // for runtime profiling @@ -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" @@ -144,6 +146,16 @@ 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) @@ -151,26 +163,35 @@ func startManager(c *cli.Context) error { // 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 } diff --git a/types/types.go b/types/types.go index 805f0edacd..1ce94c5861 100644 --- a/types/types.go +++ b/types/types.go @@ -221,6 +221,7 @@ const ( const ( EnvNodeName = "NODE_NAME" + EnvPodName = "POD_NAME" EnvPodNamespace = "POD_NAMESPACE" EnvPodIP = "POD_IP" EnvServiceAccount = "SERVICE_ACCOUNT"