Files
kubevela/pkg/workflow/providers/kube/handle.go
T
Jianbo Sun aa87d3da24 upgrade K8s dependency lib to v0.21 (#1985)
* upgrade K8s dependency to v0.21

* update CRD for new version of controller-runtime

* fix ci component revision create must fill Raw in runtime.RawExtension

* try fix test

* start control plane timeout set to 1min and fix tests

* add timeout for env test start and stop

* longer timeout time for BeforeSuit function

* upgrade kubebuilder and k8s cluster version to match with v1.21.2 in github action

* fix test

* fix resource tracker ownerRef override

* update developer guides
2021-08-03 11:30:02 +08:00

117 lines
3.0 KiB
Go

/*
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 kube
import (
"context"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/oam-dev/kubevela/pkg/cue/model"
"github.com/oam-dev/kubevela/pkg/cue/model/value"
wfContext "github.com/oam-dev/kubevela/pkg/workflow/context"
"github.com/oam-dev/kubevela/pkg/workflow/providers"
"github.com/oam-dev/kubevela/pkg/workflow/types"
)
const (
// ProviderName is provider name for install.
ProviderName = "kube"
)
// Dispatcher is a client for apply resources.
type Dispatcher func(ctx context.Context, manifests ...*unstructured.Unstructured) error
type provider struct {
apply func(ctx context.Context, manifests ...*unstructured.Unstructured) error
cli client.Client
}
// Apply create or update CR in cluster.
func (h *provider) Apply(ctx wfContext.Context, v *value.Value, act types.Action) error {
val, err := v.LookupValue("value")
if err != nil {
return err
}
var workload = new(unstructured.Unstructured)
pv, err := v.Field("patch")
if pv.Exists() && err == nil {
base, err := model.NewBase(val.CueValue())
if err != nil {
return err
}
patcher, err := model.NewOther(pv)
if err != nil {
return err
}
if err := base.Unify(patcher); err != nil {
return err
}
workload, err = base.Unstructured()
if err != nil {
return err
}
} else if err := val.UnmarshalTo(workload); err != nil {
return err
}
deployCtx := context.Background()
if workload.GetNamespace() == "" {
workload.SetNamespace("default")
}
if err := h.apply(deployCtx, workload); err != nil {
return err
}
return v.FillObject(workload.Object, "value")
}
// Read get CR from cluster.
func (h *provider) Read(ctx wfContext.Context, v *value.Value, act types.Action) error {
val, err := v.LookupValue("value")
if err != nil {
return err
}
obj := new(unstructured.Unstructured)
if err := val.UnmarshalTo(obj); err != nil {
return err
}
key := client.ObjectKeyFromObject(obj)
if key.Namespace == "" {
key.Namespace = "default"
}
if err := h.cli.Get(context.Background(), key, obj); err != nil {
return v.FillObject(err.Error(), "err")
}
return v.FillObject(obj.Object, "value")
}
// Install register handlers to provider discover.
func Install(p providers.Providers, cli client.Client, apply Dispatcher) {
prd := &provider{
apply: apply,
cli: cli,
}
p.Register(ProviderName, map[string]providers.Handler{
"apply": prd.Apply,
"read": prd.Read,
})
}