@@ -2,12 +2,12 @@ package manager
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
7
6
"github.com/operator-framework/operator-sdk/pkg/leader"
8
7
"github.com/prometheus/client_golang/prometheus"
9
8
log "github.com/sirupsen/logrus"
10
9
"github.com/webdevops/kube-pool-manager/config"
10
+ "github.com/webdevops/kube-pool-manager/k8s"
11
11
corev1 "k8s.io/api/core/v1"
12
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
13
"k8s.io/apimachinery/pkg/types"
@@ -147,6 +147,9 @@ func (m *KubePoolManager) startNodeWatch() error {
147
147
func (m * KubePoolManager ) applyNode (node * corev1.Node ) {
148
148
contextLogger := log .WithField ("node" , node .Name )
149
149
150
+ nodePatchSets := k8s .NewJsonPatchSet ()
151
+ poolNameList := []string {}
152
+
150
153
for _ , poolConfig := range m .Config .Pools {
151
154
m .prometheus .nodePoolStatus .WithLabelValues (node .Name , poolConfig .Name ).Set (0 )
152
155
poolLogger := contextLogger .WithField ("pool" , poolConfig .Name )
@@ -156,36 +159,40 @@ func (m *KubePoolManager) applyNode(node *corev1.Node) {
156
159
}
157
160
158
161
if matching {
159
- poolLogger .Infof ("applying pool \" %s\" to node \" %s\" " , poolConfig .Name , node .Name )
162
+ poolLogger .Infof ("adding configuration from pool \" %s\" to node \" %s\" " , poolConfig .Name , node .Name )
160
163
161
164
// create json patch
162
165
patchSet := poolConfig .CreateJsonPatchSet ()
163
- patchBytes , patchErr := json .Marshal (patchSet )
164
- if patchErr != nil {
165
- poolLogger .Errorf ("failed to create json patch: %v" , err )
166
- return
167
- }
166
+ nodePatchSets .AddSet (patchSet )
167
+ poolNameList = append (poolNameList , poolConfig .Name )
168
+ } else {
169
+ poolLogger .Debugf ("Node NOT matches pool \" %s\" " , poolConfig .Name )
170
+ }
171
+ }
168
172
169
- if ! m .Opts .DryRun {
170
- // patch node
171
- _ , k8sError := m .k8sClient .CoreV1 ().Nodes ().Patch (m .ctx , node .Name , types .JSONPatchType , patchBytes , metav1.PatchOptions {})
172
- if k8sError != nil {
173
- poolLogger .Errorf ("failed to apply json patch: %v" , k8sError )
174
- return
175
- }
176
- } else {
177
- poolLogger .Infof ("Not applying pool config, dry-run active" )
178
- }
173
+ // apply patches
174
+ contextLogger .Infof ("applying configuration to node \" %s\" " , node .Name )
179
175
180
- m .prometheus .nodePoolStatus .WithLabelValues (node .Name , poolConfig .Name ).Set (1 )
181
- m .prometheus .nodeApplied .WithLabelValues (node .Name ).SetToCurrentTime ()
176
+ patchBytes , patchErr := nodePatchSets .Marshal ()
177
+ if patchErr != nil {
178
+ contextLogger .Errorf ("failed to create json patch: %v" , patchErr )
179
+ return
180
+ }
182
181
183
- // check if this more pool configurations should be applied
184
- if ! poolConfig . Continue {
185
- break
186
- }
187
- } else {
188
- poolLogger . Debugf ( "Node NOT matches pool configuration \" %s \" " , poolConfig . Name )
182
+ if ! m . Opts . DryRun {
183
+ // patch node
184
+ _ , k8sError := m . k8sClient . CoreV1 (). Nodes (). Patch ( m . ctx , node . Name , types . JSONPatchType , patchBytes , metav1. PatchOptions {})
185
+ if k8sError != nil {
186
+ contextLogger . Errorf ( "failed to apply json patch: %v" , k8sError )
187
+ return
189
188
}
189
+ } else {
190
+ contextLogger .Infof ("Not applying pool config, dry-run active" )
191
+ }
192
+
193
+ // metrics
194
+ for _ , poolName := range poolNameList {
195
+ m .prometheus .nodePoolStatus .WithLabelValues (node .Name , poolName ).Set (1 )
196
+ m .prometheus .nodeApplied .WithLabelValues (node .Name ).SetToCurrentTime ()
190
197
}
191
198
}
0 commit comments