aboutsummaryrefslogtreecommitdiffstats
path: root/src/k8splugin/internal/healthcheck/healthcheck.go
blob: 70f5bec2f55690603c98bcc62dae5849ba9af964 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Copyright © 2021 Samsung Electronics
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 healthcheck

import (
	"encoding/json"
	"strings"
	"sync"

	"helm.sh/helm/v3/pkg/release"
	"helm.sh/helm/v3/pkg/time"

	"github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
	"github.com/onap/multicloud-k8s/src/k8splugin/internal/db"
	"github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
	log "github.com/onap/multicloud-k8s/src/k8splugin/internal/logutils"
	"github.com/onap/multicloud-k8s/src/k8splugin/internal/namegenerator"

	pkgerrors "github.com/pkg/errors"
)

// InstanceHCManager interface exposes instance Healthcheck fuctionalities
type InstanceHCManager interface {
	Create(instanceId string) (InstanceMiniHCStatus, error)
	Get(instanceId, healthcheckId string) (InstanceHCStatus, error)
	List(instanceId string) (InstanceHCOverview, error)
	Delete(instanceId, healthcheckId string) error
}

// HealthcheckKey is used as the primary key in the db
type HealthcheckKey struct {
	InstanceId    string `json:"instance-id"`
	HealthcheckId string `json:"healthcheck-id"`
}

// We will use json marshalling to convert to string to
// preserve the underlying structure.
func (dk HealthcheckKey) String() string {
	out, err := json.Marshal(dk)
	if err != nil {
		return ""
	}

	return string(out)
}

// InstanceHCClient implements InstanceHCManager
type InstanceHCClient struct {
	storeName string
	tagInst   string
}

// InstanceHCStatus holds healthcheck status
type InstanceHCStatus struct {
	InstanceId    string            `json:"instance-id"`
	HealthcheckId string            `json:"healthcheck-id"`
	Status        release.HookPhase `json:"status"`
	TestSuite     *TestSuite        `json:"test-suite"` //TODO could be merged with current struct
}

// TestSuite is a structure to hold compatibility with pre helm3 output
type TestSuite struct {
	StartedAt   time.Time
	CompletedAt time.Time
	Results     []*HookStatus
}

// InstanceMiniHCStatus holds only healthcheck summary
type InstanceMiniHCStatus struct {
	HealthcheckId string            `json:"healthcheck-id"`
	Status        release.HookPhase `json:"status"`
}

// InstanceHCOverview holds Healthcheck-related data
type InstanceHCOverview struct {
	InstanceId string                 `json:"instance-id"`
	HCSummary  []InstanceMiniHCStatus `json:"healthcheck-summary"`
	Hooks      []*helm.Hook           `json:"hooks"`
}

func NewHCClient() *InstanceHCClient {
	return &InstanceHCClient{
		storeName: "rbdef",
		tagInst:   "instanceHC",
	}
}

func instanceMiniHCStatusFromStatus(ihcs InstanceHCStatus) InstanceMiniHCStatus {
	return InstanceMiniHCStatus{ihcs.HealthcheckId, ihcs.Status}
}

