Files
kubevela/pkg/policy/envbinding/utils.go
Charlie Chiang edc6d9c551 Fix: address vela-core crash due to empty policy properties (#4473)
* Fix: fix topology core crash

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Test: add tests

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Fix: same problem in other places

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Style: remove empty line

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Feat: raise error when empty topology is used

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Feat: raise error when empty override policy is used

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
2022-07-27 08:45:01 +08:00

74 lines
2.5 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 envbinding
import (
"context"
"encoding/json"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
)
type contextKey string
const (
// EnvNameContextKey is the name of env
EnvNameContextKey = contextKey("EnvName")
)
// GetEnvBindingPolicy extract env-binding policy with given policy name, if policy name is empty, the first env-binding policy will be used
func GetEnvBindingPolicy(app *v1beta1.Application, policyName string) (*v1alpha1.EnvBindingSpec, error) {
for _, policy := range app.Spec.Policies {
if (policy.Name == policyName || policyName == "") && policy.Type == v1alpha1.EnvBindingPolicyType && policy.Properties != nil {
envBindingSpec := &v1alpha1.EnvBindingSpec{}
err := json.Unmarshal(policy.Properties.Raw, envBindingSpec)
return envBindingSpec, err
}
}
return nil, nil
}
// GetEnvBindingPolicyStatus extract env-binding policy status with given policy name, if policy name is empty, the first env-binding policy will be used
func GetEnvBindingPolicyStatus(app *v1beta1.Application, policyName string) (*v1alpha1.EnvBindingStatus, error) {
for _, policyStatus := range app.Status.PolicyStatus {
if (policyStatus.Name == policyName || policyName == "") && policyStatus.Type == v1alpha1.EnvBindingPolicyType {
envBindingStatus := &v1alpha1.EnvBindingStatus{}
if policyStatus.Status != nil {
err := json.Unmarshal(policyStatus.Status.Raw, envBindingStatus)
return envBindingStatus, err
}
return nil, nil
}
}
return nil, nil
}
// EnvNameInContext extract env name from context
func EnvNameInContext(ctx context.Context) string {
envName := ctx.Value(EnvNameContextKey)
if envName != nil {
return envName.(string)
}
return ""
}
// ContextWithEnvName wraps context with envName
func ContextWithEnvName(ctx context.Context, envName string) context.Context {
return context.WithValue(ctx, EnvNameContextKey, envName)
}