Skip to content

Commit

Permalink
Allow the collector run in the non-Kubernetes environment (#285)
Browse files Browse the repository at this point in the history
* remove unnecessary package alias 'kubernetes2'

Signed-off-by: Daxin Wang <daxinwang@harmonycloud.cn>

* add the disable option to kubeprocessor

Signed-off-by: Daxin Wang <daxinwang@harmonycloud.cn>

* update configuration files

Signed-off-by: Daxin Wang <daxinwang@harmonycloud.cn>

* update the option's description

Signed-off-by: Daxin Wang <daxinwang@harmonycloud.cn>

* update the changelog

Signed-off-by: Daxin Wang <daxinwang@harmonycloud.cn>

* change the option 'disable' to 'enable'

Signed-off-by: Daxin Wang <daxinwang@harmonycloud.cn>
  • Loading branch information
dxsup authored Jul 1, 2022
1 parent 66ff954 commit 01771fe
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

## Unreleased
### Enhancements
- New environment variable: IS_PRINT_EVENT, when the value is true, sinsp event can be printed ([#283](https://github.com/CloudDectective-Harmonycloud/kindling/pull/283))
- Allow the collector run in the non-Kubernetes environment by setting the option `enable` `false` under the `k8smetadataprocessor` section. ([#285](https://github.com/CloudDectective-Harmonycloud/kindling/pull/285))
- Add a new environment variable: IS_PRINT_EVENT. When the value is true, sinsp events can be printed to the stdout. ([#283](https://github.com/CloudDectective-Harmonycloud/kindling/pull/283))
- Declare the 9500 port in the agent's deployment file ([#282](https://github.com/CloudDectective-Harmonycloud/kindling/pull/282))
### Bug fixes
- Fix the bug where the table name of SQL is missed if there is no trailing character at the end of the table name. ([#284](https://github.com/CloudDectective-Harmonycloud/kindling/pull/284))
Expand Down
3 changes: 3 additions & 0 deletions collector/docker/kindling-collector-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ analyzers:

processors:
k8smetadataprocessor:
# Set "enable" false if you want to run the agent in the non-Kubernetes environment.
# Otherwise, the agent will panic if it can't connect to the API-server.
enable: true
kube_auth_type: kubeConfig
kube_config_dir: /root/.kube/config
grace_delete_period: 60
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ type Config struct {
// The unit is seconds, and the default value is 60 seconds.
// Should not be lower than 30 seconds.
GraceDeletePeriod int `mapstructure:"grace_delete_period"`
// Set "Enable" false if you want to run the agent in the non-Kubernetes environment.
// Otherwise, the agent will panic if it can't connect to the API-server.
Enable bool `mapstructure:"enable"`
}

var DefaultConfig Config = Config{
KubeAuthType: "serviceAccount",
KubeConfigDir: "~/.kube/config",
GraceDeletePeriod: 60,
Enable: true,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/Kindling-project/kindling/collector/pkg/component"
"github.com/Kindling-project/kindling/collector/pkg/component/consumer"
"github.com/Kindling-project/kindling/collector/pkg/component/consumer/processor"
kubernetes2 "github.com/Kindling-project/kindling/collector/pkg/metadata/kubernetes"
"github.com/Kindling-project/kindling/collector/pkg/metadata/kubernetes"
"github.com/Kindling-project/kindling/collector/pkg/model"
"github.com/Kindling-project/kindling/collector/pkg/model/constlabels"
"github.com/Kindling-project/kindling/collector/pkg/model/constnames"
Expand All @@ -19,7 +19,8 @@ const (
)

type K8sMetadataProcessor struct {
metadata *kubernetes2.K8sMetaDataCache
config *Config
metadata *kubernetes.K8sMetaDataCache
nextConsumer consumer.Consumer
localNodeIp string
localNodeName string
Expand All @@ -31,13 +32,22 @@ func NewKubernetesProcessor(cfg interface{}, telemetry *component.TelemetryTools
if !ok {
telemetry.Logger.Panic("Cannot convert Component config", zap.String("componentType", K8sMetadata))
}
var options []kubernetes2.Option
options = append(options, kubernetes2.WithAuthType(config.KubeAuthType))
options = append(options, kubernetes2.WithKubeConfigDir(config.KubeConfigDir))
options = append(options, kubernetes2.WithGraceDeletePeriod(config.GraceDeletePeriod))
err := kubernetes2.InitK8sHandler(options...)
if !config.Enable {
telemetry.Logger.Info("The kubernetes processor is disabled by the configuration. Won't connect to the API-server and no Kubernetes metadata will be fetched.")
return &K8sMetadataProcessor{
config: config,
metadata: kubernetes.MetaDataCache,
nextConsumer: nextConsumer,
telemetry: telemetry,
}
}
var options []kubernetes.Option
options = append(options, kubernetes.WithAuthType(config.KubeAuthType))
options = append(options, kubernetes.WithKubeConfigDir(config.KubeConfigDir))
options = append(options, kubernetes.WithGraceDeletePeriod(config.GraceDeletePeriod))
err := kubernetes.InitK8sHandler(options...)
if err != nil {
telemetry.Logger.Sugar().Panicf("Failed to initialize [%s]: %v", K8sMetadata, err)
telemetry.Logger.Sugar().Panicf("Failed to initialize [%s]: %v. Set the option 'enable' false if you want to run the agent in the non-Kubernetes environment.", K8sMetadata, err)
return nil
}

Expand All @@ -49,7 +59,8 @@ func NewKubernetesProcessor(cfg interface{}, telemetry *component.TelemetryTools
telemetry.Logger.Warn("Local NodeName can not found", zap.Error(err))
}
return &K8sMetadataProcessor{
metadata: kubernetes2.MetaDataCache,
config: config,
metadata: kubernetes.MetaDataCache,
nextConsumer: nextConsumer,
localNodeIp: localNodeIp,
localNodeName: localNodeName,
Expand All @@ -58,6 +69,9 @@ func NewKubernetesProcessor(cfg interface{}, telemetry *component.TelemetryTools
}

func (p *K8sMetadataProcessor) Consume(dataGroup *model.DataGroup) error {
if !p.config.Enable {
return p.nextConsumer.Consume(dataGroup)
}
name := dataGroup.Name
switch name {
case constnames.NetRequestMetricGroupName:
Expand Down Expand Up @@ -324,13 +338,13 @@ func (p *K8sMetadataProcessor) addK8sMetaDataViaIpDST(labelMap *model.AttributeM
}
}

func addContainerMetaInfoLabelSRC(labelMap *model.AttributeMap, containerInfo *kubernetes2.K8sContainerInfo) {
func addContainerMetaInfoLabelSRC(labelMap *model.AttributeMap, containerInfo *kubernetes.K8sContainerInfo) {
labelMap.UpdateAddStringValue(constlabels.SrcContainer, containerInfo.Name)
labelMap.UpdateAddStringValue(constlabels.SrcContainerId, containerInfo.ContainerId)
addPodMetaInfoLabelSRC(labelMap, containerInfo.RefPodInfo)
}

func addPodMetaInfoLabelSRC(labelMap *model.AttributeMap, podInfo *kubernetes2.K8sPodInfo) {
func addPodMetaInfoLabelSRC(labelMap *model.AttributeMap, podInfo *kubernetes.K8sPodInfo) {
labelMap.UpdateAddStringValue(constlabels.SrcNode, podInfo.NodeName)
labelMap.UpdateAddStringValue(constlabels.SrcNodeIp, podInfo.NodeAddress)
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, podInfo.Namespace)
Expand All @@ -343,13 +357,13 @@ func addPodMetaInfoLabelSRC(labelMap *model.AttributeMap, podInfo *kubernetes2.K
}
}

func addContainerMetaInfoLabelDST(labelMap *model.AttributeMap, containerInfo *kubernetes2.K8sContainerInfo) {
func addContainerMetaInfoLabelDST(labelMap *model.AttributeMap, containerInfo *kubernetes.K8sContainerInfo) {
labelMap.UpdateAddStringValue(constlabels.DstContainer, containerInfo.Name)
labelMap.UpdateAddStringValue(constlabels.DstContainerId, containerInfo.ContainerId)
addPodMetaInfoLabelDST(labelMap, containerInfo.RefPodInfo)
}

func addPodMetaInfoLabelDST(labelMap *model.AttributeMap, podInfo *kubernetes2.K8sPodInfo) {
func addPodMetaInfoLabelDST(labelMap *model.AttributeMap, podInfo *kubernetes.K8sPodInfo) {
labelMap.UpdateAddStringValue(constlabels.DstNode, podInfo.NodeName)
labelMap.UpdateAddStringValue(constlabels.DstNodeIp, podInfo.NodeAddress)
labelMap.UpdateAddStringValue(constlabels.DstNamespace, podInfo.Namespace)
Expand Down
3 changes: 3 additions & 0 deletions deploy/agent/kindling-collector-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ analyzers:

processors:
k8smetadataprocessor:
# Set "enable" false if you want to run the agent in the non-Kubernetes environment.
# Otherwise, the agent will panic if it can't connect to the API-server.
enable: true
kube_auth_type: serviceAccount
kube_config_dir: /root/.kube/config
grace_delete_period: 60
Expand Down

0 comments on commit 01771fe

Please sign in to comment.