Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement watch-based daemonset replica counter.
- Loading branch information
Showing
9 changed files
with
238 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
clusterloader2/pkg/measurement/util/runtimeobjects/replicaswatcher.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
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 runtimeobjects | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"k8s.io/klog" | ||
"k8s.io/perf-tests/clusterloader2/pkg/measurement/util" | ||
"k8s.io/perf-tests/clusterloader2/pkg/measurement/util/informer" | ||
) | ||
|
||
const ( | ||
informerSyncTimeout = time.Minute | ||
) | ||
|
||
// ReplicasWatcher is a struct that allows to check a number of replicas at a given time. | ||
// Usage: | ||
// var rw ReplicasWatcher = (...) | ||
// if err := rw.Start(); err != nil { | ||
// panic(err); | ||
// } | ||
// // Get number of replicas as needed. | ||
// val = rw.Replicas() | ||
// ... | ||
// val = rw.Replicas() | ||
// | ||
// // Once you are done... | ||
// rw.Stop(); | ||
type ReplicasWatcher interface { | ||
Replicas() int | ||
// Start must block until Replicas() starts returning correct value. | ||
Start() error | ||
Stop() | ||
} | ||
|
||
type ConstReplicas struct { | ||
ReplicasVal int | ||
} | ||
|
||
func (c *ConstReplicas) Replicas() int { | ||
return c.ReplicasVal | ||
} | ||
|
||
func (c *ConstReplicas) Start() error { | ||
return nil | ||
} | ||
func (c *ConstReplicas) Stop() {} | ||
|
||
var _ ReplicasWatcher = &ConstReplicas{} | ||
|
||
// nodeCounter counts a number of node objects matching nodeSelector and affinity. | ||
type nodeCounter struct { | ||
client clientset.Interface | ||
nodeSelector labels.Selector | ||
affinity *corev1.Affinity | ||
stopCh chan struct{} | ||
|
||
mu sync.Mutex | ||
replicas int | ||
} | ||
|
||
var _ ReplicasWatcher = &nodeCounter{} | ||
|
||
func NewNodeCounter(client clientset.Interface, nodeSelector labels.Selector, affinity *corev1.Affinity) *nodeCounter { | ||
return &nodeCounter{ | ||
client: client, | ||
nodeSelector: nodeSelector, | ||
affinity: affinity, | ||
stopCh: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (n *nodeCounter) Start() error { | ||
i := informer.NewInformer( | ||
n.client, | ||
"nodes", | ||
&util.ObjectSelector{ | ||
Namespace: metav1.NamespaceAll, | ||
LabelSelector: n.nodeSelector.String(), | ||
}, | ||
func(oldObj, newObj interface{}) { | ||
if err := n.handleObject(oldObj, newObj); err != nil { | ||
klog.Errorf("Error while processing node: %v", err) | ||
} | ||
}, | ||
) | ||
return informer.StartAndSync(i, n.stopCh, informerSyncTimeout) | ||
} | ||
|
||
func (n *nodeCounter) Stop() {} | ||
|
||
func (n *nodeCounter) handleObject(oldObj, newObj interface{}) error { | ||
old, err := n.shouldRun(oldObj) | ||
if err != nil { | ||
return err | ||
} | ||
new, err := n.shouldRun(newObj) | ||
if err != nil { | ||
return err | ||
} | ||
if new == old { | ||
return nil | ||
} | ||
n.mu.Lock() | ||
defer n.mu.Unlock() | ||
if new && !old { | ||
n.replicas++ | ||
} else { | ||
n.replicas-- | ||
} | ||
klog.Infof("Updated expected replicas: %v", n.replicas) | ||
return nil | ||
} | ||
|
||
func (n *nodeCounter) Replicas() int { return n.replicas } | ||
|
||
func (n *nodeCounter) shouldRun(obj interface{}) (bool, error) { | ||
if obj == nil { | ||
return false, nil | ||
} | ||
node, ok := obj.(*corev1.Node) | ||
if !ok { | ||
return false, fmt.Errorf("unexpected type of obj: %v. got %T, want *corev1.Node", obj, obj) | ||
} | ||
matched, err := podMatchesNodeAffinity(n.affinity, node) | ||
return !node.Spec.Unschedulable && matched, err | ||
} | ||
|
||
func GetReplicasOnce(rw ReplicasWatcher) (int, error) { | ||
if err := rw.Start(); err != nil { | ||
return 0, err | ||
} | ||
defer rw.Stop() | ||
return rw.Replicas(), nil | ||
} |
Oops, something went wrong.