diff --git a/cmd/cofidectl/cmd/workload/workload.go b/cmd/cofidectl/cmd/workload/workload.go index 3a14e561..897d56c5 100644 --- a/cmd/cofidectl/cmd/workload/workload.go +++ b/cmd/cofidectl/cmd/workload/workload.go @@ -119,9 +119,11 @@ This command will display the status of workloads in the Cofide configuration st ` type StatusOpts struct { - podName string - namespace string - trustZone string + podName string + namespace string + trustZone string + spiffeSocketVolume string + spiffeSocketEndpoint string } func (w *WorkloadCommand) GetStatusCommand() *cobra.Command { @@ -150,6 +152,8 @@ func (w *WorkloadCommand) GetStatusCommand() *cobra.Command { f.StringVar(&opts.podName, "pod-name", "", "Pod name for the workload") f.StringVar(&opts.namespace, "namespace", "", "Namespace for the workload") f.StringVar(&opts.trustZone, "trust-zone", "", "Trust zone for the workload") + f.StringVar(&opts.spiffeSocketVolume, "spiffe-socket-volume", "spiffe-workload-api", "The volume for the SPIFFE socket (UDS)") + f.StringVar(&opts.spiffeSocketEndpoint, "spiffe-socket-endpoint", "", "The full SPIFFE socket (UDS) endpoint URI, which should be prefixed with the /spiffe-workload-api directory.") cobra.CheckErr(cmd.MarkFlagRequired("pod-name")) cobra.CheckErr(cmd.MarkFlagRequired("namespace")) @@ -174,7 +178,7 @@ func (w *WorkloadCommand) status(ctx context.Context, ds datasource.DataSource, return err } - statusCh, dataCh := getWorkloadStatus(ctx, client, opts.podName, opts.namespace) + statusCh, dataCh := getWorkloadStatus(ctx, client, opts.podName, opts.namespace, opts.spiffeSocketEndpoint, opts.spiffeSocketVolume) // Create a spinner to display whilst the debug container is created and executed and logs retrieved if err := statusspinner.WatchProvisionStatus(ctx, statusCh, false); err != nil { @@ -239,14 +243,14 @@ func renderRegisteredWorkloads(ctx context.Context, ds datasource.DataSource, ku return nil } -func getWorkloadStatus(ctx context.Context, client *kubeutil.Client, podName string, namespace string) (<-chan *provisionpb.Status, chan string) { +func getWorkloadStatus(ctx context.Context, client *kubeutil.Client, podName, namespace, spiffeSocketEndpoint, spiffeSocketVolume string) (<-chan *provisionpb.Status, chan string) { statusCh := make(chan *provisionpb.Status) dataCh := make(chan string, 1) go func() { defer close(statusCh) defer close(dataCh) - workload.GetStatus(ctx, statusCh, dataCh, client, podName, namespace) + workload.GetStatus(ctx, statusCh, dataCh, client, podName, namespace, spiffeSocketEndpoint, spiffeSocketVolume) }() return statusCh, dataCh diff --git a/internal/pkg/workload/workload.go b/internal/pkg/workload/workload.go index 4c06b261..92a07f46 100644 --- a/internal/pkg/workload/workload.go +++ b/internal/pkg/workload/workload.go @@ -20,7 +20,7 @@ import ( ) const debugContainerNamePrefix = "cofidectl-debug" -const debugContainerImage = "ghcr.io/cofide/cofidectl-debug-container:v0.2.1" +const debugContainerImage = "ghcr.io/cofide/cofidectl-debug-container:v0.2.2-dev" type Workload struct { Name string @@ -145,7 +145,7 @@ func GetUnregisteredWorkloads(ctx context.Context, kubeCfgFile string, kubeConte return unregisteredWorkloads, nil } -func GetStatus(ctx context.Context, statusCh chan<- *provisionpb.Status, dataCh chan string, client *kubeutil.Client, podName string, namespace string) { +func GetStatus(ctx context.Context, statusCh chan<- *provisionpb.Status, dataCh chan string, client *kubeutil.Client, podName, namespace, spiffeSocketEndpoint, spiffeSocketVolumeMount string) { debugContainerName := fmt.Sprintf("%s-%s", debugContainerNamePrefix, rand.String(5)) statusCh <- provision.StatusOk( @@ -153,7 +153,7 @@ func GetStatus(ctx context.Context, statusCh chan<- *provisionpb.Status, dataCh fmt.Sprintf("Waiting for ephemeral debug container to be created in %s", podName), ) - if err := createDebugContainer(ctx, client, podName, namespace, debugContainerName); err != nil { + if err := createDebugContainer(ctx, client, podName, namespace, spiffeSocketEndpoint, spiffeSocketVolumeMount, debugContainerName); err != nil { statusCh <- provision.StatusError( "Creating", fmt.Sprintf("Failed waiting for ephemeral debug container to be created in %s", podName), @@ -193,7 +193,7 @@ func GetStatus(ctx context.Context, statusCh chan<- *provisionpb.Status, dataCh ) } -func createDebugContainer(ctx context.Context, client *kubeutil.Client, podName string, namespace string, debugContainerName string) error { +func createDebugContainer(ctx context.Context, client *kubeutil.Client, podName, namespace, spiffeSocketEndpoint, spiffeSocketVolumeName, debugContainerName string) error { pod, err := client.Clientset.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{}) if err != nil { return err @@ -209,13 +209,20 @@ func createDebugContainer(ctx context.Context, client *kubeutil.Client, podName VolumeMounts: []v1.VolumeMount{ { ReadOnly: true, - Name: "spiffe-workload-api", + Name: spiffeSocketVolumeName, MountPath: "/spiffe-workload-api", }}, }, TargetContainerName: pod.Spec.Containers[0].Name, } + if spiffeSocketEndpoint != "" { + debugContainer.Env = append(debugContainer.Env, v1.EnvVar{ + Name: "SPIFFE_ENDPOINT_SOCKET", + Value: spiffeSocketEndpoint, + }) + } + pod.Spec.EphemeralContainers = append(pod.Spec.EphemeralContainers, debugContainer) _, err = client.Clientset.CoreV1().Pods(namespace).UpdateEphemeralContainers(