Skip to content

Commit

Permalink
Merge pull request #97 from portainer/feat86-table-wide
Browse files Browse the repository at this point in the history
pod: add support for -o wide when listing pods
  • Loading branch information
deviantony authored Nov 10, 2024
2 parents 7dc11c5 + f601e8f commit 8cac22a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
9 changes: 9 additions & 0 deletions internal/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type (
//
// - Namespace deletion delay: Contains the delay that k2d waits after a namespace is deleted.
//
// - Node name: Contains the name of the node that the adapter is running on.
//
// This struct is a comprehensive utility for managing the interactions between Docker and Kubernetes.
KubeDockerAdapter struct {
cli *client.Client
Expand All @@ -59,6 +61,7 @@ type (
namespaceDeletionDelay time.Duration
registrySecretStore store.SecretStore
startTime time.Time
nodeName string
secretStore store.SecretStore
}

Expand Down Expand Up @@ -107,6 +110,11 @@ func NewKubeDockerAdapter(options *KubeDockerAdapterOptions) (*KubeDockerAdapter
return nil, fmt.Errorf("unable to initialize registry secret store: %w", err)
}

info, err := cli.Info(context.Background())
if err != nil {
return nil, fmt.Errorf("unable to get docker info: %w", err)
}

return &KubeDockerAdapter{
cli: cli,
converter: converter.NewDockerAPIConverter(configMapStore, secretStore, options.ServerConfiguration),
Expand All @@ -118,6 +126,7 @@ func NewKubeDockerAdapter(options *KubeDockerAdapterOptions) (*KubeDockerAdapter
registrySecretStore: registrySecretStore,
secretStore: secretStore,
startTime: time.Now(),
nodeName: info.Name,
}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/adapter/container_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,13 @@ func (adapter *KubeDockerAdapter) createContainerFromPodSpec(ctx context.Context
return fmt.Errorf("unable to convert versioned pod spec to internal pod spec: %w", err)
}

internalPodSpec.NodeName = adapter.nodeName

internalPodSpecData, err := json.Marshal(internalPodSpec)
if err != nil {
return fmt.Errorf("unable to marshal internal pod spec: %w", err)
}

options.labels[k2dtypes.PodLastAppliedConfigLabelKey] = string(internalPodSpecData)
options.labels[k2dtypes.NamespaceNameLabelKey] = options.namespace
options.labels[k2dtypes.WorkloadNameLabelKey] = options.containerName
Expand Down
13 changes: 12 additions & 1 deletion internal/adapter/converter/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"k8s.io/kubernetes/pkg/apis/core"
)

func (converter *DockerAPIConverter) ConvertInfoVersionToNode(info system.Info, version types.Version, startTime time.Time) core.Node {
func (converter *DockerAPIConverter) ConvertInfoVersionToNode(info system.Info, version types.Version, startTime time.Time, serverIpAddr string) core.Node {
return core.Node{
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Expand All @@ -37,6 +37,16 @@ func (converter *DockerAPIConverter) ConvertInfoVersionToNode(info system.Info,
ProviderID: "k2d",
},
Status: core.NodeStatus{
Addresses: []core.NodeAddress{
{
Type: core.NodeInternalIP,
Address: serverIpAddr,
},
{
Type: core.NodeExternalIP,
Address: serverIpAddr,
},
},
Conditions: []core.NodeCondition{
{
Type: "Ready",
Expand All @@ -55,6 +65,7 @@ func (converter *DockerAPIConverter) ConvertInfoVersionToNode(info system.Info,
MachineID: info.ID,
OperatingSystem: info.OSType,
SystemUUID: info.ID,
OSImage: info.OperatingSystem,
},
Capacity: core.ResourceList{
core.ResourceCPU: *resource.NewQuantity(int64(info.NCPU), resource.DecimalSI),
Expand Down
13 changes: 12 additions & 1 deletion internal/adapter/converter/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
//
// Returns:
// - A Kubernetes Pod object derived from the Docker container.
func (converter *DockerAPIConverter) ConvertContainerToPod(container types.Container) core.Pod {
func (converter *DockerAPIConverter) ConvertContainerToPod(container types.Container, nodeName string) core.Pod {
containerName := container.Labels[k2dtypes.WorkloadNameLabelKey]
containerState := container.State

Expand All @@ -51,6 +51,7 @@ func (converter *DockerAPIConverter) ConvertContainerToPod(container types.Conta
},
},
Spec: core.PodSpec{
NodeName: nodeName,
Containers: []core.Container{
{
Name: containerName,
Expand All @@ -69,6 +70,16 @@ func (converter *DockerAPIConverter) ConvertContainerToPod(container types.Conta
},
}

if container.NetworkSettings != nil && container.NetworkSettings.Networks != nil {
if networkSettings, ok := container.NetworkSettings.Networks[container.Labels[k2dtypes.NetworkNameLabelKey]]; ok {
pod.Status.PodIPs = []core.PodIP{
{
IP: networkSettings.IPAddress,
},
}
}
}

if containerState == "running" {
ready := true

Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (adapter *KubeDockerAdapter) getNode(ctx context.Context, nodeName string)
return nil, fmt.Errorf("unable to retrieve docker server version: %w", err)
}

node := adapter.converter.ConvertInfoVersionToNode(info, version, adapter.startTime)
node := adapter.converter.ConvertInfoVersionToNode(info, version, adapter.startTime, adapter.k2dServerConfiguration.ServerIpAddr)
return &node, nil
}

Expand All @@ -98,7 +98,7 @@ func (adapter *KubeDockerAdapter) listNodes(ctx context.Context) (core.NodeList,
APIVersion: "v1",
},
Items: []core.Node{
adapter.converter.ConvertInfoVersionToNode(info, version, adapter.startTime),
adapter.converter.ConvertInfoVersionToNode(info, version, adapter.startTime, adapter.k2dServerConfiguration.ServerIpAddr),
},
}, nil
}
2 changes: 1 addition & 1 deletion internal/adapter/pod_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// - core.Pod: The converted Pod object.
// - error: An error object if any error occurs during the conversion.
func (adapter *KubeDockerAdapter) buildPodFromContainer(container types.Container) (core.Pod, error) {
pod := adapter.converter.ConvertContainerToPod(container)
pod := adapter.converter.ConvertContainerToPod(container, adapter.nodeName)

if container.Labels[k2dtypes.PodLastAppliedConfigLabelKey] != "" {
internalPodSpecData := container.Labels[k2dtypes.PodLastAppliedConfigLabelKey]
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func GenerateTable(obj runtime.Object) (*metav1.Table, error) {
tableGenerator := printers.NewTableGenerator()
printersinternal.AddHandlers(tableGenerator)

options := printers.GenerateOptions{}
options := printers.GenerateOptions{Wide: true}
table, err := tableGenerator.GenerateTable(obj, options)
if err != nil {
return nil, err
Expand Down

0 comments on commit 8cac22a

Please sign in to comment.