github.com/kubevela/workflow@v0.6.0/pkg/utils/utils.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package utils
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  	"net/http"
    25  
    26  	"github.com/kubevela/pkg/multicluster"
    27  	corev1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/client-go/kubernetes"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  
    32  	wfContext "github.com/kubevela/workflow/pkg/context"
    33  	"github.com/kubevela/workflow/pkg/cue/model/value"
    34  	"github.com/kubevela/workflow/pkg/types"
    35  )
    36  
    37  // GetDataFromContext get data from workflow context
    38  func GetDataFromContext(ctx context.Context, cli client.Client, ctxName, name, ns string, paths ...string) (*value.Value, error) {
    39  	wfCtx, err := wfContext.LoadContext(cli, ns, name, ctxName)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	v, err := wfCtx.GetVar(paths...)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	if v.Error() != nil {
    48  		return nil, v.Error()
    49  	}
    50  	return v, nil
    51  }
    52  
    53  // GetLogConfigFromStep get log config from step
    54  func GetLogConfigFromStep(ctx context.Context, cli client.Client, ctxName, name, ns, step string) (*types.LogConfig, error) {
    55  	wfCtx, err := wfContext.LoadContext(cli, ns, name, ctxName)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	config := make(map[string]types.LogConfig)
    60  	c := wfCtx.GetMutableValue(types.ContextKeyLogConfig)
    61  	if c == "" {
    62  		return nil, fmt.Errorf("no log config found")
    63  	}
    64  
    65  	if err := json.Unmarshal([]byte(c), &config); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	stepConfig, ok := config[step]
    70  	if !ok {
    71  		return nil, fmt.Errorf("no log config found for step %s", step)
    72  	}
    73  	return &stepConfig, nil
    74  }
    75  
    76  // GetPodListFromResources get pod list from resources
    77  func GetPodListFromResources(ctx context.Context, cli client.Client, resources []types.Resource) ([]corev1.Pod, error) {
    78  	pods := make([]corev1.Pod, 0)
    79  	for _, resource := range resources {
    80  		cliCtx := multicluster.WithCluster(ctx, resource.Cluster)
    81  		if resource.LabelSelector != nil {
    82  			labels := &metav1.LabelSelector{
    83  				MatchLabels: resource.LabelSelector,
    84  			}
    85  			selector, err := metav1.LabelSelectorAsSelector(labels)
    86  			if err != nil {
    87  				return nil, err
    88  			}
    89  			var podList corev1.PodList
    90  			err = cli.List(cliCtx, &podList, &client.ListOptions{
    91  				LabelSelector: selector,
    92  			})
    93  			if err != nil {
    94  				return nil, err
    95  			}
    96  			pods = append(pods, podList.Items...)
    97  			continue
    98  		}
    99  		if resource.Namespace == "" {
   100  			resource.Namespace = metav1.NamespaceDefault
   101  		}
   102  		var pod corev1.Pod
   103  		err := cli.Get(cliCtx, client.ObjectKey{Namespace: resource.Namespace, Name: resource.Name}, &pod)
   104  		if err != nil {
   105  			return nil, err
   106  		}
   107  		pods = append(pods, pod)
   108  	}
   109  	if len(pods) == 0 {
   110  		return nil, fmt.Errorf("no pod found")
   111  	}
   112  	return pods, nil
   113  }
   114  
   115  // GetLogsFromURL get logs from url
   116  func GetLogsFromURL(ctx context.Context, url string) (io.ReadCloser, error) {
   117  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	resp, err := http.DefaultClient.Do(req)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	return resp.Body, nil
   126  }
   127  
   128  // GetLogsFromPod get logs from pod
   129  func GetLogsFromPod(ctx context.Context, clientSet kubernetes.Interface, cli client.Client, podName, ns, cluster string, opts *corev1.PodLogOptions) (io.ReadCloser, error) {
   130  	cliCtx := multicluster.WithCluster(ctx, cluster)
   131  	req := clientSet.CoreV1().Pods(ns).GetLogs(podName, opts)
   132  	readCloser, err := req.Stream(cliCtx)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	return readCloser, nil
   137  }