github.com/oam-dev/kubevela@v1.9.11/pkg/policy/envbinding/placement.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 envbinding 18 19 import ( 20 "encoding/json" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 24 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 25 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1" 26 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 27 ) 28 29 // updateClusterConnections update cluster connection in envbinding status with decisions 30 func updateClusterConnections(status *v1alpha1.EnvBindingStatus, decisions []v1alpha1.PlacementDecision, app *v1beta1.Application) { 31 var currentRev string 32 if app.Status.LatestRevision != nil { 33 currentRev = app.Status.LatestRevision.Name 34 } 35 clusterMap := map[string]bool{} 36 for _, decision := range decisions { 37 clusterMap[decision.Cluster] = true 38 } 39 for clusterName := range clusterMap { 40 exists := false 41 for idx, conn := range status.ClusterConnections { 42 if conn.ClusterName == clusterName { 43 exists = true 44 status.ClusterConnections[idx].LastActiveRevision = currentRev 45 break 46 } 47 } 48 if !exists { 49 status.ClusterConnections = append(status.ClusterConnections, v1alpha1.ClusterConnection{ 50 ClusterName: clusterName, 51 LastActiveRevision: currentRev, 52 }) 53 } 54 } 55 } 56 57 // WritePlacementDecisions write placement decisions into application status 58 // Deprecated As it is only used in EnvBinding policy 59 func WritePlacementDecisions(app *v1beta1.Application, policyName string, envName string, decisions []v1alpha1.PlacementDecision) error { 60 statusExists := false 61 for idx, policyStatus := range app.Status.PolicyStatus { 62 if policyStatus.Name == policyName && policyStatus.Type == v1alpha1.EnvBindingPolicyType { 63 envBindingStatus := &v1alpha1.EnvBindingStatus{} 64 err := json.Unmarshal(policyStatus.Status.Raw, envBindingStatus) 65 if err != nil { 66 return err 67 } 68 insert := true 69 for _idx, envStatus := range envBindingStatus.Envs { 70 if envStatus.Env == envName { 71 // TODO gc 72 envBindingStatus.Envs[_idx].Placements = decisions 73 insert = false 74 break 75 } 76 } 77 if insert { 78 envBindingStatus.Envs = append(envBindingStatus.Envs, v1alpha1.EnvStatus{ 79 Env: envName, 80 Placements: decisions, 81 }) 82 } 83 updateClusterConnections(envBindingStatus, decisions, app) 84 bs, err := json.Marshal(envBindingStatus) 85 if err != nil { 86 return err 87 } 88 app.Status.PolicyStatus[idx].Status = &runtime.RawExtension{Raw: bs} 89 statusExists = true 90 break 91 } 92 } 93 if !statusExists { 94 envBindingStatus := &v1alpha1.EnvBindingStatus{ 95 Envs: []v1alpha1.EnvStatus{{ 96 Env: envName, 97 Placements: decisions, 98 }}, 99 } 100 updateClusterConnections(envBindingStatus, decisions, app) 101 bs, err := json.Marshal(envBindingStatus) 102 if err != nil { 103 return err 104 } 105 app.Status.PolicyStatus = append(app.Status.PolicyStatus, common.PolicyStatus{ 106 Name: policyName, 107 Type: v1alpha1.EnvBindingPolicyType, 108 Status: &runtime.RawExtension{Raw: bs}, 109 }) 110 } 111 return nil 112 }