github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/api/v1alpha/client_example.go (about)

     1  // Copyright 2015 The rkt 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  // +build ignore
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  	"time"
    25  
    26  	"github.com/rkt/rkt/api/v1alpha"
    27  	"golang.org/x/net/context"
    28  	"google.golang.org/grpc"
    29  )
    30  
    31  func getLogsWithoutFollow(c v1alpha.PublicAPIClient, p *v1alpha.Pod) {
    32  	if len(p.Apps) == 0 {
    33  		fmt.Printf("Pod %q has no apps\n", p.Id)
    34  		return
    35  	}
    36  
    37  	logsResp, err := c.GetLogs(context.Background(), &v1alpha.GetLogsRequest{
    38  		PodId:     p.Id,
    39  		Follow:    false,
    40  		AppName:   p.Apps[0].Name,
    41  		SinceTime: time.Now().Add(-time.Second * 5).Unix(),
    42  		Lines:     10,
    43  	})
    44  
    45  	if err != nil {
    46  		fmt.Println(err)
    47  		os.Exit(254)
    48  	}
    49  
    50  	logsRecvResp, err := logsResp.Recv()
    51  
    52  	if err == io.EOF {
    53  		return
    54  	}
    55  
    56  	if err != nil {
    57  		fmt.Println(err)
    58  		return
    59  	}
    60  
    61  	for _, l := range logsRecvResp.Lines {
    62  		fmt.Println(l)
    63  	}
    64  }
    65  
    66  func getLogsWithFollow(c v1alpha.PublicAPIClient, p *v1alpha.Pod) {
    67  	if len(p.Apps) == 0 {
    68  		fmt.Printf("Pod %q has no apps\n", p.Id)
    69  		return
    70  	}
    71  
    72  	logsResp, err := c.GetLogs(context.Background(), &v1alpha.GetLogsRequest{
    73  		PodId:   p.Id,
    74  		Follow:  true,
    75  		AppName: p.Apps[0].Name,
    76  	})
    77  	if err != nil {
    78  		fmt.Println(err)
    79  		os.Exit(254)
    80  	}
    81  
    82  	for {
    83  		logsRecvResp, err := logsResp.Recv()
    84  		if err == io.EOF {
    85  			return
    86  		}
    87  
    88  		if err != nil {
    89  			fmt.Println(err)
    90  			return
    91  		}
    92  
    93  		for _, l := range logsRecvResp.Lines {
    94  			fmt.Println(l)
    95  		}
    96  	}
    97  }
    98  
    99  func main() {
   100  	followFlag := flag.Bool("follow", false, "enable 'follow' option on GetLogs")
   101  	flag.Parse()
   102  
   103  	conn, err := grpc.Dial("localhost:15441", grpc.WithInsecure())
   104  	if err != nil {
   105  		fmt.Println(err)
   106  		os.Exit(254)
   107  	}
   108  	c := v1alpha.NewPublicAPIClient(conn)
   109  	defer conn.Close()
   110  
   111  	// List pods.
   112  	podResp, err := c.ListPods(context.Background(), &v1alpha.ListPodsRequest{
   113  		// Specify the request: Fetch and print only running pods and their details.
   114  		Detail: true,
   115  		Filters: []*v1alpha.PodFilter{
   116  			{
   117  				States: []v1alpha.PodState{v1alpha.PodState_POD_STATE_RUNNING},
   118  			},
   119  		},
   120  	})
   121  	if err != nil {
   122  		fmt.Println(err)
   123  		os.Exit(254)
   124  	}
   125  
   126  	for _, p := range podResp.Pods {
   127  		if *followFlag {
   128  			fmt.Printf("Pod %q is running. Following logs:\n", p.Id)
   129  			getLogsWithFollow(c, p)
   130  		} else {
   131  			fmt.Printf("Pod %q is running.\n", p.Id)
   132  			getLogsWithoutFollow(c, p)
   133  		}
   134  	}
   135  
   136  	// List images.
   137  	imgResp, err := c.ListImages(context.Background(), &v1alpha.ListImagesRequest{
   138  		// In this request, we fetch the details of images whose names are prefixed with "coreos.com".
   139  		Detail: true,
   140  		Filters: []*v1alpha.ImageFilter{
   141  			{
   142  				Prefixes: []string{"coreos.com"},
   143  			},
   144  		},
   145  	})
   146  	if err != nil {
   147  		fmt.Println(err)
   148  		os.Exit(254)
   149  	}
   150  
   151  	for _, im := range imgResp.Images {
   152  		fmt.Printf("Found image %q\n", im.Name)
   153  	}
   154  }