mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-12-02 03:37:46 +08:00
feat(api): update k8s resource and change bug
This commit is contained in:
parent
ab28097965
commit
e04add857d
@ -165,6 +165,7 @@ func (t *ClusterController) ResourceImport(w http.ResponseWriter, r *http.Reques
|
||||
httputil.ReturnSuccess(r, w, rri)
|
||||
}
|
||||
|
||||
//AddResource -
|
||||
func (t *ClusterController) AddResource(w http.ResponseWriter, r *http.Request) {
|
||||
type HandleResource struct {
|
||||
Namespace string `json:"namespace"`
|
||||
@ -183,10 +184,12 @@ func (t *ClusterController) AddResource(w http.ResponseWriter, r *http.Request)
|
||||
httputil.ReturnSuccess(r, w, rri)
|
||||
}
|
||||
|
||||
//UpdateResource -
|
||||
func (t *ClusterController) UpdateResource(w http.ResponseWriter, r *http.Request) {
|
||||
type HandelResource struct {
|
||||
Name string `json:"name"`
|
||||
AppID string `json:"app_id"`
|
||||
Kind string `json:"kind"`
|
||||
Namespace string `json:"namespace"`
|
||||
ResourceYaml string `json:"resource_yaml"`
|
||||
}
|
||||
@ -194,7 +197,7 @@ func (t *ClusterController) UpdateResource(w http.ResponseWriter, r *http.Reques
|
||||
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &hr, nil); !ok {
|
||||
return
|
||||
}
|
||||
rri, err := handler.GetClusterHandler().UpdateAppK8SResource(r.Context(), hr.Namespace, hr.AppID, hr.Name, hr.ResourceYaml)
|
||||
rri, err := handler.GetClusterHandler().UpdateAppK8SResource(r.Context(), hr.Namespace, hr.AppID, hr.Name, hr.ResourceYaml, hr.Kind)
|
||||
if err != nil {
|
||||
err.Handle(r, w)
|
||||
return
|
||||
@ -202,10 +205,12 @@ func (t *ClusterController) UpdateResource(w http.ResponseWriter, r *http.Reques
|
||||
httputil.ReturnSuccess(r, w, rri)
|
||||
}
|
||||
|
||||
//DeleteResource -
|
||||
func (t *ClusterController) DeleteResource(w http.ResponseWriter, r *http.Request) {
|
||||
type HandleResource struct {
|
||||
Namespace string `json:"namespace"`
|
||||
AppID string `json:"app_id"`
|
||||
Kind string `json:"kind"`
|
||||
ResourceYaml string `json:"resource_yaml"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
@ -213,7 +218,7 @@ func (t *ClusterController) DeleteResource(w http.ResponseWriter, r *http.Reques
|
||||
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &hr, nil); !ok {
|
||||
return
|
||||
}
|
||||
err := handler.GetClusterHandler().DeleteAppK8SResource(r.Context(), hr.Namespace, hr.AppID, hr.Name, hr.ResourceYaml)
|
||||
err := handler.GetClusterHandler().DeleteAppK8SResource(r.Context(), hr.Namespace, hr.AppID, hr.Name, hr.ResourceYaml, hr.Kind)
|
||||
if err != nil {
|
||||
err.Handle(r, w)
|
||||
return
|
||||
|
@ -35,8 +35,8 @@ type ClusterHandler interface {
|
||||
ConvertResource(ctx context.Context, namespace string, lr map[string]model.LabelResource) (map[string]model.ApplicationResource, *util.APIHandleError)
|
||||
ResourceImport(ctx context.Context, namespace string, as map[string]model.ApplicationResource, eid string) (*model.ReturnResourceImport, *util.APIHandleError)
|
||||
AddAppK8SResource(ctx context.Context, namespace string, appID string, resourceYaml string) ([]*dbmodel.K8sResource, *util.APIHandleError)
|
||||
DeleteAppK8SResource(ctx context.Context, namespace string, appID string, name string, yaml string) *util.APIHandleError
|
||||
UpdateAppK8SResource(ctx context.Context, namespace string, appID string, name string, resourceYaml string) (dbmodel.K8sResource, *util.APIHandleError)
|
||||
DeleteAppK8SResource(ctx context.Context, namespace, appID, name, yaml, kind string) *util.APIHandleError
|
||||
UpdateAppK8SResource(ctx context.Context, namespace, appID, name, resourceYaml, kind string) (dbmodel.K8sResource, *util.APIHandleError)
|
||||
}
|
||||
|
||||
// NewClusterHandler -
|
||||
@ -56,6 +56,7 @@ type clusterAction struct {
|
||||
config *rest.Config
|
||||
}
|
||||
|
||||
//GetClusterInfo -
|
||||
func (c *clusterAction) GetClusterInfo(ctx context.Context) (*model.ClusterResource, error) {
|
||||
timeout, _ := strconv.Atoi(os.Getenv("CLUSTER_INFO_CACHE_TIME"))
|
||||
if timeout == 0 {
|
||||
|
@ -34,8 +34,8 @@ func (c *clusterAction) workloadHandle(ctx context.Context, cr map[string]model.
|
||||
dmCR := c.workloadDeployments(ctx, lr.Workloads.Deployments, namespace)
|
||||
sfsCR := c.workloadStateFulSets(ctx, lr.Workloads.StateFulSets, namespace)
|
||||
jCR := c.workloadJobs(ctx, lr.Workloads.Jobs, namespace)
|
||||
wCJ := c.workloadCronJobs(ctx, lr.Workloads.CronJobs, namespace)
|
||||
convertResource := append(dmCR, append(sfsCR, append(jCR, append(wCJ)...)...)...)
|
||||
cjCR := c.workloadCronJobs(ctx, lr.Workloads.CronJobs, namespace)
|
||||
convertResource := append(dmCR, append(sfsCR, append(jCR, append(cjCR)...)...)...)
|
||||
|
||||
k8sResources := c.getAppKubernetesResources(ctx, lr.Others, namespace)
|
||||
cr[app] = model.ApplicationResource{
|
||||
@ -78,7 +78,7 @@ func (c *clusterAction) workloadDeployments(ctx context.Context, dmNames []strin
|
||||
if string(port.Protocol) == "TCP" {
|
||||
ps = append(ps, model.PortManagement{
|
||||
Port: port.ContainerPort,
|
||||
Protocol: "UDP",
|
||||
Protocol: "TCP",
|
||||
Inner: false,
|
||||
Outer: false,
|
||||
})
|
||||
@ -89,13 +89,14 @@ func (c *clusterAction) workloadDeployments(ctx context.Context, dmNames []strin
|
||||
|
||||
//ENV
|
||||
var envs []model.ENVManagement
|
||||
for _, env := range resources.Spec.Template.Spec.Containers[0].Env {
|
||||
if cm := env.ValueFrom; cm == nil {
|
||||
for i := 0; i < len(resources.Spec.Template.Spec.Containers[0].Env); i++ {
|
||||
if cm := resources.Spec.Template.Spec.Containers[0].Env[i].ValueFrom; cm == nil {
|
||||
envs = append(envs, model.ENVManagement{
|
||||
ENVKey: env.Name,
|
||||
ENVValue: env.Value,
|
||||
ENVKey: resources.Spec.Template.Spec.Containers[0].Env[i].Name,
|
||||
ENVValue: resources.Spec.Template.Spec.Containers[0].Env[i].Value,
|
||||
ENVExplain: "",
|
||||
})
|
||||
resources.Spec.Template.Spec.Containers[0].Env = append(resources.Spec.Template.Spec.Containers[0].Env[:i], resources.Spec.Template.Spec.Containers[0].Env[i+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
@ -317,8 +318,9 @@ func (c *clusterAction) workloadDeployments(ctx context.Context, dmNames []strin
|
||||
hcm.SuccessThreshold = int(readinessProbe.SuccessThreshold)
|
||||
}
|
||||
}
|
||||
|
||||
var attributes []*dbmodel.ComponentK8sAttributes
|
||||
if resources.Spec.Template.Spec.Containers[0].Env != nil {
|
||||
if resources.Spec.Template.Spec.Containers[0].Env != nil && len(resources.Spec.Template.Spec.Containers[0].Env) > 0 {
|
||||
envYaml, err := ObjectToJSONORYaml("yaml", resources.Spec.Template.Spec.Containers[0].Env)
|
||||
if err != nil {
|
||||
logrus.Errorf("deployment:%v env %v", dmName, err)
|
||||
@ -455,6 +457,10 @@ func (c *clusterAction) workloadCronJobs(ctx context.Context, cjNames []string,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *clusterAction) podTemplateSpecResource() {
|
||||
|
||||
}
|
||||
|
||||
func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others model.OtherResource, namespace string) []dbmodel.K8sResource {
|
||||
var k8sResources []dbmodel.K8sResource
|
||||
servicesMap := make(map[string]corev1.Service)
|
||||
@ -468,7 +474,9 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
}
|
||||
for _, servicesName := range others.Services {
|
||||
services, _ := servicesMap[servicesName]
|
||||
services.Kind = model.Service
|
||||
services.Status = corev1.ServiceStatus{}
|
||||
services.APIVersion = "v1"
|
||||
services.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", services)
|
||||
if err != nil {
|
||||
@ -478,6 +486,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: services.Name,
|
||||
Kind: model.Service,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -496,6 +505,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
pvc, _ := pvcMap[pvcName]
|
||||
pvc.Status = corev1.PersistentVolumeClaimStatus{}
|
||||
pvc.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
pvc.Kind = model.PVC
|
||||
pvc.APIVersion = "v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", pvc)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v pvc:%v error: %v", namespace, pvc.Name, err)
|
||||
@ -504,6 +515,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: pvc.Name,
|
||||
Kind: model.PVC,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -522,6 +534,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
ingresses, _ := ingressMap[ingressName]
|
||||
ingresses.Status = networkingv1.IngressStatus{}
|
||||
ingresses.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
ingresses.Kind = model.Ingress
|
||||
ingresses.APIVersion = "networking.k8s.io/v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", ingresses)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v ingresses:%v error: %v", namespace, ingresses.Name, err)
|
||||
@ -530,6 +544,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: ingresses.Name,
|
||||
Kind: model.Ingress,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -547,6 +562,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
for _, networkPoliciesName := range others.NetworkPolicies {
|
||||
networkPolicies, _ := networkPoliciesMap[networkPoliciesName]
|
||||
networkPolicies.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
networkPolicies.Kind = model.NetworkPolicie
|
||||
networkPolicies.APIVersion = "networking.k8s.io/v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", networkPolicies)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v NetworkPolicies:%v error: %v", namespace, networkPolicies.Name, err)
|
||||
@ -555,6 +572,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: networkPolicies.Name,
|
||||
Kind: model.NetworkPolicie,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -572,6 +590,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
for _, configMapsName := range others.ConfigMaps {
|
||||
configMaps, _ := cmMap[configMapsName]
|
||||
configMaps.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
configMaps.Kind = model.ConfigMap
|
||||
configMaps.APIVersion = "v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", configMaps)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v ConfigMaps:%v error: %v", namespace, configMaps.Name, err)
|
||||
@ -580,6 +600,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: configMaps.Name,
|
||||
Kind: model.ConfigMap,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -597,6 +618,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
for _, secretsName := range others.Secrets {
|
||||
secrets, _ := secretsMap[secretsName]
|
||||
secrets.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
secrets.Kind = model.Secret
|
||||
secrets.APIVersion = "v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", secrets)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v Secrets:%v error: %v", namespace, secrets.Name, err)
|
||||
@ -605,6 +628,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: secrets.Name,
|
||||
Kind: model.Secret,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -622,6 +646,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
for _, serviceAccountsName := range others.ServiceAccounts {
|
||||
serviceAccounts, _ := serviceAccountsMap[serviceAccountsName]
|
||||
serviceAccounts.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
serviceAccounts.Kind = model.ServiceAccount
|
||||
serviceAccounts.APIVersion = "v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", serviceAccounts)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v ServiceAccounts:%v error: %v", namespace, serviceAccounts.Name, err)
|
||||
@ -631,6 +657,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: serviceAccounts.Name,
|
||||
Kind: model.ServiceAccount,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -648,6 +675,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
for _, roleBindingsName := range others.RoleBindings {
|
||||
roleBindings, _ := roleBindingsMap[roleBindingsName]
|
||||
roleBindings.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
roleBindings.Kind = model.RoleBinding
|
||||
roleBindings.APIVersion = "rbac.authorization.k8s.io/v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", roleBindings)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v RoleBindings:%v error: %v", namespace, roleBindings.Name, err)
|
||||
@ -656,6 +685,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: roleBindings.Name,
|
||||
Kind: model.RoleBinding,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -674,6 +704,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
hpa, _ := hpaMap[hpaName]
|
||||
hpa.Status = v1.HorizontalPodAutoscalerStatus{}
|
||||
hpa.ManagedFields = []metav1.ManagedFieldsEntry{}
|
||||
hpa.Kind = model.HorizontalPodAutoscaler
|
||||
hpa.APIVersion = "autoscaling/v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", hpa)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v HorizontalPodAutoscalers:%v error: %v", namespace, hpa.Name, err)
|
||||
@ -682,6 +714,7 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
Name: hpa.Name,
|
||||
Kind: model.HorizontalPodAutoscaler,
|
||||
Content: kubernetesResourcesYAML,
|
||||
Success: 1,
|
||||
Status: "创建成功",
|
||||
})
|
||||
}
|
||||
@ -698,6 +731,8 @@ func (c *clusterAction) getAppKubernetesResources(ctx context.Context, others mo
|
||||
}
|
||||
for _, rolesName := range others.Roles {
|
||||
roles, _ := rolesMap[rolesName]
|
||||
roles.Kind = model.Role
|
||||
roles.APIVersion = "rbac.authorization.k8s.io/v1"
|
||||
kubernetesResourcesYAML, err := ObjectToJSONORYaml("yaml", roles)
|
||||
if err != nil {
|
||||
logrus.Errorf("namespace:%v roles:%v error: %v", namespace, roles.Name, err)
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
"k8s.io/client-go/restmapper"
|
||||
)
|
||||
|
||||
//AddAppK8SResource -
|
||||
func (c *clusterAction) AddAppK8SResource(ctx context.Context, namespace string, appID string, resourceYaml string) ([]*dbmodel.K8sResource, *util.APIHandleError) {
|
||||
logrus.Info("begin AddAppK8SResource")
|
||||
resourceObjects, err := c.HandleResourceYaml(resourceYaml, namespace, "create", "")
|
||||
@ -27,67 +28,86 @@ func (c *clusterAction) AddAppK8SResource(ctx context.Context, namespace string,
|
||||
var resourceList []*dbmodel.K8sResource
|
||||
for _, resourceObject := range resourceObjects {
|
||||
resource := resourceObject
|
||||
resourceYaml, err := ObjectToJSONORYaml("yaml", resource.Resource)
|
||||
if err != nil {
|
||||
var rsYaml string
|
||||
if resourceObject.Success == 3 {
|
||||
rsYaml = resourceYaml
|
||||
resourceList = append(resourceList, &dbmodel.K8sResource{
|
||||
AppID: appID,
|
||||
Name: "未识别",
|
||||
Kind: "未识别",
|
||||
Content: rsYaml,
|
||||
Status: resource.Status,
|
||||
Success: resource.Success,
|
||||
})
|
||||
} else {
|
||||
rsYaml, _ = ObjectToJSONORYaml("yaml", resourceObject.Resource)
|
||||
resourceList = append(resourceList, &dbmodel.K8sResource{
|
||||
AppID: appID,
|
||||
Name: resource.Resource.GetName(),
|
||||
Kind: resource.Resource.GetKind(),
|
||||
Content: resourceYaml,
|
||||
Content: rsYaml,
|
||||
Status: resource.Status,
|
||||
Success: resource.Success,
|
||||
})
|
||||
err = db.GetManager().K8sResourceDao().CreateK8sResourceInBatch(resourceList)
|
||||
if err != nil {
|
||||
return nil, &util.APIHandleError{Code: 400, Err: fmt.Errorf("CreateK8sResource %v", err)}
|
||||
}
|
||||
}
|
||||
resourceList = append(resourceList, &dbmodel.K8sResource{
|
||||
AppID: appID,
|
||||
Name: resource.Resource.GetName(),
|
||||
Kind: resource.Resource.GetKind(),
|
||||
Content: resourceYaml,
|
||||
Status: resource.Status,
|
||||
Success: resource.Success,
|
||||
})
|
||||
}
|
||||
err = db.GetManager().K8sResourceDao().CreateK8sResourceInBatch(resourceList)
|
||||
if err != nil {
|
||||
return nil, &util.APIHandleError{Code: 400, Err: fmt.Errorf("CreateK8sResource %v", err)}
|
||||
}
|
||||
return resourceList, nil
|
||||
}
|
||||
|
||||
func (c *clusterAction) UpdateAppK8SResource(ctx context.Context, namespace string, appID string, name string, resourceYaml string) (dbmodel.K8sResource, *util.APIHandleError) {
|
||||
//resourceObjects, err := c.HandleResourceYaml(resourceYaml, namespace, "update", name)
|
||||
//if err != nil {
|
||||
// return nil, &util.APIHandleError{Code: 400, Err: fmt.Errorf("failed to parse yaml into k8s resource:%v", err)}
|
||||
//}
|
||||
//var resourceList []*dbmodel.K8sResource
|
||||
//
|
||||
//err = db.GetManager().K8sResourceDao().CreateK8sResourceInBatch(resourceList)
|
||||
//if err != nil {
|
||||
// return nil, &util.APIHandleError{Code: 400, Err: fmt.Errorf("CreateK8sResource %v", err)}
|
||||
//}
|
||||
//return resourceList, nil
|
||||
return dbmodel.K8sResource{}, nil
|
||||
//UpdateAppK8SResource -
|
||||
func (c *clusterAction) UpdateAppK8SResource(ctx context.Context, namespace, appID, name, resourceYaml, kind string) (dbmodel.K8sResource, *util.APIHandleError) {
|
||||
logrus.Info("begin UpdateAppK8SResource")
|
||||
rs, err := db.GetManager().K8sResourceDao().GetK8sResourceByNameInBatch(appID, name, kind)
|
||||
if err != nil {
|
||||
return dbmodel.K8sResource{}, &util.APIHandleError{Code: 400, Err: fmt.Errorf("get k8s resource %v", err)}
|
||||
}
|
||||
resourceObjects, err := c.HandleResourceYaml(resourceYaml, namespace, "update", name)
|
||||
if err != nil {
|
||||
return dbmodel.K8sResource{}, &util.APIHandleError{Code: 400, Err: fmt.Errorf("failed to parse yaml into k8s resource:%v", err)}
|
||||
}
|
||||
var rsYaml string
|
||||
if resourceObjects[0].Success == 4 {
|
||||
rsYaml = resourceYaml
|
||||
rs[0].Success = resourceObjects[0].Success
|
||||
rs[0].Status = resourceObjects[0].Status
|
||||
rs[0].Content = rsYaml
|
||||
db.GetManager().K8sResourceDao().UpdateModel(&rs[0])
|
||||
} else {
|
||||
rsYaml, _ = ObjectToJSONORYaml("yaml", resourceObjects[0].Resource)
|
||||
rs[0].Success = resourceObjects[0].Success
|
||||
rs[0].Status = resourceObjects[0].Status
|
||||
rs[0].Content = rsYaml
|
||||
db.GetManager().K8sResourceDao().UpdateModel(&rs[0])
|
||||
}
|
||||
return rs[0], nil
|
||||
}
|
||||
|
||||
func (c *clusterAction) DeleteAppK8SResource(ctx context.Context, namespace string, appID string, name string, resourceYaml string) *util.APIHandleError {
|
||||
//DeleteAppK8SResource -
|
||||
func (c *clusterAction) DeleteAppK8SResource(ctx context.Context, namespace, appID, name, resourceYaml, kind string) *util.APIHandleError {
|
||||
logrus.Info("begin DeleteAppK8SResource")
|
||||
_, err := c.HandleResourceYaml(resourceYaml, namespace, "delete", name)
|
||||
if err != nil {
|
||||
return &util.APIHandleError{Code: 400, Err: fmt.Errorf("DeleteAppK8SResource %v", err)}
|
||||
}
|
||||
err = db.GetManager().K8sResourceDao().DeleteK8sResourceInBatch(appID, name)
|
||||
err = db.GetManager().K8sResourceDao().DeleteK8sResourceInBatch(appID, name, kind)
|
||||
if err != nil {
|
||||
return &util.APIHandleError{Code: 400, Err: fmt.Errorf("DeleteAppK8SResource %v", err)}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//BuildResource -
|
||||
type BuildResource struct {
|
||||
Resource *unstructured.Unstructured
|
||||
Success int
|
||||
Status string
|
||||
}
|
||||
|
||||
//HandleResourceYaml -
|
||||
func (c *clusterAction) HandleResourceYaml(resourceYaml string, namespace string, change string, name string) ([]BuildResource, error) {
|
||||
var buildResourceList []BuildResource
|
||||
dc, err := dynamic.NewForConfig(c.config)
|
||||
@ -100,21 +120,30 @@ func (c *clusterAction) HandleResourceYaml(resourceYaml string, namespace string
|
||||
logrus.Errorf("%v", err)
|
||||
return nil, err
|
||||
}
|
||||
decoder := yamlt.NewYAMLOrJSONDecoder(bytes.NewReader(resourceYamlByte), 100)
|
||||
decoder := yamlt.NewYAMLOrJSONDecoder(bytes.NewReader(resourceYamlByte), 1000)
|
||||
for {
|
||||
var rawObj runtime.RawExtension
|
||||
if err = decoder.Decode(&rawObj); err != nil {
|
||||
break
|
||||
}
|
||||
var status string
|
||||
var success int
|
||||
if change == "create" {
|
||||
status = "创建失败"
|
||||
success = 3
|
||||
} else {
|
||||
} else if change == "update" {
|
||||
status = "更新失败"
|
||||
success = 4
|
||||
}
|
||||
var rawObj runtime.RawExtension
|
||||
if err = decoder.Decode(&rawObj); err != nil {
|
||||
if err.Error() == "EOF" {
|
||||
break
|
||||
}
|
||||
logrus.Errorf("%v", err)
|
||||
buildResourceList = append(buildResourceList, BuildResource{
|
||||
Resource: nil,
|
||||
Success: success,
|
||||
Status: fmt.Sprintf("%v%v", status, err),
|
||||
})
|
||||
return buildResourceList, nil
|
||||
}
|
||||
obj, gvk, err := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, nil)
|
||||
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
|
||||
if err != nil {
|
||||
@ -159,6 +188,7 @@ func (c *clusterAction) HandleResourceYaml(resourceYaml string, namespace string
|
||||
obj, err := dri.Create(context.TODO(), unstructuredObj, metav1.CreateOptions{})
|
||||
var br BuildResource
|
||||
if err != nil {
|
||||
logrus.Errorf("k8s resource create error%v", err)
|
||||
br = BuildResource{
|
||||
Resource: obj,
|
||||
Success: success,
|
||||
@ -178,17 +208,23 @@ func (c *clusterAction) HandleResourceYaml(resourceYaml string, namespace string
|
||||
logrus.Errorf("delete k8s resource error%v", err)
|
||||
}
|
||||
case "update":
|
||||
//obj, err := dri.Update(context.TODO(), unstructuredObj, metav1.UpdateOptions{})
|
||||
//if err != nil {
|
||||
// r := []BuildResource{{
|
||||
// obj,
|
||||
// fmt.Sprintf("创建失败%v", err),
|
||||
// }}
|
||||
//} else {
|
||||
//
|
||||
//}
|
||||
//
|
||||
//return r, nil
|
||||
obj, err := dri.Update(context.TODO(), unstructuredObj, metav1.UpdateOptions{})
|
||||
var br BuildResource
|
||||
if err != nil {
|
||||
logrus.Errorf("update k8s resource error%v", err)
|
||||
br = BuildResource{
|
||||
Resource: obj,
|
||||
Success: success,
|
||||
Status: fmt.Sprintf("%v%v", status, err),
|
||||
}
|
||||
} else {
|
||||
br = BuildResource{
|
||||
Resource: obj,
|
||||
Success: 2,
|
||||
Status: fmt.Sprintf("更新成功"),
|
||||
}
|
||||
}
|
||||
buildResourceList = append(buildResourceList, br)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -171,7 +171,7 @@ func (c *clusterAction) createComponent(ctx context.Context, app *dbmodel.Applic
|
||||
ContainerMemory: int(component.BasicManagement.Memory),
|
||||
ContainerGPU: 0,
|
||||
UpgradeMethod: "Rolling",
|
||||
ExtendMethod: "stateless_multiple",
|
||||
ExtendMethod: string(dbmodel.ServiceTypeStatelessMultiple),
|
||||
Replicas: int(component.BasicManagement.Replicas),
|
||||
DeployVersion: time.Now().Format("20060102150405"),
|
||||
Category: "app_publish",
|
||||
|
@ -636,5 +636,6 @@ type K8sResourceDao interface {
|
||||
Dao
|
||||
ListByAppID(appID string) ([]model.K8sResource, error)
|
||||
CreateK8sResourceInBatch(k8sResources []*model.K8sResource) error
|
||||
DeleteK8sResourceInBatch(appID, name string) error
|
||||
DeleteK8sResourceInBatch(appID, name string, kind string) error
|
||||
GetK8sResourceByNameInBatch(appID, name, kind string) ([]model.K8sResource, error)
|
||||
}
|
||||
|
@ -150,6 +150,12 @@ var ServiceTypeStateSingleton ServiceType = "state_singleton"
|
||||
// ServiceTypeStateMultiple state_multiple
|
||||
var ServiceTypeStateMultiple ServiceType = "state_multiple"
|
||||
|
||||
// ServiceTypeJob job
|
||||
var ServiceTypeJob ServiceType = "job"
|
||||
|
||||
// ServiceTypeCornJob corn_job
|
||||
var ServiceTypeCornJob ServiceType = "cronjob"
|
||||
|
||||
//TenantServices app service base info
|
||||
type TenantServices struct {
|
||||
Model
|
||||
|
@ -19,6 +19,7 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
gormbulkups "github.com/atcdot/gorm-bulk-upsert"
|
||||
"github.com/goodrain/rainbond/db/model"
|
||||
"github.com/jinzhu/gorm"
|
||||
@ -37,7 +38,11 @@ func (t *K8sResourceDaoImpl) AddModel(mo model.Interface) error {
|
||||
|
||||
// UpdateModel update model
|
||||
func (t *K8sResourceDaoImpl) UpdateModel(mo model.Interface) error {
|
||||
return nil
|
||||
resource, ok := mo.(*model.K8sResource)
|
||||
if !ok {
|
||||
return fmt.Errorf("mo.(*model.K8sResource) err")
|
||||
}
|
||||
return t.DB.Save(resource).Error
|
||||
}
|
||||
|
||||
// ListByAppID list by app id
|
||||
@ -49,6 +54,7 @@ func (t *K8sResourceDaoImpl) ListByAppID(appID string) ([]model.K8sResource, err
|
||||
return resources, nil
|
||||
}
|
||||
|
||||
//CreateK8sResourceInBatch -
|
||||
func (t *K8sResourceDaoImpl) CreateK8sResourceInBatch(k8sResources []*model.K8sResource) error {
|
||||
var objects []interface{}
|
||||
for _, cg := range k8sResources {
|
||||
@ -60,6 +66,16 @@ func (t *K8sResourceDaoImpl) CreateK8sResourceInBatch(k8sResources []*model.K8sR
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *K8sResourceDaoImpl) DeleteK8sResourceInBatch(appID, name string) error {
|
||||
return t.DB.Where("app_id=? and name=?", appID, name).Delete(&model.K8sResource{}).Error
|
||||
//DeleteK8sResourceInBatch -
|
||||
func (t *K8sResourceDaoImpl) DeleteK8sResourceInBatch(appID, name string, kind string) error {
|
||||
return t.DB.Where("app_id=? and name=? and kind=?", appID, name, kind).Delete(&model.K8sResource{}).Error
|
||||
}
|
||||
|
||||
//GetK8sResourceByNameInBatch -
|
||||
func (t *K8sResourceDaoImpl) GetK8sResourceByNameInBatch(appID, name, kind string) ([]model.K8sResource, error) {
|
||||
var resources []model.K8sResource
|
||||
if err := t.DB.Where("app_id=? and name=? and kind=?", appID, name, kind).Find(&resources).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resources, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user