Skip to content

Commit

Permalink
agent,config,helm: make agent runtime configurable.
Browse files Browse the repository at this point in the history
Add configuration for the agent itself. Currently the
only control enables access to Node Resource Topology
Custom Resources and it is on by default. Update the
agent to honor its configuration.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Dec 1, 2024
1 parent ef4c84f commit afc1046
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 23 deletions.
12 changes: 12 additions & 0 deletions config/crd/bases/config.nri_balloonspolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ spec:
spec:
description: BalloonsPolicySpec describes a balloons policy.
properties:
agent:
default:
nodeResourceTopology: true
description: AgentConfig provides access to configuration data for
the agent.
properties:
nodeResourceTopology:
description: |-
NodeResourceTopology enables support for exporting resource usage using
NodeResourceTopology Custom Resources.
type: boolean
type: object
allocatorTopologyBalancing:
description: |-
If AllocatorTopologyBalancing is true, balloons are
Expand Down
12 changes: 12 additions & 0 deletions config/crd/bases/config.nri_templatepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ spec:
spec:
description: TemplatePolicySpec describes a template policy.
properties:
agent:
default:
nodeResourceTopology: true
description: AgentConfig provides access to configuration data for
the agent.
properties:
nodeResourceTopology:
description: |-
NodeResourceTopology enables support for exporting resource usage using
NodeResourceTopology Custom Resources.
type: boolean
type: object
availableResources:
additionalProperties:
type: string
Expand Down
12 changes: 12 additions & 0 deletions config/crd/bases/config.nri_topologyawarepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ spec:
spec:
description: TopologyAwarePolicySpec describes a topology-aware policy.
properties:
agent:
default:
nodeResourceTopology: true
description: AgentConfig provides access to configuration data for
the agent.
properties:
nodeResourceTopology:
description: |-
NodeResourceTopology enables support for exporting resource usage using
NodeResourceTopology Custom Resources.
type: boolean
type: object
availableResources:
additionalProperties:
type: string
Expand Down
12 changes: 12 additions & 0 deletions deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ spec:
spec:
description: BalloonsPolicySpec describes a balloons policy.
properties:
agent:
default:
nodeResourceTopology: true
description: AgentConfig provides access to configuration data for
the agent.
properties:
nodeResourceTopology:
description: |-
NodeResourceTopology enables support for exporting resource usage using
NodeResourceTopology Custom Resources.
type: boolean
type: object
allocatorTopologyBalancing:
description: |-
If AllocatorTopologyBalancing is true, balloons are
Expand Down
12 changes: 12 additions & 0 deletions deployment/helm/template/crds/config.nri_templatepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ spec:
spec:
description: TemplatePolicySpec describes a template policy.
properties:
agent:
default:
nodeResourceTopology: true
description: AgentConfig provides access to configuration data for
the agent.
properties:
nodeResourceTopology:
description: |-
NodeResourceTopology enables support for exporting resource usage using
NodeResourceTopology Custom Resources.
type: boolean
type: object
availableResources:
additionalProperties:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ spec:
spec:
description: TopologyAwarePolicySpec describes a topology-aware policy.
properties:
agent:
default:
nodeResourceTopology: true
description: AgentConfig provides access to configuration data for
the agent.
properties:
nodeResourceTopology:
description: |-
NodeResourceTopology enables support for exporting resource usage using
NodeResourceTopology Custom Resources.
type: boolean
type: object
availableResources:
additionalProperties:
type: string
Expand Down
96 changes: 73 additions & 23 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,42 +262,88 @@ func (a *Agent) Stop() {
}
}