func (ihc InstanceHCClient) Create(instanceId string) (InstanceMiniHCStatus, error) {
	//TODO Handle hook delete policies

	//Generate ID
	id := namegenerator.Generate()

	//Determine Cloud Region and namespace
	v := app.NewInstanceClient()
	instance, err := v.Get(instanceId)
	if err != nil {
		return InstanceMiniHCStatus{}, pkgerrors.Wrap(err, "Getting instance")
	}

	k8sClient := app.KubernetesClient{}
	err = k8sClient.Init(instance.Request.CloudRegion, instanceId)
	if err != nil {
		return InstanceMiniHCStatus{}, pkgerrors.Wrap(err, "Preparing KubeClient")
	}
	key := HealthcheckKey{
		InstanceId:    instanceId,
		HealthcheckId: id,
	}

	//Filter out only relevant hooks
	hooks := hookPairs{}
	for _, hook := range instance.Hooks {
		for _, hookEvent := range hook.Hook.Events {
			if hookEvent == release.HookTest { //Helm3 no longer supports test-failure
				hooks = append(hooks, hookPair{
					Definition: hook,
					Status: &HookStatus{
						Name: hook.Hook.Name,
					},
				})
			}
		}
	}

	//Save state
	testSuite := TestSuite{
		StartedAt: time.Now(),
		Results:   hooks.statuses(),
	}
	ihcs := InstanceHCStatus{
		InstanceId:    instanceId,
		HealthcheckId: id,
		Status:        release.HookPhaseRunning,
		TestSuite:     &testSuite,
	}

	for _, h := range hooks {
		h.Status.StartedAt = time.Now()
		kr, err := k8sClient.CreateKind(h.Definition.KRT, instance.Namespace)
		if err != nil {
			// Set status fields
			h.Status.Status = release.HookPhaseFailed
			h.Status.CompletedAt = time.Now()
			testSuite.CompletedAt = time.Now()
			ihcs.Status = release.HookPhaseFailed
			retErr := "Starting hook " + h.Status.Name

			// Dump to DB
			err = db.DBconn.Create(ihc.storeName, key, ihc.tagInst, ihcs)
			if err != nil {
				retErr = retErr + " and couldn't save to DB"
			}

			return instanceMiniHCStatusFromStatus(ihcs),
				pkgerrors.Wrap(err, retErr)
		} else {
			h.Status.Status = release.HookPhaseRunning
			h.Status.KR = kr
		}
	}
	err = db.DBconn.Create(ihc.storeName, key, ihc.tagInst, ihcs)
	if err != nil {
		return instanceMiniHCStatusFromStatus(ihcs),
			pkgerrors.Wrap(err, "Creating Instance DB Entry")
	}
	log.Info("Successfully initialized Healthcheck resources", log.Fields{
		"InstanceId":    instanceId,
		"HealthcheckId": id,
	})
	go func() {
		var wg sync.WaitGroup
		update := make(chan bool) //True - hook finished, False - all hooks finished
		for _, h := range hooks {
			wg.Add(1)
			go func(hookStatus *HookStatus) {
				//TODO Handle errors here better in future, for now it's ok
				hookStatus.Status, _ = getHookState(*hookStatus, k8sClient, instance.Namespace)
				hookStatus.CompletedAt = time.Now()
				log.Info("Hook finished", log.Fields{
					"HealthcheckId": id,
					"InstanceId":    instanceId,
					"Hook":          hookStatus.Name,
					"Status":        hookStatus.Status,
				})
				update <- true
				wg.Done()
				return
			}(h.Status)
		}
		go func() {
			wg.Wait()
			log.Info("All hooks finished", log.Fields{
				"HealthcheckId": id,
				"InstanceId":    instanceId,
			})
			update <- false
			return
		}()
		for {
			select {
			case b := <-update:
				log.Info("Healthcheck update", log.Fields{
					"HealthcheckId": id,
					"InstanceId":    instanceId,
					"Reason":        map[bool]string{true: "Hook finished", false: "All hooks finished"}[b],
				})
				if b { //Some hook finished - need to update DB
					err = db.DBconn.Update(ihc.storeName, key, ihc.tagInst, ihcs)
					if err != nil {
						log.Error("Couldn't update database", log.Fields{
							"Store":   ihc.storeName,
							"Key":     key,
							"Payload": ihcs,
						})
					}
				} else { //All hooks finished - need to terminate goroutine
					testSuite.CompletedAt = time.Now()
					//If everything's fine, final result is OK
					finalResult := release.HookPhaseSucceeded
					//If at least single hook wasn't determined - it's Unknown
					for _, h := range hooks {
						if h.Status.Status == release.HookPhaseUnknown {
							finalResult = release.HookPhaseUnknown
							break
						}
					}
					//Unless at least one hook failed, then we've failed
					for _, h := range hooks {
						if h.Status.Status == release.HookPhaseFailed {
							finalResult = release.HookPhaseFailed
							break
						}
					}
					ihcs.Status = finalResult
					err = db.DBconn.Update(ihc.storeName, key, ihc.tagInst, ihcs)
					if err != nil {
						log.Error("Couldn't update database", log.Fields{
							"Store":   ihc.storeName,
							"Key":     key,
							"Payload": ihcs,
						})
					}
					return
				}
			}
		}
	}()
	return instanceMiniHCStatusFromStatus(ihcs), nil
}

