-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
137 lines (116 loc) · 4.46 KB
/
utils.go
File metadata and controls
137 lines (116 loc) · 4.46 KB
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
/*
SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
SPDX-License-Identifier: Apache-2.0
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 controller
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
"slices"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
v1ac "k8s.io/client-go/applyconfigurations/meta/v1"
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
)
func InstanceHaUrl(region, zone, hostname string) string {
if haURL, found := os.LookupEnv("KVM_HA_SERVICE_URL"); found {
return haURL
}
return fmt.Sprintf("https://kvm-ha-service-%v.%v.cloud.sap/api/hypervisors/%v", zone, region, hostname)
}
func updateInstanceHA(hypervisor *kvmv1.Hypervisor, data string, acceptedCodes []int) error {
zone, found := hypervisor.Labels[corev1.LabelTopologyZone]
if !found {
return fmt.Errorf("could not find label %v for node", corev1.LabelTopologyZone)
}
region, found := hypervisor.Labels[corev1.LabelTopologyRegion]
if !found {
return fmt.Errorf("could not find label %v for node", corev1.LabelTopologyRegion)
}
hostname, found := hypervisor.Labels[corev1.LabelHostname]
if !found {
return fmt.Errorf("could not find label %v for node", corev1.LabelHostname)
}
url := InstanceHaUrl(region, zone, hostname)
// G107: Potential HTTP request made with variable url
resp, err := http.Post(url, "application/json", bytes.NewBuffer([]byte(data))) //nolint:gosec,bodyclose
if err != nil {
return fmt.Errorf("failed to send request to ha service due to %w", err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if !slices.Contains(acceptedCodes, resp.StatusCode) {
return fmt.Errorf("ha service answered with unexpected response %v for %v from %v", resp.StatusCode, data, url)
}
return nil
}
func enableInstanceHA(hypervisor *kvmv1.Hypervisor) error {
return updateInstanceHA(hypervisor, `{"enabled": true}`, []int{http.StatusOK})
}
func disableInstanceHA(hypervisor *kvmv1.Hypervisor) error {
return updateInstanceHA(hypervisor, `{"enabled": false}`, []int{http.StatusOK, http.StatusNotFound})
}
// IsNodeConditionTrue returns true when the conditionType is present and set to `corev1.ConditionTrue`
func IsNodeConditionTrue(conditions []corev1.NodeCondition, conditionType corev1.NodeConditionType) bool {
return IsNodeConditionPresentAndEqual(conditions, conditionType, corev1.ConditionTrue)
}
// IsNodeConditionPresentAndEqual returns true when conditionType is present and equal to status.
func IsNodeConditionPresentAndEqual(conditions []corev1.NodeCondition, conditionType corev1.NodeConditionType, status corev1.ConditionStatus) bool {
for _, condition := range conditions {
if condition.Type == conditionType {
return condition.Status == status
}
}
return false
}
// FindNodeStatusCondition returns the condition of the given type if it exists.
func FindNodeStatusCondition(conditions []corev1.NodeCondition, conditionType corev1.NodeConditionType) *corev1.NodeCondition {
for _, condition := range conditions {
if condition.Type == conditionType {
return &condition
}
}
return nil
}
func HasStatusCondition(conditions []metav1.Condition, conditionType string) bool {
for _, condition := range conditions {
if condition.Type == conditionType {
return true
}
}
return false
}
// returns all elements in b not in a
func Difference[S ~[]E, E comparable](s1, s2 S) S {
diff := make(S, 0)
for item := range slices.Values(s2) {
if !slices.Contains(s1, item) {
diff = append(diff, item)
}
}
return diff
}
// returns a OwnerReference for the given object and groupversionkind info as returned by apiutil.GVKForObject
func OwnerReference(obj metav1.Object, gvk *schema.GroupVersionKind) *v1ac.OwnerReferenceApplyConfiguration {
return v1ac.OwnerReference().
WithAPIVersion(gvk.Group + "/" + gvk.Version).
WithKind(gvk.Kind).
WithName(obj.GetName()).
WithUID(obj.GetUID())
}
var ErrRetry = errors.New("ErrRetry")