func (a *Agent) hasLocalConfig() bool {
return a.configFile != ""
var (
defaultConfig = &cfgapi.AgentConfig{
PodResourceAPI: false,
NodeResourceTopology: true,
}
)

func getAgentConfig(newConfig metav1.Object) *cfgapi.AgentConfig {
cfg := cfgapi.GetAgentConfig(newConfig)
if cfg == nil {
return defaultConfig
}
return cfg
}

func (a *Agent) setupClients() error {
func (a *Agent) configure(newConfig metav1.Object) error {
if a.hasLocalConfig() {
log.Warn("running with local configuration, skipping cluster access client setup...")
return nil
}

cfg, err := a.getRESTConfig()
if err != nil {
return err
}
cfg := getAgentConfig(newConfig)

a.httpCli, err = rest.HTTPClientFor(cfg)
if err != nil {
return fmt.Errorf("failed to setup kubernetes HTTP client: %w", err)
// We always need a HTTP/REST client and a K8s client.
if a.httpCli == nil {
log.Info("setting up HTTP/REST client...")
restCfg, err := a.getRESTConfig()
if err != nil {
return err
}

a.httpCli, err = rest.HTTPClientFor(restCfg)
if err != nil {
return fmt.Errorf("failed to setup kubernetes HTTP client: %w", err)
}

log.Info("setting up K8s client...")
a.k8sCli, err = k8sclient.NewForConfigAndClient(restCfg, a.httpCli)
if err != nil {
a.cleanupClients()
return fmt.Errorf("failed to setup kubernetes client: %w", err)
}

kubeCfg := *restCfg
err = a.cfgIf.SetKubeClient(a.httpCli, &kubeCfg)
if err != nil {
return fmt.Errorf("failed to setup kubernetes config resource client: %w", err)
}
}

a.k8sCli, err = k8sclient.NewForConfigAndClient(cfg, a.httpCli)
if err != nil {
a.cleanupClients()
return fmt.Errorf("failed to setup kubernetes client: %w", err)
switch {
case cfg.NodeResourceTopology && a.nrtCli == nil:
log.Info("enabling NRT client")
cfg, err := a.getRESTConfig()
if err != nil {
return err
}
cli, err := nrtapi.NewForConfigAndClient(cfg, a.httpCli)
if err != nil {
return fmt.Errorf("failed to setup NRT client: %w", err)
}
a.nrtCli = cli

case !cfg.NodeResourceTopology && a.nrtCli != nil:
log.Info("disabling NRT client")
a.nrtCli = nil
}

restCfg := *cfg
a.nrtCli, err = nrtapi.NewForConfigAndClient(&restCfg, a.httpCli)
if err != nil {
a.cleanupClients()
return fmt.Errorf("failed to setup NRT client: %w", err)
return nil
}

func (a *Agent) hasLocalConfig() bool {
return a.configFile != ""
}

func (a *Agent) setupClients() error {
if a.hasLocalConfig() {
return nil
}

restCfg = *cfg
err = a.cfgIf.SetKubeClient(a.httpCli, &restCfg)
if err != nil {
return fmt.Errorf("failed to setup kubernetes config resource client: %w", err)
if err := a.configure(a.currentCfg); err != nil {
return fmt.Errorf("failed set up clients: %w", err)
}

return nil
Expand Down Expand Up @@ -556,6 +602,10 @@ func (a *Agent) updateConfig(cfg metav1.Object) {
}

a.currentCfg = cfg

if err := a.configure(cfg); err != nil {
log.Warn("failed to reconfigure agent: %v", err)
}
}

func (a *Agent) patchConfigStatus(prev, curr metav1.Object, errors error) {
Expand Down
31 changes: 31 additions & 0 deletions pkg/apis/config/v1alpha1/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The NRI Plugins Authors. 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 v1alpha1

// AgentConfig provides access to configuration data for the agent.
type AgentConfig struct {
// NodeResourceTopology enables support for exporting resource usage using
// NodeResourceTopology Custom Resources.
// +optional
NodeResourceTopology bool `json:"nodeResourceTopology,omitempty"`
}

// GetAgentConfig returns the agent-specific configuration if we have one.
func GetAgentConfig(cfg interface{}) *AgentConfig {
if ac, ok := cfg.(interface{ AgentConfig() *AgentConfig }); ok {
return ac.AgentConfig()
}
return nil
}
10 changes: 10 additions & 0 deletions pkg/apis/config/v1alpha1/balloons-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ var (
_ ResmgrConfig = &BalloonsPolicy{}
)

func (c *BalloonsPolicy) AgentConfig() *AgentConfig {
if c == nil {
return nil
}

a := c.Spec.Agent

return &a
}

func (c *BalloonsPolicy) CommonConfig() *CommonConfig {
if c == nil {
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/config/v1alpha1/resmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
// +kubebuilder:object:generate=false
type ResmgrConfig interface {
metav1.ObjectMetaAccessor
AgentConfig() *AgentConfig
CommonConfig() *CommonConfig
PolicyConfig() interface{}
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/config/v1alpha1/template-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ var (
_ ResmgrConfig = &TemplatePolicy{}
)

func (c *TemplatePolicy) AgentConfig() *AgentConfig {
if c == nil {
return nil
}

a := c.Spec.Agent

return &a
}

func (c *TemplatePolicy) CommonConfig() *CommonConfig {
if c == nil {
return nil
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/config/v1alpha1/topology-aware-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ var (
_ ResmgrConfig = &TopologyAwarePolicy{}
)

func (c *TopologyAwarePolicy) AgentConfig() *AgentConfig {
if c == nil {
return nil
}

a := c.Spec.Agent

return &a
}

func (c *TopologyAwarePolicy) CommonConfig() *CommonConfig {
if c == nil {
return nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/config/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type TopologyAwarePolicySpec struct {
Log log.Config `json:"log,omitempty"`
// +optional
Instrumentation instrumentation.Config `json:"instrumentation,omitempty"`
// +optional
// +kubebuilder:default={"nodeResourceTopology": true }
Agent AgentConfig `json:"agent,omitempty"`
}

// TopologyAwarePolicyList represents a list of TopologyAwarePolicies.
Expand Down Expand Up @@ -78,6 +81,9 @@ type BalloonsPolicySpec struct {
Log log.Config `json:"log,omitempty"`
// +optional
Instrumentation instrumentation.Config `json:"instrumentation,omitempty"`
// +optional
// +kubebuilder:default={"nodeResourceTopology": true }
Agent AgentConfig `json:"agent,omitempty"`
}

// BalloonsPolicyList represents a list of BalloonsPolicies.
Expand Down Expand Up @@ -110,6 +116,9 @@ type TemplatePolicySpec struct {
Log log.Config `json:"log,omitempty"`
// +optional
Instrumentation instrumentation.Config `json:"instrumentation,omitempty"`
// +optional
// +kubebuilder:default={"nodeResourceTopology": true }
Agent AgentConfig `json:"agent,omitempty"`
}

// TemplatePolicyList represents a list of TemplatePolicies.
Expand Down
Loading

0 comments on commit afc1046

Please sign in to comment.