func (ihc InstanceHCClient) Get(instanceId, healthcheckId string) (InstanceHCStatus, error) {
	key := HealthcheckKey{
		InstanceId:    instanceId,
		HealthcheckId: healthcheckId,
	}
	DBResp, err := db.DBconn.Read(ihc.storeName, key, ihc.tagInst)
	if err != nil || DBResp == nil {
		return InstanceHCStatus{}, pkgerrors.Wrap(err, "Error retrieving Healthcheck data")
	}

	resp := InstanceHCStatus{}
	err = db.DBconn.Unmarshal(DBResp, &resp)
	if err != nil {
		return InstanceHCStatus{}, pkgerrors.Wrap(err, "Unmarshaling Instance Value")
	}
	return resp, nil
}

func (ihc InstanceHCClient) Delete(instanceId, healthcheckId string) error {
	key := HealthcheckKey{instanceId, healthcheckId}
	v := app.NewInstanceClient()
	instance, err := v.Get(instanceId)
	if err != nil {
		return pkgerrors.Wrap(err, "Getting instance")
	}
	ihcs, err := ihc.Get(instanceId, healthcheckId)
	if err != nil {
		return pkgerrors.Wrap(err, "Error querying Healthcheck status")
	}
	k8sClient := app.KubernetesClient{}
	err = k8sClient.Init(instance.Request.CloudRegion, instanceId)
	if err != nil {
		return pkgerrors.Wrap(err, "Preparing KubeClient")
	}
	cumulatedErr := ""
	for _, hook := range ihcs.TestSuite.Results {
		err = k8sClient.DeleteKind(hook.KR, instance.Namespace)
		//FIXME handle "missing resource" error as not error - hook may be already deleted
		if err != nil {
			cumulatedErr += err.Error() + "\n"
		}
	}
	if cumulatedErr != "" {
		return pkgerrors.New("Removing hooks failed with errors:\n" + cumulatedErr)
	}
	err = db.DBconn.Delete(ihc.storeName, key, ihc.tagInst)
	if err != nil {
		return pkgerrors.Wrap(err, "Removing Healthcheck in DB")
	}
	return nil
}

func (ihc InstanceHCClient) List(instanceId string) (InstanceHCOverview, error) {
	ihco := InstanceHCOverview{
		InstanceId: instanceId,
	}

	// Retrieve info about available hooks
	v := app.NewInstanceClient()
	instance, err := v.Get(instanceId)
	if err != nil {
		return ihco, pkgerrors.Wrap(err, "Getting Instance data")
	}
	ihco.Hooks = instance.Hooks

	// Retrieve info about running/completed healthchecks
	dbResp, err := db.DBconn.ReadAll(ihc.storeName, ihc.tagInst)
	if err != nil {
		if !strings.Contains(err.Error(), "Did not find any objects with tag") {
			return ihco, pkgerrors.Wrap(err, "Getting Healthcheck data")
		}
	}
	miniStatus := make([]InstanceMiniHCStatus, 0)
	for key, value := range dbResp {
		//value is a byte array
		if value != nil {
			resp := InstanceHCStatus{}
			err = db.DBconn.Unmarshal(value, &resp)
			if err != nil {
				log.Error("Error unmarshaling Instance HC data", log.Fields{
					"error": err.Error(),
					"key":   key})
			}
			//Filter-out healthchecks from other instances
			if instanceId != resp.InstanceId {
				continue
			}
			miniStatus = append(miniStatus, instanceMiniHCStatusFromStatus(resp))
		}
	}
	ihco.HCSummary = miniStatus

	return ihco, nil
}