inspektor-gadget

Форк
0
/
gadget-context.go 
156 строк · 4.3 Кб
1
// Copyright 2022-2023 The Inspektor Gadget authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
/*
16
Package gadgetcontext handles initializing gadgets and installed operators before
17
handing them over to a specified runtime.
18
*/
19
package gadgetcontext
20

21
import (
22
	"context"
23
	"time"
24

25
	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
26
	runTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/run/types"
27
	"github.com/inspektor-gadget/inspektor-gadget/pkg/logger"
28
	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators"
29
	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
30
	"github.com/inspektor-gadget/inspektor-gadget/pkg/parser"
31
	"github.com/inspektor-gadget/inspektor-gadget/pkg/runtime"
32
)
33

34
// GadgetContext handles running gadgets by the gadget interface; it orchestrates the whole lifecycle of the gadget
35
// instance and communicates with gadget and runtime.
36
type GadgetContext struct {
37
	ctx                      context.Context
38
	cancel                   context.CancelFunc
39
	id                       string
40
	gadget                   gadgets.GadgetDesc
41
	gadgetParams             *params.Params
42
	args                     []string
43
	runtime                  runtime.Runtime
44
	runtimeParams            *params.Params
45
	parser                   parser.Parser
46
	operators                operators.Operators
47
	operatorsParamCollection params.Collection
48
	logger                   logger.Logger
49
	result                   []byte
50
	resultError              error
51
	timeout                  time.Duration
52
	gadgetInfo               *runTypes.GadgetInfo
53
}
54

55
func New(
56
	ctx context.Context,
57
	id string,
58
	runtime runtime.Runtime,
59
	runtimeParams *params.Params,
60
	gadget gadgets.GadgetDesc,
61
	gadgetParams *params.Params,
62
	args []string,
63
	operatorsParamCollection params.Collection,
64
	parser parser.Parser,
65
	logger logger.Logger,
66
	timeout time.Duration,
67
	gadgetInfo *runTypes.GadgetInfo,
68
) *GadgetContext {
69
	gCtx, cancel := context.WithCancel(ctx)
70

71
	return &GadgetContext{
72
		ctx:                      gCtx,
73
		cancel:                   cancel,
74
		id:                       id,
75
		runtime:                  runtime,
76
		runtimeParams:            runtimeParams,
77
		gadget:                   gadget,
78
		gadgetParams:             gadgetParams,
79
		args:                     args,
80
		parser:                   parser,
81
		logger:                   logger,
82
		operators:                operators.GetOperatorsForGadget(gadget),
83
		operatorsParamCollection: operatorsParamCollection,
84
		timeout:                  timeout,
85
		gadgetInfo:               gadgetInfo,
86
	}
87
}
88

89
func (c *GadgetContext) ID() string {
90
	return c.id
91
}
92

93
func (c *GadgetContext) Context() context.Context {
94
	return c.ctx
95
}
96

97
func (c *GadgetContext) Cancel() {
98
	c.cancel()
99
}
100

101
func (c *GadgetContext) Parser() parser.Parser {
102
	return c.parser
103
}
104

105
func (c *GadgetContext) Runtime() runtime.Runtime {
106
	return c.runtime
107
}
108

109
func (c *GadgetContext) RuntimeParams() *params.Params {
110
	return c.runtimeParams
111
}
112

113
func (c *GadgetContext) GadgetDesc() gadgets.GadgetDesc {
114
	return c.gadget
115
}
116

117
func (c *GadgetContext) Operators() operators.Operators {
118
	return c.operators
119
}
120

121
func (c *GadgetContext) Logger() logger.Logger {
122
	return c.logger
123
}
124

125
func (c *GadgetContext) GadgetParams() *params.Params {
126
	return c.gadgetParams
127
}
128

129
func (c *GadgetContext) Args() []string {
130
	return c.args
131
}
132

133
func (c *GadgetContext) OperatorsParamCollection() params.Collection {
134
	return c.operatorsParamCollection
135
}
136

137
func (c *GadgetContext) Timeout() time.Duration {
138
	return c.timeout
139
}
140

141
func (c *GadgetContext) GadgetInfo() *runTypes.GadgetInfo {
142
	return c.gadgetInfo
143
}
144

145
func WithTimeoutOrCancel(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
146
	if timeout == 0 {
147
		return context.WithCancel(ctx)
148
	}
149
	return context.WithTimeout(ctx, timeout)
150
}
151

152
func WaitForTimeoutOrDone(c gadgets.GadgetContext) {
153
	ctx, cancel := WithTimeoutOrCancel(c.Context(), c.Timeout())
154
	defer cancel()
155
	<-ctx.Done()
156
}
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.