Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the collector run in the non-Kubernetes environment #285

Merged
merged 7 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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