-
Notifications
You must be signed in to change notification settings - Fork 109
/
discovery_versions_to_install.go
125 lines (104 loc) · 4.37 KB
/
discovery_versions_to_install.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
/*
Copyright 2023 Flant JSC
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 hooks
import (
"context"
"fmt"
"sort"
"strings"
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/deckhouse/deckhouse/go_lib/dependency"
"github.com/deckhouse/deckhouse/go_lib/telemetry"
"github.com/deckhouse/deckhouse/modules/110-istio/hooks/lib"
"github.com/deckhouse/deckhouse/modules/110-istio/hooks/lib/istio_versions"
)
var _ = sdk.RegisterFunc(&go_hook.HookConfig{
// The Order below matters for ensure_crds_istio.go, it needs globalVersion to deploy proper CRDs
OnStartup: &go_hook.OrderedConfig{Order: 5},
OnBeforeHelm: &go_hook.OrderedConfig{Order: 5},
}, dependency.WithExternalDependencies(revisionsDiscovery))
func revisionsDiscovery(input *go_hook.HookInput, dc dependency.Container) error {
var globalVersion string
var versionsToInstall = make([]string, 0)
var unsupportedVersions = make([]string, 0)
var supportedVersions = make([]string, 0)
var supportedVersionsResult = input.Values.Get("istio.internal.versionMap").Map()
for versionResult := range supportedVersionsResult {
supportedVersions = append(supportedVersions, versionResult)
}
switch {
case input.ConfigValues.Exists("istio.globalVersion"):
// globalVersion is set in CM — use it
globalVersion = input.ConfigValues.Get("istio.globalVersion").String()
case input.Values.Exists("istio.internal.globalVersion"):
// globalVersion was previously discovered — use it
globalVersion = input.Values.Get("istio.internal.globalVersion").String()
default:
// maybe there is a global istiod Service with annotation?
k8sClient, err := dc.GetK8sClient()
if err != nil {
return err
}
service, err := k8sClient.CoreV1().Services("d8-istio").Get(context.TODO(), "istiod", metav1.GetOptions{})
if err == nil {
// there is the global istiod Service — let's get the annotation
if version, ok := service.GetAnnotations()["istio.deckhouse.io/global-version"]; ok {
globalVersion = version
} else {
return fmt.Errorf("can't find istio.deckhouse.io/global-version annotation for istiod global Service d8-istio/istiod")
}
}
}
// couldn't discover globalVersion — let's use default value from openapi/config-values.yaml
if globalVersion == "" {
globalVersion = input.Values.Get("istio.globalVersion").String()
}
var additionalVersionsResult = input.ConfigValues.Get("istio.additionalVersions").Array()
for _, versionResult := range additionalVersionsResult {
if !lib.Contains(supportedVersions, versionResult.String()) {
unsupportedVersions = append(unsupportedVersions, versionResult.String())
continue
}
versionsToInstall = append(versionsToInstall, versionResult.String())
}
if !lib.Contains(supportedVersions, globalVersion) {
if !lib.Contains(unsupportedVersions, globalVersion) {
unsupportedVersions = append(unsupportedVersions, globalVersion)
}
} else {
if !lib.Contains(versionsToInstall, globalVersion) {
versionsToInstall = append(versionsToInstall, globalVersion)
}
}
if len(unsupportedVersions) > 0 {
sort.Strings(unsupportedVersions)
return fmt.Errorf("unsupported versions: [%s]", strings.Join(unsupportedVersions, ","))
}
sort.Strings(versionsToInstall) // to guarantee same order
input.Values.Set("istio.internal.globalVersion", globalVersion)
input.Values.Set("istio.internal.versionsToInstall", versionsToInstall)
versionMap := istio_versions.VersionMapJSONToVersionMap(input.Values.Get("istio.internal.versionMap").String())
for _, ver := range versionsToInstall {
fullVer, ok := versionMap[ver]
if !ok {
input.LogEntry.Warnf("Not found full version for version to install %s", ver)
continue
}
input.MetricsCollector.Set(telemetry.WrapName("istio_control_plane_full_version"), 1.0, map[string]string{
"full_version": fullVer.FullVersion,
})
}
return nil
}