|
| 1 | +// +build kube |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright 2020 Docker Compose CLI authors |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package client |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 28 | + "k8s.io/client-go/kubernetes" |
| 29 | + |
| 30 | + "github.com/docker/compose-cli/api/compose" |
| 31 | +) |
| 32 | + |
| 33 | +// KubeClient API to access kube objects |
| 34 | +type KubeClient struct { |
| 35 | + client *kubernetes.Clientset |
| 36 | + namespace string |
| 37 | +} |
| 38 | + |
| 39 | +// NewKubeClient new kubernetes client |
| 40 | +func NewKubeClient(config genericclioptions.RESTClientGetter) (*KubeClient, error) { |
| 41 | + restConfig, err := config.ToRESTConfig() |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + clientset, err := kubernetes.NewForConfig(restConfig) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + namespace, _, err := config.ToRawKubeConfigLoader().Namespace() |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + return &KubeClient{ |
| 57 | + client: clientset, |
| 58 | + namespace: namespace, |
| 59 | + }, nil |
| 60 | +} |
| 61 | + |
| 62 | +// GetContainers get containers for a given compose project |
| 63 | +func (kc KubeClient) GetContainers(ctx context.Context, projectName string, all bool) ([]compose.ContainerSummary, error) { |
| 64 | + fieldSelector := "" |
| 65 | + if !all { |
| 66 | + fieldSelector = "status.phase=Running" |
| 67 | + } |
| 68 | + |
| 69 | + pods, err := kc.client.CoreV1().Pods(kc.namespace).List(ctx, metav1.ListOptions{ |
| 70 | + LabelSelector: fmt.Sprintf("%s=%s", compose.ProjectTag, projectName), |
| 71 | + FieldSelector: fieldSelector, |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + result := []compose.ContainerSummary{} |
| 77 | + for _, pod := range pods.Items { |
| 78 | + result = append(result, podToContainerSummary(pod)) |
| 79 | + } |
| 80 | + |
| 81 | + return result, nil |
| 82 | +} |
| 83 | + |
| 84 | +func podToContainerSummary(pod v1.Pod) compose.ContainerSummary { |
| 85 | + return compose.ContainerSummary{ |
| 86 | + ID: pod.GetObjectMeta().GetName(), |
| 87 | + Name: pod.GetObjectMeta().GetName(), |
| 88 | + Service: pod.GetObjectMeta().GetLabels()[compose.ServiceTag], |
| 89 | + State: string(pod.Status.Phase), |
| 90 | + Project: pod.GetObjectMeta().GetLabels()[compose.ProjectTag], |
| 91 | + } |
| 92 | +} |
0 commit comments