kubevela/pkg/utils/common/common.go

430 lines
13 KiB
Go
Raw Normal View History

/*
Copyright 2021 The KubeVela 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 common
import (
"bytes"
2020-11-18 16:08:16 +08:00
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
2020-11-18 16:08:16 +08:00
"net/http"
neturl "net/url"
"os"
"os/exec"
"path/filepath"
Feat: support to manage the configs by the CLI, UI, and workflow. (#4794) * Feat: support to manage the integrations by the CLI and the workflow Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: remove the xml Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: add the unit test for the nacos writer Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: add the integration API Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: make the provider commands to be deprecated Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: make the unit test work Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: rename the integration to the config Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: make the unit test cases work Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: refactor the config commands Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: add the distribution status for the config Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: sort the import packages Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: refine the code style Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: refine the code style Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: get the content format before render the content Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: add some examples Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: the command test cases Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: add the definitions of the workflow step Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: add some tests Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: add some tests Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: change the name Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: retry the CI Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: refine some words Signed-off-by: barnettZQG <barnett.zqg@gmail.com> Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
2022-10-17 17:15:45 +08:00
"runtime/debug"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/encoding/openapi"
"github.com/AlecAivazis/survey/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/oam-dev/terraform-config-inspect/tfconfig"
kruise "github.com/openkruise/kruise-api/apps/v1alpha1"
kruisev1alpha1 "github.com/openkruise/rollouts/api/v1alpha1"
yamlv3 "gopkg.in/yaml.v3"
Refactor: align velaux env and CLI env, they both use K8s namespace as (#2975) * Refactor: use createOrUpdateNamespace as a common util function Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: add ENV webservice handelr * Fix: fix Env usecase logic * Feat: Add Delete Env API Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: filter empty addon data Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: split makefiels and make it clear * Feat: add k8s utils test * Feat: Add env update interface Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: change env implementation Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: minor fix * Revert "Fix: minor fix" This reverts commit 9cafefa65a384795315924cb18e48681dd7086a1. * Fix: use appusecase as parameter Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Refactor: align CLI vela env with new env design Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: minor fix Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: add page index and alias of env Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: fix tests and licence header * Fix: fix makefile and add default target Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: update build swagger.json Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: change update env api Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: list env with alias * Feat: add log to env delete * Fix: can not get app status Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: support update workflow and refactor code * Fix: lint * Fix: remove swagger check Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: fix cli vela delete * Fix: update test Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: update test Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: app deploy unit test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: SortOrderDescending is not effective Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: support default project/target/env * Fix: make test and add swagger * Fix: use separated datasource for unit test * Fix: app rollback bug Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: fix e2e test * Fix: kubeapi driver sort bug Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test * Fix: api e2e test Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test fix * Fix: try fix e2e test * Fix: api e2e test Signed-off-by: barnettZQG <barnett.zqg@gmail.com> Co-authored-by: barnettZQG <barnett.zqg@gmail.com>
2021-12-25 10:36:54 +08:00
v1 "k8s.io/api/core/v1"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
metricsV1beta1api "k8s.io/metrics/pkg/apis/metrics/v1beta1"
ocmclusterv1 "open-cluster-management.io/api/cluster/v1"
2021-08-06 10:46:31 +08:00
ocmclusterv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1"
ocmworkv1 "open-cluster-management.io/api/work/v1"
2021-08-11 23:05:10 +08:00
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"sigs.k8s.io/yaml"
"github.com/kubevela/workflow/pkg/cue/model/value"
"github.com/kubevela/workflow/pkg/cue/packages"
clustergatewayapi "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1"
terraformapiv1 "github.com/oam-dev/terraform-controller/api/v1beta1"
terraformapi "github.com/oam-dev/terraform-controller/api/v1beta2"
workflowv1alpha1 "github.com/kubevela/workflow/api/v1alpha1"
oamcore "github.com/oam-dev/kubevela/apis/core.oam.dev"
"github.com/oam-dev/kubevela/apis/types"
velacue "github.com/oam-dev/kubevela/pkg/cue"
"github.com/oam-dev/kubevela/pkg/cue/process"
Refactor: align velaux env and CLI env, they both use K8s namespace as (#2975) * Refactor: use createOrUpdateNamespace as a common util function Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: add ENV webservice handelr * Fix: fix Env usecase logic * Feat: Add Delete Env API Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: filter empty addon data Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: split makefiels and make it clear * Feat: add k8s utils test * Feat: Add env update interface Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: change env implementation Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: minor fix * Revert "Fix: minor fix" This reverts commit 9cafefa65a384795315924cb18e48681dd7086a1. * Fix: use appusecase as parameter Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Refactor: align CLI vela env with new env design Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: minor fix Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: add page index and alias of env Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: fix tests and licence header * Fix: fix makefile and add default target Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: update build swagger.json Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: change update env api Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: list env with alias * Feat: add log to env delete * Fix: can not get app status Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: support update workflow and refactor code * Fix: lint * Fix: remove swagger check Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: fix cli vela delete * Fix: update test Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: update test Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: app deploy unit test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: SortOrderDescending is not effective Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: support default project/target/env * Fix: make test and add swagger * Fix: use separated datasource for unit test * Fix: app rollback bug Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: fix e2e test * Fix: kubeapi driver sort bug Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test * Fix: api e2e test Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test fix * Fix: try fix e2e test * Fix: api e2e test Signed-off-by: barnettZQG <barnett.zqg@gmail.com> Co-authored-by: barnettZQG <barnett.zqg@gmail.com>
2021-12-25 10:36:54 +08:00
"github.com/oam-dev/kubevela/pkg/oam"
)
var (
// Scheme defines the default KubeVela schema
Scheme = k8sruntime.NewScheme()
)
Refactor: align velaux env and CLI env, they both use K8s namespace as (#2975) * Refactor: use createOrUpdateNamespace as a common util function Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: add ENV webservice handelr * Fix: fix Env usecase logic * Feat: Add Delete Env API Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: filter empty addon data Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: split makefiels and make it clear * Feat: add k8s utils test * Feat: Add env update interface Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: change env implementation Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: minor fix * Revert "Fix: minor fix" This reverts commit 9cafefa65a384795315924cb18e48681dd7086a1. * Fix: use appusecase as parameter Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Refactor: align CLI vela env with new env design Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: minor fix Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: add page index and alias of env Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: fix tests and licence header * Fix: fix makefile and add default target Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: update build swagger.json Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: change update env api Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: list env with alias * Feat: add log to env delete * Fix: can not get app status Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: support update workflow and refactor code * Fix: lint * Fix: remove swagger check Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: fix cli vela delete * Fix: update test Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: update test Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: app deploy unit test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: SortOrderDescending is not effective Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: support default project/target/env * Fix: make test and add swagger * Fix: use separated datasource for unit test * Fix: app rollback bug Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: fix e2e test * Fix: kubeapi driver sort bug Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test * Fix: api e2e test Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test fix * Fix: try fix e2e test * Fix: api e2e test Signed-off-by: barnettZQG <barnett.zqg@gmail.com> Co-authored-by: barnettZQG <barnett.zqg@gmail.com>
2021-12-25 10:36:54 +08:00
// CreateCustomNamespace display the create namespace message
const CreateCustomNamespace = "create new namespace"
func init() {
_ = clientgoscheme.AddToScheme(Scheme)
_ = apiregistrationv1.AddToScheme(Scheme)
_ = crdv1.AddToScheme(Scheme)
_ = oamcore.AddToScheme(Scheme)
_ = kruise.AddToScheme(Scheme)
_ = terraformapi.AddToScheme(Scheme)
_ = terraformapiv1.AddToScheme(Scheme)
2021-08-06 10:46:31 +08:00
_ = ocmclusterv1alpha1.Install(Scheme)
_ = ocmclusterv1.Install(Scheme)
2021-08-06 10:46:31 +08:00
_ = ocmworkv1.Install(Scheme)
_ = clustergatewayapi.AddToScheme(Scheme)
_ = metricsV1beta1api.AddToScheme(Scheme)
_ = kruisev1alpha1.AddToScheme(Scheme)
_ = gatewayv1beta1.AddToScheme(Scheme)
_ = workflowv1alpha1.AddToScheme(Scheme)
// +kubebuilder:scaffold:scheme
}
// HTTPOption define the https options
type HTTPOption struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
CaFile string `json:"caFile,omitempty"`
CertFile string `json:"certFile,omitempty"`
KeyFile string `json:"keyFile,omitempty"`
InsecureSkipTLS bool `json:"insecureSkipTLS,omitempty"`
}
// InitBaseRestConfig will return reset config for create controller runtime client
func InitBaseRestConfig() (Args, error) {
args := Args{
Schema: Scheme,
}
_, err := args.GetConfig()
if err != nil && os.Getenv("IGNORE_KUBE_CONFIG") != "true" {
fmt.Println("get kubeConfig err", err)
os.Exit(1)
} else if err != nil {
return Args{}, err
}
return args, nil
}
2020-11-18 16:08:16 +08:00
// HTTPGetResponse use HTTP option and default client to send request and get raw response
func HTTPGetResponse(ctx context.Context, url string, opts *HTTPOption) (*http.Response, error) {
2020-11-18 16:08:16 +08:00
// Change NewRequest to NewRequestWithContext and pass context it
if _, err := neturl.ParseRequestURI(url); err != nil {
return nil, err
}
2020-11-18 16:08:16 +08:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
httpClient := &http.Client{}
if opts != nil && len(opts.Username) != 0 && len(opts.Password) != 0 {
req.SetBasicAuth(opts.Username, opts.Password)
}
if opts != nil && opts.InsecureSkipTLS {
httpClient.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} // nolint
}
// if specify the caFile, we cannot re-use the default httpClient, so create a new one.
if opts != nil && (len(opts.CaFile) != 0 || len(opts.KeyFile) != 0 || len(opts.CertFile) != 0) {
// must set MinVersion of TLS, otherwise will report GoSec error G402
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12}
tr := http.Transport{}
if len(opts.CaFile) != 0 {
c := x509.NewCertPool()
if !(c.AppendCertsFromPEM([]byte(opts.CaFile))) {
return nil, fmt.Errorf("failed to append certificates")
}
tlsConfig.RootCAs = c
}
if len(opts.CertFile) != 0 && len(opts.KeyFile) != 0 {
cert, err := tls.X509KeyPair([]byte(opts.CertFile), []byte(opts.KeyFile))
if err != nil {
return nil, err
}
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
}
tr.TLSClientConfig = tlsConfig
defer tr.CloseIdleConnections()
httpClient.Transport = &tr
}
return httpClient.Do(req)
}
// HTTPGetWithOption use HTTP option and default client to send get request
func HTTPGetWithOption(ctx context.Context, url string, opts *HTTPOption) ([]byte, error) {
resp, err := HTTPGetResponse(ctx, url, opts)
2020-11-18 16:08:16 +08:00
if err != nil {
return nil, err
}
//nolint:errcheck
defer resp.Body.Close()
return io.ReadAll(resp.Body)
2020-11-18 16:08:16 +08:00
}
// HTTPGetKubernetesObjects use HTTP requests to load resources from remote url
func HTTPGetKubernetesObjects(ctx context.Context, url string) ([]*unstructured.Unstructured, error) {
resp, err := HTTPGetResponse(ctx, url, nil)
if err != nil {
return nil, err
}
//nolint:errcheck
defer resp.Body.Close()
decoder := yamlv3.NewDecoder(resp.Body)
var uns []*unstructured.Unstructured
for {
obj := &unstructured.Unstructured{Object: map[string]interface{}{}}
if err := decoder.Decode(obj.Object); err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, fmt.Errorf("failed to decode object: %w", err)
}
uns = append(uns, obj)
}
return uns, nil
}
// GetCUEParameterValue converts definitions to cue format
func GetCUEParameterValue(cueStr string, pd *packages.PackageDiscover) (cue.Value, error) {
template, err := value.NewValue(cueStr+velacue.BaseTemplate, pd, "")
if err != nil {
return cue.Value{}, err
}
val, err := template.LookupValue(process.ParameterFieldName)
if err != nil || !val.CueValue().Exists() {
return cue.Value{}, velacue.ErrParameterNotExist
}
return val.CueValue(), nil
}
// GenOpenAPI generates OpenAPI json schema from cue.Instance
func GenOpenAPI(val *value.Value) (b []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("invalid cue definition to generate open api: %v", r)
Feat: support to manage the configs by the CLI, UI, and workflow. (#4794) * Feat: support to manage the integrations by the CLI and the workflow Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: remove the xml Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: add the unit test for the nacos writer Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: add the integration API Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: make the provider commands to be deprecated Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: make the unit test work Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: rename the integration to the config Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: make the unit test cases work Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: refactor the config commands Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: add the distribution status for the config Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: sort the import packages Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: refine the code style Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: refine the code style Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: get the content format before render the content Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: add some examples Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: the command test cases Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: add the definitions of the workflow step Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: add some tests Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: add some tests Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: change the name Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: retry the CI Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: refine some words Signed-off-by: barnettZQG <barnett.zqg@gmail.com> Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
2022-10-17 17:15:45 +08:00
debug.PrintStack()
return
}
}()
if val.CueValue().Err() != nil {
return nil, val.CueValue().Err()
}
paramOnlyVal, err := RefineParameterValue(val)
if err != nil {
return nil, err
}
defaultConfig := &openapi.Config{ExpandReferences: true}
b, err = openapi.Gen(paramOnlyVal, defaultConfig)
if err != nil {
return nil, err
}
var out = &bytes.Buffer{}
_ = json.Indent(out, b, "", " ")
return out.Bytes(), nil
}
// GenOpenAPIWithCueX generates OpenAPI json schema from cue.Instance
func GenOpenAPIWithCueX(val cue.Value) (b []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("invalid cue definition to generate open api: %v", r)
debug.PrintStack()
return
}
}()
if val.Err() != nil {
return nil, val.Err()
}
paramOnlyVal := FillParameterDefinitionFieldIfNotExist(val)
defaultConfig := &openapi.Config{ExpandReferences: true}
b, err = openapi.Gen(paramOnlyVal, defaultConfig)
if err != nil {
return nil, err
}
var out = &bytes.Buffer{}
_ = json.Indent(out, b, "", " ")
return out.Bytes(), nil
}
// RefineParameterValue refines cue value to merely include `parameter` identifier
func RefineParameterValue(val *value.Value) (cue.Value, error) {
defaultValue := cuecontext.New().CompileString("#parameter: {}")
parameterPath := cue.MakePath(cue.Def(process.ParameterFieldName))
v, err := val.MakeValue("{}")
if err != nil {
return defaultValue, err
}
paramVal, err := val.LookupValue(process.ParameterFieldName)
if err != nil {
// nolint:nilerr
return defaultValue, nil
}
switch k := paramVal.CueValue().IncompleteKind(); k {
case cue.BottomKind:
return defaultValue, nil
default:
paramOnlyVal := v.CueValue().FillPath(parameterPath, paramVal.CueValue())
return paramOnlyVal, nil
}
}
// FillParameterDefinitionFieldIfNotExist refines cue value to merely include `parameter` identifier
func FillParameterDefinitionFieldIfNotExist(val cue.Value) cue.Value {
defaultValue := cuecontext.New().CompileString("#parameter: {}")
defPath := cue.ParsePath("#" + process.ParameterFieldName)
if paramVal := val.LookupPath(cue.ParsePath(process.ParameterFieldName)); paramVal.Exists() {
if paramVal.IncompleteKind() == cue.BottomKind {
return defaultValue
}
paramOnlyVal := val.Context().CompileString("{}").FillPath(defPath, paramVal)
return paramOnlyVal
}
return defaultValue
}
// RealtimePrintCommandOutput prints command output in real time
// If logFile is "", it will prints the stdout, or it will write to local file
func RealtimePrintCommandOutput(cmd *exec.Cmd, logFile string) error {
var writer io.Writer
if logFile == "" {
writer = io.MultiWriter(os.Stdout)
} else {
if _, err := os.Stat(filepath.Dir(logFile)); err != nil {
return err
}
f, err := os.Create(filepath.Clean(logFile))
if err != nil {
return err
}
writer = io.MultiWriter(f)
}
cmd.Stdout = writer
cmd.Stderr = writer
if err := cmd.Run(); err != nil {
return err
}
return nil
}
Refactor: align velaux env and CLI env, they both use K8s namespace as (#2975) * Refactor: use createOrUpdateNamespace as a common util function Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: add ENV webservice handelr * Fix: fix Env usecase logic * Feat: Add Delete Env API Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: filter empty addon data Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: split makefiels and make it clear * Feat: add k8s utils test * Feat: Add env update interface Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: change env implementation Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: minor fix * Revert "Fix: minor fix" This reverts commit 9cafefa65a384795315924cb18e48681dd7086a1. * Fix: use appusecase as parameter Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Refactor: align CLI vela env with new env design Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: minor fix Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Feat: add page index and alias of env Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: fix tests and licence header * Fix: fix makefile and add default target Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: update build swagger.json Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: change update env api Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: list env with alias * Feat: add log to env delete * Fix: can not get app status Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: support update workflow and refactor code * Fix: lint * Fix: remove swagger check Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: fix cli vela delete * Fix: update test Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: update test Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com> * Fix: app deploy unit test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: SortOrderDescending is not effective Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Feat: support default project/target/env * Fix: make test and add swagger * Fix: use separated datasource for unit test * Fix: app rollback bug Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: fix e2e test * Fix: kubeapi driver sort bug Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test * Fix: api e2e test Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: e2e test fix * Fix: try fix e2e test * Fix: api e2e test Signed-off-by: barnettZQG <barnett.zqg@gmail.com> Co-authored-by: barnettZQG <barnett.zqg@gmail.com>
2021-12-25 10:36:54 +08:00
// AskToChooseOneNamespace ask for choose one namespace as env
func AskToChooseOneNamespace(c client.Client, envMeta *types.EnvMeta) error {
var nsList v1.NamespaceList
if err := c.List(context.TODO(), &nsList); err != nil {
return err
}
var ops = []string{CreateCustomNamespace}
for _, r := range nsList.Items {
ops = append(ops, r.Name)
}
prompt := &survey.Select{
Message: "Would you like to choose an existing namespaces as your env?",
Options: ops,
}
err := survey.AskOne(prompt, &envMeta.Namespace)
if err != nil {
return fmt.Errorf("choosing namespace err %w", err)
}
if envMeta.Namespace == CreateCustomNamespace {
err = survey.AskOne(&survey.Input{
Message: "Please name the new namespace:",
}, &envMeta.Namespace)
if err != nil {
return err
}
return nil
}
for _, ns := range nsList.Items {
if ns.Name == envMeta.Namespace && envMeta.Name == "" {
envMeta.Name = ns.Labels[oam.LabelNamespaceOfEnvName]
return nil
}
}
return nil
}
2021-03-20 12:08:15 +08:00
// ReadYamlToObject will read a yaml K8s object to runtime.Object
func ReadYamlToObject(path string, object k8sruntime.Object) error {
data, err := os.ReadFile(filepath.Clean(path))
2021-03-20 12:08:15 +08:00
if err != nil {
return err
}
return yaml.Unmarshal(data, object)
}
// ParseTerraformVariables get variables from Terraform Configuration
func ParseTerraformVariables(configuration string) (map[string]*tfconfig.Variable, map[string]*tfconfig.Output, error) {
p := hclparse.NewParser()
hclFile, diagnostic := p.ParseHCL([]byte(configuration), "")
if diagnostic != nil {
return nil, nil, errors.New(diagnostic.Error())
}
mod := tfconfig.Module{Variables: map[string]*tfconfig.Variable{}, Outputs: map[string]*tfconfig.Output{}}
diagnostic = tfconfig.LoadModuleFromFile(hclFile, &mod)
if diagnostic != nil {
return nil, nil, errors.New(diagnostic.Error())
}
return mod.Variables, mod.Outputs, nil
}
// GenerateUnstructuredObj generate UnstructuredObj
func GenerateUnstructuredObj(name, ns string, gvk schema.GroupVersionKind) *unstructured.Unstructured {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(gvk)
u.SetName(name)
u.SetNamespace(ns)
return u
}
// SetSpecObjIntoUnstructuredObj set UnstructuredObj spec field
func SetSpecObjIntoUnstructuredObj(spec interface{}, u *unstructured.Unstructured) error {
bts, err := json.Marshal(spec)
if err != nil {
return err
}
data := make(map[string]interface{})
if err := json.Unmarshal(bts, &data); err != nil {
return err
}
_ = unstructured.SetNestedMap(u.Object, data, "spec")
return nil
}
2021-08-11 23:05:10 +08:00
// NewK8sClient init a local k8s client which add oamcore scheme
func NewK8sClient() (client.Client, error) {
conf, err := config.GetConfig()
if err != nil {
return nil, err
}
scheme := k8sruntime.NewScheme()
if err := clientgoscheme.AddToScheme(scheme); err != nil {
return nil, err
}
if err := oamcore.AddToScheme(scheme); err != nil {
return nil, err
}
k8sClient, err := client.New(conf, client.Options{Scheme: scheme})
if err != nil {
return nil, err
}
return k8sClient, nil
}