github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/columns/examples/gadget/gadget.go (about)

     1  // Copyright 2022 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 main
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/inspektor-gadget/inspektor-gadget/pkg/columns"
    22  	"github.com/inspektor-gadget/inspektor-gadget/pkg/columns/formatter/textcolumns"
    23  )
    24  
    25  type Kubernetes struct {
    26  	Node      string `column:"node" columnTags:"kubernetes"`
    27  	Container string `column:"container" columnTags:"kubernetes"`
    28  	Pod       string `column:"pod" columnTags:"kubernetes"`
    29  }
    30  
    31  type Runtime struct {
    32  	Runtime string `column:"runtime" columnTags:"runtime"`
    33  }
    34  
    35  type GadgetData struct {
    36  	Kubernetes
    37  	Runtime
    38  	GadgetData string `column:"gadgetData"`
    39  }
    40  
    41  var GadgetOutput = []*GadgetData{
    42  	{
    43  		Kubernetes: Kubernetes{
    44  			Node:      "Node 1",
    45  			Container: "Container 1",
    46  			Pod:       "Pod 1",
    47  		},
    48  		Runtime:    Runtime{Runtime: "Runtime 1"},
    49  		GadgetData: "Data 1",
    50  	},
    51  	{
    52  		Kubernetes: Kubernetes{
    53  			Node:      "Node 2",
    54  			Container: "Container 2",
    55  			Pod:       "Pod 2",
    56  		},
    57  		Runtime:    Runtime{Runtime: "Runtime 2"},
    58  		GadgetData: "Data 2",
    59  	},
    60  	{
    61  		Kubernetes: Kubernetes{
    62  			Node:      "Node 3",
    63  			Container: "Container 3",
    64  			Pod:       "Pod 3",
    65  		},
    66  		Runtime:    Runtime{Runtime: "Runtime 3"},
    67  		GadgetData: "Data 3",
    68  	},
    69  }
    70  
    71  // Defining the column helper here lets the program crash on start if there are
    72  // errors in the syntax
    73  var gadgetColumns = columns.MustCreateColumns[GadgetData]()
    74  
    75  func main() {
    76  	// Get columnMap
    77  	cmap := gadgetColumns.GetColumnMap()
    78  
    79  	// Get a new formatter and output all data
    80  	formatter := textcolumns.NewFormatter(cmap, textcolumns.WithAutoScale(false))
    81  	formatter.WriteTable(os.Stdout, GadgetOutput)
    82  
    83  	/*
    84  		NODE             CONTAINER        POD              RUNTIME          GADGETDATA
    85  		————————————————————————————————————————————————————————————————————————————————————
    86  		Node 1           Container 1      Pod 1            Runtime 1        Data 1
    87  		Node 2           Container 2      Pod 2            Runtime 2        Data 2
    88  		Node 3           Container 3      Pod 3            Runtime 3        Data 3
    89  	*/
    90  
    91  	fmt.Println()
    92  
    93  	// Leave out kubernetes info for this one, but include gadget data (not-embedded struct) and runtime information
    94  	formatter = textcolumns.NewFormatter(
    95  		gadgetColumns.GetColumnMap(columns.Or(columns.WithEmbedded(false), columns.WithTag("runtime"))),
    96  		textcolumns.WithAutoScale(false),
    97  	)
    98  	formatter.WriteTable(os.Stdout, GadgetOutput)
    99  
   100  	/*
   101  		RUNTIME          GADGETDATA
   102  		—————————————————————————————————
   103  		Runtime 1        Data 1
   104  		Runtime 2        Data 2
   105  		Runtime 3        Data 3
   106  	*/
   107  }