Skip to content

Commit 82f5679

Browse files
committed
pods support no node scraping
Signed-off-by: mickeyzzc <[email protected]>
1 parent b91dd31 commit 82f5679

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

pkg/app/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
204204

205205
namespaces := opts.Namespaces.GetNamespaces()
206206
nsFieldSelector := namespaces.GetExcludeNSFieldSelector(opts.NamespacesDenylist)
207-
nodeFieldSelector := opts.Node.GetNodeFieldSelector()
207+
nodeFieldSelector := opts.Node.GetNodeFieldSelector(opts.NoNodeScrape)
208208
merged, err := storeBuilder.MergeFieldSelectors([]string{nsFieldSelector, nodeFieldSelector})
209209
if err != nil {
210210
return err

pkg/options/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Options struct {
4646
Namespaces NamespaceList `yaml:"namespaces"`
4747
NamespacesDenylist NamespaceList `yaml:"namespaces_denylist"`
4848
Node NodeType `yaml:"node"`
49+
NoNodeScrape bool `yaml:"no_node_scrape"`
4950
Pod string `yaml:"pod"`
5051
Port int `yaml:"port"`
5152
Resources ResourceSet `yaml:"resources"`
@@ -120,6 +121,7 @@ func (o *Options) AddFlags(cmd *cobra.Command) {
120121

121122
o.cmd.Flags().BoolVar(&o.CustomResourcesOnly, "custom-resource-state-only", false, "Only provide Custom Resource State metrics (experimental)")
122123
o.cmd.Flags().BoolVar(&o.EnableGZIPEncoding, "enable-gzip-encoding", false, "Gzip responses when requested by clients via 'Accept-Encoding: gzip' header.")
124+
o.cmd.Flags().BoolVar(&o.NoNodeScrape, "enable-no-node-scrape", false, "This configuration is used in conjunction with node configuration. When this configuration is true, node configuration is empty and the metric of no scheduled pods is scraped. This is experimental.")
123125
o.cmd.Flags().BoolVarP(&o.Help, "help", "h", false, "Print Help text")
124126
o.cmd.Flags().BoolVarP(&o.UseAPIServerCache, "use-apiserver-cache", "", false, "Sets resourceVersion=0 for ListWatch requests, using cached resources from the apiserver instead of an etcd quorum read.")
125127
o.cmd.Flags().Int32Var(&o.Shard, "shard", int32(0), "The instances shard nominal (zero indexed) within the total number of shards. (default 0)")

pkg/options/types.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@ func (r *ResourceSet) Type() string {
108108
type NodeType string
109109

110110
// GetNodeFieldSelector returns a nodename field selector.
111-
func (n *NodeType) GetNodeFieldSelector() string {
111+
func (n *NodeType) GetNodeFieldSelector(b bool) string {
112112
if string(*n) != "" {
113113
return fields.OneTermEqualSelector("spec.nodeName", string(*n)).String()
114114
}
115+
if b {
116+
return fields.OneTermEqualSelector("spec.nodeName", "").String()
117+
}
115118
return EmptyFieldSelector()
116119
}
117120

pkg/options/types_test.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,25 @@ func TestNodeFieldSelector(t *testing.T) {
175175

176176
for _, test := range tests {
177177
node := test.Node
178-
actual := node.GetNodeFieldSelector()
178+
actual := node.GetNodeFieldSelector(false)
179+
if !reflect.DeepEqual(actual, test.Wanted) {
180+
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual)
181+
}
182+
}
183+
tests1 := []struct {
184+
Desc string
185+
Node NodeType
186+
Wanted string
187+
}{
188+
{
189+
Desc: "empty node name",
190+
Node: "",
191+
Wanted: "spec.nodeName=",
192+
},
193+
}
194+
for _, test := range tests1 {
195+
node := test.Node
196+
actual := node.GetNodeFieldSelector(true)
179197
if !reflect.DeepEqual(actual, test.Wanted) {
180198
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual)
181199
}
@@ -238,7 +256,7 @@ func TestMergeFieldSelectors(t *testing.T) {
238256
ns := test.Namespaces
239257
deniedNS := test.DeniedNamespaces
240258
selector1 := ns.GetExcludeNSFieldSelector(deniedNS)
241-
selector2 := test.Node.GetNodeFieldSelector()
259+
selector2 := test.Node.GetNodeFieldSelector(false)
242260
actual, err := MergeFieldSelectors([]string{selector1, selector2})
243261
if err != nil {
244262
t.Errorf("Test error for Desc: %s. Can't merge field selector %v.", test.Desc, err)

0 commit comments

Comments
 (0)