-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathdeploy_updatesuggustion.go
159 lines (144 loc) · 5.06 KB
/
deploy_updatesuggustion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2022 The kubegems.io 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 application
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"kubegems.io/kubegems/pkg/service/handlers"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
)
type ResourceSuggestion struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ResourceSuggestionSpec `json:"spec,omitempty"`
}
func (r *ResourceSuggestion) DeepCopyObject() runtime.Object {
return r
}
type ResourceSuggestionSpec struct {
Template PodTemplateSpec `json:"template"`
}
type PodTemplateSpec struct {
Spec PodSpec `json:"spec,omitempty"`
}
type PodSpec struct {
InitContainers []Container `json:"initContainers,omitempty"`
Containers []Container `json:"containers"`
EphemeralContainers []Container `json:"ephemeralContainers,omitempty"`
}
type Container struct {
Name string `json:"name,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty" protobuf:"bytes,8,opt,name=resources"`
}
// @Tags Application
// @Summary 更新资源建议至 gitrepo
// @Description 更新资源建议至 gitrepo
// @Accept json
// @Produce json
// @Param cluster path string true "-"
// @Param group path string true "-"
// @Param version path string true "-"
// @param namespace path string true "-"
// @Param resource path string true "-"
// @Param name path string true "-"
// @Success 200 {object} handlers.ResponseStruct{Data=object} "-"
// @Router /v1/cluster/{cluster}/{group}/{version}/namespaces/{namespace}/{resource}/{name} [patch]
// @Security JWT
func (h *ApplicationHandler) UpdateWorkloadResources(c *gin.Context) {
// audit
h.SetAuditData(c, "更新", "编排建议资源", c.Param("resource"))
h.SetExtraAuditDataByClusterNamespace(c, c.Param("cluster"), c.Param("namespace"))
process := func() error {
suggestion := ResourceSuggestion{}
if err := c.ShouldBind(&suggestion); err != nil {
return err
}
if suggestion.TypeMeta.GroupVersionKind().Empty() || suggestion.Name == "" {
return errors.New("empty resource kind or name")
}
if suggestion.Annotations == nil {
suggestion.Annotations = map[string]string{}
}
ref := &PathRef{}
ref.FromJsonBase64(suggestion.Annotations[AnnotationRef])
if ref.IsEmpty() {
return errors.New("not a argo managed resource")
}
updatefunc := func(_ context.Context, fs billy.Filesystem) error {
return ForFileContentFunc(fs, "", func(filename string, content []byte) error {
if filepath.Ext(filename) != ".yaml" {
return nil
}
obj, err := DecodeResource(content)
if err != nil {
return nil
}
// check Kind Name
if (obj.GetObjectKind().GroupVersionKind() != suggestion.TypeMeta.GroupVersionKind()) || obj.GetName() != suggestion.Name {
return nil
}
// update resource
updated := UpdatedReourcesLimits(obj, suggestion)
if updated {
content, err := yaml.Marshal(obj)
if err != nil {
return err
}
return util.WriteFile(fs, filename, content, os.ModePerm)
}
return nil
})
}
// update git
msg := fmt.Sprintf("update resource suggestion for %s name=%s", suggestion.GroupVersionKind().String(), suggestion.ObjectMeta.Name)
if err := h.Manifest.UpdateContentFunc(c.Request.Context(), *ref, updatefunc, msg); err != nil {
return err
}
// sync
if err := h.ApplicationProcessor.Sync(c.Request.Context(), *ref); err != nil {
return err
}
return nil
}
if err := process(); err != nil {
handlers.NotOK(c, err)
} else {
handlers.OK(c, "ok")
}
}
func UpdatedReourcesLimits(obj client.Object, suggestion ResourceSuggestion) bool {
updated := false
updatefunc := func(template *corev1.PodTemplateSpec) {
for i, c := range template.Spec.Containers {
for _, suggestioncontainer := range suggestion.Spec.Template.Spec.Containers {
if suggestioncontainer.Name == c.Name {
template.Spec.Containers[i].Resources = suggestioncontainer.Resources
updated = true
}
}
}
}
ObjectPodTemplateFunc(obj, updatefunc)
return updated
}