2021-02-04 15:36:57 +01:00
|
|
|
// +build kube
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-02-04 16:57:06 +01:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-02-04 10:04:42 +01:00
|
|
|
"io"
|
2021-04-22 11:15:48 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-02-19 20:10:28 +01:00
|
|
|
"time"
|
2021-02-04 16:57:06 +01:00
|
|
|
|
2021-02-04 10:04:42 +01:00
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
|
|
"github.com/docker/compose-cli/utils"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
2021-02-04 16:57:06 +01:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-02-04 15:36:57 +01:00
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
2021-04-22 11:15:48 +02:00
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/portforward"
|
|
|
|
"k8s.io/client-go/transport/spdy"
|
2021-02-04 15:36:57 +01:00
|
|
|
)
|
|
|
|
|
2021-02-04 16:57:06 +01:00
|
|
|
// KubeClient API to access kube objects
|
2021-02-04 15:36:57 +01:00
|
|
|
type KubeClient struct {
|
2021-02-05 11:09:06 +01:00
|
|
|
client *kubernetes.Clientset
|
|
|
|
namespace string
|
2021-04-22 11:15:48 +02:00
|
|
|
config *rest.Config
|
2021-02-04 15:36:57 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 16:57:06 +01:00
|
|
|
// NewKubeClient new kubernetes client
|
2021-02-04 15:36:57 +01:00
|
|
|
func NewKubeClient(config genericclioptions.RESTClientGetter) (*KubeClient, error) {
|
|
|
|
restConfig, err := config.ToRESTConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
clientset, err := kubernetes.NewForConfig(restConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-05 11:09:06 +01:00
|
|
|
|
|
|
|
namespace, _, err := config.ToRawKubeConfigLoader().Namespace()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:36:57 +01:00
|
|
|
return &KubeClient{
|
2021-02-05 11:09:06 +01:00
|
|
|
client: clientset,
|
|
|
|
namespace: namespace,
|
2021-04-22 11:15:48 +02:00
|
|
|
config: restConfig,
|
2021-02-04 15:36:57 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2021-02-04 16:57:06 +01:00
|
|
|
|
|
|
|
// GetContainers get containers for a given compose project
|
|
|
|
func (kc KubeClient) GetContainers(ctx context.Context, projectName string, all bool) ([]compose.ContainerSummary, error) {
|
2021-02-05 09:36:37 +01:00
|
|
|
fieldSelector := ""
|
|
|
|
if !all {
|
|
|
|
fieldSelector = "status.phase=Running"
|
|
|
|
}
|
2021-02-04 16:57:06 +01:00
|
|
|
|
2021-02-05 11:09:06 +01:00
|
|
|
pods, err := kc.client.CoreV1().Pods(kc.namespace).List(ctx, metav1.ListOptions{
|
2021-02-05 09:36:37 +01:00
|
|
|
LabelSelector: fmt.Sprintf("%s=%s", compose.ProjectTag, projectName),
|
|
|
|
FieldSelector: fieldSelector,
|
|
|
|
})
|
2021-02-04 16:57:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := []compose.ContainerSummary{}
|
|
|
|
for _, pod := range pods.Items {
|
|
|
|
result = append(result, podToContainerSummary(pod))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 10:04:42 +01:00
|
|
|
// GetLogs retrieves pod logs
|
2021-02-05 15:03:28 +01:00
|
|
|
func (kc *KubeClient) GetLogs(ctx context.Context, projectName string, consumer compose.LogConsumer, follow bool) error {
|
|
|
|
pods, err := kc.client.CoreV1().Pods(kc.namespace).List(ctx, metav1.ListOptions{
|
2021-02-04 10:04:42 +01:00
|
|
|
LabelSelector: fmt.Sprintf("%s=%s", compose.ProjectTag, projectName),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
for _, pod := range pods.Items {
|
2021-02-05 15:03:28 +01:00
|
|
|
request := kc.client.CoreV1().Pods(kc.namespace).GetLogs(pod.Name, &corev1.PodLogOptions{Follow: follow})
|
2021-02-04 10:04:42 +01:00
|
|
|
service := pod.Labels[compose.ServiceTag]
|
2021-03-16 14:56:26 +01:00
|
|
|
w := utils.GetWriter(pod.Name, service, func(event compose.ContainerEvent) {
|
|
|
|
consumer.Log(event.Container, event.Service, event.Line)
|
2021-02-16 10:43:35 +01:00
|
|
|
})
|
2021-02-04 10:04:42 +01:00
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
r, err := request.Stream(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-19 20:10:28 +01:00
|
|
|
|
|
|
|
defer r.Close() // nolint errcheck
|
2021-02-04 10:04:42 +01:00
|
|
|
_, err = io.Copy(w, r)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return eg.Wait()
|
|
|
|
}
|
2021-02-19 20:10:28 +01:00
|
|
|
|
2021-03-02 16:19:19 +01:00
|
|
|
// WaitForPodState blocks until pods reach desired state
|
2021-02-23 12:46:55 +01:00
|
|
|
func (kc KubeClient) WaitForPodState(ctx context.Context, opts WaitForStatusOptions) error {
|
2021-05-19 17:00:13 +02:00
|
|
|
var timeout = time.Minute
|
2021-02-23 13:26:45 +01:00
|
|
|
if opts.Timeout != nil {
|
|
|
|
timeout = *opts.Timeout
|
2021-02-19 20:10:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 15:16:32 +01:00
|
|
|
errch := make(chan error, 1)
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2021-02-19 20:10:28 +01:00
|
|
|
|
2021-02-08 15:16:32 +01:00
|
|
|
pods, err := kc.client.CoreV1().Pods(kc.namespace).List(ctx, metav1.ListOptions{
|
2021-03-02 16:19:19 +01:00
|
|
|
LabelSelector: fmt.Sprintf("%s=%s", compose.ProjectTag, opts.ProjectName),
|
2021-02-08 15:16:32 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
|
|
|
}
|
2021-03-02 16:19:19 +01:00
|
|
|
stateReached, servicePods, err := checkPodsState(opts.Services, pods.Items, opts.Status)
|
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
2021-02-19 20:10:28 +01:00
|
|
|
}
|
2021-02-23 12:46:55 +01:00
|
|
|
if opts.Log != nil {
|
|
|
|
for p, m := range servicePods {
|
|
|
|
opts.Log(p, stateReached, m)
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 20:10:28 +01:00
|
|
|
|
2021-02-08 15:16:32 +01:00
|
|
|
if stateReached {
|
|
|
|
done <- true
|
2021-02-19 20:10:28 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-08 15:16:32 +01:00
|
|
|
}()
|
2021-02-19 20:10:28 +01:00
|
|
|
|
2021-02-08 15:16:32 +01:00
|
|
|
select {
|
2021-02-23 12:46:55 +01:00
|
|
|
case <-time.After(timeout):
|
|
|
|
return fmt.Errorf("timeout: pods did not reach expected state")
|
2021-02-08 15:16:32 +01:00
|
|
|
case err := <-errch:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-19 20:10:28 +01:00
|
|
|
}
|
2021-02-08 15:16:32 +01:00
|
|
|
case <-done:
|
2021-02-19 20:10:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-08 15:16:32 +01:00
|
|
|
return nil
|
2021-02-19 20:10:28 +01:00
|
|
|
}
|
2021-04-22 11:15:48 +02:00
|
|
|
|
|
|
|
func (kc KubeClient) MapPorts(ctx context.Context, opts PortMappingOptions) error {
|
|
|
|
|
|
|
|
stopChannel := make(chan struct{}, 1)
|
|
|
|
readyChannel := make(chan struct{})
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
for serviceName, servicePorts := range opts.Services {
|
|
|
|
serviceName = serviceName
|
|
|
|
servicePorts = servicePorts
|
|
|
|
eg.Go(func() error {
|
|
|
|
|
|
|
|
req := kc.client.RESTClient().Post().Resource("services").Namespace(kc.namespace).Name(serviceName).SubResource("portforward")
|
|
|
|
transport, upgrader, err := spdy.RoundTripperFor(kc.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ports := []string{}
|
|
|
|
for _, p := range servicePorts {
|
|
|
|
ports = append(ports, fmt.Sprintf("%d:%d", p.PublishedPort, p.TargetPort))
|
|
|
|
}
|
|
|
|
//println(req.URL().String())
|
|
|
|
//os.Exit(0)
|
|
|
|
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", req.URL())
|
|
|
|
fw, err := portforward.New(dialer, ports, stopChannel, readyChannel, os.Stdout, os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fw.ForwardPorts()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return eg.Wait()
|
|
|
|
}
|