github.com/oam-dev/kubevela@v1.9.11/references/appfile/run.go (about) 1 /* 2 Copyright 2021 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 appfile 18 19 import ( 20 "context" 21 "fmt" 22 23 apierrors "k8s.io/apimachinery/pkg/api/errors" 24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 25 ctypes "k8s.io/apimachinery/pkg/types" 26 "sigs.k8s.io/controller-runtime/pkg/client" 27 28 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 29 "github.com/oam-dev/kubevela/pkg/oam" 30 "github.com/oam-dev/kubevela/pkg/utils/util" 31 "github.com/oam-dev/kubevela/references/appfile/api" 32 ) 33 34 // Run will deploy OAM objects and other assistant K8s Objects including ConfigMap, OAM Scope Custom Resource. 35 func Run(ctx context.Context, client client.Client, app *v1beta1.Application, assistantObjects []oam.Object) error { 36 if err := CreateOrUpdateObjects(ctx, client, assistantObjects); err != nil { 37 return err 38 } 39 if app != nil { 40 if err := CreateOrUpdateApplication(ctx, client, app); err != nil { 41 return err 42 } 43 } 44 return nil 45 } 46 47 // CreateOrUpdateObjects will create all scopes 48 func CreateOrUpdateObjects(ctx context.Context, client client.Client, objects []oam.Object) error { 49 for _, obj := range objects { 50 key := ctypes.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()} 51 u := &unstructured.Unstructured{} 52 u.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind()) 53 err := client.Get(ctx, key, u) 54 if err == nil { 55 obj.SetResourceVersion(u.GetResourceVersion()) 56 fmt.Println("Updating: ", u.GetObjectKind().GroupVersionKind().String(), "in", u.GetNamespace()) 57 if err = client.Update(ctx, obj); err != nil { 58 return err 59 } 60 continue 61 } 62 if !apierrors.IsNotFound(err) { 63 return err 64 } 65 fmt.Println("Creating: ", u.GetObjectKind().GroupVersionKind().String(), "in", u.GetNamespace()) 66 if err = client.Create(ctx, obj); err != nil { 67 return err 68 } 69 } 70 return nil 71 } 72 73 // CreateOrUpdateApplication will create if not exist and update if exists. 74 func CreateOrUpdateApplication(ctx context.Context, client client.Client, app *v1beta1.Application) error { 75 var geta v1beta1.Application 76 key := ctypes.NamespacedName{Name: app.Name, Namespace: app.Namespace} 77 var exist = true 78 if err := client.Get(ctx, key, &geta); err != nil { 79 if !apierrors.IsNotFound(err) { 80 return err 81 } 82 exist = false 83 } 84 if !exist { 85 return client.Create(ctx, app) 86 } 87 app.ResourceVersion = geta.ResourceVersion 88 return client.Update(ctx, app) 89 } 90 91 // BuildRun will build application and deploy from Appfile 92 func BuildRun(ctx context.Context, app *api.Application, client client.Client, namespace string, io util.IOStreams) error { 93 o, err := app.ConvertToApplication(namespace, io, app.Tm, true) 94 if err != nil { 95 return err 96 } 97 return Run(ctx, client, o, nil) 98 }