inspektor-gadget
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/*
16Package gadgetcontext handles initializing gadgets and installed operators before
17handing them over to a specified runtime.
18*/
19package gadgetcontext
20
21import (
22"context"
23"time"
24
25"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
26runTypes "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.
36type GadgetContext struct {
37ctx context.Context
38cancel context.CancelFunc
39id string
40gadget gadgets.GadgetDesc
41gadgetParams *params.Params
42args []string
43runtime runtime.Runtime
44runtimeParams *params.Params
45parser parser.Parser
46operators operators.Operators
47operatorsParamCollection params.Collection
48logger logger.Logger
49result []byte
50resultError error
51timeout time.Duration
52gadgetInfo *runTypes.GadgetInfo
53}
54
55func New(
56ctx context.Context,
57id string,
58runtime runtime.Runtime,
59runtimeParams *params.Params,
60gadget gadgets.GadgetDesc,
61gadgetParams *params.Params,
62args []string,
63operatorsParamCollection params.Collection,
64parser parser.Parser,
65logger logger.Logger,
66timeout time.Duration,
67gadgetInfo *runTypes.GadgetInfo,
68) *GadgetContext {
69gCtx, cancel := context.WithCancel(ctx)
70
71return &GadgetContext{
72ctx: gCtx,
73cancel: cancel,
74id: id,
75runtime: runtime,
76runtimeParams: runtimeParams,
77gadget: gadget,
78gadgetParams: gadgetParams,
79args: args,
80parser: parser,
81logger: logger,
82operators: operators.GetOperatorsForGadget(gadget),
83operatorsParamCollection: operatorsParamCollection,
84timeout: timeout,
85gadgetInfo: gadgetInfo,
86}
87}
88
89func (c *GadgetContext) ID() string {
90return c.id
91}
92
93func (c *GadgetContext) Context() context.Context {
94return c.ctx
95}
96
97func (c *GadgetContext) Cancel() {
98c.cancel()
99}
100
101func (c *GadgetContext) Parser() parser.Parser {
102return c.parser
103}
104
105func (c *GadgetContext) Runtime() runtime.Runtime {
106return c.runtime
107}
108
109func (c *GadgetContext) RuntimeParams() *params.Params {
110return c.runtimeParams
111}
112
113func (c *GadgetContext) GadgetDesc() gadgets.GadgetDesc {
114return c.gadget
115}
116
117func (c *GadgetContext) Operators() operators.Operators {
118return c.operators
119}
120
121func (c *GadgetContext) Logger() logger.Logger {
122return c.logger
123}
124
125func (c *GadgetContext) GadgetParams() *params.Params {
126return c.gadgetParams
127}
128
129func (c *GadgetContext) Args() []string {
130return c.args
131}
132
133func (c *GadgetContext) OperatorsParamCollection() params.Collection {
134return c.operatorsParamCollection
135}
136
137func (c *GadgetContext) Timeout() time.Duration {
138return c.timeout
139}
140
141func (c *GadgetContext) GadgetInfo() *runTypes.GadgetInfo {
142return c.gadgetInfo
143}
144
145func WithTimeoutOrCancel(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
146if timeout == 0 {
147return context.WithCancel(ctx)
148}
149return context.WithTimeout(ctx, timeout)
150}
151
152func WaitForTimeoutOrDone(c gadgets.GadgetContext) {
153ctx, cancel := WithTimeoutOrCancel(c.Context(), c.Timeout())
154defer cancel()
155<-ctx.Done()
156}
157