New ๐ŸŒŠ wave for @helm

Commit 07b748a3

07b748a3a5faMerged

Svyatoslav Dumchenko <[email protected]> ยท 4 years ago ยท 1 file ยท ~1 modified

Description

Reduce complexity of MakeMultiTrackSpec

Details

Commit
07b748a3a5fa
Parent
3b11d57ec00f

pkg/kubedog/multitrack.gomodifiedView file

@@ -6,6 +6,7 @@
66 "strconv"
77 "strings"
88
9+ log "github.com/sirupsen/logrus"
910 "github.com/werf/kubedog/pkg/trackers/rollout/multitrack"
1011 )
1112
@@ -48,7 +49,6 @@
4849 }
4950
5051 // MakeMultiTrackSpec creates *multitrack.MultitrackSpec for current resource.
51-//nolint:funlen,gocognit,cyclop // TODO: split this function
5252 func (r *Resource) MakeMultiTrackSpec(ns string) (*multitrack.MultitrackSpec, error) {
5353 // Default spec
5454 spec := &multitrack.MultitrackSpec{
@@ -65,109 +65,170 @@
6565 *spec.FailureThresholdSeconds = 0
6666
6767 // Override by annotations
68-loop:
6968 for name, value := range r.Annotations {
7069 // invalid := fmt.Errorf("%s/%s annotation %s with invalid value %s", r.Name, r.Kind, name, value)
70+ var err error
7171
7272 switch name {
7373 // Parse Value
7474 case SkipLogsAnnoName:
75- v, err := strconv.ParseBool(value)
76- if err != nil {
77- return nil, err
78- }
79- spec.SkipLogs = v
75+ err = r.handleAnnotationSkipLogs(value, spec)
8076 case ShowEventsAnnoName:
81- v, err := strconv.ParseBool(value)
82- if err != nil {
83- return nil, err
84- }
85- spec.ShowServiceMessages = v
77+ err = r.handleAnnotationShowEvents(value, spec)
8678 case LogRegexAnnoName:
87- v, err := regexp.Compile(value)
88- if err != nil {
89- return nil, err
90- }
91- spec.LogRegex = v
79+ err = r.handleAnnotationLogRegex(value, spec)
9280 case FailuresAllowedPerReplicaAnnoName:
93- if r.Kind == "Job" {
94- return nil, fmt.Errorf("%s does not support for job", name)
95- }
81+ err = r.handleAnnotationFailuresAllowedPerReplica(value, spec)
82+
83+ // Choose value
84+ case TrackTerminationModeAnnoName:
85+ err = r.handleAnnotationTrackTerminationMode(value, spec)
86+ case FailModeAnnoName:
87+ err = r.handleAnnotationFailMode(value, spec)
9688
97- v, err := strconv.ParseUint(value, 10, 32)
98- if err != nil {
99- return nil, err
100- }
89+ // Parse array
90+ case SkipLogsForContainersAnnoName:
91+ err = r.handleAnnotationSkipLogsForContainers(value, spec)
92+ case ShowLogsOnlyForContainersAnnoName:
93+ err = r.handleAnnotationShowLogsOnlyForContainers(value, spec)
10194
102- replicas := 1
103- if r.Spec.Replicas != nil {
104- replicas = int(*r.Spec.Replicas)
95+ default:
96+ //nolint:gocritic // keep switch in case of more prefix-based annotations in future
97+ switch {
98+ case strings.HasPrefix(name, LogRegexForAnnoPrefix):
99+ err = r.handleAnnotationLogRegexFor(name, value, spec)
105100 }
101+ }
106102
107- *spec.AllowFailuresCount = int(v) * replicas
103+ if err != nil {
104+ return nil, err
105+ }
106+ }
108107
109- // Chose value
110- case TrackTerminationModeAnnoName:
111- v := multitrack.TrackTerminationMode(value)
112- values := []multitrack.TrackTerminationMode{
113- multitrack.WaitUntilResourceReady,
114- multitrack.NonBlocking,
115- }
116- for _, mode := range values {
117- if mode == v {
118- spec.TrackTerminationMode = v
108+ return spec, nil
109+}
119110
120- continue loop
121- }
122- }
111+func (*Resource) handleAnnotationSkipLogs(value string, spec *multitrack.MultitrackSpec) error {
112+ v, err := strconv.ParseBool(value)
113+ if err != nil {
114+ return err
115+ }
116+ spec.SkipLogs = v
123117
124- return nil, fmt.Errorf("%s not found", v)
125- case FailModeAnnoName:
126- v := multitrack.FailMode(value)
127- values := []multitrack.FailMode{
128- multitrack.IgnoreAndContinueDeployProcess,
129- multitrack.FailWholeDeployProcessImmediately,
130- multitrack.HopeUntilEndOfDeployProcess,
131- }
132- for _, mode := range values {
133- if mode == v {
134- spec.FailMode = v
118+ return nil
119+}
135120
136- continue loop
137- }
138- }
121+func (*Resource) handleAnnotationShowEvents(value string, spec *multitrack.MultitrackSpec) error {
122+ v, err := strconv.ParseBool(value)
123+ if err != nil {
124+ return err
125+ }
126+ spec.ShowServiceMessages = v
139127
140- return nil, fmt.Errorf("%s not found", v)
128+ return nil
129+}
141130
142- // Parse array
143- case SkipLogsForContainersAnnoName:
144- containers, err := splitContainers(value)
145- if err != nil {
146- return nil, err
147- }
148- spec.SkipLogsForContainers = containers
149- case ShowLogsOnlyForContainersAnnoName:
150- containers, err := splitContainers(value)
151- if err != nil {
152- return nil, err
153- }
154- spec.ShowLogsOnlyForContainers = containers
131+func (*Resource) handleAnnotationLogRegex(value string, spec *multitrack.MultitrackSpec) error {
132+ v, err := regexp.Compile(value)
133+ if err != nil {
134+ return err
135+ }
136+ spec.LogRegex = v
137+
138+ return nil
139+}
140+
141+func (r *Resource) handleAnnotationFailuresAllowedPerReplica(value string, spec *multitrack.MultitrackSpec) error {
142+ if r.Kind == "Job" {
143+ return fmt.Errorf("%s is not supported for jobs", FailuresAllowedPerReplicaAnnoName)
144+ }
145+
146+ v, err := strconv.ParseUint(value, 10, 32)
147+ if err != nil {
148+ return err
149+ }
150+
151+ replicas := 1
152+ if r.Spec.Replicas != nil {
153+ replicas = int(*r.Spec.Replicas)
154+ }
155155
156- default:
157- if strings.HasPrefix(name, LogRegexForAnnoPrefix) {
158- if containerName := strings.TrimPrefix(name, LogRegexForAnnoPrefix); containerName != "" {
159- regexpValue, err := regexp.Compile(value)
160- if err != nil {
161- return nil, err
162- }
156+ *spec.AllowFailuresCount = int(v) * replicas
157+
158+ return nil
159+}
160+
161+func (*Resource) handleAnnotationTrackTerminationMode(value string, spec *multitrack.MultitrackSpec) error {
162+ v := multitrack.TrackTerminationMode(value)
163+ values := []multitrack.TrackTerminationMode{
164+ multitrack.WaitUntilResourceReady,
165+ multitrack.NonBlocking,
166+ }
167+ for _, mode := range values {
168+ if mode == v {
169+ spec.TrackTerminationMode = v
170+
171+ return nil
172+ }
173+ }
174+
175+ return fmt.Errorf("%s not found", v)
176+}
177+
178+func (*Resource) handleAnnotationFailMode(value string, spec *multitrack.MultitrackSpec) error {
179+ v := multitrack.FailMode(value)
180+ values := []multitrack.FailMode{
181+ multitrack.IgnoreAndContinueDeployProcess,
182+ multitrack.FailWholeDeployProcessImmediately,
183+ multitrack.HopeUntilEndOfDeployProcess,
184+ }
185+ for _, mode := range values {
186+ if mode == v {
187+ spec.FailMode = v
163188
164- spec.LogRegexByContainerName[containerName] = regexpValue
165- }
166- }
189+ return nil
167190 }
168191 }
169192
170- return spec, nil
193+ return fmt.Errorf("%s not found", v)
194+}
195+
196+func (*Resource) handleAnnotationSkipLogsForContainers(value string, spec *multitrack.MultitrackSpec) error {
197+ containers, err := splitContainers(value)
198+ if err != nil {
199+ return err
200+ }
201+ spec.SkipLogsForContainers = containers
202+
203+ return nil
204+}
205+
206+func (*Resource) handleAnnotationShowLogsOnlyForContainers(value string, spec *multitrack.MultitrackSpec) error {
207+ containers, err := splitContainers(value)
208+ if err != nil {
209+ return err
210+ }
211+ spec.ShowLogsOnlyForContainers = containers
212+
213+ return nil
214+}
215+
216+func (*Resource) handleAnnotationLogRegexFor(name, value string, spec *multitrack.MultitrackSpec) error {
217+ containerName := strings.TrimPrefix(name, LogRegexForAnnoPrefix)
218+ if containerName == "" {
219+ log.WithField("annotation", name).Error("annotation is invalid: cannot get container name")
220+
221+ return nil
222+ }
223+
224+ regexpValue, err := regexp.Compile(value)
225+ if err != nil {
226+ return err
227+ }
228+
229+ spec.LogRegexByContainerName[containerName] = regexpValue
230+
231+ return nil
171232 }
172233
173234 func splitContainers(annoValue string) (containers []string, err error) {