Skip to content

Commit

Permalink
Merge pull request #418 from klihub/devel/agent-runtime-configuration
Browse files Browse the repository at this point in the history
[podresapi 1/3] agent,config,helm: make agent runtime configurable.
  • Loading branch information
kad authored Dec 4, 2024
2 parents 3b69120 + 1d397a9 commit 1620059
Show file tree
Hide file tree
Showing 14 changed files with 232 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
94 changes: 71 additions & 23 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,37 +268,39 @@ func (a *Agent) hasLocalConfig() bool {

func (a *Agent) setupClients() 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
}
// Create HTTP/REST client and K8s client on initial startup. Any failure
// to create these is a failure start up.
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(cfg)
if err != nil {
return fmt.Errorf("failed to setup kubernetes HTTP client: %w", err)
}
a.httpCli, err = rest.HTTPClientFor(restCfg)
if err != nil {
return fmt.Errorf("failed to setup kubernetes HTTP 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)
}
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)
}

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

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

return nil
}
Expand All @@ -312,6 +314,51 @@ func (a *Agent) cleanupClients() {
a.nrtCli = nil
}

var (
defaultConfig = &cfgapi.AgentConfig{
NodeResourceTopology: true,
}
)

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

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

cfg := getAgentConfig(newConfig)

// Reconfigure NRT client, both on initial startup and reconfiguration.
// Failure to create a client is not a fatal error.
switch {
case cfg.NodeResourceTopology && a.nrtCli == nil:
log.Info("enabling NRT client")
cfg, err := a.getRESTConfig()
if err != nil {
log.Error("failed to setup NRT client: %w", err)
break
}
cli, err := nrtapi.NewForConfigAndClient(cfg, a.httpCli)
if err != nil {
log.Error("failed to setup NRT client: %w", err)
break
}
a.nrtCli = cli

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

func (a *Agent) getRESTConfig() (*rest.Config, error) {
var (
cfg *rest.Config
Expand Down Expand Up @@ -556,6 +603,7 @@ func (a *Agent) updateConfig(cfg metav1.Object) {
}

a.currentCfg = cfg
a.configure(cfg)
}

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 1620059

Please sign in to comment.