github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/runtime/grpc/info.go (about) 1 // Copyright 2023-2024 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 package grpcruntime 16 17 import ( 18 "context" 19 "encoding/json" 20 "fmt" 21 "math" 22 "time" 23 24 log "github.com/sirupsen/logrus" 25 26 "github.com/inspektor-gadget/inspektor-gadget/internal/deployinfo" 27 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-service/api" 28 ) 29 30 // InitDeployInfo loads the locally stored deploy info. If no deploy info is stored locally, 31 // it will try to fetch it from one of the remotes and store it locally. It will issue warnings on 32 // failures. 33 func (r *Runtime) InitDeployInfo() { 34 // Initialize info 35 info, err := deployinfo.Load() 36 if err == nil { 37 r.info = info 38 return 39 } 40 41 info, err = r.loadRemoteDeployInfo() 42 if err != nil { 43 log.Warnf("could not load gadget info from remote: %v", err) 44 return 45 } 46 47 r.info = info 48 49 err = deployinfo.Store(info) 50 if err != nil { 51 log.Warnf("could not store gadget info: %v", err) 52 } 53 } 54 55 func (r *Runtime) UpdateDeployInfo() error { 56 info, err := r.loadRemoteDeployInfo() 57 if err != nil { 58 return fmt.Errorf("loading remote gadget info: %w", err) 59 } 60 61 return deployinfo.Store(info) 62 } 63 64 func (r *Runtime) loadRemoteDeployInfo() (*deployinfo.DeployInfo, error) { 65 duration := r.globalParams.Get(ParamConnectionTimeout).AsUint() 66 if duration > math.MaxInt64 { 67 return nil, fmt.Errorf("duration (%d) exceeds math.MaxInt64 (%d)", duration, math.MaxInt64) 68 } 69 timeout := time.Second * time.Duration(duration) 70 ctx, cancelDial := context.WithTimeout(context.Background(), timeout) 71 defer cancelDial() 72 73 // use default params for now 74 params := r.ParamDescs().ToParams() 75 conn, err := r.getConnToRandomTarget(ctx, params) 76 if err != nil { 77 return nil, fmt.Errorf("dialing random target: %w", err) 78 } 79 defer conn.Close() 80 client := api.NewBuiltInGadgetManagerClient(conn) 81 82 info, err := client.GetInfo(ctx, &api.InfoRequest{Version: "1.0"}) 83 if err != nil { 84 return nil, fmt.Errorf("get info from gadget pod: %w", err) 85 } 86 87 retInfo := &deployinfo.DeployInfo{ 88 Experimental: info.Experimental, 89 ServerVersion: info.ServerVersion, 90 } 91 err = json.Unmarshal(info.Catalog, &retInfo.Catalog) 92 if err != nil { 93 return nil, fmt.Errorf("unmarshaling info: %w", err) 94 } 95 96 return retInfo, nil 97 }