-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathinit_container_work.go
More file actions
162 lines (134 loc) · 5.02 KB
/
init_container_work.go
File metadata and controls
162 lines (134 loc) · 5.02 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
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
/*
Copyright 2026. projectsveltos.io. All rights reserved.
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 controllers
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
)
func Initialization(ctx context.Context, config *rest.Config,
scheme *runtime.Scheme, shardKey string, logger logr.Logger) {
logger.V(logs.LogInfo).Info("perform init work")
directClient, err := client.New(config, client.Options{Scheme: scheme})
if err != nil {
logger.V(logs.LogInfo).Error(err, "failed to get client")
return
}
updateHelmHashes(ctx, directClient, shardKey, logger)
}
func updateHelmHashes(ctx context.Context, directClient client.Client,
shardKey string, logger logr.Logger) {
clusterSummaries := &configv1beta1.ClusterSummaryList{}
for {
err := directClient.List(ctx, clusterSummaries)
if err == nil {
break
}
logger.V(logs.LogInfo).Error(err, "failed to get clusterSummary instances, retrying...")
select {
case <-ctx.Done():
logger.Info("stopping updateHelmHashes: context canceled during retry")
return
case <-time.After(time.Second):
continue
}
}
for i := range clusterSummaries.Items {
cs := &clusterSummaries.Items[i]
l := logger.WithValues("clusterSummary", fmt.Sprintf("%s/%s", cs.Namespace, cs.Name))
err := updateClusterSummaryHelmHashes(ctx, directClient, shardKey, cs, l)
if err != nil {
l.V(logs.LogInfo).Error(err, "failed to update helm and helm value hashes")
} else {
l.V(logs.LogInfo).Info("updated helm and helm value hashes")
}
}
}
func updateClusterSummaryHelmHashes(ctx context.Context, directClient client.Client,
shardKey string, clusterSummary *configv1beta1.ClusterSummary, logger logr.Logger) error {
if len(clusterSummary.Spec.ClusterProfileSpec.HelmCharts) == 0 {
return nil
}
isMatch, err := isClusterAShardMatch(ctx, directClient, shardKey, clusterSummary, logger)
if err != nil {
logger.V(logs.LogInfo).Error(err, "failed to verify is shard is a match")
isMatch = true
}
if !isMatch {
return nil
}
mgmtResources, err := collectTemplateResourceRefs(ctx, clusterSummary)
if err != nil {
logger.V(logs.LogInfo).Error(err, "failed to collect templateResourceRefs")
return err
}
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
currentClusterSummary := &configv1beta1.ClusterSummary{}
err := directClient.Get(ctx,
types.NamespacedName{Namespace: clusterSummary.Namespace, Name: clusterSummary.Name},
currentClusterSummary)
if err != nil {
return err
}
helmHash, err := helmHash(ctx, directClient, currentClusterSummary, logger)
if err != nil {
logger.V(logs.LogInfo).Error(err, "failed to get helm configuration hash")
return err
}
setHelmHash(currentClusterSummary, helmHash)
for i := range currentClusterSummary.Spec.ClusterProfileSpec.HelmCharts {
helmChart := ¤tClusterSummary.Spec.ClusterProfileSpec.HelmCharts[i]
instantiatedChart, err := getInstantiatedChart(ctx, currentClusterSummary, helmChart,
mgmtResources, logger)
if err != nil {
logger.V(logs.LogInfo).Error(err, "failed to get instantiated chart")
return err
}
helmChartValueHash, err := getHelmChartValuesHash(ctx, directClient, instantiatedChart,
currentClusterSummary, mgmtResources, logger)
if err != nil {
logger.V(logs.LogInfo).Error(err, "failed to get helm chart value hash")
return err
}
setHelmChartValueHash(currentClusterSummary, instantiatedChart, helmChartValueHash)
}
return directClient.Status().Update(ctx, currentClusterSummary)
})
return err
}
func setHelmHash(clusterSummary *configv1beta1.ClusterSummary, helmHash []byte) {
for i := range clusterSummary.Status.FeatureSummaries {
if clusterSummary.Status.FeatureSummaries[i].FeatureID == libsveltosv1beta1.FeatureHelm {
clusterSummary.Status.FeatureSummaries[i].Hash = helmHash
}
}
}
func setHelmChartValueHash(clusterSummary *configv1beta1.ClusterSummary, helmChart *configv1beta1.HelmChart,
helmChartValueHash []byte) {
for i := range clusterSummary.Status.HelmReleaseSummaries {
rs := &clusterSummary.Status.HelmReleaseSummaries[i]
if rs.ReleaseName == helmChart.ReleaseName &&
rs.ReleaseNamespace == helmChart.ReleaseNamespace {
rs.ValuesHash = helmChartValueHash
}
}
}